@ray-js/wechat-event 0.0.1-beta-1 → 0.0.2-beta-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.esm.js +1 -110
- package/index.js +1 -0
- package/package.json +1 -1
- package/index..js +0 -115
package/index.esm.js
CHANGED
|
@@ -1,110 +1 @@
|
|
|
1
|
-
function
|
|
2
|
-
let listeners = {};
|
|
3
|
-
/**
|
|
4
|
-
* 注册事件监听
|
|
5
|
-
* @param type
|
|
6
|
-
* @param cb
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
function on(type, cb) {
|
|
10
|
-
if (!listeners[type]) {
|
|
11
|
-
listeners[type] = [];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
listeners[type].push({
|
|
15
|
-
type: 'on',
|
|
16
|
-
cb
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* 注册事件监听
|
|
21
|
-
* 事件只会执行一次
|
|
22
|
-
* @param type
|
|
23
|
-
* @param cb
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
function once(type, cb) {
|
|
28
|
-
if (!listeners[type]) {
|
|
29
|
-
listeners[type] = [];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
listeners[type].push({
|
|
33
|
-
type: 'once',
|
|
34
|
-
cb
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* 移除事件监听
|
|
39
|
-
* @param type
|
|
40
|
-
* @param cb
|
|
41
|
-
* @returns
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function off(type, cb) {
|
|
46
|
-
if (!listeners[type]) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
listeners[type].some((item, i) => {
|
|
51
|
-
if (item.cb === cb) {
|
|
52
|
-
listeners[type].splice(i, 1);
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return false;
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* 执行事件
|
|
61
|
-
* @param type
|
|
62
|
-
* @param data
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
function emit(type, data) {
|
|
67
|
-
if (listeners[type]) {
|
|
68
|
-
// 取得事件列表的副本进行操作,以保证所有事件被执行
|
|
69
|
-
const list = [...listeners[type]];
|
|
70
|
-
const count = list.length;
|
|
71
|
-
|
|
72
|
-
for (let i = 0; i < count; i++) {
|
|
73
|
-
const {
|
|
74
|
-
cb,
|
|
75
|
-
type: _type
|
|
76
|
-
} = list[i];
|
|
77
|
-
|
|
78
|
-
if (_type === 'once') {
|
|
79
|
-
listeners[_type].splice(i, 1);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
cb(data);
|
|
84
|
-
} catch (e) {
|
|
85
|
-
// 监听事件执行不影响同步任务
|
|
86
|
-
console.error(e);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* 清除事件监听
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const clear = () => {
|
|
97
|
-
listeners = {};
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
on,
|
|
102
|
-
off,
|
|
103
|
-
once,
|
|
104
|
-
emit,
|
|
105
|
-
clear
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
var index = createEmitter();
|
|
109
|
-
|
|
110
|
-
export { createEmitter, index as default };
|
|
1
|
+
function c(){let c={};return{on:function(n,e){c[n]||(c[n]=[]),c[n].push({type:"on",cb:e})},off:function(n,e){c[n]&&c[n].some(((o,t)=>o.cb===e&&(c[n].splice(t,1),!0)))},once:function(n,e){c[n]||(c[n]=[]),c[n].push({type:"once",cb:e})},emit:function(n,e){if(c[n]){const o=[...c[n]],t=o.length;for(let n=0;n<t;n++){const{cb:t,type:f}=o[n];"once"===f&&c[f].splice(n,1);try{t(e)}catch(c){console.error(c)}}}},clear:()=>{c={}}}}var n=c();export{c as createEmitter,n as default};
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(){let e={};return{on:function(t,c){e[t]||(e[t]=[]),e[t].push({type:"on",cb:c})},off:function(t,c){e[t]&&e[t].some(((o,n)=>o.cb===c&&(e[t].splice(n,1),!0)))},once:function(t,c){e[t]||(e[t]=[]),e[t].push({type:"once",cb:c})},emit:function(t,c){if(e[t]){const o=[...e[t]],n=o.length;for(let t=0;t<n;t++){const{cb:n,type:r}=o[t];"once"===r&&e[r].splice(t,1);try{n(c)}catch(e){console.error(e)}}}},clear:()=>{e={}}}}Object.defineProperty(exports,"__esModule",{value:!0});var t=e();exports.createEmitter=e,exports.default=t;
|
package/package.json
CHANGED
package/index..js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function createEmitter() {
|
|
6
|
-
let listeners = {};
|
|
7
|
-
/**
|
|
8
|
-
* 注册事件监听
|
|
9
|
-
* @param type
|
|
10
|
-
* @param cb
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
function on(type, cb) {
|
|
14
|
-
if (!listeners[type]) {
|
|
15
|
-
listeners[type] = [];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
listeners[type].push({
|
|
19
|
-
type: 'on',
|
|
20
|
-
cb
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* 注册事件监听
|
|
25
|
-
* 事件只会执行一次
|
|
26
|
-
* @param type
|
|
27
|
-
* @param cb
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function once(type, cb) {
|
|
32
|
-
if (!listeners[type]) {
|
|
33
|
-
listeners[type] = [];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
listeners[type].push({
|
|
37
|
-
type: 'once',
|
|
38
|
-
cb
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* 移除事件监听
|
|
43
|
-
* @param type
|
|
44
|
-
* @param cb
|
|
45
|
-
* @returns
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
function off(type, cb) {
|
|
50
|
-
if (!listeners[type]) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
listeners[type].some((item, i) => {
|
|
55
|
-
if (item.cb === cb) {
|
|
56
|
-
listeners[type].splice(i, 1);
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return false;
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 执行事件
|
|
65
|
-
* @param type
|
|
66
|
-
* @param data
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
function emit(type, data) {
|
|
71
|
-
if (listeners[type]) {
|
|
72
|
-
// 取得事件列表的副本进行操作,以保证所有事件被执行
|
|
73
|
-
const list = [...listeners[type]];
|
|
74
|
-
const count = list.length;
|
|
75
|
-
|
|
76
|
-
for (let i = 0; i < count; i++) {
|
|
77
|
-
const {
|
|
78
|
-
cb,
|
|
79
|
-
type: _type
|
|
80
|
-
} = list[i];
|
|
81
|
-
|
|
82
|
-
if (_type === 'once') {
|
|
83
|
-
listeners[_type].splice(i, 1);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
cb(data);
|
|
88
|
-
} catch (e) {
|
|
89
|
-
// 监听事件执行不影响同步任务
|
|
90
|
-
console.error(e);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* 清除事件监听
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const clear = () => {
|
|
101
|
-
listeners = {};
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
on,
|
|
106
|
-
off,
|
|
107
|
-
once,
|
|
108
|
-
emit,
|
|
109
|
-
clear
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
var index = createEmitter();
|
|
113
|
-
|
|
114
|
-
exports.createEmitter = createEmitter;
|
|
115
|
-
exports["default"] = index;
|