{"id":15916961,"url":"https://github.com/ruben-arushanyan/secure-event-emitter","last_synced_at":"2025-07-17T05:42:30.047Z","repository":{"id":45285561,"uuid":"389470893","full_name":"Ruben-Arushanyan/secure-event-emitter","owner":"Ruben-Arushanyan","description":"secure-event-emitter is a tiny javascript package that uses restrict rules and mechanisms to build safer and protected event-driven architecture. It's similar to nodejs EventEmitter, but dictates stricter rules to prevent misuse.","archived":false,"fork":false,"pushed_at":"2023-01-03T08:03:24.000Z","size":1590,"stargazers_count":13,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T10:06:19.819Z","etag":null,"topics":["emit","emits","emitter","event","event-emitter","eventdispatche","eventemitter","events","handlers","javascript","listener","listeners","observer","private-event-emitter","protected-emitter","secure","secure-emitter","secure-event-emitter","trigger"],"latest_commit_sha":null,"homepage":"https://secure-event-emitter.js.org","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ruben-Arushanyan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"Ruben-Arushanyan","custom":["https://www.paypal.me/rubenarushanyan"]}},"created_at":"2021-07-26T00:52:18.000Z","updated_at":"2025-01-06T18:17:46.000Z","dependencies_parsed_at":"2023-02-01T05:31:11.222Z","dependency_job_id":null,"html_url":"https://github.com/Ruben-Arushanyan/secure-event-emitter","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ruben-Arushanyan%2Fsecure-event-emitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ruben-Arushanyan%2Fsecure-event-emitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ruben-Arushanyan%2Fsecure-event-emitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ruben-Arushanyan%2Fsecure-event-emitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ruben-Arushanyan","download_url":"https://codeload.github.com/Ruben-Arushanyan/secure-event-emitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244345099,"owners_count":20438241,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["emit","emits","emitter","event","event-emitter","eventdispatche","eventemitter","events","handlers","javascript","listener","listeners","observer","private-event-emitter","protected-emitter","secure","secure-emitter","secure-event-emitter","trigger"],"created_at":"2024-10-06T18:07:41.956Z","updated_at":"2025-03-24T07:31:44.953Z","avatar_url":"https://github.com/Ruben-Arushanyan.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Ruben-Arushanyan","https://www.paypal.me/rubenarushanyan"],"categories":[],"sub_categories":[],"readme":"# [Secure Event Emitter](https://secure-event-emitter.js.org)\n\n\u003e You can find the full documentation on the \u003chttps://secure-event-emitter.js.org\u003e\n\n**secure-event-emitter** is a tiny javascript package that uses restrict rules and mechanisms to build safer and protected event-driven architecture. It's similar to nodejs [EventEmitter](https://nodejs.org/api/events.html), but dictates stricter rules to prevent misuse.\n\n## The Main Features\n\n- All event types must be **predefined**․\n- Not possible to call the `emit()` method anywhere without the **emitterKey**.\n- Ability to **validate** emitted values․\n\n## Installation\n\n```bash\nnpm install secure-event-emitter\n```\n\n\u003cbr/\u003e\n\n## SecureEventEmitter\n\nSecureEventEmitter is the main constructor for creating emitter instances.\n\n#### Import\n\n```js\nimport {SecureEventEmitter} from 'secure-event-emitter'\n```\n#### Syntax\n#### `new SecureEventEmitter(eventTypes, emitterKey, validator?)`\n\n\n- **eventTypes** `\u003cstring[]\u003e`  \n    An non-empty array of all event types.\n\n- **emitterKey** `\u003cstring\u003e` | `\u003csymbol\u003e`  \n    Any string or symbol value without which we won't be able to call the `.emit()` method.\n\n- **validator** `\u003cFunction\u003e`  \n    Function to validate the emitted values․ The function receives the emitted values in the argument and returns an error message if something is wrong there.\n\n#### Usage\n\n```js\nimport {SecureEventEmitter} from 'secure-event-emitter'\n\nconst eventTypes = ['event-1', 'event-2']\nconst emitterKey = Symbol()\n\nconst myEmitter = new SecureEventEmitter(eventTypes, emitterKey)\n\nmyEmitter.on('event-1', (a, b) =\u003e {\n    console.log(a, b)\n})\n\nmyEmitter.on('event-2', (x) =\u003e {\n    console.log(x)\n})\n\nmyEmitter.unlock(emitterKey).emit('event-1', 2021, 2022)\nmyEmitter.unlock(emitterKey).emit('event-2', 123)\n\n```\n\n\u003cbr/\u003e\n\n### Validator\n\nWe can define a validator function to validate the emitted values.\n\nThe function receives the emitted values in the argument and returns an error message if something is wrong there.\n\n#### Example\n\nThis example defines a validator function that ensures that the emitter can emit only numbers.\n\n```js\nconst validator = (x) =\u003e {\n    if (typeof x !== 'number') {\n        return 'Can emit only numbers!' // error message\n    }\n}\n```\n#### Usage\n\n```js\nimport {SecureEventEmitter} from 'secure-event-emitter'\n\nconst eventTypes = ['event-1']\nconst emitterKey = Symbol()\nconst validator = (x) =\u003e {\n    if (typeof x !== 'number') {\n        return 'Can emit only numbers!' // error message\n    }\n}\n\nconst myEmitter = new SecureEventEmitter(eventTypes, emitterKey, validator)\n\nmyEmitter.on('event-1', (a) =\u003e {\n    console.log(a)\n})\n\n\nmyEmitter.unlock(emitterKey).emit('event-1', 2021)\nmyEmitter.unlock(emitterKey).emit('event-1', '2021') // TypeError: Can emit only numbers!\n\n```\n\n\u003cbr/\u003e\n\u003cbr/\u003e\n\n## SingularEventEmitter\n\n**SingularEventEmitter** is a special case of **SecureEventEmitter** where each emitter is designed to trigger one type of event․\n\n### basic usage\n\n```js\nimport {SingularEventEmitter} from 'secure-event-emitter'\n\n// create emitterKey\nconst emitterKey = Symbol('My Singular Emitter Key')\n\n// create onFoo instance\nconst onFoo = new SingularEventEmitter(\n    emitterKey      // emitter key is an any Symbol type value\n)\n\n// add listeners\nonFoo.on((a) =\u003e {\n    // ...\n})\nonFoo.on((a) =\u003e {\n    // ...\n})\n\n\nonFoo.unlock(emitterKey).emit(2021)\nonFoo.unlock(emitterKey).emit(2022)\n\n```\n\n## Payload\n\n**Payload** is a class with which we can create objects that meet certain standards\n\n### basic usage\n\n```js\nimport {Payload} from 'secure-event-emitter'\n\n// first argument is an origin, can be only symbol type and required\nconst payload_1 = new Payload(Symbol('My Origin 1'), 1, 2, 3)\n// {\n//     origin: Symbol('My Origin 1'),\n//     args: [1, 2, 3],\n//     meta: {\n//         date: 545125412152,\n//         _index: 1\n//     }\n// }\n\nconst payload_2 = new Payload(Symbol('My Origin 2'), 'a', 'b')\n// {\n//     origin: Symbol('My Origin 2'),\n//     args: [1, 2, 3],\n//     meta: {\n//         date: 54512541999,\n//         _index: 1\n//     }\n// }\n\n```\n\n## useListener\n\n**useListener** is a helper hook for use emitter in react component\n\n### basic usage\n\n```js\nimport {useListener} from 'secure-event-emitter/react'\n\n// ...\n// ...\nuseListener(emitter, 'event-type', () =\u003e {\n    // ...\n})\n// ...\n// ...\n\n\n```\n\n\n## [Contributing](https://github.com/ruben-arushanyan/secure-event-emitter/blob/master/CONTRIBUTING.md)\n\nRead our [contributing guide](https://github.com/ruben-arushanyan/secure-event-emitter/blob/master/CONTRIBUTING.md) to learn about our development process.\n\n## [Code of Conduct](https://github.com/ruben-arushanyan/secure-event-emitter/blob/master/CODE_OF_CONDUCT.md)\n\nThis project has adopted the [Contributor Covenant](https://www.contributor-covenant.org) as its Code of Conduct, and we expect project participants to adhere to it. Please read the [full text](https://github.com/ruben-arushanyan/secure-event-emitter/blob/master/CODE_OF_CONDUCT.md) so that you can understand what actions will and will not be tolerated.\n\n## Authors\n\n- [Ruben Arushanyan](https://github.com/ruben-arushanyan)\n\n## License\n\n[MIT](https://github.com/ruben-arushanyan/secure-event-emitter/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruben-arushanyan%2Fsecure-event-emitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruben-arushanyan%2Fsecure-event-emitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruben-arushanyan%2Fsecure-event-emitter/lists"}