@vandeurenglenn/little-pubsub 1.3.3 → 1.4.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/.travis.yml +2 -2
- package/README.md +15 -10
- package/dist/index.js +52 -0
- package/package.json +9 -7
- package/rollup.config.js +10 -12
- package/src/index.ts +56 -0
- package/test.js +10 -2
- package/tsconfig.js +16 -0
- package/typings/LittlePubSub.d.ts +36 -0
- package/index.js +0 -62
- package/src/index.js +0 -55
package/.travis.yml
CHANGED
package/README.md
CHANGED
|
@@ -5,20 +5,20 @@
|
|
|
5
5
|
|
|
6
6
|
#### npm
|
|
7
7
|
```sh
|
|
8
|
-
npm i --save little-pubsub
|
|
8
|
+
npm i --save @vandeurenglenn/little-pubsub
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## USAGE
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
import PubSub from 'little-pubsub';
|
|
14
|
+
import PubSub from '@vandeurenglenn/little-pubsub';
|
|
15
15
|
const pubsub = new PubSub();
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Example
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
|
-
import PubSub from 'little-pubsub';
|
|
21
|
+
import PubSub from '@vandeurenglenn/little-pubsub';
|
|
22
22
|
const pubsub = new PubSub();
|
|
23
23
|
|
|
24
24
|
pubsub.subscribe('event', value => { console.log(value) })
|
|
@@ -27,7 +27,9 @@ pubsub.publish('event', 'hello')
|
|
|
27
27
|
|
|
28
28
|
pubsub.unsubscribe('event', value => { console.log(value) })
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
pubsub.hasSubscribers('event')
|
|
31
|
+
|
|
32
|
+
await pubsub.once('event')
|
|
31
33
|
```
|
|
32
34
|
|
|
33
35
|
## API
|
|
@@ -66,11 +68,14 @@ pubsub.unsubscribe('event-name', data => {
|
|
|
66
68
|
pubsub.publish('event-name', 'data')
|
|
67
69
|
```
|
|
68
70
|
|
|
69
|
-
|
|
70
|
-
`
|
|
71
|
+
#### once
|
|
72
|
+
`name`: name of the channel to publish to<br>
|
|
71
73
|
```js
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
await pubsub.once('event-name')
|
|
75
|
+
```
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
#### hasSubscribers
|
|
78
|
+
`name`: name of the channel to publish to<br>
|
|
79
|
+
```js
|
|
80
|
+
pubsub.hasSubscribers('event-name')
|
|
81
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
class LittlePubSub {
|
|
2
|
+
subscribers = {};
|
|
3
|
+
verbose;
|
|
4
|
+
constructor(verbose = false) {
|
|
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, change) {
|
|
32
|
+
if (!this.hasSubscribers(event))
|
|
33
|
+
return;
|
|
34
|
+
if (this.verbose || this.subscribers[event].value !== change) {
|
|
35
|
+
this.subscribers[event].value = change;
|
|
36
|
+
this.subscribers[event].handlers.forEach((handler) => {
|
|
37
|
+
handler(change, this.subscribers[event].value);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
once(event) {
|
|
42
|
+
return new Promise((resolve) => {
|
|
43
|
+
const cb = (value) => {
|
|
44
|
+
this.unsubscribe(event, cb);
|
|
45
|
+
resolve(value);
|
|
46
|
+
};
|
|
47
|
+
this.subscribe(event, cb);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { LittlePubSub as default };
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vandeurenglenn/little-pubsub",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Publish & Subscribe",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "./typings/littlePubSub.d.ts",
|
|
7
7
|
"repository": "https://github.com/vandeurenglenn/little-pubsub",
|
|
8
8
|
"author": "vandeurenglenn <vandeurenglenn@gmail.com>",
|
|
9
9
|
"license": "MIT",
|
|
10
|
+
"type": "module",
|
|
10
11
|
"private": false,
|
|
11
12
|
"flat": true,
|
|
12
13
|
"scripts": {
|
|
@@ -19,8 +20,9 @@
|
|
|
19
20
|
"pubsub"
|
|
20
21
|
],
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"rollup": "^0.
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
"@rollup/plugin-typescript": "^10.0.1",
|
|
24
|
+
"rollup": "^3.5.1",
|
|
25
|
+
"tape": "^4.13.0",
|
|
26
|
+
"tslib": "^2.4.1"
|
|
27
|
+
}
|
|
26
28
|
}
|
package/rollup.config.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { version, name } = JSON.parse(npmPackage);
|
|
4
|
-
const production = Boolean(process.argv[2] === 'production');
|
|
1
|
+
import typescript from '@rollup/plugin-typescript';
|
|
2
|
+
|
|
5
3
|
export default [
|
|
6
4
|
// CommonJS version, for Node, Browserify & Webpack
|
|
7
5
|
{
|
|
8
|
-
input: ['src/index.
|
|
6
|
+
input: ['src/index.ts'],
|
|
9
7
|
output: {
|
|
10
|
-
dir: './',
|
|
11
|
-
format: '
|
|
12
|
-
sourcemap: false,
|
|
13
|
-
intro: `const ENVIRONMENT = {version: '${version}', production: true};`,
|
|
14
|
-
banner: `/* ${name} version ${version} */`
|
|
8
|
+
dir: './dist',
|
|
9
|
+
format: 'es'
|
|
15
10
|
},
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
plugins: [typescript({
|
|
12
|
+
compilerOptions: {
|
|
13
|
+
target: 'esnext'
|
|
14
|
+
}
|
|
15
|
+
})]
|
|
18
16
|
}
|
|
19
17
|
];
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export default class LittlePubSub implements littlePubSub {
|
|
2
|
+
subscribers: {} = {}
|
|
3
|
+
verbose: boolean
|
|
4
|
+
|
|
5
|
+
constructor(verbose: boolean = false) {
|
|
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)) this.subscribers[event] = { handlers: [], value: undefined};
|
|
22
|
+
|
|
23
|
+
context = this.#handleContext(handler, context)
|
|
24
|
+
this.subscribers[event].handlers.push(handler.bind(context))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
unsubscribe(event: string, handler: Function, context?: Function): void {
|
|
28
|
+
if (!this.hasSubscribers(event)) return
|
|
29
|
+
|
|
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) delete this.subscribers[event];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
publish(event: string, change: string | number | boolean | object | Array<any>): void {
|
|
37
|
+
if (!this.hasSubscribers(event)) return
|
|
38
|
+
|
|
39
|
+
if (this.verbose || this.subscribers[event].value !== change) {
|
|
40
|
+
this.subscribers[event].value = change;
|
|
41
|
+
this.subscribers[event].handlers.forEach((handler: Function) => {
|
|
42
|
+
handler(change, this.subscribers[event].value)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
once(event: string): Promise<string | number | boolean | object | Array<any>> {
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
const cb = (value: string | number | boolean | object | Array<any>) => {
|
|
50
|
+
this.unsubscribe(event, cb)
|
|
51
|
+
resolve(value)
|
|
52
|
+
}
|
|
53
|
+
this.subscribe(event, cb)
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
package/test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import test from 'tape'
|
|
2
|
+
import PubSub from './dist/index.js'
|
|
3
3
|
|
|
4
4
|
test('pubsub is defined', tape => {
|
|
5
5
|
tape.plan(1)
|
|
@@ -17,6 +17,14 @@ test('pubsub is defined', tape => {
|
|
|
17
17
|
pubsub.unsubscribe('on', (value) => tape.ok(value))
|
|
18
18
|
tape.ok(Boolean(Object.keys(pubsub.subscribers).length === 0))
|
|
19
19
|
})
|
|
20
|
+
|
|
21
|
+
test('pubsub once', async (tape) => {
|
|
22
|
+
tape.plan(2)
|
|
23
|
+
setTimeout(() => pubsub.publish('on', true) ,1000)
|
|
24
|
+
let value = await pubsub.once('on')
|
|
25
|
+
console.log(value);
|
|
26
|
+
tape.ok(Boolean(Object.keys(pubsub.subscribers).length === 0))
|
|
27
|
+
})
|
|
20
28
|
|
|
21
29
|
// test('classIs', tape => {
|
|
22
30
|
// tape.plan(1)
|
package/tsconfig.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
'allowSyntheticDefaultImports': true,
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
'allowSyntheticDefaultImports': true,
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"target": "latest"
|
|
11
|
+
},
|
|
12
|
+
"include": [
|
|
13
|
+
"./src/**/*",
|
|
14
|
+
"typings/**/*"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
declare interface littlePubSub {
|
|
2
|
+
subscribers: {}
|
|
3
|
+
verbose: boolean
|
|
4
|
+
|
|
5
|
+
hasSubscribers(event: string): boolean
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {String|Number} event
|
|
9
|
+
* @param {Function} handler
|
|
10
|
+
* @param {Function} context
|
|
11
|
+
*/
|
|
12
|
+
subscribe(event: string, handler: Function, context: Function): void
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {String} event
|
|
16
|
+
* @param {Function} handler
|
|
17
|
+
* @param {Function} context
|
|
18
|
+
*/
|
|
19
|
+
unsubscribe(event: string, handler: Function, context: Function): void
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {String} event
|
|
23
|
+
* @param {String|Number|Boolean|Object|Array} change
|
|
24
|
+
*/
|
|
25
|
+
publish(event: string, change: string | boolean | object | Array<any>): void
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param {String|Number} event
|
|
30
|
+
*/
|
|
31
|
+
once(event: string): Promise<string | number | boolean | object | Array<any>>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare module '@vandeurenglenn/little-pubsub' {
|
|
35
|
+
export default littlePubSub
|
|
36
|
+
}
|
package/index.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/* @vandeurenglenn/little-pubsub version 1.3.0 */
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
const ENVIRONMENT = {version: '1.3.0', production: true};
|
|
5
|
-
|
|
6
|
-
class LittlePubSub {
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates handlers
|
|
10
|
-
*/
|
|
11
|
-
constructor(verbose = true) {
|
|
12
|
-
this.subscribers = {};
|
|
13
|
-
this.verbose = verbose;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @param {String} event
|
|
18
|
-
* @param {Method} handler
|
|
19
|
-
* @param {HTMLElement} context
|
|
20
|
-
*/
|
|
21
|
-
subscribe(event, handler, context) {
|
|
22
|
-
if (typeof context === 'undefined') {
|
|
23
|
-
context = handler;
|
|
24
|
-
}
|
|
25
|
-
this.subscribers[event] = this.subscribers[event] || { handlers: [], value: null};
|
|
26
|
-
this.subscribers[event].handlers.push(handler.bind(context));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @param {String} event
|
|
31
|
-
* @param {Method} handler
|
|
32
|
-
* @param {HTMLElement} context
|
|
33
|
-
*/
|
|
34
|
-
unsubscribe(event, handler, context) {
|
|
35
|
-
if (typeof context === 'undefined') {
|
|
36
|
-
context = handler;
|
|
37
|
-
}
|
|
38
|
-
if (this.subscribers[event]) {
|
|
39
|
-
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
40
|
-
this.subscribers[event].handlers.splice(index);
|
|
41
|
-
if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @param {String} event
|
|
48
|
-
* @param {String|Number|Boolean|Object|Array} change
|
|
49
|
-
*/
|
|
50
|
-
publish(event, change) {
|
|
51
|
-
if (this.subscribers[event]) {
|
|
52
|
-
if (this.verbose || this.subscribers[event].value !== change) {
|
|
53
|
-
this.subscribers[event].value = change;
|
|
54
|
-
this.subscribers[event].handlers.forEach(handler => {
|
|
55
|
-
handler(change, this.subscribers[event].value);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
module.exports = LittlePubSub;
|
package/src/index.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
export default class LittlePubSub {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Creates handlers
|
|
5
|
-
*/
|
|
6
|
-
constructor(verbose = true) {
|
|
7
|
-
this.subscribers = {};
|
|
8
|
-
this.verbose = verbose
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param {String} event
|
|
13
|
-
* @param {Method} handler
|
|
14
|
-
* @param {HTMLElement} context
|
|
15
|
-
*/
|
|
16
|
-
subscribe(event, handler, context) {
|
|
17
|
-
if (typeof context === 'undefined') {
|
|
18
|
-
context = handler;
|
|
19
|
-
}
|
|
20
|
-
this.subscribers[event] = this.subscribers[event] || { handlers: [], value: null};
|
|
21
|
-
this.subscribers[event].handlers.push(handler.bind(context))
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @param {String} event
|
|
26
|
-
* @param {Method} handler
|
|
27
|
-
* @param {HTMLElement} context
|
|
28
|
-
*/
|
|
29
|
-
unsubscribe(event, handler, context) {
|
|
30
|
-
if (typeof context === 'undefined') {
|
|
31
|
-
context = handler;
|
|
32
|
-
}
|
|
33
|
-
if (this.subscribers[event]) {
|
|
34
|
-
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
35
|
-
this.subscribers[event].handlers.splice(index);
|
|
36
|
-
if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @param {String} event
|
|
43
|
-
* @param {String|Number|Boolean|Object|Array} change
|
|
44
|
-
*/
|
|
45
|
-
publish(event, change) {
|
|
46
|
-
if (this.subscribers[event]) {
|
|
47
|
-
if (this.verbose || this.subscribers[event].value !== change) {
|
|
48
|
-
this.subscribers[event].value = change;
|
|
49
|
-
this.subscribers[event].handlers.forEach(handler => {
|
|
50
|
-
handler(change, this.subscribers[event].value)
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|