@vandeurenglenn/little-pubsub 1.4.5 → 1.4.7
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/.prettierrc +7 -0
- package/.travis.yml +8 -8
- package/README.md +113 -81
- package/index.d.ts +17 -16
- package/index.js +57 -51
- package/package.json +33 -33
- package/src/index.ts +78 -57
- package/test.js +47 -27
- package/tsconfig.json +16 -16
package/.prettierrc
ADDED
package/.travis.yml
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
language: node_js
|
|
2
|
-
node_js:
|
|
3
|
-
- '16'
|
|
4
|
-
- '18'
|
|
5
|
-
cache:
|
|
6
|
-
yarn: true
|
|
7
|
-
before_script:
|
|
8
|
-
- npm run build
|
|
1
|
+
language: node_js
|
|
2
|
+
node_js:
|
|
3
|
+
- '16'
|
|
4
|
+
- '18'
|
|
5
|
+
cache:
|
|
6
|
+
yarn: true
|
|
7
|
+
before_script:
|
|
8
|
+
- npm run build
|
package/README.md
CHANGED
|
@@ -1,81 +1,113 @@
|
|
|
1
|
-
# little-pubsub
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
pubsub
|
|
25
|
-
|
|
26
|
-
pubsub.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
pubsub.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
pubsub
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
`
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
1
|
+
# little-pubsub
|
|
2
|
+
|
|
3
|
+
> Small publish & subscribe class
|
|
4
|
+
|
|
5
|
+
## INSTALL
|
|
6
|
+
|
|
7
|
+
#### npm
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i --save @vandeurenglenn/little-pubsub
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## USAGE
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import PubSub from '@vandeurenglenn/little-pubsub'
|
|
17
|
+
const pubsub = new PubSub()
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import PubSub from '@vandeurenglenn/little-pubsub'
|
|
24
|
+
const pubsub = new PubSub()
|
|
25
|
+
|
|
26
|
+
pubsub.subscribe('event', (value) => {
|
|
27
|
+
console.log(value)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
pubsub.publish('event', 'hello')
|
|
31
|
+
// always runs handler
|
|
32
|
+
// (can use to overide littlePubsub.verbose setting without changing the behavior of the rest)
|
|
33
|
+
pubsub.publishVerbose('event', 'hello')
|
|
34
|
+
|
|
35
|
+
pubsub.unsubscribe('event', (value) => {
|
|
36
|
+
console.log(value)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
pubsub.hasSubscribers('event')
|
|
40
|
+
|
|
41
|
+
await pubsub.once('event')
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### pubsub([options])
|
|
47
|
+
|
|
48
|
+
`verbose`: when false only fires after value change<br>
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
pubsub = new PubSub({
|
|
52
|
+
verbose: false // default: true
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### subscribe
|
|
57
|
+
|
|
58
|
+
`name`: name of the channel to subscribe to<br>
|
|
59
|
+
`handler`: method<br>
|
|
60
|
+
`context`: context<br>
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
pubsub.subscribe('event-name', (data) => {
|
|
64
|
+
console.log(data)
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### unsubscribe
|
|
69
|
+
|
|
70
|
+
`name`: name of the channel to unsubscribe<br>
|
|
71
|
+
`handler`: method<br>
|
|
72
|
+
`context`: context<br>
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
pubsub.unsubscribe('event-name', (data) => {
|
|
76
|
+
console.log(data)
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### publish
|
|
81
|
+
|
|
82
|
+
`name`: name of the channel to publish to<br>
|
|
83
|
+
`handler`: method<br>
|
|
84
|
+
`verbose`: boolean<br>
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
pubsub.publish('event-name', 'data')
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### publish
|
|
91
|
+
|
|
92
|
+
`name`: name of the channel to publish to<br>
|
|
93
|
+
`handler`: method<br>
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
pubsub.publishVerbose('event-name', 'data')
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### once
|
|
100
|
+
|
|
101
|
+
`name`: name of the channel to publish to<br>
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
await pubsub.once('event-name')
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### hasSubscribers
|
|
108
|
+
|
|
109
|
+
`name`: name of the channel to publish to<br>
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
pubsub.hasSubscribers('event-name')
|
|
113
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -1,16 +1,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
|
-
subscribe(event: string, handler: Function, context?: Function): void;
|
|
13
|
-
unsubscribe(event: string, handler: Function, context?: Function): void;
|
|
14
|
-
publish(event: string, value: string | number | boolean | object | Array<any
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
subscribe(event: string, handler: Function, context?: Function): void;
|
|
13
|
+
unsubscribe(event: string, handler: Function, context?: Function): void;
|
|
14
|
+
publish(event: string, value: string | number | boolean | object | Array<any>, verbose?: boolean): void;
|
|
15
|
+
publishVerbose(event: string, value: string | number | boolean | object | Array<any>): void;
|
|
16
|
+
once(event: string): Promise<string | number | boolean | object | Array<any>>;
|
|
17
|
+
}
|
package/index.js
CHANGED
|
@@ -1,51 +1,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
|
-
subscribe(event, handler, context) {
|
|
17
|
-
if (!this.hasSubscribers(event))
|
|
18
|
-
this.subscribers[event] = { handlers: [], value: undefined };
|
|
19
|
-
context = this._handleContext(handler, context);
|
|
20
|
-
this.subscribers[event].handlers.push(handler.bind(context));
|
|
21
|
-
}
|
|
22
|
-
unsubscribe(event, handler, context) {
|
|
23
|
-
if (!this.hasSubscribers(event))
|
|
24
|
-
return;
|
|
25
|
-
context = this._handleContext(handler, context);
|
|
26
|
-
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
27
|
-
this.subscribers[event].handlers.splice(index);
|
|
28
|
-
if (this.subscribers[event].handlers.length === 0)
|
|
29
|
-
delete this.subscribers[event];
|
|
30
|
-
}
|
|
31
|
-
publish(event, value) {
|
|
32
|
-
// always set value even when having no subscribers
|
|
33
|
-
if (!this.hasSubscribers(event))
|
|
34
|
-
this.subscribers[event] = {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
subscribe(event, handler, context) {
|
|
17
|
+
if (!this.hasSubscribers(event))
|
|
18
|
+
this.subscribers[event] = { handlers: [], value: undefined };
|
|
19
|
+
context = this._handleContext(handler, context);
|
|
20
|
+
this.subscribers[event].handlers.push(handler.bind(context));
|
|
21
|
+
}
|
|
22
|
+
unsubscribe(event, handler, context) {
|
|
23
|
+
if (!this.hasSubscribers(event))
|
|
24
|
+
return;
|
|
25
|
+
context = this._handleContext(handler, context);
|
|
26
|
+
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
27
|
+
this.subscribers[event].handlers.splice(index);
|
|
28
|
+
if (this.subscribers[event].handlers.length === 0)
|
|
29
|
+
delete this.subscribers[event];
|
|
30
|
+
}
|
|
31
|
+
publish(event, value, verbose) {
|
|
32
|
+
// always set value even when having no subscribers
|
|
33
|
+
if (!this.hasSubscribers(event))
|
|
34
|
+
this.subscribers[event] = {
|
|
35
|
+
handlers: []
|
|
36
|
+
};
|
|
37
|
+
const oldValue = this.subscribers[event]?.value;
|
|
38
|
+
if (this.verbose || verbose || oldValue !== value) {
|
|
39
|
+
this.subscribers[event].value = value;
|
|
40
|
+
for (const handler of this.subscribers[event].handlers) {
|
|
41
|
+
handler(value, oldValue);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
publishVerbose(event, value) {
|
|
46
|
+
this.publish(event, value, true);
|
|
47
|
+
}
|
|
48
|
+
once(event) {
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
const cb = (value) => {
|
|
51
|
+
resolve(value);
|
|
52
|
+
this.unsubscribe(event, cb);
|
|
53
|
+
};
|
|
54
|
+
this.subscribe(event, cb);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@vandeurenglenn/little-pubsub",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "Publish & Subscribe",
|
|
5
|
-
"main": "./index.js",
|
|
6
|
-
"types": "./index.d.ts",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": {
|
|
9
|
-
"import": "./index.js",
|
|
10
|
-
"types": "./index.d.ts"
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
"repository": "https://github.com/vandeurenglenn/little-pubsub",
|
|
14
|
-
"author": "vandeurenglenn <vandeurenglenn@gmail.com>",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"type": "module",
|
|
17
|
-
"private": false,
|
|
18
|
-
"flat": true,
|
|
19
|
-
"scripts": {
|
|
20
|
-
"build": "npx tsc",
|
|
21
|
-
"test": "node test.js"
|
|
22
|
-
},
|
|
23
|
-
"keywords": [
|
|
24
|
-
"publish",
|
|
25
|
-
"subscribe",
|
|
26
|
-
"pubsub",
|
|
27
|
-
"once"
|
|
28
|
-
],
|
|
29
|
-
"devDependencies": {
|
|
30
|
-
"tape": "^4.13.0",
|
|
31
|
-
"typescript": "^5.1.6"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@vandeurenglenn/little-pubsub",
|
|
3
|
+
"version": "1.4.7",
|
|
4
|
+
"description": "Publish & Subscribe",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./index.js",
|
|
10
|
+
"types": "./index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"repository": "https://github.com/vandeurenglenn/little-pubsub",
|
|
14
|
+
"author": "vandeurenglenn <vandeurenglenn@gmail.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"private": false,
|
|
18
|
+
"flat": true,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "npx tsc",
|
|
21
|
+
"test": "node test.js"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"publish",
|
|
25
|
+
"subscribe",
|
|
26
|
+
"pubsub",
|
|
27
|
+
"once"
|
|
28
|
+
],
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"tape": "^4.13.0",
|
|
31
|
+
"typescript": "^5.1.6"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,57 +1,78 @@
|
|
|
1
|
-
export default class LittlePubSub
|
|
2
|
-
subscribers: {[index: string]: { value?: any
|
|
3
|
-
verbose: boolean
|
|
4
|
-
|
|
5
|
-
constructor(verbose?: boolean) {
|
|
6
|
-
this.verbose = verbose
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
_handleContext(handler: Function, context?: Function): Function {
|
|
10
|
-
if (typeof context === 'undefined') {
|
|
11
|
-
context = handler
|
|
12
|
-
}
|
|
13
|
-
return context
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
hasSubscribers(event: string): boolean {
|
|
17
|
-
return this.subscribers[event] ? true : false
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
subscribe(event: string, handler: Function, context?: Function): void {
|
|
21
|
-
if (!this.hasSubscribers(event))
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.subscribers[event].handlers.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
1
|
+
export default class LittlePubSub {
|
|
2
|
+
subscribers: { [index: string]: { value?: any; handlers?: Function[] } } = {}
|
|
3
|
+
verbose: boolean
|
|
4
|
+
|
|
5
|
+
constructor(verbose?: boolean) {
|
|
6
|
+
this.verbose = verbose
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
_handleContext(handler: Function, context?: Function): Function {
|
|
10
|
+
if (typeof context === 'undefined') {
|
|
11
|
+
context = handler
|
|
12
|
+
}
|
|
13
|
+
return context
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
hasSubscribers(event: string): boolean {
|
|
17
|
+
return this.subscribers[event] ? true : false
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
subscribe(event: string, handler: Function, context?: Function): void {
|
|
21
|
+
if (!this.hasSubscribers(event))
|
|
22
|
+
this.subscribers[event] = { handlers: [], value: undefined }
|
|
23
|
+
|
|
24
|
+
context = this._handleContext(handler, context)
|
|
25
|
+
this.subscribers[event].handlers.push(handler.bind(context))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
unsubscribe(event: string, handler: Function, context?: Function): void {
|
|
29
|
+
if (!this.hasSubscribers(event)) return
|
|
30
|
+
|
|
31
|
+
context = this._handleContext(handler, context)
|
|
32
|
+
const index = this.subscribers[event].handlers.indexOf(
|
|
33
|
+
handler.bind(context)
|
|
34
|
+
)
|
|
35
|
+
this.subscribers[event].handlers.splice(index)
|
|
36
|
+
if (this.subscribers[event].handlers.length === 0)
|
|
37
|
+
delete this.subscribers[event]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
publish(
|
|
41
|
+
event: string,
|
|
42
|
+
value: string | number | boolean | object | Array<any>,
|
|
43
|
+
verbose?: boolean
|
|
44
|
+
): void {
|
|
45
|
+
// always set value even when having no subscribers
|
|
46
|
+
if (!this.hasSubscribers(event))
|
|
47
|
+
this.subscribers[event] = {
|
|
48
|
+
handlers: []
|
|
49
|
+
}
|
|
50
|
+
const oldValue = this.subscribers[event]?.value
|
|
51
|
+
|
|
52
|
+
if (this.verbose || verbose || oldValue !== value) {
|
|
53
|
+
this.subscribers[event].value = value
|
|
54
|
+
for (const handler of this.subscribers[event].handlers) {
|
|
55
|
+
handler(value, oldValue)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
publishVerbose(
|
|
61
|
+
event: string,
|
|
62
|
+
value: string | number | boolean | object | Array<any>
|
|
63
|
+
) {
|
|
64
|
+
this.publish(event, value, true)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
once(
|
|
68
|
+
event: string
|
|
69
|
+
): Promise<string | number | boolean | object | Array<any>> {
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
const cb = (value: string | number | boolean | object | Array<any>) => {
|
|
72
|
+
resolve(value)
|
|
73
|
+
this.unsubscribe(event, cb)
|
|
74
|
+
}
|
|
75
|
+
this.subscribe(event, cb)
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
package/test.js
CHANGED
|
@@ -1,27 +1,47 @@
|
|
|
1
|
-
import test from 'tape'
|
|
2
|
-
import PubSub
|
|
3
|
-
|
|
4
|
-
test('pubsub is defined', tape => {
|
|
5
|
-
tape.plan(1)
|
|
6
|
-
const pubsub = new PubSub()
|
|
7
|
-
tape.ok(typeof pubsub === 'object')
|
|
8
|
-
|
|
9
|
-
test('pubsub subscribes & publishes', tape => {
|
|
10
|
-
tape.plan(1)
|
|
11
|
-
pubsub.subscribe('on', (value) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
import test from 'tape'
|
|
2
|
+
import PubSub from './index.js'
|
|
3
|
+
|
|
4
|
+
test('pubsub is defined', (tape) => {
|
|
5
|
+
tape.plan(1)
|
|
6
|
+
const pubsub = new PubSub()
|
|
7
|
+
tape.ok(typeof pubsub === 'object')
|
|
8
|
+
|
|
9
|
+
test('pubsub subscribes & publishes & unsubscribes', (tape) => {
|
|
10
|
+
tape.plan(1)
|
|
11
|
+
pubsub.subscribe('on', (value) => {
|
|
12
|
+
tape.ok(value)
|
|
13
|
+
pubsub.unsubscribe('on', (value) => tape.ok(value))
|
|
14
|
+
})
|
|
15
|
+
pubsub.publish('on', 5)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('pubsub subscribes & publishesVerbose', (tape) => {
|
|
19
|
+
tape.plan(2)
|
|
20
|
+
pubsub.subscribe('on', (value) => tape.ok(value))
|
|
21
|
+
pubsub.publish('on', 5)
|
|
22
|
+
pubsub.publishVerbose('on', 5)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('pubsub unsubscribes', (tape) => {
|
|
26
|
+
tape.plan(1)
|
|
27
|
+
pubsub.unsubscribe('on', (value) => tape.ok(value))
|
|
28
|
+
tape.ok(Boolean(Object.keys(pubsub.subscribers).length === 0))
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('pubsub once', async (tape) => {
|
|
32
|
+
tape.plan(1)
|
|
33
|
+
setTimeout(() => pubsub.publish('on', true), 1000)
|
|
34
|
+
let value = await pubsub.once('on')
|
|
35
|
+
tape.ok(value === true)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('pubsub without subscribers', async (tape) => {
|
|
39
|
+
tape.plan(1)
|
|
40
|
+
try {
|
|
41
|
+
pubsub.publish('on', true)
|
|
42
|
+
tape.ok(true)
|
|
43
|
+
} catch (error) {
|
|
44
|
+
tape.ok(false)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
})
|
package/tsconfig.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"outDir": "./",
|
|
4
|
-
"esModuleInterop": true,
|
|
5
|
-
"allowSyntheticDefaultImports": true,
|
|
6
|
-
"allowJs": false,
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"target": "ES2022",
|
|
9
|
-
"module": "es2022"
|
|
10
|
-
},
|
|
11
|
-
"include": [
|
|
12
|
-
"./src/**/*"
|
|
13
|
-
],
|
|
14
|
-
"exclude": [
|
|
15
|
-
"node_modules"
|
|
16
|
-
]
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./",
|
|
4
|
+
"esModuleInterop": true,
|
|
5
|
+
"allowSyntheticDefaultImports": true,
|
|
6
|
+
"allowJs": false,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"target": "ES2022",
|
|
9
|
+
"module": "es2022"
|
|
10
|
+
},
|
|
11
|
+
"include": [
|
|
12
|
+
"./src/**/*"
|
|
13
|
+
],
|
|
14
|
+
"exclude": [
|
|
15
|
+
"node_modules"
|
|
16
|
+
]
|
|
17
17
|
}
|