Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
view-container / dist / utils / create-broadcast.js
Size: Mime:
"use strict";
// @flow
/**
 * Creates a broadcast that can be listened to, i.e. simple event emitter
 *
 * @see https://github.com/ReactTraining/react-broadcast
 */
Object.defineProperty(exports, "__esModule", { value: true });
const createBroadcast = (initialState) => {
    const listeners = {};
    let id = 0;
    let state = initialState;
    function publish(nextState) {
        state = nextState;
        // eslint-disable-next-line guard-for-in, no-restricted-syntax
        for (const key in listeners) {
            const listener = listeners[key];
            if (listener === undefined) {
                // eslint-disable-next-line no-continue
                continue;
            }
            listener(state);
        }
    }
    function subscribe(listener) {
        const currentId = id;
        listeners[currentId] = listener;
        id += 1;
        listener(state);
        return currentId;
    }
    function unsubscribe(unsubID) {
        listeners[unsubID] = undefined;
    }
    return { publish, subscribe, unsubscribe };
};
exports.default = createBroadcast;
//# sourceMappingURL=create-broadcast.js.map