{"id":26684253,"url":"https://github.com/johnapache/event-proxy","last_synced_at":"2025-03-26T09:19:28.974Z","repository":{"id":35133506,"uuid":"210983074","full_name":"JohnApache/event-proxy","owner":"JohnApache","description":"event proxy plug-in implemented with TS","archived":false,"fork":false,"pushed_at":"2022-07-20T15:19:08.000Z","size":63,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T02:19:54.566Z","etag":null,"topics":["event","event-emitter","event-plugin","event-proxy","typescript"],"latest_commit_sha":null,"homepage":null,"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/JohnApache.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}},"created_at":"2019-09-26T02:33:47.000Z","updated_at":"2019-09-26T07:25:55.000Z","dependencies_parsed_at":"2022-08-08T05:15:52.143Z","dependency_job_id":null,"html_url":"https://github.com/JohnApache/event-proxy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevent-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevent-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevent-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevent-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohnApache","download_url":"https://codeload.github.com/JohnApache/event-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245623077,"owners_count":20645681,"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","event-emitter","event-plugin","event-proxy","typescript"],"created_at":"2025-03-26T09:19:28.387Z","updated_at":"2025-03-26T09:19:28.964Z","avatar_url":"https://github.com/JohnApache.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EventProxy\nevent proxy plug-in implemented with TS\n用ts实现的事件代理插件\n\n## 安装方法\n```javascript\n    npm install @dking/event-proxy \n```\n\n## 示例\n\n### 创建事件代理实例\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep1 = EventProxy.create();\n    const ep2 = new EventProxy();\n\n    class SomeComponent extends EventProxy {\n        componentDidMount() {\n            this.on('Test', (data) =\u003e {\n                \n            })\n        }\n    }\n\n```\n\n### 单事件绑定\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    ep.on('Test', (payload) =\u003e {\n        console.log(payload);\n    })\n    // 别名\n    ep.register('Test1', (payload) =\u003e {\n        console.log(payload);\n    })\n    ep.subscribe('Test1', (payload) =\u003e {\n        console.log(payload);\n    })\n    ep.bind('Test1', (payload) =\u003e {\n        console.log(payload);\n    })\n\n    setTimeout(() =\u003e {\n        ep.emit('Test1', {\n            time: new Date()\n        })\n    }, 1000)\n```\n\n### 联合事件绑定 \n当事件都触发后才会触发的事件\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    ep.on(['Test1', 'Test2'], (...payload) =\u003e {\n        console.log(...payload);\n    })\n    setTimeout(() =\u003e {\n        ep.emit('Test1', {\n            time: new Date()\n        })\n    }, 1000)\n    setTimeout(() =\u003e {\n        ep.emit('Test2', {\n            time: new Date()\n        })\n    }, 1000)\n```\n\n### once只监视一次的绑定方式\n该绑定方法 会在事件触发后 立即解绑\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.done('Test1', data);\n    })\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.done('Test2', data);\n    })\n    ep.once(['Test1', 'Test2'], (v1, v2) =\u003e {\n        {`... 只执行一次的业务 ... `}\n    })\n```\n\n### bindNTime 绑定 N 次 事件\n该绑定方法 会在事件触发 指定 次数后 解绑， 当 n = 1 时 和 once 效果相同\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    ep.bindNTime('Test', 2 /* 指定绑定次数 */, (v) =\u003e {\n        console.log(v);\n    })\n\n    ep.done('Test', {\n        time: new Date()\n    })\n\n    ep.done('Test', {\n        time: new Date()\n    })\n\n    ep.done('Test', {\n        time: new Date()\n    }) // 该事件不会触发\n```\n\n### wait绑定的事件\n该绑定方法 会等待事件触发 指定次数后 ，才触发回调函数\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    const fetch1 = () =\u003e {\n        fetch(url, options).then(function(response) {\n            {`... 复杂业务 ...`}\n            ep.emit('Test1', data1);\n        })\n    }\n     const fetch2 = () =\u003e {\n        fetch(url, options).then(function(response) {\n            {`... 复杂业务 ...`}\n            ep.emit('Test2', data2);\n        })\n    }\n    fetch1();\n    fetch2(); //第一次满足条件不会触发\n    fetch1();\n    fetch2(); //第二次满足条件才会触发\n    \n    ep.wait(['Test1', 'Test2'], 2/* 等待深度 */, (v1, v2) =\u003e {\n        {`... 执行的业务 ... `}\n        {v1 == [data1, data2]} //等于每一层深度的所有data数组\n    })\n```\n\n### 取消监视某事件\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    const unregister = ep.register(['Test1', 'Test2'], (data1, data2) =\u003e {\n        console.log(data1, data2)\n    });  //register的返回了卸载函数\n    unregister(); // 可以取消解绑事件\n    ep.emit('Test1', {\n        time: new Date()\n    })\n\n    ep.emit('Test2', {\n        time: new Date()\n    })\n```\n\n### 继承的方式使用 EventProxy\n```js\n    import EventProxy from '@dking/event-proxy';\n    const ep = EventProxy.create();\n    const EVENT_NAME_1 = 'Test1';\n    const EVENT_NAME_2 = 'Test1';\n    class SomeComponent extends EventProxy {\n        componentDidMount() {\n            const ug1 = this.on(EVENT_NAME_1, (data) =\u003e {\n                console.log(data);\n            })\n            const ug2 = this.on(EVENT_NAME_2, (data) =\u003e {\n                console.log(data);\n            })\n\n            this.ugs = [ug1, ug2];\n        }\n        componentWillUnMount() {\n            this.ugs \u0026\u0026 this.ugs.forEach(ug =\u003e ug());\n        }\n\n        run(): void {\n            setTimeout(() =\u003e {\n                this.emit(EVENT_NAME_1, {\n                    type: EVENT_NAME_1\n                })\n            }, 100)\n\n            setTimeout(() =\u003e {\n                this.emit(EVENT_NAME_2, {\n                    type: EVENT_NAME_2\n                })\n            }, 100)\n        }\n    }\n\n    const component = new SomeComponent();\n    component.componentDidMount();\n    component.run();\n    component.componentWillUnMount();\n    component.run(); // 没有事件会触发\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnapache%2Fevent-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnapache%2Fevent-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnapache%2Fevent-proxy/lists"}