@lijuhong1981/jsevents 1.0.0

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.js ADDED
@@ -0,0 +1,9 @@
1
+ import Event from "./src/Event";
2
+ import EventDispatcher from "./src/EventDispatcher";
3
+ import EventEmitter from "./src/EventEmitter";
4
+
5
+ export {
6
+ Event,
7
+ EventDispatcher,
8
+ EventEmitter,
9
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@lijuhong1981/jsevents",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "module": "index.js",
7
+ "type": "module",
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/lijuhong1981/jsevents.git"
14
+ },
15
+ "author": "lijuhong1981",
16
+ "license": "ISC",
17
+ "bugs": {
18
+ "url": "https://github.com/lijuhong1981/jsevents/issues"
19
+ },
20
+ "homepage": "https://github.com/lijuhong1981/jsevents#readme",
21
+ "dependencies": {
22
+ "@lijuhong1981/jscheck": "^1.0.2",
23
+ "@lijuhong1981/jsdestroy": "^1.0.1"
24
+ }
25
+ }
package/src/Event.js ADDED
@@ -0,0 +1,118 @@
1
+ import Check from "@lijuhong1981/jscheck";
2
+ import { Destroyable } from "@lijuhong1981/jsdestroy";
3
+
4
+ class Event extends Destroyable {
5
+ constructor() {
6
+ super();
7
+ this._listeners = [];
8
+ }
9
+
10
+ /**
11
+ * 事件监听器数量
12
+ * @returns {Number}
13
+ */
14
+ get numberOfListeners() {
15
+ return this._listeners.length;
16
+ }
17
+
18
+ /**
19
+ * 判断事件监听器是否已存在
20
+ * @param {Function} callback 事件监听回调函数
21
+ * @returns {Boolean}
22
+ */
23
+ hasEventListener(callback) {
24
+ for (let i = 0, length = this._listeners.length; i < length; i++) {
25
+ if (this._listeners[i].callback === callback) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ }
31
+
32
+ /**
33
+ * 添加事件监听
34
+ * @param {Function} callback 回调函数
35
+ * @param {Object} options 事件配置项,如once等,可不填
36
+ * @returns {Function} 移除函数,调用该函数可直接移除事件监听
37
+ */
38
+ addEventListener(callback, options) {
39
+ Check.typeOf.func('callback', callback);
40
+
41
+ let listener;
42
+ for (let i = 0, length = this._listeners.length; i < length; i++) {
43
+ if (this._listeners[i].callback === callback) {
44
+ listener = this._listeners[i];
45
+ break;
46
+ }
47
+ }
48
+
49
+ if (!listener) {
50
+ listener = {
51
+ callback: callback,
52
+ options: options,
53
+ removeFunc: () => {
54
+ this.removeEventListener(callback);
55
+ }
56
+ };
57
+ this._listeners.push(listener);
58
+ }
59
+
60
+ return listener.removeFunc;
61
+ }
62
+
63
+ /**
64
+ * 移除事件监听
65
+ * @param {Function} callback 回调函数
66
+ * @returns {this}
67
+ */
68
+ removeEventListener(callback) {
69
+ Check.typeOf.func('callback', callback);
70
+
71
+ for (let i = 0, length = this._listeners.length; i < length; i++) {
72
+ if (this._listeners[i].callback === callback) {
73
+ this._listeners.splice(i, 1);
74
+ break;
75
+ }
76
+ }
77
+
78
+ return this;
79
+ }
80
+
81
+ /**
82
+ * 清除所有事件监听
83
+ * @returns {this}
84
+ */
85
+ clear() {
86
+ this._listeners.length = 0;
87
+ return this;
88
+ }
89
+
90
+ /**
91
+ * 发送事件
92
+ * @param {...any} ...args 事件参数
93
+ * @returns {this}
94
+ */
95
+ raiseEvent(...args) {
96
+ const listeners = this._listeners.slice();
97
+ listeners.forEach(listener => {
98
+ listener.callback(...args);
99
+ if (listener.options.once) {
100
+ const index = this._listeners.indexOf(listener);
101
+ this._listeners.splice(index, 1);
102
+ }
103
+ });
104
+
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * 销毁
110
+ * @returns {this}
111
+ */
112
+ destroy() {
113
+ this.clear();
114
+ return super.destroy();
115
+ }
116
+ };
117
+
118
+ export default Event;
@@ -0,0 +1,176 @@
1
+ import Check, { isArray, isFunction, isString, isValid } from "@lijuhong1981/jscheck";
2
+ const { Destroyable } = require("@lijuhong1981/jsdestroy");
3
+
4
+ function indexOfListener(listeners, callback) {
5
+ for (let i = 0; i < listeners.length; i++) {
6
+ if (listeners[i].callback === callback)
7
+ return i;
8
+ }
9
+ return -1;
10
+ }
11
+
12
+ function hasListener(listeners, callback) {
13
+ return indexOfListener(listeners, callback) !== -1;
14
+ }
15
+
16
+ function addListener(listenersMap, type, callback, options = {}) {
17
+ let listeners = listenersMap.get(type);
18
+ if (!listeners) {
19
+ listeners = [];
20
+ listenersMap.set(type, listeners);
21
+ }
22
+ if (!hasListener(listeners, callback)) {
23
+ listeners.push({
24
+ type: type,
25
+ callback: callback,
26
+ options: options,
27
+ });
28
+ }
29
+ }
30
+
31
+ function removeListener(listenersMap, type, callback) {
32
+ const listeners = listenersMap.get(type);
33
+ if (listeners) {
34
+ if (isFunction(callback)) {
35
+ const index = indexOfListener(listeners, callback);
36
+ if (index !== -1)
37
+ listeners.splice(index, 1);
38
+ } else {
39
+ listeners.length = 0;
40
+ listenersMap.delete(type);
41
+ }
42
+ }
43
+ }
44
+
45
+ /**
46
+ * 事件分发管理器
47
+ */
48
+ class EventDispatcher extends Destroyable {
49
+ constructor() {
50
+ super();
51
+ this._listenersMap = new Map();
52
+ }
53
+
54
+ /**
55
+ * 添加事件监听
56
+ * @param {String|Object} type 事件类型
57
+ * @param {Function} callback 回调函数
58
+ * @param {Object} options 事件配置项,如once等,可不填
59
+ * @returns {this}
60
+ */
61
+ addEventListener(type, callback, options) {
62
+ Check.isValid('type', type);
63
+ Check.typeOf.func('callback', callback);
64
+
65
+ if (isString(type) && type.match(/\s+/)) {
66
+ type = type.split(/\s+/);
67
+ }
68
+
69
+ if (isArray(type)) {
70
+ type.forEach(element => {
71
+ addListener(this._listenersMap, element, callback, options);
72
+ });
73
+ } else
74
+ addListener(this._listenersMap, type, callback, options);
75
+
76
+ return this;
77
+ }
78
+
79
+ /**
80
+ * 移除事件监听
81
+ * @param {String|Object} type 事件类型
82
+ * @param {Function} callback 回调函数,不填时可移除type下所有的事件监听
83
+ * @returns {this}
84
+ */
85
+ removeEventListener(type, callback) {
86
+ Check.isValid('type', type);
87
+
88
+ if (isString(type) && type.match(/\s+/)) {
89
+ type = type.split(/\s+/);
90
+ }
91
+
92
+ if (isArray(type)) {
93
+ type.forEach(element => {
94
+ removeListener(this._listenersMap, element, callback);
95
+ });
96
+ } else
97
+ removeListener(this._listenersMap, type, callback);
98
+
99
+ return this;
100
+ }
101
+
102
+ /**
103
+ * 清除所有事件监听
104
+ * @returns {this}
105
+ */
106
+ clear() {
107
+ this._listenersMap.forEach(function (listeners, type) {
108
+ listeners.length = 0;
109
+ });
110
+ this._listenersMap.clear();
111
+
112
+ return this;
113
+ }
114
+
115
+ /**
116
+ * 发送事件参数
117
+ * @param {String|Object} type 事件类型
118
+ * @param {...any} ...args 事件参数
119
+ * @returns {this}
120
+ */
121
+ dispatch(type, ...args) {
122
+ Check.isValid('type', type);
123
+
124
+ const listeners = this._listenersMap.get(type);
125
+ if (listeners && listeners.length > 0) {
126
+ const _listeners = listeners.slice();
127
+ _listeners.forEach(listener => {
128
+ listener.callback(type, ...args);
129
+ if (listener.options.once) {
130
+ const index = listeners.indexOf(listener);
131
+ listeners.splice(index, 1);
132
+ }
133
+ });
134
+ }
135
+
136
+ return this;
137
+ }
138
+
139
+ /**
140
+ * 发送事件对象
141
+ * @param {Object} event 事件对象
142
+ * @param {Object} owner 事件对象所有者,可不填
143
+ * @returns {this}
144
+ */
145
+ dispatchEvent(event, owner) {
146
+ Check.typeOf.object('event', event);
147
+ Check.isValid('event.type', event.type);
148
+
149
+ event.owner = event.owner || owner || this;
150
+
151
+ const listeners = this._listenersMap.get(event.type);
152
+ if (listeners && listeners.length > 0) {
153
+ const _listeners = listeners.slice();
154
+ _listeners.forEach(listener => {
155
+ listener.callback(event);
156
+ if (listener.options.once) {
157
+ const index = listeners.indexOf(listener);
158
+ listeners.splice(index, 1);
159
+ }
160
+ });
161
+ }
162
+
163
+ return this;
164
+ }
165
+
166
+ /**
167
+ * 销毁
168
+ * @returns {this}
169
+ */
170
+ destroy() {
171
+ this.clear();
172
+ return super.destroy();
173
+ }
174
+ };
175
+
176
+ export default EventDispatcher;
@@ -0,0 +1,86 @@
1
+ import { Destroyable } from "@lijuhong1981/jsdestroy";
2
+ import EventDispatcher from "./EventDispatcher";
3
+
4
+ class EventEmitter extends Destroyable {
5
+ constructor() {
6
+ super();
7
+ this._dispatcher = new EventDispatcher();
8
+ }
9
+
10
+ /**
11
+ * 添加事件监听
12
+ * @param {String|Object} type 事件类型
13
+ * @param {Function} callback 回调函数
14
+ * @returns {this}
15
+ */
16
+ on(type, callback) {
17
+ this._dispatcher.addEventListener(type, callback);
18
+ return this;
19
+ }
20
+
21
+ /**
22
+ * 添加单次事件监听
23
+ * @param {String|Object} type 事件类型
24
+ * @param {Function} callback 事件回调函数
25
+ * @returns {this}
26
+ */
27
+ once(type, callback) {
28
+ this._dispatcher.addEventListener(type, callback, {
29
+ once: true
30
+ });
31
+ return this;
32
+ }
33
+
34
+ /**
35
+ * 移除事件监听
36
+ * @param {String|Object} type 事件类型
37
+ * @param {Function} callback 回调函数,不填时可移除type下所有的事件监听
38
+ * @returns {this}
39
+ */
40
+ off(type, callback) {
41
+ this._dispatcher.removeEventListener(type, callback);
42
+ return this;
43
+ }
44
+
45
+ /**
46
+ * 清除所有事件监听
47
+ * @returns {this}
48
+ */
49
+ clear() {
50
+ this._dispatcher.clear();
51
+ return this;
52
+ }
53
+
54
+ /**
55
+ * 发送事件参数
56
+ * @param {String|Object} type 事件类型
57
+ * @param {...any} ...args 事件参数
58
+ * @returns {this}
59
+ */
60
+ emit(type, ...args) {
61
+ this._dispatcher.dispatch(type, ...args);
62
+ return this;
63
+ }
64
+
65
+ /**
66
+ * 发送事件对象
67
+ * @param {Object} event 事件对象
68
+ * @param {Object} owner 事件对象所有者,可不填
69
+ * @returns {this}
70
+ */
71
+ emitEvent(event) {
72
+ this._dispatcher.dispatchEvent(event);
73
+ return this;
74
+ }
75
+
76
+ /**
77
+ * 销毁
78
+ * @returns {this}
79
+ */
80
+ destroy() {
81
+ this._dispatcher.destroy();
82
+ return super.destroy();
83
+ }
84
+ };
85
+
86
+ export default EventEmitter;