import{Pubsub}from'pubsub-state'// When there is predefined state dataconstpredefinedData={userInputs: {w: 1500,d: [0,230,],},chartEvent: {lastIdx: -1,selectedIdx: {start: -1,end: -1}}}conststore=Pubsub(predefinedData)// When there is no initial dataconststore=Pubsub()
Publish data
store.publish(topic, object data)
It creates new topic when there's no applicable topic in the store
store.publish('chartData',{dataLength:10})
If there's an existing topic in the store, it applies to it
store.publish('chartEvent',{lastIdx:5})
Subscribe Topic
store.subscribe(topic, function)
Register subscriber function to a specific Topic
Subscriber functions will be called when store.action is executed.
store.action(topic, anonym function with topic's data arguments)
It mutates the target topic's data and executes all subscribers passing the topic's data as arguments
store.action's second argument (anonym function)' parameter is the topic's data. You need to return corresponding data object to mutate the topic's data.