@vandeurenglenn/little-pubsub 1.4.3 → 1.4.5

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/index.d.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  export default class LittlePubSub {
2
- subscribers: {};
2
+ subscribers: {
3
+ [index: string]: {
4
+ value?: any;
5
+ handlers?: Function[];
6
+ };
7
+ };
3
8
  verbose: boolean;
4
9
  constructor(verbose?: boolean);
5
10
  _handleContext(handler: Function, context?: Function): Function;
6
11
  hasSubscribers(event: string): boolean;
7
12
  subscribe(event: string, handler: Function, context?: Function): void;
8
13
  unsubscribe(event: string, handler: Function, context?: Function): void;
9
- publish(event: string, change: string | number | boolean | object | Array<any>): void;
14
+ publish(event: string, value: string | number | boolean | object | Array<any>): void;
10
15
  once(event: string): Promise<string | number | boolean | object | Array<any>>;
11
16
  }
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- class LittlePubSub {
1
+ export default class LittlePubSub {
2
2
  subscribers = {};
3
3
  verbose;
4
4
  constructor(verbose) {
@@ -28,13 +28,16 @@ class LittlePubSub {
28
28
  if (this.subscribers[event].handlers.length === 0)
29
29
  delete this.subscribers[event];
30
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
- }
31
+ publish(event, value) {
32
+ // always set value even when having no subscribers
33
+ if (!this.hasSubscribers(event))
34
+ this.subscribers[event] = {};
35
+ const oldValue = this.subscribers[event]?.value;
36
+ this.subscribers[event].value = value;
37
+ if (this.verbose || oldValue !== value)
38
+ for (const handler of this.subscribers[event].handlers) {
39
+ handler(value, oldValue);
40
+ }
38
41
  }
39
42
  once(event) {
40
43
  return new Promise((resolve) => {
@@ -46,5 +49,3 @@ class LittlePubSub {
46
49
  });
47
50
  }
48
51
  }
49
-
50
- export { LittlePubSub as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vandeurenglenn/little-pubsub",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Publish & Subscribe",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
@@ -17,18 +17,17 @@
17
17
  "private": false,
18
18
  "flat": true,
19
19
  "scripts": {
20
- "build": "rollup -c",
20
+ "build": "npx tsc",
21
21
  "test": "node test.js"
22
22
  },
23
23
  "keywords": [
24
24
  "publish",
25
25
  "subscribe",
26
- "pubsub"
26
+ "pubsub",
27
+ "once"
27
28
  ],
28
29
  "devDependencies": {
29
- "@rollup/plugin-typescript": "^10.0.1",
30
- "rollup": "^3.5.1",
31
30
  "tape": "^4.13.0",
32
- "tslib": "^2.4.1"
31
+ "typescript": "^5.1.6"
33
32
  }
34
33
  }
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export default class LittlePubSub {
2
- subscribers: {} = {}
2
+ subscribers: {[index: string]: { value?: any, handlers?: Function[] }} = {}
3
3
  verbose: boolean
4
4
 
5
5
  constructor(verbose?: boolean) {
@@ -30,16 +30,19 @@ export default class LittlePubSub {
30
30
  context = this._handleContext(handler, context)
31
31
  const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
32
32
  this.subscribers[event].handlers.splice(index);
33
- if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
33
+ if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
34
34
  }
35
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
- }
36
+ publish(event: string, value: string | number | boolean | object | Array<any>): void {
37
+ // always set value even when having no subscribers
38
+ if (!this.hasSubscribers(event)) this.subscribers[event] = {}
39
+ const oldValue = this.subscribers[event]?.value
40
+ this.subscribers[event].value = value;
41
+
42
+ if (this.verbose || oldValue !== value)
43
+ for (const handler of this.subscribers[event].handlers) {
44
+ handler(value, oldValue)
45
+ }
43
46
  }
44
47
 
45
48
  once(event: string): Promise<string | number | boolean | object | Array<any>> {
package/test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import test from 'tape'
2
- import PubSub from './dist/index.js'
2
+ import PubSub from './index.js'
3
3
 
4
4
  test('pubsub is defined', tape => {
5
5
  tape.plan(1)
@@ -19,15 +19,9 @@ test('pubsub is defined', tape => {
19
19
  })
20
20
 
21
21
  test('pubsub once', async (tape) => {
22
- tape.plan(2)
22
+ tape.plan(1)
23
23
  setTimeout(() => pubsub.publish('on', true) ,1000)
24
24
  let value = await pubsub.once('on')
25
- console.log(value);
26
- tape.ok(Boolean(Object.keys(pubsub.subscribers).length === 0))
25
+ tape.ok((value === true))
27
26
  })
28
-
29
- // test('classIs', tape => {
30
- // tape.plan(1)
31
- // tape.ok(PubSub.isLittlePubSub(pubsub))
32
- // })
33
27
  });
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "outDir": "./",
4
4
  "esModuleInterop": true,
5
5
  "allowSyntheticDefaultImports": true,
6
- "allowJs": true,
6
+ "allowJs": false,
7
7
  "declaration": true,
8
8
  "target": "ES2022",
9
9
  "module": "es2022"
package/rollup.config.js DELETED
@@ -1,14 +0,0 @@
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
- ];