What's the proper way to document callbacks with jsdoc?

后端 未结 4 869
春和景丽
春和景丽 2021-01-30 04:52

I\'ve spent quite a while scouring the internet looking for the best way to properly document callbacks with jsdoc, but unfortunately, I haven\'t found a great one yet.

4条回答
  •  梦毁少年i
    2021-01-30 05:26

    Possibly I'm arriving late to this answer...however this is my contribution. Using ES6 we can do this:

        /**
     *
     * @param {import('../clients')} clients  
     */
    export default function socketServer(clients) {
    io.on('connection', (webClient) => {
    
    
        webClient.on('register', (data) => {
          clients.add(data, webClient);
        });
    }
    
     server.listen(8081, function (err) {
        if (err) throw err;
        console.log('listening on port 8081');
      });
    
    )
    

    In the folder 'clients' we have an index.js file with this code

    let clients = new Map();
    
    /**
     * Add Client to Collection
     * @param {string} key
     * @param {object} client
     */
    export function add(key, client) {
      clients.set(key, client);
    }
    /**
     * Remove Client from Collection
     * @param {string} key
     */
    export function remove(key) {
      clients.delete(key);
    }
    
    export const size = () => clients.size;
    

    So, all functions that are being exported in file /clients/index.js are available as JsDOC and you can refer them via IntelliSense

提交回复
热议问题