@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
|
@@ -100,16 +100,107 @@ return /******/ (function(modules) { // webpackBootstrap
|
|
|
100
100
|
/*!**************************************************************************!*\
|
|
101
101
|
!*** ./bazel-out/k8-fastbuild/bin/plugins/pubsub/core/dist/index.esm.js ***!
|
|
102
102
|
\**************************************************************************/
|
|
103
|
-
/*! exports provided: PubSubPlugin, PubSubPluginSymbol */
|
|
103
|
+
/*! exports provided: PubSubHandlerPlugin, PubSubPlugin, PubSubPluginSymbol */
|
|
104
104
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
105
105
|
|
|
106
106
|
"use strict";
|
|
107
107
|
__webpack_require__.r(__webpack_exports__);
|
|
108
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PubSubHandlerPlugin", function() { return PubSubHandlerPlugin; });
|
|
108
109
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PubSubPlugin", function() { return PubSubPlugin; });
|
|
109
110
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PubSubPluginSymbol", function() { return PubSubPluginSymbol; });
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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();
|
|
113
204
|
|
|
114
205
|
const PubSubPluginSymbol = Symbol.for("PubSubPlugin");
|
|
115
206
|
|
|
@@ -119,432 +210,77 @@ const _PubSubPlugin = class {
|
|
|
119
210
|
this.symbol = _PubSubPlugin.Symbol;
|
|
120
211
|
var _a;
|
|
121
212
|
this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
|
|
213
|
+
this.pubsub = pubsub;
|
|
122
214
|
}
|
|
123
215
|
apply(player) {
|
|
216
|
+
const existing = player.findPlugin(PubSubPluginSymbol);
|
|
217
|
+
if (existing !== void 0) {
|
|
218
|
+
this.pubsub = existing.pubsub;
|
|
219
|
+
}
|
|
124
220
|
player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
|
|
125
|
-
expEvaluator.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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();
|
|
130
234
|
});
|
|
131
235
|
}
|
|
132
|
-
publish(event,
|
|
133
|
-
|
|
236
|
+
publish(event, ...args) {
|
|
237
|
+
this.pubsub.publish(event, ...args);
|
|
134
238
|
}
|
|
135
239
|
subscribe(event, handler) {
|
|
136
|
-
return
|
|
240
|
+
return this.pubsub.subscribe(event, handler);
|
|
137
241
|
}
|
|
138
242
|
unsubscribe(token) {
|
|
139
|
-
|
|
243
|
+
this.pubsub.unsubscribe(token);
|
|
244
|
+
}
|
|
245
|
+
clear() {
|
|
246
|
+
this.pubsub.clear();
|
|
140
247
|
}
|
|
141
248
|
};
|
|
142
249
|
let PubSubPlugin = _PubSubPlugin;
|
|
143
250
|
PubSubPlugin.Symbol = PubSubPluginSymbol;
|
|
144
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
|
+
}
|
|
145
260
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
*
|
|
162
|
-
* https://github.com/mroderick/PubSubJS
|
|
163
|
-
*/
|
|
164
|
-
|
|
165
|
-
(function (root, factory){
|
|
166
|
-
'use strict';
|
|
167
|
-
|
|
168
|
-
var PubSub = {};
|
|
169
|
-
|
|
170
|
-
if (root.PubSub) {
|
|
171
|
-
PubSub = root.PubSub;
|
|
172
|
-
console.warn("PubSub already loaded, using existing version");
|
|
173
|
-
} else {
|
|
174
|
-
root.PubSub = PubSub;
|
|
175
|
-
factory(PubSub);
|
|
176
|
-
}
|
|
177
|
-
// CommonJS and Node.js module support
|
|
178
|
-
if (true){
|
|
179
|
-
if (module !== undefined && module.exports) {
|
|
180
|
-
exports = module.exports = PubSub; // Node.js specific `module.exports`
|
|
181
|
-
}
|
|
182
|
-
exports.PubSub = PubSub; // CommonJS module 1.1.1 spec
|
|
183
|
-
module.exports = exports = PubSub; // CommonJS
|
|
184
|
-
}
|
|
185
|
-
// AMD support
|
|
186
|
-
/* eslint-disable no-undef */
|
|
187
|
-
else {}
|
|
188
|
-
|
|
189
|
-
}(( typeof window === 'object' && window ) || this, function (PubSub){
|
|
190
|
-
'use strict';
|
|
191
|
-
|
|
192
|
-
var messages = {},
|
|
193
|
-
lastUid = -1,
|
|
194
|
-
ALL_SUBSCRIBING_MSG = '*';
|
|
195
|
-
|
|
196
|
-
function hasKeys(obj){
|
|
197
|
-
var key;
|
|
198
|
-
|
|
199
|
-
for (key in obj){
|
|
200
|
-
if ( Object.prototype.hasOwnProperty.call(obj, key) ){
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Returns a function that throws the passed exception, for use as argument for setTimeout
|
|
209
|
-
* @alias throwException
|
|
210
|
-
* @function
|
|
211
|
-
* @param { Object } ex An Error object
|
|
212
|
-
*/
|
|
213
|
-
function throwException( ex ){
|
|
214
|
-
return function reThrowException(){
|
|
215
|
-
throw ex;
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function callSubscriberWithDelayedExceptions( subscriber, message, data ){
|
|
220
|
-
try {
|
|
221
|
-
subscriber( message, data );
|
|
222
|
-
} catch( ex ){
|
|
223
|
-
setTimeout( throwException( ex ), 0);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function callSubscriberWithImmediateExceptions( subscriber, message, data ){
|
|
228
|
-
subscriber( message, data );
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function deliverMessage( originalMessage, matchedMessage, data, immediateExceptions ){
|
|
232
|
-
var subscribers = messages[matchedMessage],
|
|
233
|
-
callSubscriber = immediateExceptions ? callSubscriberWithImmediateExceptions : callSubscriberWithDelayedExceptions,
|
|
234
|
-
s;
|
|
235
|
-
|
|
236
|
-
if ( !Object.prototype.hasOwnProperty.call( messages, matchedMessage ) ) {
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
for (s in subscribers){
|
|
241
|
-
if ( Object.prototype.hasOwnProperty.call(subscribers, s)){
|
|
242
|
-
callSubscriber( subscribers[s], originalMessage, data );
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
function createDeliveryFunction( message, data, immediateExceptions ){
|
|
248
|
-
return function deliverNamespaced(){
|
|
249
|
-
var topic = String( message ),
|
|
250
|
-
position = topic.lastIndexOf( '.' );
|
|
251
|
-
|
|
252
|
-
// deliver the message as it is now
|
|
253
|
-
deliverMessage(message, message, data, immediateExceptions);
|
|
254
|
-
|
|
255
|
-
// trim the hierarchy and deliver message to each level
|
|
256
|
-
while( position !== -1 ){
|
|
257
|
-
topic = topic.substr( 0, position );
|
|
258
|
-
position = topic.lastIndexOf('.');
|
|
259
|
-
deliverMessage( message, topic, data, immediateExceptions );
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
deliverMessage(message, ALL_SUBSCRIBING_MSG, data, immediateExceptions);
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function hasDirectSubscribersFor( message ) {
|
|
267
|
-
var topic = String( message ),
|
|
268
|
-
found = Boolean(Object.prototype.hasOwnProperty.call( messages, topic ) && hasKeys(messages[topic]));
|
|
269
|
-
|
|
270
|
-
return found;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function messageHasSubscribers( message ){
|
|
274
|
-
var topic = String( message ),
|
|
275
|
-
found = hasDirectSubscribersFor(topic) || hasDirectSubscribersFor(ALL_SUBSCRIBING_MSG),
|
|
276
|
-
position = topic.lastIndexOf( '.' );
|
|
277
|
-
|
|
278
|
-
while ( !found && position !== -1 ){
|
|
279
|
-
topic = topic.substr( 0, position );
|
|
280
|
-
position = topic.lastIndexOf( '.' );
|
|
281
|
-
found = hasDirectSubscribersFor(topic);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return found;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function publish( message, data, sync, immediateExceptions ){
|
|
288
|
-
message = (typeof message === 'symbol') ? message.toString() : message;
|
|
289
|
-
|
|
290
|
-
var deliver = createDeliveryFunction( message, data, immediateExceptions ),
|
|
291
|
-
hasSubscribers = messageHasSubscribers( message );
|
|
292
|
-
|
|
293
|
-
if ( !hasSubscribers ){
|
|
294
|
-
return false;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if ( sync === true ){
|
|
298
|
-
deliver();
|
|
299
|
-
} else {
|
|
300
|
-
setTimeout( deliver, 0 );
|
|
301
|
-
}
|
|
302
|
-
return true;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Publishes the message, passing the data to it's subscribers
|
|
307
|
-
* @function
|
|
308
|
-
* @alias publish
|
|
309
|
-
* @param { String } message The message to publish
|
|
310
|
-
* @param {} data The data to pass to subscribers
|
|
311
|
-
* @return { Boolean }
|
|
312
|
-
*/
|
|
313
|
-
PubSub.publish = function( message, data ){
|
|
314
|
-
return publish( message, data, false, PubSub.immediateExceptions );
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Publishes the message synchronously, passing the data to it's subscribers
|
|
319
|
-
* @function
|
|
320
|
-
* @alias publishSync
|
|
321
|
-
* @param { String } message The message to publish
|
|
322
|
-
* @param {} data The data to pass to subscribers
|
|
323
|
-
* @return { Boolean }
|
|
324
|
-
*/
|
|
325
|
-
PubSub.publishSync = function( message, data ){
|
|
326
|
-
return publish( message, data, true, PubSub.immediateExceptions );
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Subscribes the passed function to the passed message. Every returned token is unique and should be stored if you need to unsubscribe
|
|
331
|
-
* @function
|
|
332
|
-
* @alias subscribe
|
|
333
|
-
* @param { String } message The message to subscribe to
|
|
334
|
-
* @param { Function } func The function to call when a new message is published
|
|
335
|
-
* @return { String }
|
|
336
|
-
*/
|
|
337
|
-
PubSub.subscribe = function( message, func ){
|
|
338
|
-
if ( typeof func !== 'function'){
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
message = (typeof message === 'symbol') ? message.toString() : message;
|
|
343
|
-
|
|
344
|
-
// message is not registered yet
|
|
345
|
-
if ( !Object.prototype.hasOwnProperty.call( messages, message ) ){
|
|
346
|
-
messages[message] = {};
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// forcing token as String, to allow for future expansions without breaking usage
|
|
350
|
-
// and allow for easy use as key names for the 'messages' object
|
|
351
|
-
var token = 'uid_' + String(++lastUid);
|
|
352
|
-
messages[message][token] = func;
|
|
353
|
-
|
|
354
|
-
// return token for unsubscribing
|
|
355
|
-
return token;
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
PubSub.subscribeAll = function( func ){
|
|
359
|
-
return PubSub.subscribe(ALL_SUBSCRIBING_MSG, func);
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Subscribes the passed function to the passed message once
|
|
364
|
-
* @function
|
|
365
|
-
* @alias subscribeOnce
|
|
366
|
-
* @param { String } message The message to subscribe to
|
|
367
|
-
* @param { Function } func The function to call when a new message is published
|
|
368
|
-
* @return { PubSub }
|
|
369
|
-
*/
|
|
370
|
-
PubSub.subscribeOnce = function( message, func ){
|
|
371
|
-
var token = PubSub.subscribe( message, function(){
|
|
372
|
-
// before func apply, unsubscribe message
|
|
373
|
-
PubSub.unsubscribe( token );
|
|
374
|
-
func.apply( this, arguments );
|
|
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`);
|
|
375
276
|
});
|
|
376
|
-
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
* Clears all subscriptions
|
|
381
|
-
* @function
|
|
382
|
-
* @public
|
|
383
|
-
* @alias clearAllSubscriptions
|
|
384
|
-
*/
|
|
385
|
-
PubSub.clearAllSubscriptions = function clearAllSubscriptions(){
|
|
386
|
-
messages = {};
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Clear subscriptions by the topic
|
|
391
|
-
* @function
|
|
392
|
-
* @public
|
|
393
|
-
* @alias clearAllSubscriptions
|
|
394
|
-
* @return { int }
|
|
395
|
-
*/
|
|
396
|
-
PubSub.clearSubscriptions = function clearSubscriptions(topic){
|
|
397
|
-
var m;
|
|
398
|
-
for (m in messages){
|
|
399
|
-
if (Object.prototype.hasOwnProperty.call(messages, m) && m.indexOf(topic) === 0){
|
|
400
|
-
delete messages[m];
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
Count subscriptions by the topic
|
|
407
|
-
* @function
|
|
408
|
-
* @public
|
|
409
|
-
* @alias countSubscriptions
|
|
410
|
-
* @return { Array }
|
|
411
|
-
*/
|
|
412
|
-
PubSub.countSubscriptions = function countSubscriptions(topic){
|
|
413
|
-
var m;
|
|
414
|
-
// eslint-disable-next-line no-unused-vars
|
|
415
|
-
var token;
|
|
416
|
-
var count = 0;
|
|
417
|
-
for (m in messages) {
|
|
418
|
-
if (Object.prototype.hasOwnProperty.call(messages, m) && m.indexOf(topic) === 0) {
|
|
419
|
-
for (token in messages[m]) {
|
|
420
|
-
count++;
|
|
421
|
-
}
|
|
422
|
-
break;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
return count;
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
Gets subscriptions by the topic
|
|
431
|
-
* @function
|
|
432
|
-
* @public
|
|
433
|
-
* @alias getSubscriptions
|
|
434
|
-
*/
|
|
435
|
-
PubSub.getSubscriptions = function getSubscriptions(topic){
|
|
436
|
-
var m;
|
|
437
|
-
var list = [];
|
|
438
|
-
for (m in messages){
|
|
439
|
-
if (Object.prototype.hasOwnProperty.call(messages, m) && m.indexOf(topic) === 0){
|
|
440
|
-
list.push(m);
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
return list;
|
|
444
|
-
};
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Removes subscriptions
|
|
448
|
-
*
|
|
449
|
-
* - When passed a token, removes a specific subscription.
|
|
450
|
-
*
|
|
451
|
-
* - When passed a function, removes all subscriptions for that function
|
|
452
|
-
*
|
|
453
|
-
* - When passed a topic, removes all subscriptions for that topic (hierarchy)
|
|
454
|
-
* @function
|
|
455
|
-
* @public
|
|
456
|
-
* @alias subscribeOnce
|
|
457
|
-
* @param { String | Function } value A token, function or topic to unsubscribe from
|
|
458
|
-
* @example // Unsubscribing with a token
|
|
459
|
-
* var token = PubSub.subscribe('mytopic', myFunc);
|
|
460
|
-
* PubSub.unsubscribe(token);
|
|
461
|
-
* @example // Unsubscribing with a function
|
|
462
|
-
* PubSub.unsubscribe(myFunc);
|
|
463
|
-
* @example // Unsubscribing from a topic
|
|
464
|
-
* PubSub.unsubscribe('mytopic');
|
|
465
|
-
*/
|
|
466
|
-
PubSub.unsubscribe = function(value){
|
|
467
|
-
var descendantTopicExists = function(topic) {
|
|
468
|
-
var m;
|
|
469
|
-
for ( m in messages ){
|
|
470
|
-
if ( Object.prototype.hasOwnProperty.call(messages, m) && m.indexOf(topic) === 0 ){
|
|
471
|
-
// a descendant of the topic exists:
|
|
472
|
-
return true;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
return false;
|
|
477
|
-
},
|
|
478
|
-
isTopic = typeof value === 'string' && ( Object.prototype.hasOwnProperty.call(messages, value) || descendantTopicExists(value) ),
|
|
479
|
-
isToken = !isTopic && typeof value === 'string',
|
|
480
|
-
isFunction = typeof value === 'function',
|
|
481
|
-
result = false,
|
|
482
|
-
m, message, t;
|
|
483
|
-
|
|
484
|
-
if (isTopic){
|
|
485
|
-
PubSub.clearSubscriptions(value);
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
for ( m in messages ){
|
|
490
|
-
if ( Object.prototype.hasOwnProperty.call( messages, m ) ){
|
|
491
|
-
message = messages[m];
|
|
492
|
-
|
|
493
|
-
if ( isToken && message[value] ){
|
|
494
|
-
delete message[value];
|
|
495
|
-
result = value;
|
|
496
|
-
// tokens are unique, so we can just stop here
|
|
497
|
-
break;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
if (isFunction) {
|
|
501
|
-
for ( t in message ){
|
|
502
|
-
if (Object.prototype.hasOwnProperty.call(message, t) && message[t] === value){
|
|
503
|
-
delete message[t];
|
|
504
|
-
result = true;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
return result;
|
|
512
|
-
};
|
|
513
|
-
}));
|
|
514
|
-
|
|
515
|
-
/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../webpack/buildin/module.js */ "./node_modules/webpack/buildin/module.js")(module)))
|
|
516
|
-
|
|
517
|
-
/***/ }),
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
518
281
|
|
|
519
|
-
/***/ "./node_modules/webpack/buildin/module.js":
|
|
520
|
-
/*!***********************************!*\
|
|
521
|
-
!*** (webpack)/buildin/module.js ***!
|
|
522
|
-
\***********************************/
|
|
523
|
-
/*! no static exports found */
|
|
524
|
-
/***/ (function(module, exports) {
|
|
525
282
|
|
|
526
|
-
|
|
527
|
-
if (!module.webpackPolyfill) {
|
|
528
|
-
module.deprecate = function() {};
|
|
529
|
-
module.paths = [];
|
|
530
|
-
// module.parent = undefined by default
|
|
531
|
-
if (!module.children) module.children = [];
|
|
532
|
-
Object.defineProperty(module, "loaded", {
|
|
533
|
-
enumerable: true,
|
|
534
|
-
get: function() {
|
|
535
|
-
return module.l;
|
|
536
|
-
}
|
|
537
|
-
});
|
|
538
|
-
Object.defineProperty(module, "id", {
|
|
539
|
-
enumerable: true,
|
|
540
|
-
get: function() {
|
|
541
|
-
return module.i;
|
|
542
|
-
}
|
|
543
|
-
});
|
|
544
|
-
module.webpackPolyfill = 1;
|
|
545
|
-
}
|
|
546
|
-
return module;
|
|
547
|
-
};
|
|
283
|
+
//# sourceMappingURL=index.esm.js.map
|
|
548
284
|
|
|
549
285
|
|
|
550
286
|
/***/ })
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t
|
|
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.
|
|
3
|
+
"version": "0.4.1-next.1",
|
|
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.
|
|
9
|
+
"@player-ui/player": "0.4.1-next.1"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"tapable-ts": "^0.2.3",
|
|
@@ -55,6 +55,14 @@
|
|
|
55
55
|
{
|
|
56
56
|
"name": "Kelly Harrop",
|
|
57
57
|
"url": "https://github.com/kharrop"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"name": "Alejandro Fimbres",
|
|
61
|
+
"url": "https://github.com/lexfm"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "Rafael Campos",
|
|
65
|
+
"url": "https://github.com/rafbcampos"
|
|
58
66
|
}
|
|
59
67
|
],
|
|
60
68
|
"bundle": "./dist/pubsub-plugin.prod.js"
|