@vandeurenglenn/little-pubsub 1.4.7 → 1.4.8
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/.travis.yml +2 -2
- package/README.md +8 -0
- package/index.d.ts +18 -17
- package/index.js +62 -57
- package/package.json +3 -3
- package/src/index.ts +5 -0
- package/test.js +6 -0
- package/tsconfig.json +11 -14
package/.travis.yml
CHANGED
package/README.md
CHANGED
|
@@ -98,6 +98,14 @@ pubsub.publishVerbose('event-name', 'data')
|
|
|
98
98
|
|
|
99
99
|
#### once
|
|
100
100
|
|
|
101
|
+
`name`: name of the channel to get the value from<br>
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
pubsub.getValue('event-name')
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### once
|
|
108
|
+
|
|
101
109
|
`name`: name of the channel to publish to<br>
|
|
102
110
|
|
|
103
111
|
```js
|
package/index.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
export default class LittlePubSub {
|
|
2
|
-
subscribers: {
|
|
3
|
-
[index: string]: {
|
|
4
|
-
value?: any;
|
|
5
|
-
handlers?: Function[];
|
|
6
|
-
};
|
|
7
|
-
};
|
|
8
|
-
verbose: boolean;
|
|
9
|
-
constructor(verbose?: boolean);
|
|
10
|
-
_handleContext(handler: Function, context?: Function): Function;
|
|
11
|
-
hasSubscribers(event: string): boolean;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
export default class LittlePubSub {
|
|
2
|
+
subscribers: {
|
|
3
|
+
[index: string]: {
|
|
4
|
+
value?: any;
|
|
5
|
+
handlers?: Function[];
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
verbose: boolean;
|
|
9
|
+
constructor(verbose?: boolean);
|
|
10
|
+
_handleContext(handler: Function, context?: Function): Function;
|
|
11
|
+
hasSubscribers(event: string): boolean;
|
|
12
|
+
getValue(event: string): any;
|
|
13
|
+
subscribe(event: string, handler: Function, context?: Function): void;
|
|
14
|
+
unsubscribe(event: string, handler: Function, context?: Function): void;
|
|
15
|
+
publish(event: string, value: string | number | boolean | object | Array<any>, verbose?: boolean): void;
|
|
16
|
+
publishVerbose(event: string, value: string | number | boolean | object | Array<any>): void;
|
|
17
|
+
once(event: string): Promise<string | number | boolean | object | Array<any>>;
|
|
18
|
+
}
|
package/index.js
CHANGED
|
@@ -1,57 +1,62 @@
|
|
|
1
|
-
export default class LittlePubSub {
|
|
2
|
-
subscribers = {};
|
|
3
|
-
verbose;
|
|
4
|
-
constructor(verbose) {
|
|
5
|
-
this.verbose = verbose;
|
|
6
|
-
}
|
|
7
|
-
_handleContext(handler, context) {
|
|
8
|
-
if (typeof context === 'undefined') {
|
|
9
|
-
context = handler;
|
|
10
|
-
}
|
|
11
|
-
return context;
|
|
12
|
-
}
|
|
13
|
-
hasSubscribers(event) {
|
|
14
|
-
return this.subscribers[event] ? true : false;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
this.subscribers[event]
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (this.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (
|
|
34
|
-
this.subscribers[event]
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (this.
|
|
39
|
-
this.subscribers[event]
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
export default class LittlePubSub {
|
|
2
|
+
subscribers = {};
|
|
3
|
+
verbose;
|
|
4
|
+
constructor(verbose) {
|
|
5
|
+
this.verbose = verbose;
|
|
6
|
+
}
|
|
7
|
+
_handleContext(handler, context) {
|
|
8
|
+
if (typeof context === 'undefined') {
|
|
9
|
+
context = handler;
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
13
|
+
hasSubscribers(event) {
|
|
14
|
+
return this.subscribers[event] ? true : false;
|
|
15
|
+
}
|
|
16
|
+
getValue(event) {
|
|
17
|
+
if (this.subscribers[event])
|
|
18
|
+
return this.subscribers[event].value;
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
subscribe(event, handler, context) {
|
|
22
|
+
if (!this.hasSubscribers(event))
|
|
23
|
+
this.subscribers[event] = { handlers: [], value: undefined };
|
|
24
|
+
context = this._handleContext(handler, context);
|
|
25
|
+
this.subscribers[event].handlers.push(handler.bind(context));
|
|
26
|
+
}
|
|
27
|
+
unsubscribe(event, handler, context) {
|
|
28
|
+
if (!this.hasSubscribers(event))
|
|
29
|
+
return;
|
|
30
|
+
context = this._handleContext(handler, context);
|
|
31
|
+
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
32
|
+
this.subscribers[event].handlers.splice(index);
|
|
33
|
+
if (this.subscribers[event].handlers.length === 0)
|
|
34
|
+
delete this.subscribers[event];
|
|
35
|
+
}
|
|
36
|
+
publish(event, value, verbose) {
|
|
37
|
+
// always set value even when having no subscribers
|
|
38
|
+
if (!this.hasSubscribers(event))
|
|
39
|
+
this.subscribers[event] = {
|
|
40
|
+
handlers: []
|
|
41
|
+
};
|
|
42
|
+
const oldValue = this.subscribers[event]?.value;
|
|
43
|
+
if (this.verbose || verbose || oldValue !== value) {
|
|
44
|
+
this.subscribers[event].value = value;
|
|
45
|
+
for (const handler of this.subscribers[event].handlers) {
|
|
46
|
+
handler(value, oldValue);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
publishVerbose(event, value) {
|
|
51
|
+
this.publish(event, value, true);
|
|
52
|
+
}
|
|
53
|
+
once(event) {
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
const cb = (value) => {
|
|
56
|
+
resolve(value);
|
|
57
|
+
this.unsubscribe(event, cb);
|
|
58
|
+
};
|
|
59
|
+
this.subscribe(event, cb);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vandeurenglenn/little-pubsub",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
4
4
|
"description": "Publish & Subscribe",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"once"
|
|
28
28
|
],
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"tape": "^
|
|
31
|
-
"typescript": "^5.
|
|
30
|
+
"tape": "^5.8.1",
|
|
31
|
+
"typescript": "^5.5.3"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/index.ts
CHANGED
|
@@ -17,6 +17,11 @@ export default class LittlePubSub {
|
|
|
17
17
|
return this.subscribers[event] ? true : false
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
getValue(event: string): any {
|
|
21
|
+
if (this.subscribers[event]) return this.subscribers[event].value
|
|
22
|
+
return undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
20
25
|
subscribe(event: string, handler: Function, context?: Function): void {
|
|
21
26
|
if (!this.hasSubscribers(event))
|
|
22
27
|
this.subscribers[event] = { handlers: [], value: undefined }
|
package/test.js
CHANGED
|
@@ -22,6 +22,12 @@ test('pubsub is defined', (tape) => {
|
|
|
22
22
|
pubsub.publishVerbose('on', 5)
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
+
test('pubsub getValue', (tape) => {
|
|
26
|
+
tape.plan(1)
|
|
27
|
+
let value = pubsub.getValue('on')
|
|
28
|
+
tape.ok(value === 5)
|
|
29
|
+
})
|
|
30
|
+
|
|
25
31
|
test('pubsub unsubscribes', (tape) => {
|
|
26
32
|
tape.plan(1)
|
|
27
33
|
pubsub.unsubscribe('on', (value) => tape.ok(value))
|
package/tsconfig.json
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
"outDir": "./",
|
|
4
|
+
"moduleResolution": "NodeNext",
|
|
5
|
+
"esModuleInterop": true,
|
|
6
|
+
"allowSyntheticDefaultImports": true,
|
|
7
|
+
"allowJs": false,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"target": "ES2022",
|
|
10
|
+
"module": "NodeNext"
|
|
10
11
|
},
|
|
11
|
-
"include": [
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"exclude": [
|
|
15
|
-
"node_modules"
|
|
16
|
-
]
|
|
17
|
-
}
|
|
12
|
+
"include": ["./src/**/*"],
|
|
13
|
+
"exclude": ["node_modules"]
|
|
14
|
+
}
|