New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

emitter-master

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emitter-master - npm Package Compare versions

Comparing version 1.0.2 to 1.0.5

@@ -1,1 +0,1 @@

var Emitter=function(e){"use strict";const o={},i={};function r(e){return i[e]??(i[e]=[])}function c(e){return o[e]??(o[e]=[])}function n(t,o){return new Promise(e=>{var n=c(t);if(0!==n.length)if(o){for(let e=0;e<n.length;e++){var l=n[e];o(l)}n.length=0,e(null)}else e(n.pop());else e(null);e=r(t);o&&e.push({type:"normal",handle:o})})}function l(e,n){var l=r(e);if(0===l.length)c(e).push(n);else for(let e=0;e<l.length;e++){var t=l[e];t.handle(n),"once"===t.type&&(l.splice(e,1),e--)}}return e.Emitter={sender:l,receiver:n,listener:n,once:function(e,n){var l=c(e);if(0!==l.length)for(let e=0;e<l.length;e++)n(...l[e]),l.splice(e,1),e--;else r(e).push({type:"once",handle:n})},trigger:l,clear:function(e,n){var l={offline:!1};if(void 0!==n&&("function"==typeof n?l.callback=n:(void 0!==n.offline&&(l.offline=n.offline),void 0!==n.callback&&(l.callback=n.callback))),void 0===l.callback)l.offline&&(o[e]=[]),i[e]=[];else{var t=r(e);for(let e=0;e<t.length;e++)t[e].handle===n&&(t.splice(e,1),e--)}},debug_print:function(){console.log("事件缓存--\x3e",i),console.log("离线缓存--\x3e",o)}},Object.defineProperty(e,"__esModule",{value:!0}),e}({});
var Emitter=function(e){"use strict";const a={},i={};function c(e){return i[e]??(i[e]=[])}function p(e){return a[e]??(a[e]=[])}function r(e,f){return new Promise(n=>{let t=[],o;if("string"==typeof e)o="string",t.push(e);else{if(!Array.isArray(e))throw new Error("错误的参数:"+e);o="array",t=e}let l=!1;for(let e=0,r=t.length;e<r;e++){var a=t[e],i=p(a);if(0!==i.length){l=!0,"array"===o?n({event_name:a,data:i.pop()}):n(i.pop());break}}if(l||(f instanceof Function?n(null):n(f)),f instanceof Function)for(let e=0,r=t.length;e<r;e++)c(t[e]).push({type:"normal",event_name_type:o,handle:f})})}function n(e,t){let r=[];if("string"==typeof e)r.push(e);else{if(!Array.isArray(e))throw new Error("错误的参数:"+e);r=e}for(let n=0,e=r.length;n<e;n++){var o=r[n],l=c(o);if(0===l.length)p(o).push(t);else for(let e=0,r=l.length;e<r;e++){var a=l[n];"string"===a.event_name_type?a.handle(t):a.handle({event_name:o,data:t}),"once"===a.type&&(l.splice(e,1),e--)}}}function t(e,r){let t=[];if("string"==typeof e)t.push(e);else{if(!Array.isArray(e))throw new Error("错误的参数:"+e);t=e}var o={offline:!1};if(void 0!==r&&("function"==typeof r?o.callback=r:(void 0!==r.offline&&(o.offline=r.offline),void 0!==r.callback&&(o.callback=r.callback))),void 0===o.callback)for(let e=0,r=t.length;e<r;e++){var n=t[e];o.offline&&(a[n]=[]),i[n]=[]}else for(let n=0,e=t.length;n<e;n++){var l=c(t[n]);for(let e=0,r=l.length;e<r;e++)l[e].handle===o.callback&&(l.splice(n,1),n--)}}var o=r,l=n,f=t,l=(console.log("%cemitter-master:%c 1.0.5 %c=> %curl:%c https://www.npmjs.com/package/emitter-master","color: #fff;background: purple;padding: 2px 5px;border-radius:4px;font-size: 14px;","color: purple;font-size:14px;","color: #333;font-size:14px;","background: purple;font-size:14px; padding: 2px 5px; border-radius: 4px; color: #fff;","font-size:14px;"),{sender:l,receiver:o,listener:r,once:function(e,f){return new Promise(n=>{let t=[],o;if("string"==typeof e)o="string",t.push(e);else{if(!Array.isArray(e))throw new Error("错误的参数:"+e);o="array",t=e}let l=!1;for(let e=0,r=t.length;e<r;e++){var a=t[e],i=p(a);if(0!==i.length){l=!0;i=i.pop();"array"===o?n({event_name:a,data:i}):n(i),f&&f(i);break}}if(!l&&f)for(let e=0,r=t.length;e<r;e++)c(t[e]).push({type:"once",event_name_type:o,handle:f})})},trigger:n,clear:t,debug_print:function(){console.log("事件缓存--\x3e",i),console.log("离线缓存--\x3e",a)},remove:f});return e.Emitter=l,Object.defineProperty(e,"__esModule",{value:!0}),e}({});

@@ -1,4 +0,18 @@

