@player-ui/pubsub-plugin 0.4.0-next.9 → 0.4.1-next.0

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/dist/index.cjs.js CHANGED
@@ -104,8 +104,13 @@ const _PubSubPlugin = class {
104
104
  this.symbol = _PubSubPlugin.Symbol;
105
105
  var _a;
106
106
  this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
107
+ this.pubsub = pubsub;
107
108
  }
108
109
  apply(player) {
110
+ const existing = player.findPlugin(PubSubPluginSymbol);
111
+ if (existing !== void 0) {
112
+ this.pubsub = existing.pubsub;
113
+ }
109
114
  player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
110
115
  const existingExpression = expEvaluator.operators.expressions.get(this.expressionName);
111
116
  if (existingExpression) {
@@ -123,16 +128,16 @@ const _PubSubPlugin = class {
123
128
  });
124
129
  }
125
130
  publish(event, ...args) {
126
- pubsub.publish(event, ...args);
131
+ this.pubsub.publish(event, ...args);
127
132
  }
128
133
  subscribe(event, handler) {
129
- return pubsub.subscribe(event, handler);
134
+ return this.pubsub.subscribe(event, handler);
130
135
  }
131
136
  unsubscribe(token) {
132
- pubsub.unsubscribe(token);
137
+ this.pubsub.unsubscribe(token);
133
138
  }
134
139
  clear() {
135
- pubsub.clear();
140
+ this.pubsub.clear();
136
141
  }
137
142
  };
138
143
  let PubSubPlugin = _PubSubPlugin;
package/dist/index.d.ts CHANGED
@@ -5,6 +5,51 @@ import { PlayerPlugin, Player, InProgressState } from '@player-ui/player';
5
5
  * multiple arguments could be passed into the publish and subscription handlers.
6
6
  */
7
7
  declare type SubscribeHandler<T extends string, A extends unknown[]> = (type: T, ...args: A) => void;
8
+ /**
9
+ * Tiny pubsub maker
10
+ */
11
+ declare class TinyPubSub {
12
+ private events;
13
+ private tokens;
14
+ constructor();
15
+ /**
16
+ * Publish an event with any number of additional arguments
17
+ */
18
+ publish(event: string, ...args: unknown[]): void;
19
+ /**
20
+ * Subscribe to an event
21
+ *
22
+ * Events are also heirarchical when separated by a period. Given the following:
23
+ *
24
+ * publish('a.b.c', 'one', 'two', 'three)
25
+ *
26
+ * The subscribe event will be called when the event is passed as 'a', 'a.b', or 'a.b.c'.
27
+ *
28
+ * @example
29
+ * // subscribes to the top level 'a' publish
30
+ * subscribe('a', (event, ...args) => console.log(event, ...args))
31
+ */
32
+ subscribe(event: string, handler: SubscribeHandler<any, any>): string;
33
+ /**
34
+ * Unsubscribes to a specific subscription given it's symbol or an entire
35
+ * event when passed as a string.
36
+ *
37
+ * When existing subscriptions exist for heirarchical events such as 'a.b.c',
38
+ * when passing an event 'a' to unsubscribe, all subscriptions for 'a', 'a.b',
39
+ * & 'a.b.c' will be unsubscribed as well.
40
+ */
41
+ unsubscribe(value: string | symbol): void;
42
+ /**
43
+ * Get the number of subscriptions for a specific event, or when left blank
44
+ * will return the overall number of subscriptions for the entire pubsub.
45
+ */
46
+ count(event?: string): number;
47
+ /**
48
+ * Deletes all existing subscriptions
49
+ */
50
+ clear(): void;
51
+ private deliver;
52
+ }
8
53
 
