@player-ui/shared-constants-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,50 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var binding = require('@player-ui/binding');
6
+ var get = require('dlv');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
11
+
12
+ class ConstantsPlugin {
13
+ constructor(params) {
14
+ this.name = "constants";
15
+ this.updatePlayerConstants = new Set();
16
+ var _a, _b;
17
+ this.data = params.data;
18
+ this.namespace = (_a = params.namespace) != null ? _a : "constants";
19
+ this.dataPath = new binding.BindingInstance((_b = params.dataPath) != null ? _b : ["data", "constants"]);
20
+ }
21
+ apply(player) {
22
+ const updatePlayerConstants = () => player.constantsController.addConstants(this.data, this.namespace);
23
+ updatePlayerConstants();
24
+ player.hooks.onStart.tap(this.name, (flowObj) => {
25
+ var _a;
26
+ this.updatePlayerConstants.add(updatePlayerConstants);
27
+ updatePlayerConstants();
28
+ const tempData = (_a = get__default["default"](flowObj, this.dataPath.asString())) != null ? _a : {};
29
+ player.constantsController.clearTemporaryValues();
30
+ player.constantsController.setTemporaryValues(tempData, this.namespace);
31
+ });
32
+ player.hooks.onEnd.tap(this.name, () => {
33
+ player.constantsController.clearTemporaryValues();
34
+ this.updatePlayerConstants.delete(updatePlayerConstants);
35
+ });
36
+ }
37
+ updateConstants() {
38
+ this.updatePlayerConstants.forEach((update) => update());
39
+ }
40
+ getConstants() {
41
+ return this.data;
42
+ }
43
+ setConstants(data) {
44
+ this.data = data;
45
+ this.updateConstants();
46
+ }
47
+ }
48
+
49
+ exports.ConstantsPlugin = ConstantsPlugin;
50
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1,44 @@
1
+ import { PlayerPlugin, Player } from '@player-ui/player';
2
+
3
+ interface ConstantsPluginConfig {
4
+ /** Constants defined in an Content */
5
+ data: any;
6
+ /** Namespace to store the constants under. If none is specified it defaults to constants */
7
+ namespace?: string;
8
+ /** Path to data in the content root to apply as temporary data */
9
+ dataPath?: string;
10
+ }
11
+ /**
12
+ * A plugin to manage constant strings across flows
13
+ * It allows for runtime extensions/overrides through a `constants` property in the flow
14
+ */
15
+ declare class ConstantsPlugin implements PlayerPlugin {
16
+ name: string;
17
+ /**
18
+ * Namespace to store the constants under
19
+ * If none is specified it defaults to constants
20
+ */
21
+ private namespace;
22
+ /** Constants defined in an Content */
23
+ private data;
24
+ /** Path to data in the content root to apply as temporary data */
25
+ private dataPath;
26
+ /** Functions to update the constants within applied players */
27
+ private updatePlayerConstants;
28
+ /**
29
+ * Store the initial constants in the constant map
30
+ */
31
+ constructor(params: ConstantsPluginConfig);
32
+ /**
33
+ * Grab constants from content then store them store them over after every content
34
+ */
35
+ apply(player: Player): void;
36
+ /** Update constants for all active players */
37
+ private updateConstants;
38
+ /** Get constants for this namespace */
39
+ getConstants(): any;
40
+ /** Update constants for this namespace */
41
+ setConstants(data: any): void;
42
+ }
43
+
44
+ export { ConstantsPlugin, ConstantsPluginConfig };
@@ -0,0 +1,42 @@
1
+ import { BindingInstance } from '@player-ui/binding';
2
+ import get from 'dlv';
3
+
4
+ class ConstantsPlugin {
5
+ constructor(params) {
6
+ this.name = "constants";
7
+ this.updatePlayerConstants = new Set();
8
+ var _a, _b;
9
+ this.data = params.data;
10
+ this.namespace = (_a = params.namespace) != null ? _a : "constants";
11
+ this.dataPath = new BindingInstance((_b = params.dataPath) != null ? _b : ["data", "constants"]);
12
+ }
13
+ apply(player) {
14
+ const updatePlayerConstants = () => player.constantsController.addConstants(this.data, this.namespace);
15
+ updatePlayerConstants();
16
+ player.hooks.onStart.tap(this.name, (flowObj) => {
17
+ var _a;
18
+ this.updatePlayerConstants.add(updatePlayerConstants);
19
+ updatePlayerConstants();
20
+ const tempData = (_a = get(flowObj, this.dataPath.asString())) != null ? _a : {};
21
+ player.constantsController.clearTemporaryValues();
22
+ player.constantsController.setTemporaryValues(tempData, this.namespace);
23
+ });
24
+ player.hooks.onEnd.tap(this.name, () => {
25
+ player.constantsController.clearTemporaryValues();
26
+ this.updatePlayerConstants.delete(updatePlayerConstants);
27
+ });
28
+ }
29
+ updateConstants() {
30
+ this.updatePlayerConstants.forEach((update) => update());
31
+ }
32
+ getConstants() {
33
+ return this.data;
34
+ }
35
+ setConstants(data) {
36
+ this.data = data;
37
+ this.updateConstants();
38
+ }
39
+ }
40
+
41
+ export { ConstantsPlugin };
42
+ //# sourceMappingURL=index.esm.js.map
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@player-ui/shared-constants-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
+ "dlv": "^1.1.3",
15
+ "@babel/runtime": "7.15.4"
16
+ },
17
+ "main": "dist/index.cjs.js",
18
+ "module": "dist/index.esm.js",
19
+ "typings": "dist/index.d.ts"
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,90 @@
1
+ import type { Player, PlayerPlugin } from '@player-ui/player';
2
+ import { BindingInstance } from '@player-ui/binding';
3
+ import get from 'dlv';
4
+
5
+ export interface ConstantsPluginConfig {
6
+ /** Constants defined in an Content */
7
+ data: any;
8
+
9
+ /** Namespace to store the constants under. If none is specified it defaults to constants */
10
+ namespace?: string;
11
+
12
+ /** Path to data in the content root to apply as temporary data */
13
+ dataPath?: string;
14
+ }
15
+
16
+ /**
17
+ * A plugin to manage constant strings across flows
18
+ * It allows for runtime extensions/overrides through a `constants` property in the flow
19
+ */
20
+ export class ConstantsPlugin implements PlayerPlugin {
21
+ name = 'constants';
22
+
23
+ /**
24
+ * Namespace to store the constants under
25
+ * If none is specified it defaults to constants
26
+ */
27
+ private namespace: string;
28
+
29
+ /** Constants defined in an Content */
30
+ private data: Record<string, any>;
31
+
32
+ /** Path to data in the content root to apply as temporary data */
33
+ private dataPath: BindingInstance;
34
+
35
+ /** Functions to update the constants within applied players */
36
+ private updatePlayerConstants: Set<() => void> = new Set();
37
+
38
+ /**
39
+ * Store the initial constants in the constant map
40
+ */
41
+ constructor(params: ConstantsPluginConfig) {
42
+ this.data = params.data;
43
+ this.namespace = params.namespace ?? 'constants';
44
+ this.dataPath = new BindingInstance(
45
+ params.dataPath ?? ['data', 'constants']
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Grab constants from content then store them store them over after every content
51
+ */
52
+ apply(player: Player) {
53
+ /** Update constants for this player */
54
+ const updatePlayerConstants = () =>
55
+ player.constantsController.addConstants(this.data, this.namespace);
56
+ updatePlayerConstants();
57
+
58
+ // Setup hook to load latest constants and flow specific overrides to constants
59
+ player.hooks.onStart.tap(this.name, (flowObj) => {
60
+ this.updatePlayerConstants.add(updatePlayerConstants);
61
+ updatePlayerConstants();
62
+
63
+ const tempData = get(flowObj, this.dataPath.asString()) ?? {};
64
+ player.constantsController.clearTemporaryValues();
65
+ player.constantsController.setTemporaryValues(tempData, this.namespace);
66
+ });
67
+ // Clear flow specific overrides at the end of the flow and remove strong ref to player
68
+ player.hooks.onEnd.tap(this.name, () => {
69
+ player.constantsController.clearTemporaryValues();
70
+
71
+ this.updatePlayerConstants.delete(updatePlayerConstants);
72
+ });
73
+ }
74
+
75
+ /** Update constants for all active players */
76
+ private updateConstants() {
77
+ this.updatePlayerConstants.forEach((update) => update());
78
+ }
79
+
80
+ /** Get constants for this namespace */
81
+ getConstants(): any {
82
+ return this.data;
83
+ }
84
+
85
+ /** Update constants for this namespace */
86
+ setConstants(data: any) {
87
+ this.data = data;
88
+ this.updateConstants();
89
+ }
90
+ }