https://github.com/blackchef/leave-a-message
https://github.com/blackchef/leave-a-message
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/blackchef/leave-a-message
- Owner: blackChef
- Created: 2018-03-23T06:48:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-11-17T05:34:46.000Z (over 3 years ago)
- Last Synced: 2025-03-09T20:04:08.238Z (over 1 year ago)
- Language: JavaScript
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# leave-a-message
A pub-sub pattern like event system. But instead of push message to subscribers, subscribers must manually pull message from pub-sub center.
# Usage
import { publish, subscribe } from 'leave-a-message';
let options = {
// If `saveHistory === false`, only the latest message would be kept,
// otherwise, user can read an array of new messages. Default is false.
saveHistory: true
};
let channelName = 'channel_foo';
let { readMsg, unsubscribe } = subscribe(channelName, options);
publish(channelName, 'abc');
publish(channelName, 'def');
// Get ['def', 'abc'] or 'def' if `saveHistory === false`
console.log( readMsg() );
// No new messages. Get [] or undefined if `saveHistory === false`.
console.log( readMsg() );
unsubscribe();
// Throw error if user already unsubscribed the channel.
readMsg();