9
54
  interface PubSubConfig {
10
55
  /** A custom expression name to register */
@@ -23,6 +68,7 @@ declare class PubSubPlugin implements PlayerPlugin {
23
68
  name: string;
24
69
  static Symbol: symbol;
25
70
  readonly symbol: symbol;
71
+ protected pubsub: TinyPubSub;
26
72
  private expressionName;
27
73
  constructor(config?: PubSubConfig);
28
74
  apply(player: Player): void;
package/dist/index.esm.js CHANGED
@@ -100,8 +100,13 @@ const _PubSubPlugin = class {
100
100
  this.symbol = _PubSubPlugin.Symbol;
101
101
  var _a;
102
102
  this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
103
+ this.pubsub = pubsub;
103
104
  }
104
105
  apply(player) {
106
+ const existing = player.findPlugin(PubSubPluginSymbol);
107
+ if (existing !== void 0) {
108
+ this.pubsub = existing.pubsub;
109
+ }
105
110
  player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
106
111
  const existingExpression = expEvaluator.operators.expressions.get(this.expressionName);
107
112
  if (existingExpression) {
@@ -119,16 +124,16 @@ const _PubSubPlugin = class {
119
124
  });
120
125
  }
121
126
  publish(event, ...args) {
122
- pubsub.publish(event, ...args);
127
+ this.pubsub.publish(event, ...args);
123
128
  }
124
129
  subscribe(event, handler) {
125
- return pubsub.subscribe(event, handler);
130
+ return this.pubsub.subscribe(event, handler);
126
131
  }
127
132
  unsubscribe(token) {
128
- pubsub.unsubscribe(token);
133
+ this.pubsub.unsubscribe(token);
129
134
  }
130
135
  clear() {
131
- pubsub.clear();
136
+ this.pubsub.clear();
132
137
  }
133
138
  };
134
139
  let PubSubPlugin = _PubSubPlugin;
@@ -210,8 +210,13 @@ const _PubSubPlugin = class {
210
210
  this.symbol = _PubSubPlugin.Symbol;
211
211
  var _a;
212
212
  this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
213
+ this.pubsub = pubsub;
213
214
  }
214
215
  apply(player) {
216
+ const existing = player.findPlugin(PubSubPluginSymbol);
217
+ if (existing !== void 0) {
218
+ this.pubsub = existing.pubsub;
219
+ }
215
220
  player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
216
221
  const existingExpression = expEvaluator.operators.expressions.get(this.expressionName);
217
222
  if (existingExpression) {
@@ -229,16 +234,16 @@ const _PubSubPlugin = class {
229
234
  });
230
235
  }
231
236
  publish(event, ...args) {
232
- pubsub.publish(event, ...args);
237
+ this.pubsub.publish(event, ...args);
233
238
  }
234
239
  subscribe(event, handler) {
235
- return pubsub.subscribe(event, handler);
240
+ return this.pubsub.subscribe(event, handler);
236
241
  }
237
242
  unsubscribe(token) {
238
- pubsub.unsubscribe(token);
243
+ this.pubsub.unsubscribe(token);
239
244
  }
240
245
  clear() {
241
- pubsub.clear();
246
+ this.pubsub.clear();
242
247
  }
243
248
  };
244
249
  let PubSubPlugin = _PubSubPlugin;
