@lijuhong1981/jsevents 1.2.12 → 1.2.14
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/LICENSE +15 -0
- package/README.md +22 -11
- package/package.json +5 -5
- package/src/EventDispatcher.js +5 -1
- package/src/EventSubscriber.js +4 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2026 lijuhong1981
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
# @lijuhong1981/jsevents
|
|
2
|
+
|
|
3
|
+
> 事件系统,提供 `EventDispatcher`(事件分发)、`EventEmitter`(发布/订阅)与 `EventSubscriber`(事件封装),均支持 `Destroyable` 销毁。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lijuhong1981/jsevents
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 导出
|
|
12
|
+
|
|
13
|
+
- `EventDispatcher` — 事件分发管理器(`addEventListener`/`removeEventListener`/`dispatch`/`dispatchEvent` 等)
|
|
14
|
+
- `EventEmitter` — 发布/订阅模式
|
|
15
|
+
- `EventSubscriber` — 单类型事件的监听器封装
|
|
16
|
+
|
|
17
|
+
监听选项 `options`:
|
|
18
|
+
|
|
19
|
+
| 项 | 说明 |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `scope` | 回调函数的 `this` 指针对象 |
|
|
22
|
+
| `once` | 是否单次事件(触发后自动移除) |
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lijuhong1981/jsevents",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.14",
|
|
4
|
+
"description": "Event system with EventDispatcher, EventEmitter and EventSubscriber.",
|
|
5
5
|
"author": "lijuhong1981 <lijuhong@hotmail.com>",
|
|
6
6
|
"homepage": "https://github.com/lijuhong1981/jslibs#readme",
|
|
7
7
|
"license": "ISC",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"url": "https://github.com/lijuhong1981/jslibs/issues"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@lijuhong1981/jscheck": "^1.1.
|
|
23
|
-
"@lijuhong1981/jsdestroy": "^1.2.
|
|
22
|
+
"@lijuhong1981/jscheck": "^1.1.8",
|
|
23
|
+
"@lijuhong1981/jsdestroy": "^1.2.8"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "5d4bd57b654be98c46364160070d5dd11600afa6"
|
|
26
26
|
}
|
package/src/EventDispatcher.js
CHANGED
|
@@ -33,6 +33,10 @@ function removeListener(events, type, callback) {
|
|
|
33
33
|
if (event) {
|
|
34
34
|
if (callback) {
|
|
35
35
|
event.removeEventListener(callback);
|
|
36
|
+
if (event.numberOfListeners === 0) {
|
|
37
|
+
events.delete(type);
|
|
38
|
+
event.destroy();
|
|
39
|
+
}
|
|
36
40
|
} else {
|
|
37
41
|
events.delete(type);
|
|
38
42
|
event.destroy();
|
|
@@ -147,7 +151,7 @@ class EventDispatcher extends Destroyable {
|
|
|
147
151
|
|
|
148
152
|
const event = this._events.get(type);
|
|
149
153
|
|
|
150
|
-
return event && event.numberOfListeners > 0;
|
|
154
|
+
return !!(event && event.numberOfListeners > 0);
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
/**
|
package/src/EventSubscriber.js
CHANGED
|
@@ -2,8 +2,8 @@ import Check from "@lijuhong1981/jscheck/src/Check.js";
|
|
|
2
2
|
import Destroyable from "@lijuhong1981/jsdestroy/src/Destroyable.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* 用于管理某个事件的订阅者的通用工具类。该类通常在容器类内部实例化,并作为属性暴露给其他对象以进行订阅。
|
|
6
|
+
*
|
|
7
7
|
* @class EventSubscriber
|
|
8
8
|
* @extends Destroyable
|
|
9
9
|
*/
|
|
@@ -134,11 +134,11 @@ class EventSubscriber extends Destroyable {
|
|
|
134
134
|
* @returns {this}
|
|
135
135
|
*/
|
|
136
136
|
raiseEvent(...args) {
|
|
137
|
-
const listeners = this._listenersMap.values();
|
|
137
|
+
const listeners = Array.from(this._listenersMap.values());
|
|
138
138
|
for (const listener of listeners) {
|
|
139
139
|
const options = listener.options;
|
|
140
140
|
const callback = listener.callback;
|
|
141
|
-
callback.apply(options.scope,
|
|
141
|
+
callback.apply(options.scope, args);
|
|
142
142
|
if (options.once) {
|
|
143
143
|
this._listenersMap.delete(callback);
|
|
144
144
|
}
|