@vandeurenglenn/little-pubsub 1.4.2 → 1.4.3

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 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,81 @@
1
- # little-pubsub
2
- > Small publish & subscribe class
3
-
4
- ## INSTALL
5
-
6
- #### npm
7
- ```sh
8
- npm i --save @vandeurenglenn/little-pubsub
9
- ```
10
-
11
- ## USAGE
12
-
13
- ```js
14
- import PubSub from '@vandeurenglenn/little-pubsub';
15
- const pubsub = new PubSub();
16
- ```
17
-
18
- ## Example
19
-
20
- ```js
21
- import PubSub from '@vandeurenglenn/little-pubsub';
22
- const pubsub = new PubSub();
23
-
24
- pubsub.subscribe('event', value => { console.log(value) })
25
-
26
- pubsub.publish('event', 'hello')
27
-
28
- pubsub.unsubscribe('event', value => { console.log(value) })
29
-
30
- pubsub.hasSubscribers('event')
31
-
32
- await pubsub.once('event')
33
- ```
34
-
35
- ## API
36
- ### pubsub([options])
37
- `verbose`: when false only fires after value change<br>
38
- ```js
39
- pubsub = new PubSub({
40
- verbose: false // default: true
41
- })
42
- ```
43
-
44
- #### subscribe
45
- `name`: name of the channel to subscribe to<br>
46
- `handler`: method<br>
47
- `context`: context<br>
48
- ```js
49
- pubsub.subscribe('event-name', data => {
50
- console.log(data);
51
- })
52
- ```
53
- #### unsubscribe
54
- `name`: name of the channel to unsubscribe<br>
55
- `handler`: method<br>
56
- `context`: context<br>
57
- ```js
58
- pubsub.unsubscribe('event-name', data => {
59
- console.log(data);
60
- })
61
- ```
62
-
63
- #### publish
64
- `name`: name of the channel to publish to<br>
65
- `handler`: method<br>
66
- `context`: context<br>
67
- ```js
68
- pubsub.publish('event-name', 'data')
69
- ```
70
-
71
- #### once
72
- `name`: name of the channel to publish to<br>
73
- ```js
74
- await pubsub.once('event-name')
75
- ```
76
-
77
- #### hasSubscribers
78
- `name`: name of the channel to publish to<br>
79
- ```js
80
- pubsub.hasSubscribers('event-name')
1
+ # little-pubsub
2
+ > Small publish & subscribe class
3
+
4
+ ## INSTALL
5
+
6
+ #### npm
7
+ ```sh
8
+ npm i --save @vandeurenglenn/little-pubsub
9
+ ```
10
+
11
+ ## USAGE
12
+
13
+ ```js
14
+ import PubSub from '@vandeurenglenn/little-pubsub';
15
+ const pubsub = new PubSub();
16
+ ```
17
+
18
+ ## Example
19
+
20
+ ```js
21
+ import PubSub from '@vandeurenglenn/little-pubsub';
22
+ const pubsub = new PubSub();
23
+
24
+ pubsub.subscribe('event', value => { console.log(value) })
25
+
26
+ pubsub.publish('event', 'hello')
27
+
28
+ pubsub.unsubscribe('event', value => { console.log(value) })
29
+
30
+ pubsub.hasSubscribers('event')
31
+
32
+ await pubsub.once('event')
33
+ ```
34
+
35
+ ## API
36
+ ### pubsub([options])
37
+ `verbose`: when false only fires after value change<br>
38
+ ```js
39
+ pubsub = new PubSub({
40
+ verbose: false // default: true
41
+ })
42
+ ```
43
+
44
+ #### subscribe
45
+ `name`: name of the channel to subscribe to<br>
46
+ `handler`: method<br>
47
+ `context`: context<br>
48
+ ```js
49
+ pubsub.subscribe('event-name', data => {
50
+ console.log(data);
51
+ })
52
+ ```
53
+ #### unsubscribe
54
+ `name`: name of the channel to unsubscribe<br>
55
+ `handler`: method<br>
56
+ `context`: context<br>
57
+ ```js
58
+ pubsub.unsubscribe('event-name', data => {
59
+ console.log(data);
60
+ })
61
+ ```
62
+
63
+ #### publish
64
+ `name`: name of the channel to publish to<br>
65
+ `handler`: method<br>
66
+ `context`: context<br>
67
+ ```js
68
+ pubsub.publish('event-name', 'data')
69
+ ```
70
+
71
+ #### once
72
+ `name`: name of the channel to publish to<br>
73
+ ```js
74
+ await pubsub.once('event-name')
75
+ ```
76
+
77
+ #### hasSubscribers
78
+ `name`: name of the channel to publish to<br>
79
+ ```js
80
+ pubsub.hasSubscribers('event-name')
81
81
  ```
package/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- export default class LittlePubSub {
2
- subscribers: {};
3
- verbose: boolean;
4
- constructor(verbose?: boolean);
5
- _handleContext(handler: Function, context?: Function): Function;
6
- hasSubscribers(event: string): boolean;
7
- subscribe(event: string, handler: Function, context?: Function): void;
8
- unsubscribe(event: string, handler: Function, context?: Function): void;
9
- publish(event: string, change: string | number | boolean | object | Array<any>): void;
10
- once(event: string): Promise<string | number | boolean | object | Array<any>>;
11
- }
1
+ export default class LittlePubSub {
2
+ subscribers: {};
3
+ verbose: boolean;
4
+ constructor(verbose?: boolean);
5
+ _handleContext(handler: Function, context?: Function): Function;
6
+ hasSubscribers(event: string): boolean;
7
+ subscribe(event: string, handler: Function, context?: Function): void;
8
+ unsubscribe(event: string, handler: Function, context?: Function): void;
9
+ publish(event: string, change: string | number | boolean | object | Array<any>): void;
10
+ once(event: string): Promise<string | number | boolean | object | Array<any>>;
11
+ }
package/index.js CHANGED
@@ -1,52 +1,50 @@
1
- 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, 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
- }
1
+ 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, change) {
32
+ if (this.verbose && this.hasSubscribers(event) || this.subscribers?.[event].value !== change) {
33
+ this.subscribers[event].value = change;
34
+ this.subscribers[event].handlers.forEach((handler) => {
35
+ handler(change, this.subscribers[event].value);
36
+ });
37
+ }
38
+ }
39
+ once(event) {
40
+ return new Promise((resolve) => {
41
+ const cb = (value) => {
42
+ this.unsubscribe(event, cb);
43
+ resolve(value);
44
+ };
45
+ this.subscribe(event, cb);
46
+ });
47
+ }
50
48
  }
51
49
 
52
50
  export { LittlePubSub as default };
package/package.json CHANGED
@@ -1,28 +1,34 @@
1
- {
2
- "name": "@vandeurenglenn/little-pubsub",
3
- "version": "1.4.2",
4
- "description": "Publish & Subscribe",
5
- "main": "dist/index.js",
6
- "typings": "./typings/littlePubSub.d.ts",
7
- "repository": "https://github.com/vandeurenglenn/little-pubsub",
8
- "author": "vandeurenglenn <vandeurenglenn@gmail.com>",
9
- "license": "MIT",
10
- "type": "module",
11
- "private": false,
12
- "flat": true,
13
- "scripts": {
14
- "build": "rollup -c",
15
- "test": "node test.js"
16
- },
17
- "keywords": [
18
- "publish",
19
- "subscribe",
20
- "pubsub"
21
- ],
22
- "devDependencies": {
23
- "@rollup/plugin-typescript": "^10.0.1",
24
- "rollup": "^3.5.1",
25
- "tape": "^4.13.0",
26
- "tslib": "^2.4.1"
27
- }
28
- }
1
+ {
2
+ "name": "@vandeurenglenn/little-pubsub",
3
+ "version": "1.4.3",
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": "rollup -c",
21
+ "test": "node test.js"
22
+ },
23
+ "keywords": [
24
+ "publish",
25
+ "subscribe",
26
+ "pubsub"
27
+ ],
28
+ "devDependencies": {
29
+ "@rollup/plugin-typescript": "^10.0.1",
30
+ "rollup": "^3.5.1",
31
+ "tape": "^4.13.0",
32
+ "tslib": "^2.4.1"
33
+ }
34
+ }
package/rollup.config.js CHANGED
@@ -1,14 +1,14 @@
1
- import typescript from '@rollup/plugin-typescript';
2
- import tsconfig from './tsconfig.json' assert { type: 'json'}
3
-
4
- export default [
5
- // CommonJS version, for Node, Browserify & Webpack
6
- {
7
- input: ['src/index.ts'],
8
- output: {
9
- dir: './',
10
- format: 'es'
11
- },
12
- plugins: [typescript(tsconfig)]
13
- }
14
- ];
1
+ import typescript from '@rollup/plugin-typescript';
2
+ import tsconfig from './tsconfig.json' assert { type: 'json'}
3
+
4
+ export default [
5
+ // CommonJS version, for Node, Browserify & Webpack
6
+ {
7
+ input: ['src/index.ts'],
8
+ output: {
9
+ dir: './',
10
+ format: 'es'
11
+ },
12
+ plugins: [typescript(tsconfig)]
13
+ }
14
+ ];
package/src/index.ts CHANGED
@@ -1,56 +1,54 @@
1
- export default class LittlePubSub {
2
- subscribers: {} = {}
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)) 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
- }
1
+ export default class LittlePubSub {
2
+ subscribers: {} = {}
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)) 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.verbose && this.hasSubscribers(event) || this.subscribers?.[event].value !== change) {
38
+ this.subscribers[event].value = change;
39
+ this.subscribers[event].handlers.forEach((handler: Function) => {
40
+ handler(change, this.subscribers[event].value)
41
+ })
42
+ }
43
+ }
44
+
45
+ once(event: string): Promise<string | number | boolean | object | Array<any>> {
46
+ return new Promise((resolve) => {
47
+ const cb = (value: string | number | boolean | object | Array<any>) => {
48
+ this.unsubscribe(event, cb)
49
+ resolve(value)
50
+ }
51
+ this.subscribe(event, cb)
52
+ })
53
+ }
54
+ }
package/test.js CHANGED
@@ -1,33 +1,33 @@
1
- import test from 'tape'
2
- import PubSub from './dist/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', tape => {
10
- tape.plan(1)
11
- pubsub.subscribe('on', (value) => tape.ok(value))
12
- pubsub.publish('on', 5)
13
- })
14
-
15
- test('pubsub unsubscribes', tape => {
16
- tape.plan(1)
17
- pubsub.unsubscribe('on', (value) => tape.ok(value))
18
- tape.ok(Boolean(Object.keys(pubsub.subscribers).length === 0))
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
- })
28
-
29
- // test('classIs', tape => {
30
- // tape.plan(1)
31
- // tape.ok(PubSub.isLittlePubSub(pubsub))
32
- // })
33
- });
1
+ import test from 'tape'
2
+ import PubSub from './dist/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', tape => {
10
+ tape.plan(1)
11
+ pubsub.subscribe('on', (value) => tape.ok(value))
12
+ pubsub.publish('on', 5)
13
+ })
14
+
15
+ test('pubsub unsubscribes', tape => {
16
+ tape.plan(1)
17
+ pubsub.unsubscribe('on', (value) => tape.ok(value))
18
+ tape.ok(Boolean(Object.keys(pubsub.subscribers).length === 0))
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
+ })
28
+
29
+ // test('classIs', tape => {
30
+ // tape.plan(1)
31
+ // tape.ok(PubSub.isLittlePubSub(pubsub))
32
+ // })
33
+ });
package/tsconfig.json CHANGED
@@ -1,17 +1,17 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "./",
4
- "esModuleInterop": true,
5
- "allowSyntheticDefaultImports": true,
6
- "allowJs": true,
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": true,
7
+ "declaration": true,
8
+ "target": "ES2022",
9
+ "module": "es2022"
10
+ },
11
+ "include": [
12
+ "./src/**/*"
13
+ ],
14
+ "exclude": [
15
+ "node_modules"
16
+ ]
17
17
  }
package/dist/index.js DELETED
@@ -1,52 +0,0 @@
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 };