keep-posted
Advanced tools
+2
-2
| { | ||
| "name": "keep-posted", | ||
| "description": "Event emitting done functional way", | ||
| "version": "1.0.0", | ||
| "description": "Event emitting done the simplest functional way", | ||
| "version": "1.0.1", | ||
| "author": "Jakub Szwacz <jakub@szwacz.com>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
+50
-30
| keep-posted | ||
| =========== | ||
| Event emitting done functional way. No silly strings to pass around. Only functions. | ||
| Event emitting done the simplest, functional way. No silly strings to pass around. Only functions. | ||
@@ -12,42 +12,62 @@ Actually *keep-posted* resembles more [Signal](https://github.com/millermedeiros/js-signals/wiki/Comparison-between-different-Observer-Pattern-implementations#signals) than [EventEmitter](https://github.com/millermedeiros/js-signals/wiki/Comparison-between-different-Observer-Pattern-implementations#event-emittertargetdispatcher) pattern but anyway. You can give it a try. For me this little piece of code gets job done faster and in less error-prone fashion than any other, more complicated PubSub/EventEmitter pattern. | ||
| ``` | ||
| Or just grap `keep-posted.js` file from this repo (it's UMD). | ||
| ## Intended Usage | ||
| Can be imported via CommonJS, AMD and plain <scrip> tag. Here using CommonJS: | ||
| ## Basic Usage | ||
| ```js | ||
| var sillyModule = function () { | ||
| var keepPosted = requite('keep-posted'); | ||
| // Create an instance | ||
| var somethingHappened = keepPosted.create(); | ||
| // Attach event listener | ||
| var unsubscribe = somethingHappened.subscribe(function (a, b, c) { | ||
| console.log('Event!', a, b, c); | ||
| }); | ||
| // Just call the function to trigger the event. | ||
| // Pass as many arguments as you like. | ||
| somethingHappened(1, 2, 3); | ||
| // Call the function returned to you when you did the subscribe | ||
| // to stop listening on that event. | ||
| unsubscribe(); | ||
| ``` | ||
| ## Intended Usage in Modules | ||
| ```js | ||
| var sillyStoreModule = function () { | ||
| var keepPosted = requite('keep-posted'); | ||
| var store = []; | ||
| // One keep-posted instance is for only one event type. | ||
| // Create more instances if you need to support more event types. | ||
| var newStuffAdded = keepPosted.create(); | ||
| var storeCapacityReached = keepPosted.create(); | ||
| // One instance is for only one event type. Create more | ||
| // instances if you need to support more event types. | ||
| var somethingHappened = keepPosted.create(); | ||
| var somethingDifferentHappened = keepPosted.create(); | ||
| var doSomething = function () { | ||
| // Calling keep-posted instance as function triggers | ||
| // the event, and allows you to pass as many parameters | ||
| // as you like to listeners. | ||
| somethingHappened(1, 2, 3); | ||
| somethingDifferentHappened(); | ||
| var addStuff = function (stuff) { | ||
| store.push(stuff); | ||
| newStuffAdded(stuff); | ||
| if (store.length > 99) { | ||
| storeCapacityReached(); | ||
| } | ||
| }; | ||
| return { | ||
| doSomething: doSomething, | ||
| // Anyone who wants to listen to events have to call | ||
| // 'subscribe' function with a callback. | ||
| onSomething: somethingHappened.subscribe, | ||
| onSomethingDifferent: somethingDifferentHappened.subscribe, | ||
| addStuff: addStuff, | ||
| onNewStuffAdded: newStuffAdded.subscribe, | ||
| onStoreFull: storeCapacityReached.subscribe, | ||
| } | ||
| }; | ||
| var myModule = sillyModule(); | ||
| myModule.onSomething(function (a, b, c) { | ||
| console.log('Something happened with params:', a, b, c); | ||
| var store = sillyStoreModule(); | ||
| store.onNewStuffAdded(function (stuff) { | ||
| console.log('New stuff added:', stuff); | ||
| }); | ||
| var unsubscribe = myModule.onSomethingDifferent(function () { | ||
| console.log('Something different happened!'); | ||
| store.onStoreFull(function () { | ||
| console.log("Can't store more!"); | ||
| }); | ||
| myModule.doSomething(); | ||
| // Don't want to listen anymore to 'onSomethingDifferent'. | ||
| unsubscribe(); | ||
| store.addStuff('abc'); | ||
| ``` | ||
@@ -76,3 +96,3 @@ | ||
| **Parameters:** | ||
| `params...` - any number of parameters, passed as payload to all listeners. | ||
| `params...` - any number of parameters which will be passed to all listeners. | ||
@@ -88,3 +108,3 @@ | ||
| **Returns:** | ||
| Unsubscribe trigger. A `function` which you can call when don't want to listen to that event anymore. | ||
| Unsubscribe trigger. A `function` which you can call when you don't want to listen to that event anymore. | ||
@@ -91,0 +111,0 @@ |
9623
2.07%132
17.86%