@vandeurenglenn/little-pubsub 1.4.0 → 1.4.2

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 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 ADDED
@@ -0,0 +1,52 @@
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
+ }
50
+ }
51
+
52
+ export { LittlePubSub as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vandeurenglenn/little-pubsub",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Publish & Subscribe",
5
5
  "main": "dist/index.js",
6
6
  "typings": "./typings/littlePubSub.d.ts",
package/rollup.config.js CHANGED
@@ -1,17 +1,14 @@
1
1
  import typescript from '@rollup/plugin-typescript';
2
+ import tsconfig from './tsconfig.json' assert { type: 'json'}
2
3
 
3
4
  export default [
4
5
  // CommonJS version, for Node, Browserify & Webpack
5
6
  {
6
7
  input: ['src/index.ts'],
7
8
  output: {
8
- dir: './dist',
9
+ dir: './',
9
10
  format: 'es'
10
11
  },
11
- plugins: [typescript({
12
- compilerOptions: {
13
- target: 'esnext'
14
- }
15
- })]
12
+ plugins: [typescript(tsconfig)]
16
13
  }
17
14
  ];
package/src/index.ts CHANGED
@@ -1,12 +1,12 @@
1
- export default class LittlePubSub implements littlePubSub {
1
+ export default class LittlePubSub {
2
2
  subscribers: {} = {}
3
3
  verbose: boolean
4
4
 
5
- constructor(verbose: boolean = false) {
5
+ constructor(verbose?: boolean) {
6
6
  this.verbose = verbose
7
7
  }
8
8
 
9
- #handleContext(handler: Function, context?: Function): Function {
9
+ _handleContext(handler: Function, context?: Function): Function {
10
10
  if (typeof context === 'undefined') {
11
11
  context = handler;
12
12
  }
@@ -20,14 +20,14 @@ export default class LittlePubSub implements littlePubSub {
20
20
  subscribe(event: string, handler: Function, context?: Function): void {
21
21
  if (!this.hasSubscribers(event)) this.subscribers[event] = { handlers: [], value: undefined};
22
22
 
23
- context = this.#handleContext(handler, context)
23
+ context = this._handleContext(handler, context)
24
24
  this.subscribers[event].handlers.push(handler.bind(context))
25
25
  }
26
26
 
27
27
  unsubscribe(event: string, handler: Function, context?: Function): void {
28
28
  if (!this.hasSubscribers(event)) return
29
29
 
30
- context = this.#handleContext(handler, context)
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
33
  if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
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/tsconfig.js DELETED
@@ -1,16 +0,0 @@
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
- }
@@ -1,36 +0,0 @@
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
- }