@player-ui/pubsub-plugin 0.8.0--canary.307.9645 → 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/PubSubPlugin.native.js +479 -0
- package/dist/PubSubPlugin.native.js.map +1 -0
- package/dist/cjs/index.cjs +270 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{index.cjs.js → index.legacy-esm.js} +97 -35
- package/dist/{index.esm.js → index.mjs} +97 -29
- package/dist/index.mjs.map +1 -0
- package/package.json +24 -61
- package/src/__test__/handler.test.ts +89 -0
- package/src/__test__/plugin.test.ts +163 -0
- package/src/__test__/pubsub.test.ts +309 -0
- package/src/handler.ts +5 -5
- package/src/index.ts +3 -3
- package/src/plugin.ts +11 -11
- package/src/pubsub.ts +9 -9
- package/src/symbols.ts +1 -1
- package/src/utils.ts +3 -3
- package/types/handler.d.ts +13 -0
- package/types/index.d.ts +4 -0
- package/types/plugin.d.ts +50 -0
- package/types/pubsub.d.ts +53 -0
- package/types/symbols.d.ts +2 -0
- package/types/utils.d.ts +7 -0
- package/dist/index.d.ts +0 -116
- package/dist/pubsub-plugin.dev.js +0 -289
- package/dist/pubsub-plugin.prod.js +0 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
PubSubHandlerPlugin: () => PubSubHandlerPlugin,
|
|
24
|
+
PubSubPlugin: () => PubSubPlugin,
|
|
25
|
+
PubSubPluginSymbol: () => PubSubPluginSymbol
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/pubsub.ts
|
|
30
|
+
function splitEvent(event) {
|
|
31
|
+
return event.split(".").reduce((prev, curr, index) => {
|
|
32
|
+
if (index === 0) {
|
|
33
|
+
return [curr];
|
|
34
|
+
}
|
|
35
|
+
return [...prev, `${prev[index - 1]}.${curr}`];
|
|
36
|
+
}, []);
|
|
37
|
+
}
|
|
38
|
+
var count = 1;
|
|
39
|
+
var TinyPubSub = class {
|
|
40
|
+
constructor() {
|
|
41
|
+
this.events = /* @__PURE__ */ new Map();
|
|
42
|
+
this.tokens = /* @__PURE__ */ new Map();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Publish an event with any number of additional arguments
|
|
46
|
+
*/
|
|
47
|
+
publish(event, ...args) {
|
|
48
|
+
if (typeof event !== "string") {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (event.includes(".")) {
|
|
52
|
+
const eventKeys = splitEvent(event);
|
|
53
|
+
eventKeys.forEach((key) => {
|
|
54
|
+
this.deliver(key, event, ...args);
|
|
55
|
+
});
|
|
56
|
+
} else {
|
|
57
|
+
this.deliver(event, event, ...args);
|
|
58
|
+
}
|
|
59
|
+
this.deliver("*", event, ...args);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Subscribe to an event
|
|
63
|
+
*
|
|
64
|
+
* Events are also heirarchical when separated by a period. Given the following:
|
|
65
|
+
*
|
|
66
|
+
* publish('a.b.c', 'one', 'two', 'three)
|
|
67
|
+
*
|
|
68
|
+
* The subscribe event will be called when the event is passed as 'a', 'a.b', or 'a.b.c'.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* // subscribes to the top level 'a' publish
|
|
72
|
+
* subscribe('a', (event, ...args) => console.log(event, ...args))
|
|
73
|
+
*/
|
|
74
|
+
subscribe(event, handler) {
|
|
75
|
+
const uuid = `uuid_${++count}`;
|
|
76
|
+
if (typeof event === "string") {
|
|
77
|
+
if (!this.events.has(event)) {
|
|
78
|
+
this.events.set(event, /* @__PURE__ */ new Map());
|
|
79
|
+
}
|
|
80
|
+
const handlers = this.events.get(event);
|
|
81
|
+
handlers.set(uuid, handler);
|
|
82
|
+
this.tokens.set(uuid, event);
|
|
83
|
+
}
|
|
84
|
+
return uuid;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Unsubscribes to a specific subscription given it's symbol or an entire
|
|
88
|
+
* event when passed as a string.
|
|
89
|
+
*
|
|
90
|
+
* When existing subscriptions exist for heirarchical events such as 'a.b.c',
|
|
91
|
+
* when passing an event 'a' to unsubscribe, all subscriptions for 'a', 'a.b',
|
|
92
|
+
* & 'a.b.c' will be unsubscribed as well.
|
|
93
|
+
*/
|
|
94
|
+
unsubscribe(value) {
|
|
95
|
+
if (typeof value === "string" && value.startsWith("uuid")) {
|
|
96
|
+
const path = this.tokens.get(value);
|
|
97
|
+
if (typeof path === "undefined") {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const innerPath = this.events.get(path);
|
|
101
|
+
innerPath?.delete(value);
|
|
102
|
+
this.tokens.delete(value);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (typeof value === "string") {
|
|
106
|
+
for (const key of this.events.keys()) {
|
|
107
|
+
if (key.indexOf(value) === 0) {
|
|
108
|
+
const tokens = this.events.get(key);
|
|
109
|
+
if (tokens && tokens.size) {
|
|
110
|
+
for (const token of tokens.keys()) {
|
|
111
|
+
this.tokens.delete(token);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
this.events.delete(key);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get the number of subscriptions for a specific event, or when left blank
|
|
121
|
+
* will return the overall number of subscriptions for the entire pubsub.
|
|
122
|
+
*/
|
|
123
|
+
count(event) {
|
|
124
|
+
let counter = 0;
|
|
125
|
+
if (typeof event === "undefined") {
|
|
126
|
+
for (const handlers2 of this.events.values()) {
|
|
127
|
+
counter += handlers2.size;
|
|
128
|
+
}
|
|
129
|
+
return counter;
|
|
130
|
+
}
|
|
131
|
+
const handlers = this.events.get(event);
|
|
132
|
+
if (handlers?.size) {
|
|
133
|
+
return handlers.size;
|
|
134
|
+
}
|
|
135
|
+
return counter;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Deletes all existing subscriptions
|
|
139
|
+
*/
|
|
140
|
+
clear() {
|
|
141
|
+
this.events.clear();
|
|
142
|
+
this.tokens.clear();
|
|
143
|
+
}
|
|
144
|
+
deliver(path, event, ...args) {
|
|
145
|
+
const handlers = this.events.get(path);
|
|
146
|
+
if (handlers && handlers.size) {
|
|
147
|
+
for (const handler of handlers.values()) {
|
|
148
|
+
handler(event, ...args);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var pubsub = new TinyPubSub();
|
|
154
|
+
|
|
155
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/symbols.ts
|
|
156
|
+
var PubSubPluginSymbol = Symbol.for("PubSubPlugin");
|
|
157
|
+
|
|
158
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/plugin.ts
|
|
159
|
+
var _PubSubPlugin = class _PubSubPlugin {
|
|
160
|
+
constructor(config) {
|
|
161
|
+
this.name = "pub-sub";
|
|
162
|
+
this.symbol = _PubSubPlugin.Symbol;
|
|
163
|
+
this.expressionName = config?.expressionName ?? "publish";
|
|
164
|
+
this.pubsub = pubsub;
|
|
165
|
+
}
|
|
166
|
+
apply(player) {
|
|
167
|
+
const existing = player.findPlugin(PubSubPluginSymbol);
|
|
168
|
+
if (existing !== void 0) {
|
|
169
|
+
this.pubsub = existing.pubsub;
|
|
170
|
+
}
|
|
171
|
+
player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
|
|
172
|
+
const existingExpression = expEvaluator.operators.expressions.get(
|
|
173
|
+
this.expressionName
|
|
174
|
+
);
|
|
175
|
+
if (existingExpression) {
|
|
176
|
+
player.logger.warn(
|
|
177
|
+
`[PubSubPlugin] expression ${this.expressionName} is already registered.`
|
|
178
|
+
);
|
|
179
|
+
} else {
|
|
180
|
+
expEvaluator.addExpressionFunction(
|
|
181
|
+
this.expressionName,
|
|
182
|
+
(_ctx, event, ...args) => {
|
|
183
|
+
if (typeof event === "string") {
|
|
184
|
+
this.publish(event, ...args);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
player.hooks.onEnd.tap(this.name, () => {
|
|
191
|
+
this.clear();
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A way of publishing an event, notifying any listeners
|
|
196
|
+
*
|
|
197
|
+
* @param event - The name of the event to publish. Can take sub-topics like: foo.bar
|
|
198
|
+
* @param data - Any additional data to attach to the event
|
|
199
|
+
*/
|
|
200
|
+
publish(event, ...args) {
|
|
201
|
+
this.pubsub.publish(event, ...args);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Subscribe to an event with the given name. The handler will get called for any published event
|
|
205
|
+
*
|
|
206
|
+
* @param event - The name of the event to subscribe to
|
|
207
|
+
* @param handler - A function to be called when the event is triggered
|
|
208
|
+
* @returns A token to be used to unsubscribe from the event
|
|
209
|
+
*/
|
|
210
|
+
subscribe(event, handler) {
|
|
211
|
+
return this.pubsub.subscribe(event, handler);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Remove any subscriptions using the given token
|
|
215
|
+
*
|
|
216
|
+
* @param token - A token from a `subscribe` call
|
|
217
|
+
*/
|
|
218
|
+
unsubscribe(token) {
|
|
219
|
+
this.pubsub.unsubscribe(token);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Remove all subscriptions
|
|
223
|
+
*/
|
|
224
|
+
clear() {
|
|
225
|
+
this.pubsub.clear();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
_PubSubPlugin.Symbol = PubSubPluginSymbol;
|
|
229
|
+
var PubSubPlugin = _PubSubPlugin;
|
|
230
|
+
|
|
231
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/utils.ts
|
|
232
|
+
function getPubSubPlugin(player) {
|
|
233
|
+
const existing = player.findPlugin(PubSubPluginSymbol);
|
|
234
|
+
const plugin = existing || new PubSubPlugin();
|
|
235
|
+
if (!existing) {
|
|
236
|
+
player.registerPlugin(plugin);
|
|
237
|
+
}
|
|
238
|
+
return plugin;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/handler.ts
|
|
242
|
+
var PubSubHandlerPlugin = class {
|
|
243
|
+
constructor(subscriptions) {
|
|
244
|
+
this.name = "pubsub-handler";
|
|
245
|
+
this.subscriptions = subscriptions;
|
|
246
|
+
}
|
|
247
|
+
apply(player) {
|
|
248
|
+
const pubsub2 = getPubSubPlugin(player);
|
|
249
|
+
player.hooks.onStart.tap(this.name, () => {
|
|
250
|
+
this.subscriptions.forEach((handler, key) => {
|
|
251
|
+
pubsub2.subscribe(key, (_, ...args) => {
|
|
252
|
+
const state = player.getState();
|
|
253
|
+
if (state.status === "in-progress") {
|
|
254
|
+
return handler(state, ...args);
|
|
255
|
+
}
|
|
256
|
+
player.logger.info(
|
|
257
|
+
`[PubSubHandlerPlugin] subscriber for ${key} was called when player was not in-progress`
|
|
258
|
+
);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
265
|
+
0 && (module.exports = {
|
|
266
|
+
PubSubHandlerPlugin,
|
|
267
|
+
PubSubPlugin,
|
|
268
|
+
PubSubPluginSymbol
|
|
269
|
+
});
|
|
270
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/index.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/pubsub.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/symbols.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/plugin.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/utils.ts","../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/handler.ts"],"sourcesContent":["export * from \"./plugin\";\nexport * from \"./symbols\";\nexport * from \"./handler\";\n","/**\n * Based off the pubsub-js library and rewritten to match the same used APIs but modified so that\n * multiple arguments could be passed into the publish and subscription handlers.\n */\n\nexport type SubscribeHandler<T extends string, A extends unknown[]> = (\n type: T,\n ...args: A\n) => void;\n\nexport type PubSubUUID = `uuid_${number}`;\n\n/**\n * Split a string into an array of event layers\n */\nfunction splitEvent(event: string) {\n return event.split(\".\").reduce<string[]>((prev, curr, index) => {\n if (index === 0) {\n return [curr];\n }\n\n return [...prev, `${prev[index - 1]}.${curr}`];\n }, []);\n}\n\nlet count = 1;\n\n/**\n * Tiny pubsub maker\n */\nexport class TinyPubSub {\n private events: Map<string, Map<PubSubUUID, SubscribeHandler<any, any>>>;\n private tokens: Map<PubSubUUID, string>;\n\n constructor() {\n this.events = new Map();\n this.tokens = new Map();\n }\n\n /**\n * Publish an event with any number of additional arguments\n */\n publish(event: string, ...args: unknown[]) {\n if (typeof event !== \"string\") {\n return;\n }\n\n if (event.includes(\".\")) {\n const eventKeys = splitEvent(event);\n\n eventKeys.forEach((key) => {\n this.deliver(key, event, ...args);\n });\n } else {\n this.deliver(event, event, ...args);\n }\n\n this.deliver(\"*\", event, ...args);\n }\n\n /**\n * Subscribe to an event\n *\n * Events are also heirarchical when separated by a period. Given the following:\n *\n * publish('a.b.c', 'one', 'two', 'three)\n *\n * The subscribe event will be called when the event is passed as 'a', 'a.b', or 'a.b.c'.\n *\n * @example\n * // subscribes to the top level 'a' publish\n * subscribe('a', (event, ...args) => console.log(event, ...args))\n */\n subscribe(event: string, handler: SubscribeHandler<any, any>) {\n const uuid = `uuid_${++count}`;\n\n if (typeof event === \"string\") {\n if (!this.events.has(event)) {\n this.events.set(event, new Map());\n }\n\n const handlers = this.events.get(event);\n handlers!.set(uuid as PubSubUUID, handler);\n this.tokens.set(uuid as PubSubUUID, event);\n }\n\n return uuid;\n }\n\n /**\n * Unsubscribes to a specific subscription given it's symbol or an entire\n * event when passed as a string.\n *\n * When existing subscriptions exist for heirarchical events such as 'a.b.c',\n * when passing an event 'a' to unsubscribe, all subscriptions for 'a', 'a.b',\n * & 'a.b.c' will be unsubscribed as well.\n */\n unsubscribe(value: string | symbol) {\n if (typeof value === \"string\" && value.startsWith(\"uuid\")) {\n const path = this.tokens.get(value as PubSubUUID);\n\n if (typeof path === \"undefined\") {\n return;\n }\n\n const innerPath = this.events.get(path);\n innerPath?.delete(value as PubSubUUID);\n this.tokens.delete(value as PubSubUUID);\n return;\n }\n\n if (typeof value === \"string\") {\n for (const key of this.events.keys()) {\n if (key.indexOf(value) === 0) {\n const tokens = this.events.get(key);\n\n if (tokens && tokens.size) {\n // eslint-disable-next-line max-depth\n for (const token of tokens.keys()) {\n this.tokens.delete(token);\n }\n }\n\n this.events.delete(key);\n }\n }\n }\n }\n\n /**\n * Get the number of subscriptions for a specific event, or when left blank\n * will return the overall number of subscriptions for the entire pubsub.\n */\n count(event?: string) {\n let counter = 0;\n\n if (typeof event === \"undefined\") {\n for (const handlers of this.events.values()) {\n counter += handlers.size;\n }\n\n return counter;\n }\n\n const handlers = this.events.get(event);\n\n if (handlers?.size) {\n return handlers.size;\n }\n\n return counter;\n }\n\n /**\n * Deletes all existing subscriptions\n */\n clear() {\n this.events.clear();\n this.tokens.clear();\n }\n\n private deliver(path: string, event: string, ...args: unknown[]) {\n const handlers = this.events.get(path);\n\n if (handlers && handlers.size) {\n for (const handler of handlers.values()) {\n handler(event, ...args);\n }\n }\n }\n}\n\nexport const pubsub = new TinyPubSub();\n","export const PubSubPluginSymbol = Symbol.for(\"PubSubPlugin\");\n","import type {\n Player,\n PlayerPlugin,\n ExpressionContext,\n} from \"@player-ui/player\";\nimport type { SubscribeHandler, TinyPubSub } from \"./pubsub\";\nimport { pubsub } from \"./pubsub\";\nimport { PubSubPluginSymbol } from \"./symbols\";\n\nexport interface PubSubConfig {\n /** A custom expression name to register */\n expressionName: string;\n}\n\n/**\n * The PubSubPlugin is a great way to enable your FRF content to publish events back to your app\n * It injects a publish() function into the expression language, and will forward all events back to any subscribers.\n *\n * Published/Subscribed events support a hierarchy:\n * - publish('foo', 'data') -- will trigger any listeners for 'foo'\n * - publish('foo.bar', 'data') -- will trigger any listeners for 'foo' or 'foo.bar'\n *\n */\nexport class PubSubPlugin implements PlayerPlugin {\n name = \"pub-sub\";\n\n static Symbol = PubSubPluginSymbol;\n public readonly symbol = PubSubPlugin.Symbol;\n\n protected pubsub: TinyPubSub;\n\n private expressionName: string;\n\n constructor(config?: PubSubConfig) {\n this.expressionName = config?.expressionName ?? \"publish\";\n this.pubsub = pubsub;\n }\n\n apply(player: Player) {\n // if there is already a pubsub plugin, reuse its pubsub instance\n // to maintain the singleton across bundles for iOS/Android\n const existing = player.findPlugin<PubSubPlugin>(PubSubPluginSymbol);\n if (existing !== undefined) {\n this.pubsub = existing.pubsub;\n }\n\n player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {\n const existingExpression = expEvaluator.operators.expressions.get(\n this.expressionName,\n );\n\n if (existingExpression) {\n player.logger.warn(\n `[PubSubPlugin] expression ${this.expressionName} is already registered.`,\n );\n } else {\n expEvaluator.addExpressionFunction(\n this.expressionName,\n (_ctx: ExpressionContext, event: unknown, ...args: unknown[]) => {\n if (typeof event === \"string\") {\n this.publish(event, ...args);\n }\n },\n );\n }\n });\n\n player.hooks.onEnd.tap(this.name, () => {\n this.clear();\n });\n }\n\n /**\n * A way of publishing an event, notifying any listeners\n *\n * @param event - The name of the event to publish. Can take sub-topics like: foo.bar\n * @param data - Any additional data to attach to the event\n */\n publish(event: string, ...args: unknown[]) {\n this.pubsub.publish(event, ...args);\n }\n\n /**\n * Subscribe to an event with the given name. The handler will get called for any published event\n *\n * @param event - The name of the event to subscribe to\n * @param handler - A function to be called when the event is triggered\n * @returns A token to be used to unsubscribe from the event\n */\n subscribe<T extends string, A extends unknown[]>(\n event: T,\n handler: SubscribeHandler<T, A>,\n ) {\n return this.pubsub.subscribe(event, handler);\n }\n\n /**\n * Remove any subscriptions using the given token\n *\n * @param token - A token from a `subscribe` call\n */\n unsubscribe(token: string) {\n this.pubsub.unsubscribe(token);\n }\n\n /**\n * Remove all subscriptions\n */\n clear() {\n this.pubsub.clear();\n }\n}\n","import type { Player } from \"@player-ui/player\";\nimport { PubSubPlugin } from \"./plugin\";\nimport { PubSubPluginSymbol } from \"./symbols\";\n\n/**\n * Returns the existing PubSubPlugin or creates and registers a new plugin\n */\nexport function getPubSubPlugin(player: Player) {\n const existing = player.findPlugin<PubSubPlugin>(PubSubPluginSymbol);\n const plugin = existing || new PubSubPlugin();\n\n if (!existing) {\n player.registerPlugin(plugin);\n }\n\n return plugin;\n}\n","import type { Player, PlayerPlugin, InProgressState } from \"@player-ui/player\";\nimport { getPubSubPlugin } from \"./utils\";\n\nexport type PubSubHandler<T extends unknown[]> = (\n context: InProgressState,\n ...args: T\n) => void;\n\nexport type SubscriptionMap = Map<string, PubSubHandler<any>>;\n\n/**\n * Plugin to easily add subscribers to the PubSubPlugin\n */\nexport class PubSubHandlerPlugin implements PlayerPlugin {\n name = \"pubsub-handler\";\n private subscriptions: SubscriptionMap;\n\n constructor(subscriptions: SubscriptionMap) {\n this.subscriptions = subscriptions;\n }\n\n apply(player: Player) {\n const pubsub = getPubSubPlugin(player);\n\n player.hooks.onStart.tap(this.name, () => {\n this.subscriptions.forEach((handler, key) => {\n pubsub.subscribe(key, (_, ...args) => {\n const state = player.getState();\n\n if (state.status === \"in-progress\") {\n return handler(state, ...args);\n }\n\n player.logger.info(\n `[PubSubHandlerPlugin] subscriber for ${key} was called when player was not in-progress`,\n );\n });\n });\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACeA,SAAS,WAAW,OAAe;AACjC,SAAO,MAAM,MAAM,GAAG,EAAE,OAAiB,CAAC,MAAM,MAAM,UAAU;AAC9D,QAAI,UAAU,GAAG;AACf,aAAO,CAAC,IAAI;AAAA,IACd;AAEA,WAAO,CAAC,GAAG,MAAM,GAAG,KAAK,QAAQ,CAAC,CAAC,IAAI,IAAI,EAAE;AAAA,EAC/C,GAAG,CAAC,CAAC;AACP;AAEA,IAAI,QAAQ;AAKL,IAAM,aAAN,MAAiB;AAAA,EA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AI,aAAa;AAE5C,MAAI,CAAC,UAAU;AACb,WAAO,eAAe,MAAM;AAAA,EAC9B;AAEA,SAAO;AACT;;;ACHO,IAAM,sBAAN,MAAkD;AAAA,EAIvD,YAAY,eAAgC;AAH5C,gBAAO;AAIL,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,MAAM,QAAgB;AACpB,UAAMC,UAAS,gBAAgB,MAAM;AAErC,WAAO,MAAM,QAAQ,IAAI,KAAK,MAAM,MAAM;AACxC,WAAK,cAAc,QAAQ,CAAC,SAAS,QAAQ;AAC3C,QAAAA,QAAO,UAAU,KAAK,CAAC,MAAM,SAAS;AACpC,gBAAM,QAAQ,OAAO,SAAS;AAE9B,cAAI,MAAM,WAAW,eAAe;AAClC,mBAAO,QAAQ,OAAO,GAAG,IAAI;AAAA,UAC/B;AAEA,iBAAO,OAAO;AAAA,YACZ,wCAAwC,GAAG;AAAA,UAC7C;AAAA,QACF,CAAC;AAAA,MACH,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":["handlers","pubsub"]}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
1
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/pubsub.ts
|
|
5
2
|
function splitEvent(event) {
|
|
6
3
|
return event.split(".").reduce((prev, curr, index) => {
|
|
7
4
|
if (index === 0) {
|
|
@@ -10,12 +7,15 @@ function splitEvent(event) {
|
|
|
10
7
|
return [...prev, `${prev[index - 1]}.${curr}`];
|
|
11
8
|
}, []);
|
|
12
9
|
}
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
var count = 1;
|
|
11
|
+
var TinyPubSub = class {
|
|
15
12
|
constructor() {
|
|
16
|
-
this.events = new Map();
|
|
17
|
-
this.tokens = new Map();
|
|
13
|
+
this.events = /* @__PURE__ */ new Map();
|
|
14
|
+
this.tokens = /* @__PURE__ */ new Map();
|
|
18
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Publish an event with any number of additional arguments
|
|
18
|
+
*/
|
|
19
19
|
publish(event, ...args) {
|
|
20
20
|
if (typeof event !== "string") {
|
|
21
21
|
return;
|
|
@@ -30,11 +30,24 @@ class TinyPubSub {
|
|
|
30
30
|
}
|
|
31
31
|
this.deliver("*", event, ...args);
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Subscribe to an event
|
|
35
|
+
*
|
|
36
|
+
* Events are also heirarchical when separated by a period. Given the following:
|
|
37
|
+
*
|
|
38
|
+
* publish('a.b.c', 'one', 'two', 'three)
|
|
39
|
+
*
|
|
40
|
+
* The subscribe event will be called when the event is passed as 'a', 'a.b', or 'a.b.c'.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // subscribes to the top level 'a' publish
|
|
44
|
+
* subscribe('a', (event, ...args) => console.log(event, ...args))
|
|
45
|
+
*/
|
|
33
46
|
subscribe(event, handler) {
|
|
34
47
|
const uuid = `uuid_${++count}`;
|
|
35
48
|
if (typeof event === "string") {
|
|
36
49
|
if (!this.events.has(event)) {
|
|
37
|
-
this.events.set(event, new Map());
|
|
50
|
+
this.events.set(event, /* @__PURE__ */ new Map());
|
|
38
51
|
}
|
|
39
52
|
const handlers = this.events.get(event);
|
|
40
53
|
handlers.set(uuid, handler);
|
|
@@ -42,6 +55,14 @@ class TinyPubSub {
|
|
|
42
55
|
}
|
|
43
56
|
return uuid;
|
|
44
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Unsubscribes to a specific subscription given it's symbol or an entire
|
|
60
|
+
* event when passed as a string.
|
|
61
|
+
*
|
|
62
|
+
* When existing subscriptions exist for heirarchical events such as 'a.b.c',
|
|
63
|
+
* when passing an event 'a' to unsubscribe, all subscriptions for 'a', 'a.b',
|
|
64
|
+
* & 'a.b.c' will be unsubscribed as well.
|
|
65
|
+
*/
|
|
45
66
|
unsubscribe(value) {
|
|
46
67
|
if (typeof value === "string" && value.startsWith("uuid")) {
|
|
47
68
|
const path = this.tokens.get(value);
|
|
@@ -49,7 +70,7 @@ class TinyPubSub {
|
|
|
49
70
|
return;
|
|
50
71
|
}
|
|
51
72
|
const innerPath = this.events.get(path);
|
|
52
|
-
innerPath
|
|
73
|
+
innerPath?.delete(value);
|
|
53
74
|
this.tokens.delete(value);
|
|
54
75
|
return;
|
|
55
76
|
}
|
|
@@ -67,6 +88,10 @@ class TinyPubSub {
|
|
|
67
88
|
}
|
|
68
89
|
}
|
|
69
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Get the number of subscriptions for a specific event, or when left blank
|
|
93
|
+
* will return the overall number of subscriptions for the entire pubsub.
|
|
94
|
+
*/
|
|
70
95
|
count(event) {
|
|
71
96
|
let counter = 0;
|
|
72
97
|
if (typeof event === "undefined") {
|
|
@@ -76,11 +101,14 @@ class TinyPubSub {
|
|
|
76
101
|
return counter;
|
|
77
102
|
}
|
|
78
103
|
const handlers = this.events.get(event);
|
|
79
|
-
if (handlers
|
|
104
|
+
if (handlers?.size) {
|
|
80
105
|
return handlers.size;
|
|
81
106
|
}
|
|
82
107
|
return counter;
|
|
83
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Deletes all existing subscriptions
|
|
111
|
+
*/
|
|
84
112
|
clear() {
|
|
85
113
|
this.events.clear();
|
|
86
114
|
this.tokens.clear();
|
|
@@ -93,17 +121,18 @@ class TinyPubSub {
|
|
|
93
121
|
}
|
|
94
122
|
}
|
|
95
123
|
}
|
|
96
|
-
}
|
|
97
|
-
|
|
124
|
+
};
|
|
125
|
+
var pubsub = new TinyPubSub();
|
|
98
126
|
|
|
99
|
-
|
|
127
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/symbols.ts
|
|
128
|
+
var PubSubPluginSymbol = Symbol.for("PubSubPlugin");
|
|
100
129
|
|
|
101
|
-
|
|
130
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/plugin.ts
|
|
131
|
+
var _PubSubPlugin = class _PubSubPlugin {
|
|
102
132
|
constructor(config) {
|
|
103
133
|
this.name = "pub-sub";
|
|
104
134
|
this.symbol = _PubSubPlugin.Symbol;
|
|
105
|
-
|
|
106
|
-
this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
|
|
135
|
+
this.expressionName = config?.expressionName ?? "publish";
|
|
107
136
|
this.pubsub = pubsub;
|
|
108
137
|
}
|
|
109
138
|
apply(player) {
|
|
@@ -112,37 +141,66 @@ const _PubSubPlugin = class {
|
|
|
112
141
|
this.pubsub = existing.pubsub;
|
|
113
142
|
}
|
|
114
143
|
player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
|
|
115
|
-
const existingExpression = expEvaluator.operators.expressions.get(
|
|
144
|
+
const existingExpression = expEvaluator.operators.expressions.get(
|
|
145
|
+
this.expressionName
|
|
146
|
+
);
|
|
116
147
|
if (existingExpression) {
|
|
117
|
-
player.logger.warn(
|
|
148
|
+
player.logger.warn(
|
|
149
|
+
`[PubSubPlugin] expression ${this.expressionName} is already registered.`
|
|
150
|
+
);
|
|
118
151
|
} else {
|
|
119
|
-
expEvaluator.addExpressionFunction(
|
|
120
|
-
|
|
121
|
-
|
|
152
|
+
expEvaluator.addExpressionFunction(
|
|
153
|
+
this.expressionName,
|
|
154
|
+
(_ctx, event, ...args) => {
|
|
155
|
+
if (typeof event === "string") {
|
|
156
|
+
this.publish(event, ...args);
|
|
157
|
+
}
|
|
122
158
|
}
|
|
123
|
-
|
|
159
|
+
);
|
|
124
160
|
}
|
|
125
161
|
});
|
|
126
162
|
player.hooks.onEnd.tap(this.name, () => {
|
|
127
163
|
this.clear();
|
|
128
164
|
});
|
|
129
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* A way of publishing an event, notifying any listeners
|
|
168
|
+
*
|
|
169
|
+
* @param event - The name of the event to publish. Can take sub-topics like: foo.bar
|
|
170
|
+
* @param data - Any additional data to attach to the event
|
|
171
|
+
*/
|
|
130
172
|
publish(event, ...args) {
|
|
131
173
|
this.pubsub.publish(event, ...args);
|
|
132
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Subscribe to an event with the given name. The handler will get called for any published event
|
|
177
|
+
*
|
|
178
|
+
* @param event - The name of the event to subscribe to
|
|
179
|
+
* @param handler - A function to be called when the event is triggered
|
|
180
|
+
* @returns A token to be used to unsubscribe from the event
|
|
181
|
+
*/
|
|
133
182
|
subscribe(event, handler) {
|
|
134
183
|
return this.pubsub.subscribe(event, handler);
|
|
135
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Remove any subscriptions using the given token
|
|
187
|
+
*
|
|
188
|
+
* @param token - A token from a `subscribe` call
|
|
189
|
+
*/
|
|
136
190
|
unsubscribe(token) {
|
|
137
191
|
this.pubsub.unsubscribe(token);
|
|
138
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Remove all subscriptions
|
|
195
|
+
*/
|
|
139
196
|
clear() {
|
|
140
197
|
this.pubsub.clear();
|
|
141
198
|
}
|
|
142
199
|
};
|
|
143
|
-
|
|
144
|
-
PubSubPlugin
|
|
200
|
+
_PubSubPlugin.Symbol = PubSubPluginSymbol;
|
|
201
|
+
var PubSubPlugin = _PubSubPlugin;
|
|
145
202
|
|
|
203
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/utils.ts
|
|
146
204
|
function getPubSubPlugin(player) {
|
|
147
205
|
const existing = player.findPlugin(PubSubPluginSymbol);
|
|
148
206
|
const plugin = existing || new PubSubPlugin();
|
|
@@ -152,28 +210,32 @@ function getPubSubPlugin(player) {
|
|
|
152
210
|
return plugin;
|
|
153
211
|
}
|
|
154
212
|
|
|
155
|
-
|
|
213
|
+
// ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/plugins/pubsub/core/src/handler.ts
|
|
214
|
+
var PubSubHandlerPlugin = class {
|
|
156
215
|
constructor(subscriptions) {
|
|
157
216
|
this.name = "pubsub-handler";
|
|
158
217
|
this.subscriptions = subscriptions;
|
|
159
218
|
}
|
|
160
219
|
apply(player) {
|
|
161
|
-
const
|
|
220
|
+
const pubsub2 = getPubSubPlugin(player);
|
|
162
221
|
player.hooks.onStart.tap(this.name, () => {
|
|
163
222
|
this.subscriptions.forEach((handler, key) => {
|
|
164
|
-
|
|
223
|
+
pubsub2.subscribe(key, (_, ...args) => {
|
|
165
224
|
const state = player.getState();
|
|
166
225
|
if (state.status === "in-progress") {
|
|
167
226
|
return handler(state, ...args);
|
|
168
227
|
}
|
|
169
|
-
player.logger.info(
|
|
228
|
+
player.logger.info(
|
|
229
|
+
`[PubSubHandlerPlugin] subscriber for ${key} was called when player was not in-progress`
|
|
230
|
+
);
|
|
170
231
|
});
|
|
171
232
|
});
|
|
172
233
|
});
|
|
173
234
|
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
235
|
+
};
|
|
236
|
+
export {
|
|
237
|
+
PubSubHandlerPlugin,
|
|
238
|
+
PubSubPlugin,
|
|
239
|
+
PubSubPluginSymbol
|
|
240
|
+
};
|
|
241
|
+
//# sourceMappingURL=index.mjs.map
|