@vandeurenglenn/little-pubsub 1.4.7 → 1.4.9

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
1
  language: node_js
2
2
  node_js:
3
3
  - '16'
4
- - '18'
5
- cache:
4
+ - '22'
5
+ cache:
6
6
  yarn: true
7
7
  before_script:
8
8
  - npm run build
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
- 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
- }
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,65 @@
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
- }
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
+ const _handler = handler.bind(context);
26
+ this.subscribers[event].handlers.push(_handler);
27
+ if (this.subscribers[event].value !== undefined)
28
+ _handler(this.subscribers[event].value, undefined);
29
+ }
30
+ unsubscribe(event, handler, context) {
31
+ if (!this.hasSubscribers(event))
32
+ return;
33
+ context = this._handleContext(handler, context);
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)
37
+ delete this.subscribers[event];
38
+ }
39
+ publish(event, value, verbose) {
40
+ // always set value even when having no subscribers
41
+ if (!this.hasSubscribers(event))
42
+ this.subscribers[event] = {
43
+ handlers: []
44
+ };
45
+ const oldValue = this.subscribers[event]?.value;
46
+ if (this.verbose || verbose || oldValue !== value) {
47
+ this.subscribers[event].value = value;
48
+ for (const handler of this.subscribers[event].handlers) {
49
+ handler(value, oldValue);
50
+ }
51
+ }
52
+ }
53
+ publishVerbose(event, value) {
54
+ this.publish(event, value, true);
55
+ }
56
+ once(event) {
57
+ return new Promise((resolve) => {
58
+ const cb = (value) => {
59
+ resolve(value);
60
+ this.unsubscribe(event, cb);
61
+ };
62
+ this.subscribe(event, cb);
63
+ });
64
+ }
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vandeurenglenn/little-pubsub",
3
- "version": "1.4.7",
3
+ "version": "1.4.9",
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": "^4.13.0",
31
- "typescript": "^5.1.6"
30
+ "tape": "^5.8.1",
31
+ "typescript": "^5.5.3"
32
32
  }
33
33
  }
package/src/index.ts CHANGED
@@ -17,12 +17,22 @@ 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 }
23
28
 
24
29
  context = this._handleContext(handler, context)
25
- this.subscribers[event].handlers.push(handler.bind(context))
30
+ const _handler = handler.bind(context)
31
+
32
+ this.subscribers[event].handlers.push(_handler)
33
+
34
+ if (this.subscribers[event].value !== undefined)
35
+ _handler(this.subscribers[event].value, undefined)
26
36
  }
27
37
 
28
38
  unsubscribe(event: string, handler: Function, context?: Function): void {
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
- "outDir": "./",
4
- "esModuleInterop": true,
5
- "allowSyntheticDefaultImports": true,
6
- "allowJs": false,
7
- "declaration": true,
8
- "target": "ES2022",
9
- "module": "es2022"
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
- "./src/**/*"
13
- ],
14
- "exclude": [
15
- "node_modules"
16
- ]
17
- }
12
+ "include": ["./src/**/*"],
13
+ "exclude": ["node_modules"]
14
+ }