{"id":20604802,"url":"https://github.com/themost-framework/events","last_synced_at":"2025-04-15T02:26:07.270Z","repository":{"id":59506659,"uuid":"536854759","full_name":"themost-framework/events","owner":"themost-framework","description":"Sync and async event emitters","archived":false,"fork":false,"pushed_at":"2025-02-01T13:10:57.000Z","size":160,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T14:11:28.331Z","etag":null,"topics":["async","emitter","emitting","event","event-driven","events","subscribe","subscription","sync"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/themost-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-15T03:50:21.000Z","updated_at":"2025-02-01T13:10:17.000Z","dependencies_parsed_at":"2024-06-21T02:13:21.889Z","dependency_job_id":null,"html_url":"https://github.com/themost-framework/events","commit_stats":{"total_commits":22,"total_committers":1,"mean_commits":22.0,"dds":0.0,"last_synced_commit":"d41cb8261c88340d910d590fe045a729adc6f727"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fevents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fevents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fevents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fevents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/themost-framework","download_url":"https://codeload.github.com/themost-framework/events/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248992953,"owners_count":21195114,"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":["async","emitter","emitting","event","event-driven","events","subscribe","subscription","sync"],"created_at":"2024-11-16T09:24:54.611Z","updated_at":"2025-04-15T02:26:07.261Z","avatar_url":"https://github.com/themost-framework.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/@themost%2Fevents.svg)](https://www.npmjs.com/package/@themost%2Fevents)\n![GitHub top language](https://img.shields.io/github/languages/top/themost-framework/events)\n[![License](https://img.shields.io/npm/l/@themost/events)](https://github.com/themost-framework/events/blob/master/LICENSE)\n![GitHub last commit](https://img.shields.io/github/last-commit/themost-framework/events)\n![GitHub Release Date](https://img.shields.io/github/release-date/themost-framework/events)\n[![npm](https://img.shields.io/npm/dw/@themost/events)](https://www.npmjs.com/package/@themost%2Fevents)\n\n![MOST Web Framework Logo](https://github.com/themost-framework/common/raw/master/docs/img/themost_framework_v3_128.png)\n\n# @themost/events\nSync and async event emitters for both browser and node.js\n\n## Usage\n\n    npm i @themost/events\n\n### AsyncSeriesEventEmitter\n\nUse `AsyncSeriesEventEmitter` for executing a collection of async event subscribers in series. \nThe following example demonstrates a before load event which executes event subscribers and continues.\n\n\n    const { AsyncSeriesEventEmitter } = require('@themost/events');\n    class UserAction {\n        \n        constructor() {\n            this.beforeLoad = new AsyncSeriesEventEmitter();\n        }\n        \n        async load() {\n            await this.beforeLoad.emit({\n                target: this\n            });\n            this.dateCreated = new Date();\n        }        \n    }\n\n    (async function () {\n        const item = new UserAction();\n        item.beforeLoad.subscribe((event) =\u003e {\n            event.target.status = 'waiting';\n        });\n    \n        item.beforeLoad.subscribe((event) =\u003e {\n            return new Promise((resolve) =\u003e {\n                // wait for something\n                setTimeout(() =\u003e {\n                    event.target.status = 'active';\n                    resolve();\n                }, 1000);\n            });\n        });\n        await item.load();\n        console.log('Loaded', 'status', item.status);\n    })().then(() =\u003e {\n        //\n    });\n\n### AsyncEventEmitter\n\nUse `AsyncEventEmitter` for executing a collection of async event subscribers in parallel.\n\n    const { AsyncEventEmitter } = require('@themost/events');\n    class UserAction {\n        \n        constructor() {\n            this.beforeLoad = new AsyncEventEmitter();\n        }\n        \n        async load() {\n            this.beforeLoad.emit({\n                target: this\n            });\n            this.dateCreated = new Date();\n        }        \n    }\n\n    const item = new UserAction();\n    item.beforeLoad.subscribe((event) =\u003e {\n        event.target.status = 'waiting';\n    });\n\n    item.beforeLoad.subscribe((event) =\u003e {\n        return new Promise((resolve) =\u003e {\n            // wait for something\n            setTimeout(() =\u003e {\n                event.target.status = 'active';\n                resolve();\n            }, 1000);\n        });\n    });\n    item.load();\n\n### SyncSeriesEventEmitter\n\nUse `SyncSeriesEventEmitter` for executing a collection of sync event subscribers in series.\nThe following example demonstrates an after load event which executes event subscribers and continues.\n\n\n    const { SyncSeriesEventEmitter } = require('@themost/events');\n    class UserAction {\n        constructor() {\n            this.afterLoad = new SyncSeriesEventEmitter();\n        }\n        \n        load() {\n            this.status = 'unknown';\n            this.afterLoad.emit({\n                target: this\n            });\n        }        \n    }\n\n    const item = new UserAction();\n\n    item.afterLoad.subscribe((event) =\u003e {\n        event.target.status = 'waiting';\n        event.target.dateCreated = new Date();\n    });\n\n    item.afterLoad.subscribe((event) =\u003e {\n        if (event.target.status === 'waiting') {\n            event.target.status = 'active';\n        }\n    });\n    \n    // perform load\n    item.load();\n    console.log('Loaded', 'status', item.status);\n    console.log('Loaded', 'dateCreated', item.dateCreated);\n\n### ProcessEventEmitter\n\nUse `ProcessEventEmitter` for sending and receiving process messages in both fork and cluster mode under node.js.\n\nImport `@themost/events/platform-server/register` in your startup script\n\n    import '@themost/events/platform-server/register'\n\nIf your application is running in cluster mode, each message received by the primary process will be forwarded to each worker of a cluster. This operation is very important when you are implementing shared services across cluster workers and enables the communication between of them.\n\nStart sending and receiving messages:\n\n    new ProcessEventEmitter().emit(msg);\n\n    ...\n\n    new ProcessEventEmitter().subscribe((value) =\u003e {\n        // write your code here\n    });\n\n### @before and @after decorators\n\nUse `@before` and `@after` decorators for decorating any class method and execute a procedure before and after method execution.\n\n```javascript\n    import { before, after } from '@themost/events';\n\n    class UserAction {\n        \n        constructor() {\n            this.status = 'unknown';\n        }\n        \n        @before((event) =\u003e {\n            event.target.status = 'waiting';\n        })\n        @after((event) =\u003e {\n            event.target.status = 'active';\n        })\n        load() {\n            //\n        }        \n    }\n    const item = new UserAction();\n    item.load();\n    console.log('Loaded', 'status', item.status);\n```\n\nThe `event` object contains the following properties:\n\n- `target` - the target object which the method is called\n- `args` - the method arguments\n- `result` - the method return value for `@after` and `@afterAsync` decorators\n\n`@before` and `@after` callables may return a value which overrides the original method return value. The following example demonstrates how to override the original method return value.\n\n```javascript\n    import { before, after } from '@themost/events';\n\n    class UserAction {\n        constructor() {\n            this.status = 'unknown';\n        }\n        \n        @before((event) =\u003e {\n            event.target.status = 'waiting';\n            return {\n                value: 'loaded'\n            };\n        })\n        load() {\n            return 'loading';\n        }\n    }\n    const item = new UserAction();\n    const result = item.load();\n    console.log('Loaded', 'status', item.status, 'result', result);\n```\n\n### @before and @after decorators with callback\n\nUse `@before` and `@after` decorators with callback for decorating any class method and execute a procedure before and after method execution.\n\n```javascript\n    import { before, after } from '@themost/events';\n\n    class UserAction {\n        \n        constructor() {\n            this.status = 'unknown';\n        }\n        \n        @before((event, callback) =\u003e {\n            void setTimeout(() =\u003e {\n                event.target.status = 'loaded';\n                return callback();\n            }, 1000);\n        })\n        load(callback) {\n            this.status = 'loading';\n            return callback();\n        }        \n    }\n    const item = new UserAction();\n    item.load(() =\u003e {\n        console.log('Loaded', 'status', item.status);\n    });\n```\n\n### @beforeAsync and @afterAsync decorators\n\nUse `@beforeAsync` and `@afterAsync` decorators for decorating any class method and execute an async procedure before and after method execution.\n\n```javascript\n    import { beforeAsync, afterAsync } from '@themost/events';\n\n    class UserAction {\n        \n        constructor() {\n            this.status = 'unknown';\n        }\n        \n        @beforeAsync(async (event) =\u003e {\n            await new Promise((resolve) =\u003e {\n                setTimeout(() =\u003e {\n                    event.target.status = 'waiting';\n                    resolve();\n                }, 1000);\n            });\n        })\n        @afterAsync(async (event) =\u003e {\n            event.target.status = 'active';\n        })\n        async load() {\n            return this.status;\n        }        \n    }\n    (async function () {\n        const item = new UserAction();\n        await item.load();\n        console.log('Loaded', 'status', item.status);\n    })();\n```\n\n`@beforeAsync` and `@afterAsync` callables may return a value which overrides the original method return value. The following example demonstrates how to override the original method return value.\n\n```javascript\n    import { beforeAsync, afterAsync } from '@themost/events';\n\n    class UserAction {\n        constructor() {\n            this.status = 'unknown';\n        }\n        \n        @beforeAsync(async (event) =\u003e {\n            return await new Promise((resolve) =\u003e {\n                setTimeout(() =\u003e {\n                    resolve({\n                        value: 'loaded'\n                    });\n                }, 1000);\n            });\n        })\n        async load() {\n            return 'loading';\n        }\n    }\n    (async function () {\n        const item = new UserAction();\n        const result = await item.load();\n        console.log('Loaded', 'status', item.status, 'result', result);\n    })();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthemost-framework%2Fevents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthemost-framework%2Fevents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthemost-framework%2Fevents/lists"}