@@ -1 +1 @@
1
- !function(e,t){"object"===typeof exports&&"object"===typeof module?module.exports=t():"function"===typeof define&&define.amd?define([],t):"object"===typeof exports?exports.PubSubPlugin=t():e.PubSubPlugin=t()}(this,(function(){return function(e){var t={};function n(s){if(t[s])return t[s].exports;var r=t[s]={i:s,l:!1,exports:{}};return e[s].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,s){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:s})},n.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"===typeof e&&e&&e.__esModule)return e;var s=Object.create(null);if(n.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(s,r,function(t){return e[t]}.bind(null,r));return s},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t),n.d(t,"PubSubHandlerPlugin",(function(){return l})),n.d(t,"PubSubPlugin",(function(){return u})),n.d(t,"PubSubPluginSymbol",(function(){return i}));let s=1;const r=new class{constructor(){this.events=new Map,this.tokens=new Map}publish(e,...t){if("string"===typeof e){if(e.includes(".")){const n=function(e){return e.split(".").reduce(((e,t,n)=>0===n?[t]:[...e,`${e[n-1]}.${t}`]),[])}(e);n.forEach((n=>{this.deliver(n,e,...t)}))}else this.deliver(e,e,...t);this.deliver("*",e,...t)}}subscribe(e,t){const n="uuid_"+ ++s;if("string"===typeof e){this.events.has(e)||this.events.set(e,new Map);this.events.get(e).set(n,t),this.tokens.set(n,e)}return n}unsubscribe(e){if("string"===typeof e&&e.startsWith("uuid")){const t=this.tokens.get(e);if("undefined"===typeof t)return;const n=this.events.get(t);return null==n||n.delete(e),void this.tokens.delete(e)}if("string"===typeof e)for(const t of this.events.keys())if(0===t.indexOf(e)){const e=this.events.get(t);if(e&&e.size)for(const t of e.keys())this.tokens.delete(t);this.events.delete(t)}}count(e){let t=0;if("undefined"===typeof e){for(const e of this.events.values())t+=e.size;return t}const n=this.events.get(e);return(null==n?void 0:n.size)?n.size:t}clear(){this.events.clear(),this.tokens.clear()}deliver(e,t,...n){const s=this.events.get(e);if(s&&s.size)for(const r of s.values())r(t,...n)}},i=Symbol.for("PubSubPlugin"),o=class{constructor(e){var t;this.name="pub-sub",this.symbol=o.Symbol,this.expressionName=null!=(t=null==e?void 0:e.expressionName)?t:"publish"}apply(e){e.hooks.expressionEvaluator.tap(this.name,(t=>{t.operators.expressions.get(this.expressionName)?e.logger.warn(`[PubSubPlugin] expression ${this.expressionName} is already registered.`):t.addExpressionFunction(this.expressionName,((e,t,...n)=>{"string"===typeof t&&this.publish(t,...n)}))})),e.hooks.onEnd.tap(this.name,(()=>{this.clear()}))}publish(e,...t){r.publish(e,...t)}subscribe(e,t){return r.subscribe(e,t)}unsubscribe(e){r.unsubscribe(e)}clear(){r.clear()}};let u=o;u.Symbol=i;class l{constructor(e){this.name="pubsub-handler",this.subscriptions=e}apply(e){const t=function(e){const t=e.findPlugin(i),n=t||new u;return t||e.registerPlugin(n),n}(e);e.hooks.onStart.tap(this.name,(()=>{this.subscriptions.forEach(((n,s)=>{t.subscribe(s,((t,...r)=>{const i=e.getState();if("in-progress"===i.status)return n(i,...r);e.logger.info(`[PubSubHandlerPlugin] subscriber for ${s} was called when player was not in-progress`)}))}))}))}}}])}));
1
+ !function(e,t){"object"===typeof exports&&"object"===typeof module?module.exports=t():"function"===typeof define&&define.amd?define([],t):"object"===typeof exports?exports.PubSubPlugin=t():e.PubSubPlugin=t()}(this,(function(){return function(e){var t={};function s(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,s),i.l=!0,i.exports}return s.m=e,s.c=t,s.d=function(e,t,n){s.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},s.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},s.t=function(e,t){if(1&t&&(e=s(e)),8&t)return e;if(4&t&&"object"===typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(s.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)s.d(n,i,function(t){return e[t]}.bind(null,i));return n},s.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return s.d(t,"a",t),t},s.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},s.p="",s(s.s=0)}([function(e,t,s){"use strict";s.r(t),s.d(t,"PubSubHandlerPlugin",(function(){return l})),s.d(t,"PubSubPlugin",(function(){return u})),s.d(t,"PubSubPluginSymbol",(function(){return r}));let n=1;const i=new class{constructor(){this.events=new Map,this.tokens=new Map}publish(e,...t){if("string"===typeof e){if(e.includes(".")){const s=function(e){return e.split(".").reduce(((e,t,s)=>0===s?[t]:[...e,`${e[s-1]}.${t}`]),[])}(e);s.forEach((s=>{this.deliver(s,e,...t)}))}else this.deliver(e,e,...t);this.deliver("*",e,...t)}}subscribe(e,t){const s="uuid_"+ ++n;if("string"===typeof e){this.events.has(e)||this.events.set(e,new Map);this.events.get(e).set(s,t),this.tokens.set(s,e)}return s}unsubscribe(e){if("string"===typeof e&&e.startsWith("uuid")){const t=this.tokens.get(e);if("undefined"===typeof t)return;const s=this.events.get(t);return null==s||s.delete(e),void this.tokens.delete(e)}if("string"===typeof e)for(const t of this.events.keys())if(0===t.indexOf(e)){const e=this.events.get(t);if(e&&e.size)for(const t of e.keys())this.tokens.delete(t);this.events.delete(t)}}count(e){let t=0;if("undefined"===typeof e){for(const e of this.events.values())t+=e.size;return t}const s=this.events.get(e);return(null==s?void 0:s.size)?s.size:t}clear(){this.events.clear(),this.tokens.clear()}deliver(e,t,...s){const n=this.events.get(e);if(n&&n.size)for(const i of n.values())i(t,...s)}},r=Symbol.for("PubSubPlugin"),o=class{constructor(e){var t;this.name="pub-sub",this.symbol=o.Symbol,this.expressionName=null!=(t=null==e?void 0:e.expressionName)?t:"publish",this.pubsub=i}apply(e){const t=e.findPlugin(r);void 0!==t&&(this.pubsub=t.pubsub),e.hooks.expressionEvaluator.tap(this.name,(t=>{t.operators.expressions.get(this.expressionName)?e.logger.warn(`[PubSubPlugin] expression ${this.expressionName} is already registered.`):t.addExpressionFunction(this.expressionName,((e,t,...s)=>{"string"===typeof t&&this.publish(t,...s)}))})),e.hooks.onEnd.tap(this.name,(()=>{this.clear()}))}publish(e,...t){this.pubsub.publish(e,...t)}subscribe(e,t){return this.pubsub.subscribe(e,t)}unsubscribe(e){this.pubsub.unsubscribe(e)}clear(){this.pubsub.clear()}};let u=o;u.Symbol=r;class l{constructor(e){this.name="pubsub-handler",this.subscriptions=e}apply(e){const t=function(e){const t=e.findPlugin(r),s=t||new u;return t||e.registerPlugin(s),s}(e);e.hooks.onStart.tap(this.name,(()=>{this.subscriptions.forEach(((s,n)=>{t.subscribe(n,((t,...i)=>{const r=e.getState();if("in-progress"===r.status)return s(r,...i);e.logger.info(`[PubSubHandlerPlugin] subscriber for ${n} was called when player was not in-progress`)}))}))}))}}}])}));
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@player-ui/pubsub-plugin",
3
- "version": "0.4.0-next.9",
3
+ "version": "0.4.1-next.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org"
7
7
  },
