{"_id":"@aedart/js-events","_rev":"7-a66bc66fb946218be939168963ac5579","name":"@aedart/js-events","description":"Event Dispatcher for ES6","dist-tags":{"latest":"1.1.1"},"versions":{"1.0.0":{"name":"@aedart/js-events","version":"1.0.0","description":"Event Dispatcher for ES6","main":"index.js","scripts":{"test":"karma start karma.conf.js"},"repository":{"type":"git","url":"git+https://github.com/aedart/js-events.git"},"keywords":["event","dispatcher","event handling"],"author":{"name":"Alin Eugen Deac","email":"aedart@gmail.com","url":"https://github.com/aedart"},"license":"BSD-3-Clause","bugs":{"url":"https://github.com/aedart/js-events/issues"},"homepage":"https://github.com/aedart/js-events#readme","dependencies":{"@aedart/js-service-provider":"^1.0.0","@aedart/js-timer":"^1.0.0"},"devDependencies":{"@aedart/js-ioc":"^1.0.0","babel-plugin-external-helpers":"^6.18.0","babel-plugin-transform-builtin-extend":"^1.1.0","babel-preset-es2015-rollup":"^1.2.0","faker":"^3.1.0","jasmine":"^2.5.2","karma":"^1.3.0","karma-chrome-launcher":"^2.0.0","karma-firefox-launcher":"^1.0.0","karma-ie-launcher":"^1.0.0","karma-jasmine":"^1.0.2","karma-phantomjs-launcher":"^1.0.2","karma-rollup-plugin":"^0.2.4","karma-spec-reporter":"0.0.26","rollup":"^0.36.3","rollup-plugin-babel":"^2.6.1","rollup-plugin-buble":"^0.14.0","rollup-plugin-commonjs":"^5.0.5","rollup-plugin-eslint":"^3.0.0","rollup-plugin-node-builtins":"^2.0.0","rollup-plugin-node-globals":"^1.0.9","rollup-plugin-node-resolve":"^2.0.0","rollup-plugin-replace":"^1.1.1","rollup-plugin-uglify":"^1.0.1","uglify-js-harmony":"^2.6.2"},"gitHead":"6c14c0a135f6e56553cbe43b3d972188e7d025a9","_id":"@aedart/js-events@1.0.0","_shasum":"8a34291ae3854a23100c31ce1eba1e879c7bab40","_from":".","_npmVersion":"3.10.8","_nodeVersion":"6.8.1","_npmUser":{"name":"aedart","email":"aedart@gmail.com"},"dist":{"shasum":"8a34291ae3854a23100c31ce1eba1e879c7bab40","tarball":"https://registry.npmjs.org/@aedart/js-events/-/js-events-1.0.0.tgz","integrity":"sha512-Fg/8ajK7EtONu3BqGuWeHQwgg16rNNfDJNBI9pCeokDP4gfnnlBhuCbieH7BLys+u2mTbUXtSO2ZUPVCHi3TcA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCNqpjWjJpl65RRFJLSnhaSCoodd6YA7N75CsB1PPwaHgIhALfeOw52WymuNcuNWooehnbmfLaiEAarQd8Xldr8wcEt"}]},"maintainers":[{"name":"aedart","email":"aedart@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/js-events-1.0.0.tgz_1483876287231_0.8771161667536944"},"directories":{},"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."},"1.1.0":{"name":"@aedart/js-events","version":"1.1.0","description":"Event Dispatcher for ES6","main":"index.js","scripts":{"test":"karma start karma.conf.js"},"repository":{"type":"git","url":"git+https://github.com/aedart/js-events.git"},"keywords":["event","dispatcher","event handling"],"author":{"name":"Alin Eugen Deac","email":"aedart@gmail.com","url":"https://github.com/aedart"},"license":"BSD-3-Clause","bugs":{"url":"https://github.com/aedart/js-events/issues"},"homepage":"https://github.com/aedart/js-events#readme","dependencies":{"@aedart/js-service-provider":"^1.2.0","@aedart/js-timer":"^1.2.0","@vcas/js-mixin":"~1.0"},"devDependencies":{"@aedart/js-ioc":"^1.2.0","babel-plugin-external-helpers":"^6.18.0","babel-plugin-transform-builtin-extend":"^1.1.0","babel-preset-es2015-rollup":"^1.2.0","faker":"^3.1.0","jasmine":"^2.5.2","karma":"^1.3.0","karma-chrome-launcher":"^2.0.0","karma-firefox-launcher":"^1.0.0","karma-ie-launcher":"^1.0.0","karma-jasmine":"^1.0.2","karma-phantomjs-launcher":"^1.0.2","karma-rollup-plugin":"^0.2.4","karma-spec-reporter":"0.0.26","rollup":"^0.36.3","rollup-plugin-babel":"^2.6.1","rollup-plugin-buble":"^0.14.0","rollup-plugin-commonjs":"^5.0.5","rollup-plugin-eslint":"^3.0.0","rollup-plugin-node-builtins":"^2.0.0","rollup-plugin-node-globals":"^1.0.9","rollup-plugin-node-resolve":"^2.0.0","rollup-plugin-replace":"^1.1.1","rollup-plugin-uglify":"^1.0.1","uglify-js-harmony":"^2.6.2"},"gitHead":"d8ff45d03b4b4d53a8f01477752ede39174e4582","_id":"@aedart/js-events@1.1.0","_shasum":"e1fc2d6105dc43b538c298973b5b6fb14231506d","_from":".","_npmVersion":"3.10.8","_nodeVersion":"6.8.1","_npmUser":{"name":"aedart","email":"aedart@gmail.com"},"dist":{"shasum":"e1fc2d6105dc43b538c298973b5b6fb14231506d","tarball":"https://registry.npmjs.org/@aedart/js-events/-/js-events-1.1.0.tgz","integrity":"sha512-EF3Q5XALrb/yqG1h8SgMQlkiALuMZsHFJsf0Fkt5WbbiQZzR0HTxXT7rW1pFmyzJ+kjsoUXnS3mEbV5pQF5A2Q==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCID4y2u1eSPW80S4qJtTiqRNKfLNo7vJgdBIaA/EPZHkIAiEAmph+suE/4nQ7hFzLZWzEuFnLCSLFUdTzTlLZVm9CbHE="}]},"maintainers":[{"name":"aedart","email":"aedart@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/js-events-1.1.0.tgz_1496670061512_0.7388926451094449"},"directories":{},"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."},"1.1.1":{"name":"@aedart/js-events","version":"1.1.1","description":"Event Dispatcher for ES6","main":"index.js","scripts":{"test":"karma start karma.conf.js"},"repository":{"type":"git","url":"git+https://github.com/aedart/js-events.git"},"keywords":["event","dispatcher","event handling"],"author":{"name":"Alin Eugen Deac","email":"aedart@gmail.com","url":"https://github.com/aedart"},"license":"BSD-3-Clause","bugs":{"url":"https://github.com/aedart/js-events/issues"},"homepage":"https://github.com/aedart/js-events#readme","dependencies":{"@aedart/js-service-provider":"^1.2.1","@aedart/js-timer":"^1.2.1","@vestergaard-company/js-mixin":"^1.0.3"},"devDependencies":{"@aedart/js-ioc":"^1.2.1","babel-plugin-external-helpers":"^6.18.0","babel-plugin-transform-builtin-extend":"^1.1.0","babel-preset-es2015-rollup":"^1.2.0","faker":"^3.1.0","jasmine":"^2.5.2","karma":"^1.3.0","karma-chrome-launcher":"^2.0.0","karma-firefox-launcher":"^1.0.0","karma-ie-launcher":"^1.0.0","karma-jasmine":"^1.0.2","karma-phantomjs-launcher":"^1.0.2","karma-rollup-plugin":"^0.2.4","karma-spec-reporter":"0.0.26","rollup":"^0.36.3","rollup-plugin-babel":"^2.6.1","rollup-plugin-buble":"^0.14.0","rollup-plugin-commonjs":"^5.0.5","rollup-plugin-eslint":"^3.0.0","rollup-plugin-node-builtins":"^2.0.0","rollup-plugin-node-globals":"^1.0.9","rollup-plugin-node-resolve":"^2.0.0","rollup-plugin-replace":"^1.1.1","rollup-plugin-uglify":"^1.0.1","uglify-js-harmony":"^2.6.2"},"gitHead":"78fd7f10785f6e4b0dab2743fdfd1fc6308d7545","_id":"@aedart/js-events@1.1.1","_npmVersion":"5.0.3","_nodeVersion":"6.10.3","_npmUser":{"name":"aedart","email":"aedart@gmail.com"},"dist":{"integrity":"sha512-mdMx+Kif/zdNbfk0TVz62ch3qXm+bkT5DOLqfZ0qJ+pZphNmLnYiq9D8Ky460VT0sshpyoY+B++OGb/suBn4tA==","shasum":"ba9c1f5c034854b625456135aa182478e8396b9d","tarball":"https://registry.npmjs.org/@aedart/js-events/-/js-events-1.1.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDFntXIaA9gZd+/AFzot2orlPJeR83PRfFfMC5qFdOQpwIgC3ZyxSgPJz12+8grFEx4OptEYjnf1L0JR2yEOSdhZ4w="}]},"maintainers":[{"name":"aedart","email":"aedart@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/js-events-1.1.1.tgz_1496819944268_0.5793180800974369"},"directories":{},"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."}},"readme":"# Js Events\n\nEvent Dispatcher for ES6\n\nBefore you continue reading, you should know that this package is heavily inspired by [Taylor Otwell's](https://github.com/taylorotwell) [Event Dispatcher](https://laravel.com/docs/master/events) in [Laravel](https://laravel.com/).\nPlease go read the documentation, to gain a better understanding of what a Service Container is and how they are intended to work... and please do support Laravel!\n\n  * [How to install](#how-to-install)\n  * [Quick Start](#quick-start)\n    + [Prerequisite](#prerequisite)\n    + [Service Provider](#service-provider)\n    + [Mixin Event Dispatcher](#mixin-event-dispatcher)\n    + [Listen for Events](#listen-for-events)\n      - [Via Callback Listener](#via-callback-listener)\n      - [Via Listener Class](#via-listener-class)\n      - [Via Subscriber](#via-subscriber)\n    + [Dispatch Events](#dispatch-events)\n      - [Dispatch Events in the Background](#dispatch-events-in-the-background)\n  * [Stop Event Propagation](#stop-event-propagation)\n  * [Background Listeners](#background-listeners)\n  * [Onward](#onward)\n  * [Contribution](#contribution)\n  * [Acknowledgement](#acknowledgement)\n  * [Versioning](#versioning)\n  * [License](#license)\n\n## How to install\n\n```console\nnpm install @aedart/js-events\n```\n\n## Quick Start\n\n### Prerequisite\n\nThis package assumes that you are using an [IoC Service Container](https://github.com/aedart/js-ioc) and that you have registered the [Timer](https://github.com/aedart/js-timer) service, via a [Service Registrar](https://github.com/aedart/js-service-provider) or similar.\n\nPlease make sure that you have some prior knowledge about those packages / components before attempting to use this package.\n\n### Service Provider\n\nYou need to register the `EventServiceProvider` before you can obtain the event dispatcher.\n\nBy default, the event dispatcher is registered as a singleton instance.\n\n### Mixin Event Dispatcher\n \nOnce you have registered the event service provider, you can mixin the event `Dispatcher` into your components, by making use of [Mixwith](https://github.com/justinfagnani/mixwith.js) utility.\n \n```javascript\n'use strict';\n\nimport { DispatcherAware } from '@aedart/js-events'\nimport { mix } from '@vestergaard-company/js-mixin';\n\nclass MyApplication extends mix(Object).with(\n    DispatcherAware\n){\n    constructor(){\n        super();\n    }\n    \n    init(){\n        // Dispatcher is available as a property;\n        let d = this.dispatcher;\n    }\n}\n\nexport default MyApplication;\n```\n\n### Listen for Events\n\n#### Via Callback Listener\n\nThe easiest way to listen for event, is via callback listeners. Once the event is dispatched, the callback is invoked.\n\n```javascript\n'use strict';\n\nimport { DispatcherAware } from '@aedart/js-events'\nimport { mix } from '@vestergaard-company/js-mixin';\n\nclass MyApplication extends mix(Object).with(\n    DispatcherAware\n){\n    // ... constructor not shown ...///\n    \n    init(){\n        // Unique Event name or id \n        let event = 'My-Event-Id';\n        \n        // Callback listener\n        let listener = (event, payload) => {\n            // Do something when the event has occurred\n            console.log(event, payload);\n        }\n        \n        // Listen for the event\n        this.dispatcher.listen(event, listener);\n    }\n}\n\nexport default MyApplication;\n```\n\n#### Via Listener Class\n\nSometimes it is more convenient to create a dedicated Listener class. This is especially true, if you have complex logic that need to be performed and you wish to encapsulate that logic separate from the rest of you application. \n\n**Listener Class**\n\n```javascript\n'use strict';\n\nimport { Listener } from '@aedart/js-events';\n\nclass NotifyUser extends Listener{\n    \n    handle(event, payload){\n        alert('The ' + event + ' event has occurred');\n    }\n}\n\nexport default NotifyUser;\n```\n\n**Register Listener**\n\n```javascript\n'use strict';\n\nimport NotifyUser from './MyListeners/NotifyUser';\nimport { DispatcherAware } from '@aedart/js-events'\nimport { mix } from '@vestergaard-company/js-mixin';\n\nclass MyApplication extends mix(Object).with(\n    DispatcherAware\n){\n    // ... constructor not shown ...///\n    \n    init(){\n        // Unique Event name or id \n        let event = 'My-Event-Id';\n\n        // Listen for the event\n        this.dispatcher.listen(event, new NotifyUser());\n    }\n}\n\nexport default MyApplication;\n```\n\n#### Via Subscriber\n\nIf you application grows, it might make sense to group listeners. This can be achieved via a `Subscriber` class.\n\n**Subscriber class**\n\n```javascript\n'use strict';\n\nimport NotifyUser from './MyListeners/NotifyUser';\nimport SaveStatus from './MyListeners/SaveStatus';\nimport RedirectToLogin from './MyListeners/RedirectToLogin';\nimport { Subscriber } from '@aedart/js-events';\n\nclass MySubscriber extends Subscriber{\n    \n    constructor(){\n        this.listeners = new Map();\n        \n        // Key = event name / id\n        // Value = array of listeners\n        this.listeners.set('my-event-id', [\n            new SaveStatus(),\n            new NotifyUser()\n        ]);\n        \n        this.listeners.set('my-other-event-id', [\n            new RedirectToLogin()\n        ]);        \n    }\n    \n}\n\nexport default MySubscriber;\n```\n\n**Subscribe to the events**\n\n```javascript\n'use strict';\n\nimport MySubscriber from './Subscribers/MySubscriber';\nimport { DispatcherAware } from '@aedart/js-events'\nimport { mix } from '@vestergaard-company/js-mixin';\n\nclass MyApplication extends mix(Object).with(\n    DispatcherAware\n){\n    // ... constructor not shown ...///\n    \n    init(){\n        // Subscribe\n        this.dispatcher.subscribe(new MySubscriber());\n    }\n}\n\nexport default MyApplication;\n```\n\n### Dispatch Events\n\nTo dispatch an event, you simply invoke the `fire` method on the dispatcher.\n\n```javascript\n'use strict';\n\nimport { DispatcherAware } from '@aedart/js-events'\nimport { mix } from '@vestergaard-company/js-mixin';\n\nclass MyApplication extends mix(Object).with(\n    DispatcherAware\n){\n    // ... not shown ...///\n    \n    saveFormData(data){\n        \n        // Create the payload object\n        let payload = {\n            data: data,\n            application: this\n        }; \n        \n        // Dispatch event\n        this.dispatcher.fire('save-form-data', payload);\n    }\n}\n\nexport default MyApplication;\n```\n\n#### Dispatch Events in the Background\n\nIf you are not required to wait until all listeners have been handled, then you can dispatch an event in the background.\nThis means is that a `setTimeout` is created, with `0` delay, in which all registered listeners are handled. \n\n```javascript\n'use strict';\n\nimport { DispatcherAware } from '@aedart/js-events'\nimport { mix } from '@vestergaard-company/js-mixin';\n\nclass MyApplication extends mix(Object).with(\n    DispatcherAware\n){\n    // ... not shown ...///\n    \n    saveFormData(data){\n        \n        // Create the payload object\n        let payload = {\n            data: data,\n            application: this\n        }; \n        \n        // Dispatch event in the background - we do not wait for this to finish\n        this.dispatcher.fire('save-form-data', payload, true);\n        \n        // Do something else... code is invoked immediately\n        this.lastFormData = data;\n        \n        // ... continue with other logic here ... ///\n    }\n}\n\nexport default MyApplication;\n```\n\n## Stop Event Propagation\n\nShould you need to stop events from propagating, then your listener(s) just need to return `false`, in their handle method.\n\nThe same is true if you listeners is a callback!\n\n```javascript\n'use strict';\n\nimport { Listener } from '@aedart/js-events';\n\nclass NotifyUser extends Listener{\n    \n    handle(event, payload){\n        \n        // Stop other listeners from reacting to the event\n        return false;\n    }\n}\n\nexport default NotifyUser;\n```\n\n## Background Listeners\n\nJust like when you wish to dispatch events in the background, you can create listeners that process an event in the background, which is separate from the dispatching logic.\n\nIn other words, if you wish to execute \"slow / time demanding\" logic independently from other listeners, then you can create a `BackgroundListener`.\nAs soon as it is triggered, it will create a timeout via `setTimeout`, and execute it's logic after a specifiable delay. \n\n**Warning** Background listeners are unable to stop event propagation.\n\n```javascript\n'use strict';\n\nimport { BackgroundListener } from '@aedart/js-events';\n\nclass NotifyUser extends BackgroundListener{\n    \n    constructor(){\n        // Set delay in milliseconds of when this listener\n        // should process the event.\n        this.processDelay = 500; // Optional, default is 0 (zero)\n    }\n    \n    // Process event in the background\n    // WARNING: cannot stop event propagation!\n    process(event, payload){\n        // ... logic not shown ...//\n    }\n}\n\nexport default NotifyUser;\n```\n\n## Onward\n\nFor further API reference, please review the source code.\n\n## Contribution\n\nHave you found a defect ( [bug or design flaw](https://en.wikipedia.org/wiki/Software_bug) ), or do you wish improvements? In the following sections, you might find some useful information\non how you can help this project. In any case, I thank you for taking the time to help me improve this project's deliverables and overall quality.\n\n### Bug Report\n\nIf you are convinced that you have found a bug, then at the very least you should create a new issue. In that given issue, you should as a minimum describe the following;\n\n* Where is the defect located\n* A good, short and precise description of the defect (Why is it a defect)\n* How to replicate the defect\n* (_A possible solution for how to resolve the defect_)\n\nWhen time permits it, I will review your issue and take action upon it.\n\n### Fork, code and send pull-request\n\nA good and well written bug report can help me a lot. Nevertheless, if you can or wish to resolve the defect by yourself, here is how you can do so;\n\n* Fork this project\n* Create a new local development branch for the given defect-fix\n* Write your code / changes\n* Create executable test-cases (prove that your changes are solid!)\n* Commit and push your changes to your fork-repository\n* Send a pull-request with your changes\n* _Drink a [Beer](https://en.wikipedia.org/wiki/Beer) - you earned it_ :)\n\nAs soon as I receive the pull-request (_and have time for it_), I will review your changes and merge them into this project. If not, I will inform you why I choose not to.\n\n## Acknowledgement\n\n* [Taylor Otwell](https://github.com/taylorotwell), for his [Event Dispatcher](https://laravel.com/docs/master/events)\n\n## Versioning\n\nThis package follows [Semantic Versioning 2.0.0](http://semver.org/)\n\n## License\n\n[BSD-3-Clause](http://spdx.org/licenses/BSD-3-Clause), Read the LICENSE file included in this package","maintainers":[{"name":"aedart","email":"aedart@gmail.com"}],"time":{"modified":"2023-02-16T11:37:52.237Z","created":"2017-01-08T11:51:27.980Z","1.0.0":"2017-01-08T11:51:27.980Z","1.1.0":"2017-06-05T13:41:02.498Z","1.1.1":"2017-06-07T07:19:05.321Z"},"homepage":"https://github.com/aedart/js-events#readme","keywords":["event","dispatcher","event handling"],"repository":{"type":"git","url":"git+https://github.com/aedart/js-events.git"},"author":{"name":"Alin Eugen Deac","email":"aedart@gmail.com","url":"https://github.com/aedart"},"bugs":{"url":"https://github.com/aedart/js-events/issues"},"license":"BSD-3-Clause","readmeFilename":"README.md"}