-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da6ea3d
commit 38b6f86
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,34 @@ | ||
localpubsub | ||
=== | ||
|
||
Simple pubsub lightweight library using Window event listener and storage event. | ||
|
||
#### Subscribing | ||
`subscribe(topic, callbackFunction);` | ||
|
||
|
||
``` | ||
import localpubsub from 'localpubsub'; | ||
function displayMessage(data) { | ||
console.log(data) | ||
} | ||
localpubsub.subscribe('my topic', displayMessage); | ||
``` | ||
|
||
#### Publishing | ||
`publish(topic, message);` | ||
|
||
``` | ||
localpubsub.publish('my topic', 'Hello'); | ||
``` | ||
|
||
#### Unsubscribe | ||
`unsubscribe(topic);` | ||
|
||
``` | ||
const subscription = localpubsub.subscribe(topic, displayMessage); | ||
subscription.unsubscribe(); | ||
localpubsub.unsubscribe(topic); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
let topics = {}; | ||
|
||
function subscribe(topic, listener) { | ||
if (!topics.hasOwnProperty(topic)) { | ||
topics[topic] = listener; | ||
} | ||
window.addEventListener( | ||
topic, | ||
eventData => processCustomEvent(topic, listener, eventData), | ||
false | ||
); | ||
window.addEventListener( | ||
"storage", | ||
eventData => processStorage(topic, listener, eventData), | ||
false | ||
); | ||
return { | ||
unsubscribe: () => { | ||
unsubscribe(topic); | ||
} | ||
}; | ||
} | ||
|
||
function processCustomEvent(topic, cb, result) { | ||
if (topic === result.type) { | ||
return cb({ topic, value: result.detail, eventObject: result }); | ||
} | ||
} | ||
|
||
function processStorage(topic, cb, result) { | ||
if (topic === result.key) { | ||
return cb({ topic, value: result.newValue, eventObject: result }); | ||
} | ||
} | ||
|
||
function publish(eventName, eventValue) { | ||
if (topics[eventName]) { | ||
window.localStorage.setItem(eventName, eventValue); | ||
const customEvent = new CustomEvent(eventName, { detail: eventValue }); | ||
window.dispatchEvent(customEvent); | ||
} | ||
} | ||
|
||
function unsubscribe(subscribeKey) { | ||
window.localStorage.removeItem(subscribeKey); | ||
delete topics[subscribeKey]; | ||
} | ||
|
||
export default { | ||
topics, | ||
publish, | ||
subscribe, | ||
unsubscribe | ||
}; |