@player-ui/pubsub-plugin 0.8.0--canary.307.9621 → 0.8.0-next.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/dist/index.d.ts DELETED
@@ -1,116 +0,0 @@
1
- import { PlayerPlugin, Player, InProgressState } from '@player-ui/player';
2
-
3
- /**
4
- * Based off the pubsub-js library and rewritten to match the same used APIs but modified so that
5
- * multiple arguments could be passed into the publish and subscription handlers.
6
- */
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
- }
53
-
54
- interface PubSubConfig {
55
- /** A custom expression name to register */
56
- expressionName: string;
57
- }
58
- /**
59
- * The PubSubPlugin is a great way to enable your FRF content to publish events back to your app
60
- * It injects a publish() function into the expression language, and will forward all events back to any subscribers.
61
- *
62
- * Published/Subscribed events support a hierarchy:
63
- * - publish('foo', 'data') -- will trigger any listeners for 'foo'
64
- * - publish('foo.bar', 'data') -- will trigger any listeners for 'foo' or 'foo.bar'
65
- *
66
- */
67
- declare class PubSubPlugin implements PlayerPlugin {
68
- name: string;
69
- static Symbol: symbol;
70
- readonly symbol: symbol;
71
- protected pubsub: TinyPubSub;
72
- private expressionName;
73
- constructor(config?: PubSubConfig);
74
- apply(player: Player): void;
75
- /**
76
- * A way of publishing an event, notifying any listeners
77
- *
78
- * @param event - The name of the event to publish. Can take sub-topics like: foo.bar
79
- * @param data - Any additional data to attach to the event
80
- */
81
- publish(event: string, ...args: unknown[]): void;
82
- /**
83
- * Subscribe to an event with the given name. The handler will get called for any published event
84
- *
85
- * @param event - The name of the event to subscribe to
86
- * @param handler - A function to be called when the event is triggered
87
- * @returns A token to be used to unsubscribe from the event
88
- */
89
- subscribe<T extends string, A extends unknown[]>(event: T, handler: SubscribeHandler<T, A>): string;
90
- /**
91
- * Remove any subscriptions using the given token
92
- *
93
- * @param token - A token from a `subscribe` call
94
- */
95
- unsubscribe(token: string): void;
96
- /**
97
- * Remove all subscriptions
98
- */
99
- clear(): void;
100
- }
101
-
102
- declare const PubSubPluginSymbol: unique symbol;
103
-
104
- declare type PubSubHandler<T extends unknown[]> = (context: InProgressState, ...args: T) => void;
105
- declare type SubscriptionMap = Map<string, PubSubHandler<any>>;
106
- /**
107
- * Plugin to easily add subscribers to the PubSubPlugin
108
- */
109
- declare class PubSubHandlerPlugin implements PlayerPlugin {
110
- name: string;
111
- private subscriptions;
112
- constructor(subscriptions: SubscriptionMap);
113
- apply(player: Player): void;
114
- }
115
-
116
- export { PubSubConfig, PubSubHandler, PubSubHandlerPlugin, PubSubPlugin, PubSubPluginSymbol, SubscriptionMap };
@@ -1,289 +0,0 @@
1
- (function webpackUniversalModuleDefinition(root, factory) {
2
- if(typeof exports === 'object' && typeof module === 'object')
3
- module.exports = factory();
4
- else if(typeof define === 'function' && define.amd)
5
- define([], factory);
6
- else if(typeof exports === 'object')
7
- exports["PubSubPlugin"] = factory();
8
- else
9
- root["PubSubPlugin"] = factory();
10
- })(this, function() {
11
- return /******/ (function(modules) { // webpackBootstrap
12
- /******/ // The module cache
13
- /******/ var installedModules = {};
14
- /******/
15
- /******/ // The require function
16
- /******/ function __webpack_require__(moduleId) {
17
- /******/
18
- /******/ // Check if module is in cache
19
- /******/ if(installedModules[moduleId]) {
20
- /******/ return installedModules[moduleId].exports;
21
- /******/ }
22
- /******/ // Create a new module (and put it into the cache)
23
- /******/ var module = installedModules[moduleId] = {
24
- /******/ i: moduleId,
25
- /******/ l: false,
26
- /******/ exports: {}
27
- /******/ };
28
- /******/
29
- /******/ // Execute the module function
30
- /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31
- /******/
32
- /******/ // Flag the module as loaded
33
- /******/ module.l = true;
34
- /******/
35
- /******/ // Return the exports of the module
36
- /******/ return module.exports;
37
- /******/ }
38
- /******/
39
- /******/
40
- /******/ // expose the modules object (__webpack_modules__)
41
- /******/ __webpack_require__.m = modules;
42
- /******/
43
- /******/ // expose the module cache
44
- /******/ __webpack_require__.c = installedModules;
45
- /******/
46
- /******/ // define getter function for harmony exports
47
- /******/ __webpack_require__.d = function(exports, name, getter) {
48
- /******/ if(!__webpack_require__.o(exports, name)) {
49
- /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
50
- /******/ }
51
- /******/ };
52
- /******/
53
- /******/ // define __esModule on exports
54
- /******/ __webpack_require__.r = function(exports) {
55
- /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
56
- /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
57
- /******/ }
58
- /******/ Object.defineProperty(exports, '__esModule', { value: true });
59
- /******/ };
60
- /******/
61
- /******/ // create a fake namespace object
62
- /******/ // mode & 1: value is a module id, require it
63
- /******/ // mode & 2: merge all properties of value into the ns
64
- /******/ // mode & 4: return value when already ns object
65
- /******/ // mode & 8|1: behave like require
66
- /******/ __webpack_require__.t = function(value, mode) {
67
- /******/ if(mode & 1) value = __webpack_require__(value);
68
- /******/ if(mode & 8) return value;
69
- /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
70
- /******/ var ns = Object.create(null);
71
- /******/ __webpack_require__.r(ns);
72
- /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
73
- /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
74
- /******/ return ns;
75
- /******/ };
76
- /******/
77
- /******/ // getDefaultExport function for compatibility with non-harmony modules
78
- /******/ __webpack_require__.n = function(module) {
79
- /******/ var getter = module && module.__esModule ?
80
- /******/ function getDefault() { return module['default']; } :
81
- /******/ function getModuleExports() { return module; };
82
- /******/ __webpack_require__.d(getter, 'a', getter);
83
- /******/ return getter;
84
- /******/ };
85
- /******/
86
- /******/ // Object.prototype.hasOwnProperty.call
87
- /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
88
- /******/
89
- /******/ // __webpack_public_path__
90
- /******/ __webpack_require__.p = "";
91
- /******/
92
- /******/
93
- /******/ // Load entry module and return exports
94
- /******/ return __webpack_require__(__webpack_require__.s = "./bazel-out/k8-fastbuild/bin/plugins/pubsub/core/dist/index.esm.js");
95
- /******/ })
96
- /************************************************************************/
97
- /******/ ({
98
-
99
- /***/ "./bazel-out/k8-fastbuild/bin/plugins/pubsub/core/dist/index.esm.js":
100
- /*!**************************************************************************!*\
101
- !*** ./bazel-out/k8-fastbuild/bin/plugins/pubsub/core/dist/index.esm.js ***!
102
- \**************************************************************************/
103
- /*! exports provided: PubSubHandlerPlugin, PubSubPlugin, PubSubPluginSymbol */
104
- /***/ (function(module, __webpack_exports__, __webpack_require__) {
105
-
106
- "use strict";
107
- __webpack_require__.r(__webpack_exports__);
108
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PubSubHandlerPlugin", function() { return PubSubHandlerPlugin; });
109
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PubSubPlugin", function() { return PubSubPlugin; });
110
- /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PubSubPluginSymbol", function() { return PubSubPluginSymbol; });
111
- function splitEvent(event) {
112
- return event.split(".").reduce((prev, curr, index) => {
113
- if (index === 0) {
114
- return [curr];
115
- }
116
- return [...prev, `${prev[index - 1]}.${curr}`];
117
- }, []);
118
- }
119
- let count = 1;
120
- class TinyPubSub {
121
- constructor() {
122
- this.events = new Map();
123
- this.tokens = new Map();
124
- }
125
- publish(event, ...args) {
126
- if (typeof event !== "string") {
127
- return;
128
- }
129
- if (event.includes(".")) {
130
- const eventKeys = splitEvent(event);
131
- eventKeys.forEach((key) => {
132
- this.deliver(key, event, ...args);
133
- });
134
- } else {
135
- this.deliver(event, event, ...args);
136
- }
137
- this.deliver("*", event, ...args);
138
- }
139
- subscribe(event, handler) {
140
- const uuid = `uuid_${++count}`;
141
- if (typeof event === "string") {
142
- if (!this.events.has(event)) {
143
- this.events.set(event, new Map());
144
- }
145
- const handlers = this.events.get(event);
146
- handlers.set(uuid, handler);
147
- this.tokens.set(uuid, event);
148
- }
149
- return uuid;
150
- }
151
- unsubscribe(value) {
152
- if (typeof value === "string" && value.startsWith("uuid")) {
153
- const path = this.tokens.get(value);
154
- if (typeof path === "undefined") {
155
- return;
156
- }
157
- const innerPath = this.events.get(path);
158
- innerPath == null ? void 0 : innerPath.delete(value);
159
- this.tokens.delete(value);
160
- return;
161
- }
162
- if (typeof value === "string") {
163
- for (const key of this.events.keys()) {
164
- if (key.indexOf(value) === 0) {
165
- const tokens = this.events.get(key);
166
- if (tokens && tokens.size) {
167
- for (const token of tokens.keys()) {
168
- this.tokens.delete(token);
169
- }
170
- }
171
- this.events.delete(key);
172
- }
173
- }
174
- }
175
- }
176
- count(event) {
177
- let counter = 0;
178
- if (typeof event === "undefined") {
179
- for (const handlers2 of this.events.values()) {
180
- counter += handlers2.size;
181
- }
182
- return counter;
183
- }
184
- const handlers = this.events.get(event);
185
- if (handlers == null ? void 0 : handlers.size) {
186
- return handlers.size;
187
- }
188
- return counter;
189
- }
190
- clear() {
191
- this.events.clear();
192
- this.tokens.clear();
193
- }
194
- deliver(path, event, ...args) {
195
- const handlers = this.events.get(path);
196
- if (handlers && handlers.size) {
197
- for (const handler of handlers.values()) {
198
- handler(event, ...args);
199
- }
200
- }
201
- }
202
- }
203
- const pubsub = new TinyPubSub();
204
-
205
- const PubSubPluginSymbol = Symbol.for("PubSubPlugin");
206
-
207
- const _PubSubPlugin = class {
208
- constructor(config) {
209
- this.name = "pub-sub";
210
- this.symbol = _PubSubPlugin.Symbol;
211
- var _a;
212
- this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
213
- this.pubsub = pubsub;
214
- }
215
- apply(player) {
216
- const existing = player.findPlugin(PubSubPluginSymbol);
217
- if (existing !== void 0) {
218
- this.pubsub = existing.pubsub;
219
- }
220
- player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
221
- const existingExpression = expEvaluator.operators.expressions.get(this.expressionName);
222
- if (existingExpression) {
223
- player.logger.warn(`[PubSubPlugin] expression ${this.expressionName} is already registered.`);
224
- } else {
225
- expEvaluator.addExpressionFunction(this.expressionName, (_ctx, event, ...args) => {
226
- if (typeof event === "string") {
227
- this.publish(event, ...args);
228
- }
229
- });
230
- }
231
- });
232
- player.hooks.onEnd.tap(this.name, () => {
233
- this.clear();
234
- });
235
- }
236
- publish(event, ...args) {
237
- this.pubsub.publish(event, ...args);
238
- }
239
- subscribe(event, handler) {
240
- return this.pubsub.subscribe(event, handler);
241
- }
242
- unsubscribe(token) {
243
- this.pubsub.unsubscribe(token);
244
- }
245
- clear() {
246
- this.pubsub.clear();
247
- }
248
- };
249
- let PubSubPlugin = _PubSubPlugin;
250
- PubSubPlugin.Symbol = PubSubPluginSymbol;
251
-
252
- function getPubSubPlugin(player) {
253
- const existing = player.findPlugin(PubSubPluginSymbol);
254
- const plugin = existing || new PubSubPlugin();
255
- if (!existing) {
256
- player.registerPlugin(plugin);
257
- }
258
- return plugin;
259
- }
260
-
261
- class PubSubHandlerPlugin {
262
- constructor(subscriptions) {
263
- this.name = "pubsub-handler";
264
- this.subscriptions = subscriptions;
265
- }
266
- apply(player) {
267
- const pubsub = getPubSubPlugin(player);
268
- player.hooks.onStart.tap(this.name, () => {
269
- this.subscriptions.forEach((handler, key) => {
270
- pubsub.subscribe(key, (_, ...args) => {
271
- const state = player.getState();
272
- if (state.status === "in-progress") {
273
- return handler(state, ...args);
274
- }
275
- player.logger.info(`[PubSubHandlerPlugin] subscriber for ${key} was called when player was not in-progress`);
276
- });
277
- });
278
- });
279
- }
280
- }
281
-
282
-
283
- //# sourceMappingURL=index.esm.js.map
284
-
285
-
286
- /***/ })
287
-
288
- /******/ });
289
- });
@@ -1 +0,0 @@
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`)}))}))}))}}}])}));