src/methods/node.js
async function create(channelName, options = {}) {
    options = fillOptionsWithDefaults(options);
    const time = microSeconds();
    const paths = getPaths(channelName);
    const ensureFolderExistsPromise = ensureFoldersExist(channelName, paths);
    const uuid = randomToken(10);

    const state = {
        time,
        channelName,
        options,
        uuid,
        paths,
        // contains all messages that have been emitted before
        emittedMessagesIds: new ObliviousSet(options.node.ttl * 2),
        messagesCallbackTime: null,
        messagesCallback: null,
        // ensures we do not read messages in parrallel
        writeBlockPromise: Promise.resolve(),
        otherReaderClients: {},
        // ensure if process crashes, everything is cleaned up
        removeUnload: unload.add(() => close(state)),
        closed: false
    };

    if (!OTHER_INSTANCES[channelName]) OTHER_INSTANCES[channelName] = [];
    OTHER_INSTANCES[channelName].push(state);

    await ensureFolderExistsPromise;
    const [
        socketEE,
        infoFilePath
    ] = await Promise.all([
        createSocketEventEmitter(channelName, uuid, paths),
        createSocketInfoFile(channelName, uuid, paths),
        refreshReaderClients(state)
    ]);
    state.socketEE = socketEE;
    state.infoFilePath = infoFilePath;

    // when new message comes in, we read it and emit it
    socketEE.emitter.on('data', data => {

        // if the socket is used fast, it may appear that multiple messages are flushed at once
        // so we have to split them before
        const singleOnes = data.split('|');
        singleOnes
            .filter(single => single !== '')
            .forEach(single => {
                try {
                    const obj = JSON.parse(single);
                    handleMessagePing(state, obj);
                } catch (err) {
                    throw new Error('could not parse data: ' + single);
                }
            });
    });

    return state;
}
async function create(channelName, options = {}) {
    options = fillOptionsWithDefaults(options);
    const time = microSeconds();
    const paths = getPaths(channelName);
    const ensureFolderExistsPromise = ensureFoldersExist(channelName, paths);
    const uuid = randomToken(10);

    const state = {
        time,
        channelName,
        options,
        uuid,
        paths,
        // contains all messages that have been emitted before
        emittedMessagesIds: new ObliviousSet(options.node.ttl * 2),
        messagesCallbackTime: null,
        messagesCallback: null,
        // ensures we do not read messages in parrallel
        writeBlockPromise: Promise.resolve(),
        otherReaderClients: {},
        // ensure if process crashes, everything is cleaned up
        removeUnload: unload.add(() => close(state)),
        closed: false
    };

    if (!OTHER_INSTANCES[channelName]) OTHER_INSTANCES[channelName] = [];
    OTHER_INSTANCES[channelName].push(state);

    await ensureFolderExistsPromise;
    const [
        socketEE,
        infoFilePath
    ] = await Promise.all([
        createSocketEventEmitter(channelName, uuid, paths),
        createSocketInfoFile(channelName, uuid, paths),
        refreshReaderClients(state)
    ]);
    state.socketEE = socketEE;
    state.infoFilePath = infoFilePath;

    // when new message comes in, we read it and emit it
    socketEE.emitter.on('data', data => {

        // if the socket is used fast, it may appear that multiple messages are flushed at once
        // so we have to split them before
        const singleOnes = data.split('|');
        singleOnes
            .filter(single => single !== '')
            .forEach(single => {
                try {
                    const obj = JSON.parse(single);
                    handleMessagePing(state, obj);
                } catch (err) {
                    throw new Error('could not parse data: ' + single);
                }
            });
    });

    return state;
}
async function create ( channelName , options = { } ) { options = fillOptionsWithDefaults ( options ) ; const time = microSeconds ( ) ; const paths = getPaths ( channelName ) ; const ensureFolderExistsPromise = ensureFoldersExist ( channelName , paths ) ; const uuid = randomToken ( 10 ) ; const state = { time , channelName , options , uuid , paths , // contains all messages that have been emitted before emittedMessagesIds : new ObliviousSet ( options . node . ttl * 2 ) , messagesCallbackTime : null , messagesCallback : null , // ensures we do not read messages in parrallel writeBlockPromise : Promise . resolve ( ) , otherReaderClients : { } , // ensure if process crashes, everything is cleaned up removeUnload : unload . add ( ( ) => close ( state ) ) , closed : false } ; if ( ! OTHER_INSTANCES [ channelName ] ) OTHER_INSTANCES [ channelName ] = [ ] ; OTHER_INSTANCES [ channelName ] . push ( state ) ; await ensureFolderExistsPromise ; const [ socketEE , infoFilePath ] = await Promise . all ( [ createSocketEventEmitter ( channelName , uuid , paths ) , createSocketInfoFile ( channelName , uuid , paths ) , refreshReaderClients ( state ) ] ) ; state . socketEE = socketEE ; state . infoFilePath = infoFilePath ; // when new message comes in, we read it and emit it socketEE . emitter . on ( 'data' , data => { // if the socket is used fast, it may appear that multiple messages are flushed at once // so we have to split them before const singleOnes = data . split ( '|' ) ; singleOnes . filter ( single => single !== '' ) . forEach ( single => { try { const obj = JSON . parse ( single ) ; handleMessagePing ( state , obj ) ; } catch ( err ) { throw new Error ( 'could not parse data: ' + single ) ; } } ) ; } ) ; return state ; }
creates a new channelState
@return {Promise<any>}
creates a new channelState