UNPKG

1.68 kBSource Map (JSON)View Raw
1{"version":3,"file":"brcast.cjs.min.js","sources":["../index.js"],"sourcesContent":["export default function createBroadcast (initialState) {\n let listeners = {}\n let id = 1\n let _state = initialState\n\n function getState () {\n return _state\n }\n\n function setState (state) {\n _state = state\n const keys = Object.keys(listeners)\n let i = 0\n const len = keys.length\n for (; i < len; i++) {\n // if a listener gets unsubscribed during setState we just skip it\n if (listeners[keys[i]]) listeners[keys[i]](state)\n }\n }\n\n // subscribe to changes and return the subscriptionId\n function subscribe (listener) {\n if (typeof listener !== 'function') {\n throw new Error('listener must be a function.')\n }\n const currentId = id\n listeners[currentId] = listener\n id += 1\n return currentId\n }\n\n // remove subscription by removing the listener function\n function unsubscribe (id) {\n delete listeners[id]\n }\n\n return { getState, setState, subscribe, unsubscribe }\n}\n"],"names":["createBroadcast","initialState","let","listeners","id","_state","getState","setState","state","const","keys","Object","i","len","length","subscribe","listener","Error","currentId","unsubscribe"],"mappings":"AAAe,SAASA,gBAAiBC,GACvCC,IAAIC,KACAC,EAAK,EACLC,EAASJ,EAiCb,OAASK,SA/BT,WACE,OAAOD,GA8BUE,SA3BnB,SAAmBC,GACjBH,EAASG,EAIT,IAHAC,IAAMC,EAAOC,OAAOD,KAAKP,GACrBS,EAAI,EACFC,EAAMH,EAAKI,OACVF,EAAIC,EAAKD,IAEVT,EAAUO,EAAKE,KAAKT,EAAUO,EAAKE,IAAIJ,IAoBlBO,UAf7B,SAAoBC,GAClB,GAAwB,mBAAbA,EACT,MAAM,IAAIC,MAAM,gCAElBR,IAAMS,EAAYd,EAGlB,OAFAD,EAAUe,GAAaF,EACvBZ,GAAM,EACCc,GAQ+BC,YAJxC,SAAsBf,UACbD,EAAUC"}
\No newline at end of file