@player-ui/pubsub-plugin 0.4.0 → 0.4.1-next.1
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 +149 -14
- package/dist/index.d.ts +73 -5
- package/dist/index.esm.js +149 -11
- package/dist/pubsub-plugin.dev.js +148 -412
- package/dist/pubsub-plugin.prod.js +1 -1
- package/package.json +10 -2
- package/src/handler.ts +41 -0
- package/src/index.ts +2 -1
- package/src/plugin.ts +112 -0
- package/src/pubsub.ts +149 -53
- package/src/utils.ts +17 -0
package/src/handler.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Player, PlayerPlugin, InProgressState } from '@player-ui/player';
|
|
2
|
+
import { getPubSubPlugin } from './utils';
|
|
3
|
+
|
|
4
|
+
export type PubSubHandler<T extends unknown[]> = (
|
|
5
|
+
context: InProgressState,
|
|
6
|
+
...args: T
|
|
7
|
+
) => void;
|
|
8
|
+
|
|
9
|
+
export type SubscriptionMap = Map<string, PubSubHandler<any>>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Plugin to easily add subscribers to the PubSubPlugin
|
|
13
|
+
*/
|
|
14
|
+
export class PubSubHandlerPlugin implements PlayerPlugin {
|
|
15
|
+
name = 'pubsub-handler';
|
|
16
|
+
private subscriptions: SubscriptionMap;
|
|
17
|
+
|
|
18
|
+
constructor(subscriptions: SubscriptionMap) {
|
|
19
|
+
this.subscriptions = subscriptions;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
apply(player: Player) {
|
|
23
|
+
const pubsub = getPubSubPlugin(player);
|
|
24
|
+
|
|
25
|
+
player.hooks.onStart.tap(this.name, () => {
|
|
26
|
+
this.subscriptions.forEach((handler, key) => {
|
|
27
|
+
pubsub.subscribe(key, (_, ...args) => {
|
|
28
|
+
const state = player.getState();
|
|
29
|
+
|
|
30
|
+
if (state.status === 'in-progress') {
|
|
31
|
+
return handler(state, ...args);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
player.logger.info(
|
|
35
|
+
`[PubSubHandlerPlugin] subscriber for ${key} was called when player was not in-progress`
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
CHANGED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Player,
|
|
3
|
+
PlayerPlugin,
|
|
4
|
+
ExpressionContext,
|
|
5
|
+
} from '@player-ui/player';
|
|
6
|
+
import type { SubscribeHandler, TinyPubSub } from './pubsub';
|
|
7
|
+
import { pubsub } from './pubsub';
|
|
8
|
+
import { PubSubPluginSymbol } from './symbols';
|
|
9
|
+
|
|
10
|
+
export interface PubSubConfig {
|
|
11
|
+
/** A custom expression name to register */
|
|
12
|
+
expressionName: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The PubSubPlugin is a great way to enable your FRF content to publish events back to your app
|
|
17
|
+
* It injects a publish() function into the expression language, and will forward all events back to any subscribers.
|
|
18
|
+
*
|
|
19
|
+
* Published/Subscribed events support a hierarchy:
|
|
20
|
+
* - publish('foo', 'data') -- will trigger any listeners for 'foo'
|
|
21
|
+
* - publish('foo.bar', 'data') -- will trigger any listeners for 'foo' or 'foo.bar'
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
export class PubSubPlugin implements PlayerPlugin {
|
|
25
|
+
name = 'pub-sub';
|
|
26
|
+
|
|
27
|
+
static Symbol = PubSubPluginSymbol;
|
|
28
|
+
public readonly symbol = PubSubPlugin.Symbol;
|
|
29
|
+
|
|
30
|
+
protected pubsub: TinyPubSub;
|
|
31
|
+
|
|
32
|
+
private expressionName: string;
|
|
33
|
+
|
|
34
|
+
constructor(config?: PubSubConfig) {
|
|
35
|
+
this.expressionName = config?.expressionName ?? 'publish';
|
|
36
|
+
this.pubsub = pubsub;
|
|
37
|
+
}
|
|
38
|
+
|
|
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
|
+
|
|
47
|
+
player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
|
|
48
|
+
const existingExpression = expEvaluator.operators.expressions.get(
|
|
49
|
+
this.expressionName
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
if (existingExpression) {
|
|
53
|
+
player.logger.warn(
|
|
54
|
+
`[PubSubPlugin] expression ${this.expressionName} is already registered.`
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
expEvaluator.addExpressionFunction(
|
|
58
|
+
this.expressionName,
|
|
59
|
+
(_ctx: ExpressionContext, event: unknown, ...args: unknown[]) => {
|
|
60
|
+
if (typeof event === 'string') {
|
|
61
|
+
this.publish(event, ...args);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
player.hooks.onEnd.tap(this.name, () => {
|
|
69
|
+
this.clear();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A way of publishing an event, notifying any listeners
|
|
75
|
+
*
|
|
76
|
+
* @param event - The name of the event to publish. Can take sub-topics like: foo.bar
|
|
77
|
+
* @param data - Any additional data to attach to the event
|
|
78
|
+
*/
|
|
79
|
+
publish(event: string, ...args: unknown[]) {
|
|
80
|
+
this.pubsub.publish(event, ...args);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Subscribe to an event with the given name. The handler will get called for any published event
|
|
85
|
+
*
|
|
86
|
+
* @param event - The name of the event to subscribe to
|
|
87
|
+
* @param handler - A function to be called when the event is triggered
|
|
88
|
+
* @returns A token to be used to unsubscribe from the event
|
|
89
|
+
*/
|
|
90
|
+
subscribe<T extends string, A extends unknown[]>(
|
|
91
|
+
event: T,
|
|
92
|
+
handler: SubscribeHandler<T, A>
|
|
93
|
+
) {
|
|
94
|
+
return this.pubsub.subscribe(event, handler);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Remove any subscriptions using the given token
|
|
99
|
+
*
|
|
100
|
+
* @param token - A token from a `subscribe` call
|
|
101
|
+
*/
|
|
102
|
+
unsubscribe(token: string) {
|
|
103
|
+
this.pubsub.unsubscribe(token);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Remove all subscriptions
|
|
108
|
+
*/
|
|
109
|
+
clear() {
|
|
110
|
+
this.pubsub.clear();
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/pubsub.ts
CHANGED
|
@@ -1,77 +1,173 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Based off the pubsub-js library and rewritten to match the same used APIs but modified so that
|
|
3
|
+
* multiple arguments could be passed into the publish and subscription handlers.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type SubscribeHandler<T extends string, A extends unknown[]> = (
|
|
7
|
+
type: T,
|
|
8
|
+
...args: A
|
|
9
|
+
) => void;
|
|
10
|
+
|
|
11
|
+
export type PubSubUUID = `uuid_${number}`;
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
|
-
*
|
|
16
|
-
* It injects a publish() function into the expression language, and will forward all events back to any subscribers.
|
|
17
|
-
*
|
|
18
|
-
* Published/Subscribed events support a hierarchy:
|
|
19
|
-
* - publish('foo', 'data') -- will trigger any listeners for 'foo'
|
|
20
|
-
* - publish('foo.bar', 'data') -- will trigger any listeners for 'foo' or 'foo.bar'
|
|
21
|
-
*
|
|
14
|
+
* Split a string into an array of event layers
|
|
22
15
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
function splitEvent(event: string) {
|
|
17
|
+
return event.split('.').reduce<string[]>((prev, curr, index) => {
|
|
18
|
+
if (index === 0) {
|
|
19
|
+
return [curr];
|
|
20
|
+
}
|
|
25
21
|
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
return [...prev, `${prev[index - 1]}.${curr}`];
|
|
23
|
+
}, []);
|
|
24
|
+
}
|
|
28
25
|
|
|
29
|
-
|
|
26
|
+
let count = 1;
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Tiny pubsub maker
|
|
30
|
+
*/
|
|
31
|
+
export class TinyPubSub {
|
|
32
|
+
private events: Map<string, Map<PubSubUUID, SubscribeHandler<any, any>>>;
|
|
33
|
+
private tokens: Map<PubSubUUID, string>;
|
|
34
|
+
|
|
35
|
+
constructor() {
|
|
36
|
+
this.events = new Map();
|
|
37
|
+
this.tokens = new Map();
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
);
|
|
45
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Publish an event with any number of additional arguments
|
|
42
|
+
*/
|
|
43
|
+
publish(event: string, ...args: unknown[]) {
|
|
44
|
+
if (typeof event !== 'string') {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (event.includes('.')) {
|
|
49
|
+
const eventKeys = splitEvent(event);
|
|
50
|
+
|
|
51
|
+
eventKeys.forEach((key) => {
|
|
52
|
+
this.deliver(key, event, ...args);
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
this.deliver(event, event, ...args);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.deliver('*', event, ...args);
|
|
46
59
|
}
|
|
47
60
|
|
|
48
61
|
/**
|
|
49
|
-
*
|
|
62
|
+
* Subscribe to an event
|
|
63
|
+
*
|
|
64
|
+
* Events are also heirarchical when separated by a period. Given the following:
|
|
50
65
|
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
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))
|
|
53
73
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
74
|
+
subscribe(event: string, handler: SubscribeHandler<any, any>) {
|
|
75
|
+
const uuid = `uuid_${++count}`;
|
|
76
|
+
|
|
77
|
+
if (typeof event === 'string') {
|
|
78
|
+
if (!this.events.has(event)) {
|
|
79
|
+
this.events.set(event, new Map());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const handlers = this.events.get(event);
|
|
83
|
+
handlers!.set(uuid as PubSubUUID, handler);
|
|
84
|
+
this.tokens.set(uuid as PubSubUUID, event);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return uuid;
|
|
56
88
|
}
|
|
57
89
|
|
|
58
90
|
/**
|
|
59
|
-
*
|
|
91
|
+
* Unsubscribes to a specific subscription given it's symbol or an entire
|
|
92
|
+
* event when passed as a string.
|
|
60
93
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
94
|
+
* When existing subscriptions exist for heirarchical events such as 'a.b.c',
|
|
95
|
+
* when passing an event 'a' to unsubscribe, all subscriptions for 'a', 'a.b',
|
|
96
|
+
* & 'a.b.c' will be unsubscribed as well.
|
|
64
97
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
98
|
+
unsubscribe(value: string | symbol) {
|
|
99
|
+
if (typeof value === 'string' && value.startsWith('uuid')) {
|
|
100
|
+
const path = this.tokens.get(value as PubSubUUID);
|
|
101
|
+
|
|
102
|
+
if (typeof path === 'undefined') {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const innerPath = this.events.get(path);
|
|
107
|
+
innerPath?.delete(value as PubSubUUID);
|
|
108
|
+
this.tokens.delete(value as PubSubUUID);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (typeof value === 'string') {
|
|
113
|
+
for (const key of this.events.keys()) {
|
|
114
|
+
if (key.indexOf(value) === 0) {
|
|
115
|
+
const tokens = this.events.get(key);
|
|
116
|
+
|
|
117
|
+
if (tokens && tokens.size) {
|
|
118
|
+
// eslint-disable-next-line max-depth
|
|
119
|
+
for (const token of tokens.keys()) {
|
|
120
|
+
this.tokens.delete(token);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
this.events.delete(key);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
67
128
|
}
|
|
68
129
|
|
|
69
130
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param token - A token from a `subscribe` call
|
|
131
|
+
* Get the number of subscriptions for a specific event, or when left blank
|
|
132
|
+
* will return the overall number of subscriptions for the entire pubsub.
|
|
73
133
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
134
|
+
count(event?: string) {
|
|
135
|
+
let counter = 0;
|
|
136
|
+
|
|
137
|
+
if (typeof event === 'undefined') {
|
|
138
|
+
for (const handlers of this.events.values()) {
|
|
139
|
+
counter += handlers.size;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return counter;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const handlers = this.events.get(event);
|
|
146
|
+
|
|
147
|
+
if (handlers?.size) {
|
|
148
|
+
return handlers.size;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return counter;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Deletes all existing subscriptions
|
|
156
|
+
*/
|
|
157
|
+
clear() {
|
|
158
|
+
this.events.clear();
|
|
159
|
+
this.tokens.clear();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private deliver(path: string, event: string, ...args: unknown[]) {
|
|
163
|
+
const handlers = this.events.get(path);
|
|
164
|
+
|
|
165
|
+
if (handlers && handlers.size) {
|
|
166
|
+
for (const handler of handlers.values()) {
|
|
167
|
+
handler(event, ...args);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
76
170
|
}
|
|
77
171
|
}
|
|
172
|
+
|
|
173
|
+
export const pubsub = new TinyPubSub();
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Player } from '@player-ui/player';
|
|
2
|
+
import { PubSubPlugin } from './plugin';
|
|
3
|
+
import { PubSubPluginSymbol } from './symbols';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the existing PubSubPlugin or creates and registers a new plugin
|
|
7
|
+
*/
|
|
8
|
+
export function getPubSubPlugin(player: Player) {
|
|
9
|
+
const existing = player.findPlugin<PubSubPlugin>(PubSubPluginSymbol);
|
|
10
|
+
const plugin = existing || new PubSubPlugin();
|
|
11
|
+
|
|
12
|
+
if (!existing) {
|
|
13
|
+
player.registerPlugin(plugin);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return plugin;
|
|
17
|
+
}
|