{"id":26255189,"url":"https://github.com/reaxi/events","last_synced_at":"2025-03-13T19:17:47.215Z","repository":{"id":60546044,"uuid":"532714157","full_name":"reaxi/events","owner":"reaxi","description":"events system with observer pattern implementation: PubSub and Topic Based alternative","archived":false,"fork":false,"pushed_at":"2022-10-05T04:55:00.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-20T22:19:10.250Z","etag":null,"topics":["event-system","hackertoberfest","observer","pattern","pubsub"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/reaxi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-05T02:16:56.000Z","updated_at":"2022-10-05T04:53:08.000Z","dependencies_parsed_at":"2023-01-19T06:15:34.119Z","dependency_job_id":null,"html_url":"https://github.com/reaxi/events","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaxi%2Fevents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaxi%2Fevents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaxi%2Fevents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaxi%2Fevents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reaxi","download_url":"https://codeload.github.com/reaxi/events/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243467017,"owners_count":20295309,"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":["event-system","hackertoberfest","observer","pattern","pubsub"],"created_at":"2025-03-13T19:17:46.516Z","updated_at":"2025-03-13T19:17:47.202Z","avatar_url":"https://github.com/reaxi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"### @reaxi/events\n\n-   events (system)\n\nobserver pattern implementation : PubSub and Topic Based alternative\n\n## - Pub (Publisher) \u0026 Sub (Subscriber)\n\n```ts\nimport { PubSub } from '@reaxi/events';\n\nconst s = new PubSub();\n\ns.subscribe(Function);\n\ns.publish(payload);\n\ns.unsubscribe(Function);\n\n//example:\n\nconst myFn = (a: string) =\u003e console.log(a);\n\ns.subscribe(myFn);\n\ns.publish('Hello There');\n```\n\n## - Topic Based (EventSystem)\n\n```ts\nimport { EventSystem } from '@reaxi/events';\n\nconst ev = new EventSystem();\n\nev.register('topic', Function);\n\nev.emit('topic', PayloadData);\n\nev.unregister('topic', Function);\n```\n\n### Example:\n\n```ts\nconst ev = new EventSystem\u003cstring, any\u003e(); // string payload, and any topic keys\n\nconst fn1 = a =\u003e console.log(a);\nconst fn2 = b =\u003e console.log(`data: ${b}`);\nconst fn3 = c =\u003e console.log(c.toUpperCase());\n\nev.register('topic 1', fn1);\nev.register('topic 2', fn2);\nev.register('topic 3', fn3);\n\nev.emit('topic 1', 'hello');\nev.emit('topic 1', 'hello');\nev.emit('topic 1', 'hello');\n```\n\n### Typescript example:\n\n```ts\nconst ev = new EventSystem\u003c\n    string, // payload type\n    {\n        ['preAction'];\n        ['showMessage'];\n        ['postAction'];\n    } // topics types\n\u003e();\n\nfunction setup() {\n    ev.register('preAction', console.log('starting...'));\n    ev.register('showMessage', message =\u003e console.log(message)); // will show \"hello there\"\n    ev.register('postAction', () =\u003e console.log('done'));\n}\n\nfunction main() {\n    ev.emit('preAction');\n    //\n    // program functions...\n    //\n    ev.emit('showMessage', 'hello there');\n    ev.emit('postAction');\n}\n\nsetup();\nmain();\n```\n\n### Custom Class example:\n\n```ts\ntype HooksPayload = string;\n\ntype Topics = {\n    ['bootstrap'];\n    ['onRequest'];\n    ['onCleanup'];\n};\n\nclass Hooks extends EventSystem\u003cHooksPayload, Topics\u003e {\n    //... constructor, methods, properties\n}\n\nconst hooks = new Hooks();\n\n// EventSystem methods:\n// hooks.register()\n// hooks.emit()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freaxi%2Fevents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freaxi%2Fevents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freaxi%2Fevents/lists"}