{"id":26684259,"url":"https://github.com/johnapache/events-proxy","last_synced_at":"2025-04-12T14:42:36.605Z","repository":{"id":40784418,"uuid":"158183790","full_name":"JohnApache/events-proxy","owner":"JohnApache","description":"javascript 自定义事件代理","archived":false,"fork":false,"pushed_at":"2025-03-17T06:18:42.000Z","size":1928,"stargazers_count":8,"open_issues_count":16,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-26T09:19:24.340Z","etag":null,"topics":["event","event-emitter","eventproxy","events","events-manager","eventsproxy","proxy"],"latest_commit_sha":null,"homepage":null,"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/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":"2018-11-19T08:03:15.000Z","updated_at":"2020-04-27T02:52:13.000Z","dependencies_parsed_at":"2023-02-02T04:17:01.378Z","dependency_job_id":null,"html_url":"https://github.com/JohnApache/events-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%2Fevents-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevents-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevents-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnApache%2Fevents-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohnApache","download_url":"https://codeload.github.com/JohnApache/events-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248583326,"owners_count":21128565,"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","eventproxy","events","events-manager","eventsproxy","proxy"],"created_at":"2025-03-26T09:19:29.000Z","updated_at":"2025-04-12T14:42:36.583Z","avatar_url":"https://github.com/JohnApache.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Javascript 自定义事件代理\n[![Build Status](https://www.travis-ci.org/JohnApache/events-proxy.svg?branch=master)](https://www.travis-ci.org/JohnApache/events-proxy)\n[![codecov](https://codecov.io/gh/JohnApache/events-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/JohnApache/events-proxy)\n[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)\n\n[![NPM](https://nodei.co/npm/eventsproxy.png)](https://nodei.co/npm/eventsproxy/)\n*****\n+ 插件主要特点：\n    - 深层嵌套回调函数，以事件模式完全解除了嵌套的问题, 解耦复杂业务逻辑\n    - 在不同场景里多个异步同时完成时即执行某个函数，且多个异步执行并行执行 不会堵塞\n    - 提供了before after wait等 特殊场景api ，应用场景更丰富 \n    - 全局监听事件，订阅模式\n    - 兼容node端 浏览器端\n    - 遵循umd规范\n*****\n\n## 安装方法\n```javascript\n    npm install eventsproxy\n```\n\n## 示例\n\n### 单事件绑定\n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test1', response);\n    })\n    ep.register('Test1', (data) =\u003e {\n        {`... 处理data ...`}\n    })\n\n```\n\n### 合成事件绑定\n```javascript\n    // 传统模式\n    fetch(url, options).then(function(response) { \n        {`... 复杂业务 ...`}\n        fetch(url, options).then(function(response) { \n            {`... 复杂业务 ...`}\n            fetch(url, options).then(function(response) { \n                {`... 复杂业务 ...`}\n                console.log('finshied');\n            })\n        })\n    })\n    \n    // EventsProxy 模式\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Task1', data);\n    })\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Task2', data);\n    })\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Task3', data);\n    })\n    ep.register(['Task1', 'Task1', 'Task3'], (data1, data2, data3) =\u003e {\n        console.log('finshied', data1, data2, data3);\n    })\n```\n\n### 多事件绑定 \n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test1', data);\n    })\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test2', data);\n    })\n    ep.register({\n        'Test1': (data) =\u003e {\n            {`... 处理data ...`}\n        },\n        'Test2': (data) =\u003e {\n            {`... 处理data ...`}\n        },\n        'Test1~Test2': (data1, data2) =\u003e {\n            {`... 处理data ...`}\n        }\n    })\n```\n\n### （* New Api）async 事件 监听， 支持 await then 等待返回结果， 返回结果是一次性的\n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test', 10)\n    });\n    const data1 = await ep.await('Test'); // 为了兼容合成事件 resolve返回值 为一个数组\n    console.log(data); // data = [ 10 ]\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test1', 10)\n    });\n\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test2', 20)\n    });\n    const data2 = await ep.await(['Test1', 'Test2']); // 为了兼容合成事件 resolve返回值 为一个数组\n    console.log(data); // data = [ 10, 20 ]\n```\n\n### before绑定事件栈触发之前，该事件栈总是先于绑定该事件的方法之前执行 after绑定事件栈触发之后，该事件栈总是先于绑定该事件的方法之后执行\n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test1', data);\n    })\n    fetch(url, options).then(function(response) {\n        {`... 复杂业务 ...`}\n        ep.emit('Test2', data);\n    })\n    ep.after(['Test1', 'Test2'], (v1, v2) =\u003e {\n        {`... 最后执行的业务 ... `}\n    })\n    ep.bind(['Test1', 'Test2'], (v1, v2) =\u003e {\n        {`... 再执行的业务 ... `}\n    })\n    ep.before(['Test1', 'Test2'], (v1, v2) =\u003e {\n        {`... 先执行的业务 ... `}\n    })\n```\n### once只监视一次的绑定方式\n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\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### wait绑定的事件触发的次数达到预先设置的waitcount 才会触发回调\n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\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'], (v1, v2) =\u003e {\n        {`... 执行的业务 ... `}\n        {v1 == [data1, data2]} //等于每一层深度的所有data数组\n    }, 2 /* 等待的深度 */)\n```\n\n### 取消监视某事件\n```javascript\n    const createEventsProxy = require('eventsproxy');\n    const ep = createEventsProxy();\n    const fn = (v1, v2) =\u003e {\n        {`... 执行的业务 ... `}\n    }\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.register(['Test1', 'Test2'], fn);\n    ep.unregister(['Test1', 'Test2'], fn); // 取消解绑事件 函数必须指向同一个函数\n\n    const unregister = ep.register(['Test1', 'Test2'], fn); //register的返回值也返回了卸载函数\n    unregister(); // 也可以取消解绑事件\n    /* ⚠️ ： 当event为对象模式  批量绑定 因为存在无限递归情况 不返回卸载函数  卸载需要通过 ep.unregister ep.unbind 等主动卸载方式卸载 */\n```\n\n\n## API文档\n``` javascript\n    // 引入方式\n    const createEventsProxy = require('eventsproxy');\n    const ep2 = createEventsProxy(['Task1', 'Task2'], () =\u003e {}); // 快捷方式注册事件\n\n    const ep = createEventsProxy();\n    // 注册事件 三种方式\n    ep.register('Task', (data) =\u003e {\n        // 字符串单事件注册\n    }); \n    ep.bind('Task', () =\u003e {}); // register alias\n    ep.on('Task', () =\u003e {}); // register alias\n    ep.subscribe('Task', () =\u003e {}); // register alias\n\n    ep.register(['Task1', 'Task2'], (v1, v2) =\u003e {\n        // 数组复合事件 回调函数 参数 v1 v2分别是自Task1 Task2传递来的data\n    }) ;\n    ep.register({\n        'Task1': (data) =\u003e {},\n        'Task2': (data) =\u003e {},\n        'Task1,Task2': (v1, v2) =\u003e {\n            // 对象批量注册事件 对象事件注册复合事件key的分割线，默认是 ‘,’  \n        }\n    });\n\n    // 卸载事件\n    const fn = (v1, v2) =\u003e {}\n    const unregister = ep.register(['Test1', 'Test2'], fn) ;\n    unregister();// 注册返回值是一个卸载函数\n    ep.unregister(['Test1', 'Test2'], fn); // 也可以主动卸载事件\n    ep.unbind(['Test1', 'Test2'], fn);\n    ep.unsubscribe(['Test1', 'Test2'], fn);\n    ep.off(['Test1', 'Test2'], fn); // unregister alias\n\n\n    // 设置对象事件注册复合事件key的分割线，默认是 ‘,’  该注册复合事件的方式只在对象注册事件有效\n    ep.setProxyLoopSplit('~');\n    ep.register({\n        'Task1~Task2': (v1, v2) =\u003e {\n            // 对象批量注册事件 对象事件注册复合事件key的分割线，默认是 ‘,’  \n        }\n    })\n    \n    ep.async('Test1') // 异步监听 事件\n    ep.async(['Test1', 'Test2']) // 异步监听 合成事件\n\n    // 总是执行事件之前 以及 之后的钩子\n    ep.before(['Test1', 'Test2'], () =\u003e {});\n    ep.after(['Test1', 'Test2'], () =\u003e {});\n\n    // 触发次数执行\n    ep.after(['Test1', 'Test2'], () =\u003e {}, 3 /* waitcount */); // 需要触发3次才会执行回调函数\n   \n    // 触发事件\n    ep.emit('Task', data);\n    ep.done('Task', data); // emit alias\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnapache%2Fevents-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnapache%2Fevents-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnapache%2Fevents-proxy/lists"}