declare function listener(event_name: string, handle?: Function): Promise<any>;
declare function once(event_name: string, handle: Function): void;
declare function trigger(event_name: string, data: any): void;
declare function listener(event_name: string, handle?: (arg: any) => any): Promise<any>;
declare function listener(event_names: string[], handle?: (arg: {
event_name: string;
data: any;
}) => any): Promise<{
event_name: string;
data: any;
}>;
declare function once(event_name: string, handle?: (data: any) => any): Promise<any>;
declare function once(event_names: string[], handle?: (arg: {
event_name: string;
data: any;
}) => any): Promise<{
event_name: string;
data: any;
}>;
declare function trigger(event_names: string | string[], data: any): void;
interface EventConfig {

@@ -8,3 +22,3 @@ offline: boolean;

}
declare function clear(event_name: string, callback?: ((arg: any) => any) | EventConfig): void;
declare function clear(event_names: string | string[], callback_or_config?: ((arg: any) => any) | EventConfig): void;
declare function debug_print(): void;

@@ -19,4 +33,5 @@ declare const Emitter: {

debug_print: typeof debug_print;
remove: typeof clear;
};
export { Emitter };
{
"name": "emitter-master",
"version": "1.0.2",
"version": "1.0.5",
"description": "Support for offline event emitters, event, listener",

@@ -5,0 +5,0 @@ "main": "./cjs/index.cjs",

# emitter-master
> 为公司编写的离线发送订阅,剥离出来形成一个很小的库,暂时没时间大量测试,反正有问题会修,因为我司用的就是这个。
> Support for offline publishing and subscribing events
## 基本使用
## Usage
支持离线队列的事件发射器
```typescript
import { Emitter } from "emitter-master"
Emitter.sender("A<->B", 123) // 先发送在接收也没问题,这个是该库的特点
Emitter.sender("A<->B", 123) // You can send before you receive, which is a characteristic of the library
const ret = await Emitter.receiver("A<->B") // 不论是否有值都会立即返回,没有值的情况下会返回null
// 或
const ret = await Emitter.receiver("A<->B" ) // This is returned immediately in any case, or null in the case of no value
// or
Emitter.receiver("A<->B",function(val){

@@ -19,28 +17,40 @@ console.log(val)

// 清楚
// clear
Emitter.clear("A<->B")
```
sender有一个别名叫做trigger
```ts
function handle({event_name, data}){
if(event_name === "A->B"){
...
}else if(event_name === "B->C"){
...
}else{
...
}
}
Emitter.receiver(["A->B", "B->C"], handle)
receiver有一个别名叫做listener
Emitter.clear(["A->B", "B->C"], handle)
```
还有一个once表示只监听一次
根据你自己的场景来决定使用那个名称可以带来更好的语义。
**使用场景:**
sender has an alias called trigger
- 两个距离很远的组件直接通信,支持A先发数据,然后A被销毁,B创建的时候可以接收到A销毁前的数据
- 一个组件需要根据特定来源来执行特定操作,如果不是特定来源就跳过特定操作,由于在receiver没有回调的时候会立即返回不会阻塞,这个场景非常适合用这个库解决
- 接入ws数据,这个普通的发布订阅就够了
receiver has an alias called listener
## vue和react中的推荐规范
once indicates that the system listens only once
首先要明确的是该库与框架无关,但是现在前端很多都是用vue和react来构建网站,所以这里给出我的推荐使用方式
Depending on your scenario, the name you use will provide better semantics.
**建议一:**定义存储事件名称的event_name.ts 文件,该文件只定义事件名称和事件名称的注释,注意每个事件名称都要有注释,否则这样做将毫无意义,事件名称以 `A->B` 的形式表示A组件会项B组件发送数据,以`A<->B` 的形式表示A和B会互相发送数据,下面是示例
## Recommendation specifications in vue and react
The first thing to be clear is that the library is not framework-related, but many front ends now use vue and react to build websites, so here is my recommendation
**Suggestion 1:**Define the event_name.ts file to store the event name. This file only defines the event name and the comment of the event name. Note that each event name must have A comment, otherwise it is meaningless. The form 'A<->B' indicates that A and B send data to each other. Here is an example
```ts
// 这里描述事件是干啥的,这样在ide中是可以通过快速预览来查看详情的
// This describes what the event does, so you can see the details in the ide with a quick preview
const CompA_CompB = "CompA->CompB"

@@ -53,11 +63,11 @@

**建议二:** 对于非组件与组件的通信依然要明确指定来源和目标,例如我在Axios的封装中向组件CompA发,那么可以命名为:`Axios->CompA`
**Suggestion 2:** For non-component-to-component communication that still explicitly specifies the source and destination, such as when I send to component CompA in Axios encapsulation, I can name it:`Axios->CompA`
**建议三:**记得在组件卸载之前清除事件
**Suggestion 3:**Remember to clear events before the component uninstalls
## 示例
## Example
```ts
import {Emitter} from "emitter-master"
Emitter.sender( "a-b" , true); // 把这行注释掉 ret 将会是null,不注释就是true
Emitter.sender( "a-b" , true); // Comment this out ret will be null, and uncomment will be true

@@ -64,0 +74,0 @@ ( async function ()

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet