@vandeurenglenn/little-pubsub 1.3.3 → 1.4.1

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,7 +1,7 @@
1
1
  language: node_js
2
2
  node_js:
3
- - '8'
4
- - '9'
3
+ - '16'
4
+ - '18'
5
5
  cache:
6
6
  yarn: true
7
7
  before_script:
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
- PubSub.isLittlePubSub(pubsub)
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
- <!-- #### isLittlePubSub
70
- `instance`: instance to check<br>
71
+ #### once
72
+ `name`: name of the channel to publish to<br>
71
73
  ```js
72
- const LittlePubSub = require('little-pubsub')
73
- const pubsub = new LittlePubSub()
74
+ await pubsub.once('event-name')
75
+ ```
74
76
 
75
- LittlePubSub.isLittlePubSub(pubsub)
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/index.d.ts ADDED
@@ -0,0 +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
+ }
package/index.js CHANGED
@@ -1,62 +1,52 @@
1
- /* @vandeurenglenn/little-pubsub version 1.3.0 */
2
- 'use strict';
3
-
4
- const ENVIRONMENT = {version: '1.3.0', production: true};
5
-
6
1
  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;
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));
24
21
  }
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;
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];
37
30
  }
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];
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
+ }
42
40
  }
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);
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);
56
48
  });
57
- }
58
49
  }
59
- }
60
50
  }
61
51
 
62
- module.exports = LittlePubSub;
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.3",
3
+ "version": "1.4.1",
4
4
  "description": "Publish & Subscribe",
5
- "main": "index.js",
6
- "module": "src/index.js",
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.58.2",
23
- "tape": "^4.13.0"
24
- },
25
- "dependencies": {}
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,14 @@
1
- const readFileSync = require('fs').readFileSync;
2
- const npmPackage = readFileSync('package.json', 'utf8');
3
- const { version, name } = JSON.parse(npmPackage);
4
- const production = Boolean(process.argv[2] === 'production');
1
+ import typescript from '@rollup/plugin-typescript';
2
+ import tsconfig from './tsconfig.json' assert { type: 'json'}
3
+
5
4
  export default [
6
5
  // CommonJS version, for Node, Browserify & Webpack
7
6
  {
8
- input: ['src/index.js'],
7
+ input: ['src/index.ts'],
9
8
  output: {
10
9
  dir: './',
11
- format: 'cjs',
12
- sourcemap: false,
13
- intro: `const ENVIRONMENT = {version: '${version}', production: true};`,
14
- banner: `/* ${name} version ${version} */`
10
+ format: 'es'
15
11
  },
16
- experimentalCodeSplitting: true,
17
- experimentalDynamicImport: true
12
+ plugins: [typescript(tsconfig)]
18
13
  }
19
14
  ];
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ export default class 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
- const test = require('tape')
2
- const PubSub = require('.');
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.json ADDED
@@ -0,0 +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
+ ]
17
+ }
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
- }