8
8
  "peerDependencies": {
9
- "@player-ui/player": "0.4.0-next.9"
9
+ "@player-ui/player": "0.4.1-next.0"
10
10
  },
11
11
  "dependencies": {
12
12
  "tapable-ts": "^0.2.3",
package/src/plugin.ts CHANGED
@@ -3,7 +3,7 @@ import type {
3
3
  PlayerPlugin,
4
4
  ExpressionContext,
5
5
  } from '@player-ui/player';
6
- import type { SubscribeHandler } from './pubsub';
6
+ import type { SubscribeHandler, TinyPubSub } from './pubsub';
7
7
  import { pubsub } from './pubsub';
8
8
  import { PubSubPluginSymbol } from './symbols';
9
9
 
@@ -27,13 +27,23 @@ export class PubSubPlugin implements PlayerPlugin {
27
27
  static Symbol = PubSubPluginSymbol;
28
28
  public readonly symbol = PubSubPlugin.Symbol;
29
29
 
30
+ protected pubsub: TinyPubSub;
31
+
30
32
  private expressionName: string;
31
33
 
32
34
  constructor(config?: PubSubConfig) {
33
35
  this.expressionName = config?.expressionName ?? 'publish';
36
+ this.pubsub = pubsub;
34
37
  }
35
38
 
36
39
  apply(player: Player) {
40
+ // if there is already a pubsub plugin, reuse its pubsub instance
41
+ // to maintain the singleton across bundles for iOS/Android
42
+ const existing = player.findPlugin<PubSubPlugin>(PubSubPluginSymbol);
43
+ if (existing !== undefined) {
44
+ this.pubsub = existing.pubsub;
45
+ }
46
+
37
47
  player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
38
48
  const existingExpression = expEvaluator.operators.expressions.get(
39
49
  this.expressionName
@@ -67,7 +77,7 @@ export class PubSubPlugin implements PlayerPlugin {
67
77
  * @param data - Any additional data to attach to the event
68
78
  */
69
79
  publish(event: string, ...args: unknown[]) {
70
- pubsub.publish(event, ...args);
80
+ this.pubsub.publish(event, ...args);
71
81
  }
72
82
 
73
83
  /**
@@ -81,7 +91,7 @@ export class PubSubPlugin implements PlayerPlugin {
81
91
  event: T,
82
92
  handler: SubscribeHandler<T, A>
83
93
  ) {
84
- return pubsub.subscribe(event, handler);
94
+ return this.pubsub.subscribe(event, handler);
85
95
  }
86
96
 
87
97
  /**
@@ -90,13 +100,13 @@ export class PubSubPlugin implements PlayerPlugin {
90
100
  * @param token - A token from a `subscribe` call
91
101
  */
92
102
  unsubscribe(token: string) {
93
- pubsub.unsubscribe(token);
103
+ this.pubsub.unsubscribe(token);
94
104
  }
95
105
 
96
106
  /**
97
107
  * Remove all subscriptions
98
108
  */
99
109
  clear() {
100
- pubsub.clear();
110
+ this.pubsub.clear();
101
111
  }
102
112
  }
package/src/pubsub.ts CHANGED
@@ -28,7 +28,7 @@ let count = 1;
28
28
  /**
29
29
  * Tiny pubsub maker
30
30
  */
31
- class TinyPubSub {
31
+ export class TinyPubSub {
32
32
  private events: Map<string, Map<PubSubUUID, SubscribeHandler<any, any>>>;
33
33
  private tokens: Map<PubSubUUID, string>;
34
34