@player-ui/pubsub-plugin 0.0.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.
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var pubsub = require('pubsub-js');
6
+
7
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
+
9
+ var pubsub__default = /*#__PURE__*/_interopDefaultLegacy(pubsub);
10
+
11
+ const PubSubPluginSymbol = Symbol.for("PubSubPlugin");
12
+
13
+ const _PubSubPlugin = class {
14
+ constructor(config) {
15
+ this.name = "pub-sub";
16
+ this.symbol = _PubSubPlugin.Symbol;
17
+ var _a;
18
+ this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
19
+ }
20
+ apply(player) {
21
+ player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
22
+ expEvaluator.addExpressionFunction(this.expressionName, (_ctx, event, data) => {
23
+ if (typeof event === "string") {
24
+ this.publish(event, data);
25
+ }
26
+ });
27
+ });
28
+ }
29
+ publish(event, data) {
30
+ pubsub__default["default"].publishSync(event, data);
31
+ }
32
+ subscribe(event, handler) {
33
+ return pubsub__default["default"].subscribe(event, handler);
34
+ }
35
+ unsubscribe(token) {
36
+ pubsub__default["default"].unsubscribe(token);
37
+ }
38
+ };
39
+ let PubSubPlugin = _PubSubPlugin;
40
+ PubSubPlugin.Symbol = PubSubPluginSymbol;
41
+
42
+ exports.PubSubPlugin = PubSubPlugin;
43
+ exports.PubSubPluginSymbol = PubSubPluginSymbol;
44
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1,48 @@
1
+ import { PlayerPlugin, Player } from '@player-ui/player';
2
+
3
+ interface PubSubConfig {
4
+ /** A custom expression name to register */
5
+ expressionName: string;
6
+ }
7
+ /**
8
+ * The PubSubPlugin is a great way to enable your Content content to publish events back to your app
9
+ * It injects a publish() function into the expression language, and will forward all events back to any subscribers.
10
+ *
11
+ * Published/Subscribed events support a hierarchy:
12
+ * - publish('foo', 'data') -- will trigger any listeners for 'foo'
13
+ * - publish('foo.bar', 'data') -- will trigger any listeners for 'foo' or 'foo.bar'
14
+ *
15
+ */
16
+ declare class PubSubPlugin implements PlayerPlugin {
17
+ name: string;
18
+ static Symbol: symbol;
19
+ readonly symbol: symbol;
20
+ private expressionName;
21
+ constructor(config?: PubSubConfig);
22
+ apply(player: Player): void;
23
+ /**
24
+ * A way of publishing an event, notifying any listeners
25
+ *
26
+ * @param event - The name of the event to publish. Can take sub-topics like: foo.bar
27
+ * @param data - Any additional data to attach to the event
28
+ */
29
+ publish(event: string, data: unknown): void;
30
+ /**
31
+ * Subscribe to an event with the given name. The handler will get called for any published event
32
+ *
33
+ * @param event - The name of the event to subscribe to
34
+ * @param handler - A function to be called when the event is triggered
35
+ * @returns A token to be used to unsubscribe from the event
36
+ */
37
+ subscribe(event: string, handler: (e: string, data: unknown) => void): string;
38
+ /**
39
+ * Remove any subscriptions using the given token
40
+ *
41
+ * @param token - A token from a `subscribe` call
42
+ */
43
+ unsubscribe(token: string): void;
44
+ }
45
+
46
+ declare const PubSubPluginSymbol: unique symbol;
47
+
48
+ export { PubSubConfig, PubSubPlugin, PubSubPluginSymbol };
@@ -0,0 +1,35 @@
1
+ import pubsub from 'pubsub-js';
2
+
3
+ const PubSubPluginSymbol = Symbol.for("PubSubPlugin");
4
+
5
+ const _PubSubPlugin = class {
6
+ constructor(config) {
7
+ this.name = "pub-sub";
8
+ this.symbol = _PubSubPlugin.Symbol;
9
+ var _a;
10
+ this.expressionName = (_a = config == null ? void 0 : config.expressionName) != null ? _a : "publish";
11
+ }
12
+ apply(player) {
13
+ player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
14
+ expEvaluator.addExpressionFunction(this.expressionName, (_ctx, event, data) => {
15
+ if (typeof event === "string") {
16
+ this.publish(event, data);
17
+ }
18
+ });
19
+ });
20
+ }
21
+ publish(event, data) {
22
+ pubsub.publishSync(event, data);
23
+ }
24
+ subscribe(event, handler) {
25
+ return pubsub.subscribe(event, handler);
26
+ }
27
+ unsubscribe(token) {
28
+ pubsub.unsubscribe(token);
29
+ }
30
+ };
31
+ let PubSubPlugin = _PubSubPlugin;
32
+ PubSubPlugin.Symbol = PubSubPluginSymbol;
33
+
34
+ export { PubSubPlugin, PubSubPluginSymbol };
35
+ //# sourceMappingURL=index.esm.js.map
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@player-ui/pubsub-plugin",
3
+ "version": "0.0.1-next.1",
4
+ "private": false,
5
+ "publishConfig": {
6
+ "registry": "https://registry.npmjs.org"
7
+ },
8
+ "peerDependencies": {
9
+ "@player-ui/binding-grammar": "0.0.1-next.1"
10
+ },
11
+ "dependencies": {
12
+ "tapable": "1.1.3",
13
+ "@types/tapable": "^1.0.5",
14
+ "pubsub-js": "^1.9.3",
15
+ "@types/pubsub-js": "^1.8.3",
16
+ "@babel/runtime": "7.15.4"
17
+ },
18
+ "main": "dist/index.cjs.js",
19
+ "module": "dist/index.esm.js",
20
+ "typings": "dist/index.d.ts"
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './pubsub';
2
+ export * from './symbols';
package/src/pubsub.ts ADDED
@@ -0,0 +1,74 @@
1
+ import pubsub from 'pubsub-js';
2
+ import type { Player, PlayerPlugin } from '@player-ui/player';
3
+ import type { ExpressionContext } from '@player-ui/expressions';
4
+ import { PubSubPluginSymbol } from './symbols';
5
+
6
+ export interface PubSubConfig {
7
+ /** A custom expression name to register */
8
+ expressionName: string;
9
+ }
10
+
11
+ /**
12
+ * The PubSubPlugin is a great way to enable your Content content to publish events back to your app
13
+ * It injects a publish() function into the expression language, and will forward all events back to any subscribers.
14
+ *
15
+ * Published/Subscribed events support a hierarchy:
16
+ * - publish('foo', 'data') -- will trigger any listeners for 'foo'
17
+ * - publish('foo.bar', 'data') -- will trigger any listeners for 'foo' or 'foo.bar'
18
+ *
19
+ */
20
+ export class PubSubPlugin implements PlayerPlugin {
21
+ name = 'pub-sub';
22
+
23
+ static Symbol = PubSubPluginSymbol;
24
+ public readonly symbol = PubSubPlugin.Symbol;
25
+
26
+ private expressionName: string;
27
+
28
+ constructor(config?: PubSubConfig) {
29
+ this.expressionName = config?.expressionName ?? 'publish';
30
+ }
31
+
32
+ apply(player: Player) {
33
+ player.hooks.expressionEvaluator.tap(this.name, (expEvaluator) => {
34
+ expEvaluator.addExpressionFunction(
35
+ this.expressionName,
36
+ (_ctx: ExpressionContext, event: unknown, data: unknown) => {
37
+ if (typeof event === 'string') {
38
+ this.publish(event, data);
39
+ }
40
+ }
41
+ );
42
+ });
43
+ }
44
+
45
+ /**
46
+ * A way of publishing an event, notifying any listeners
47
+ *
48
+ * @param event - The name of the event to publish. Can take sub-topics like: foo.bar
49
+ * @param data - Any additional data to attach to the event
50
+ */
51
+ publish(event: string, data: unknown) {
52
+ pubsub.publishSync(event, data);
53
+ }
54
+
55
+ /**
56
+ * Subscribe to an event with the given name. The handler will get called for any published event
57
+ *
58
+ * @param event - The name of the event to subscribe to
59
+ * @param handler - A function to be called when the event is triggered
60
+ * @returns A token to be used to unsubscribe from the event
61
+ */
62
+ subscribe(event: string, handler: (e: string, data: unknown) => void) {
63
+ return pubsub.subscribe(event, handler);
64
+ }
65
+
66
+ /**
67
+ * Remove any subscriptions using the given token
68
+ *
69
+ * @param token - A token from a `subscribe` call
70
+ */
71
+ unsubscribe(token: string) {
72
+ pubsub.unsubscribe(token);
73
+ }
74
+ }
package/src/symbols.ts ADDED
@@ -0,0 +1 @@
1
+ export const PubSubPluginSymbol = Symbol.for('PubSubPlugin');