@player-ui/player 0.8.0-next.4 → 0.8.0-next.6

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.
Files changed (37) hide show
  1. package/dist/Player.native.js +240 -252
  2. package/dist/Player.native.js.map +1 -1
  3. package/dist/cjs/index.cjs +208 -268
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/index.legacy-esm.js +206 -270
  6. package/dist/index.mjs +206 -270
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +4 -4
  9. package/src/plugins/default-view-plugin.ts +4 -0
  10. package/src/view/__tests__/view.immutable.test.ts +8 -1
  11. package/src/view/__tests__/view.test.ts +59 -1
  12. package/src/view/parser/__tests__/parser.test.ts +16 -1
  13. package/src/view/parser/index.ts +46 -262
  14. package/src/view/parser/utils.ts +23 -3
  15. package/src/view/plugins/__tests__/__snapshots__/asset.test.ts.snap +215 -0
  16. package/src/view/plugins/__tests__/__snapshots__/multi-node.test.ts.snap +67 -0
  17. package/src/view/plugins/__tests__/__snapshots__/template.test.ts.snap +56 -54
  18. package/src/view/plugins/__tests__/applicability.test.ts +24 -16
  19. package/src/view/plugins/__tests__/asset.test.ts +140 -0
  20. package/src/view/plugins/__tests__/multi-node.test.ts +38 -0
  21. package/src/view/plugins/__tests__/template.test.ts +48 -388
  22. package/src/view/plugins/applicability.ts +39 -23
  23. package/src/view/plugins/asset.ts +42 -0
  24. package/src/view/plugins/index.ts +3 -1
  25. package/src/view/plugins/multi-node.ts +73 -0
  26. package/src/view/plugins/switch.ts +81 -50
  27. package/src/view/plugins/{template-plugin.ts → template.ts} +38 -24
  28. package/src/view/resolver/__tests__/edgecases.test.ts +14 -1
  29. package/src/view/view.ts +2 -7
  30. package/types/view/parser/index.d.ts +6 -11
  31. package/types/view/parser/utils.d.ts +11 -2
  32. package/types/view/plugins/applicability.d.ts +1 -0
  33. package/types/view/plugins/asset.d.ts +8 -0
  34. package/types/view/plugins/index.d.ts +3 -1
  35. package/types/view/plugins/multi-node.d.ts +8 -0
  36. package/types/view/plugins/switch.d.ts +2 -1
  37. package/types/view/plugins/{template-plugin.d.ts → template.d.ts} +2 -2
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@player-ui/player",
3
- "version": "0.8.0-next.4",
3
+ "version": "0.8.0-next.6",
4
4
  "main": "dist/cjs/index.cjs",
5
5
  "dependencies": {
6
- "@player-ui/partial-match-registry": "0.8.0-next.4",
7
- "@player-ui/make-flow": "0.8.0-next.4",
8
- "@player-ui/types": "0.8.0-next.4",
6
+ "@player-ui/partial-match-registry": "0.8.0-next.6",
7
+ "@player-ui/make-flow": "0.8.0-next.6",
8
+ "@player-ui/types": "0.8.0-next.6",
9
9
  "@types/dlv": "^1.1.4",
10
10
  "dequal": "^2.0.2",
11
11
  "dlv": "^1.1.3",
@@ -1,6 +1,8 @@
1
1
  import type { Player, PlayerPlugin } from "../player";
2
2
  import {
3
3
  ApplicabilityPlugin,
4
+ AssetPlugin,
5
+ MultiNodePlugin,
4
6
  StringResolverPlugin,
5
7
  SwitchPlugin,
6
8
  TemplatePlugin,
@@ -17,12 +19,14 @@ export class DefaultViewPlugin implements PlayerPlugin {
17
19
  player.hooks.viewController.tap(this.name, (viewController) => {
18
20
  viewController.hooks.view.tap(this.name, (view) => {
19
21
  const pluginOptions = toNodeResolveOptions(view.resolverOptions);
22
+ new AssetPlugin().apply(view);
20
23
  new SwitchPlugin(pluginOptions).apply(view);
21
24
  new ApplicabilityPlugin().apply(view);
22
25
  new StringResolverPlugin().apply(view);
23
26
  const templatePlugin = new TemplatePlugin(pluginOptions);
24
27
  templatePlugin.apply(view);
25
28
  view.hooks.onTemplatePluginCreated.call(templatePlugin);
29
+ new MultiNodePlugin().apply(view);
26
30
  });
27
31
  });
28
32
  }
@@ -3,7 +3,12 @@ import { LocalModel, withParser, PipelinedDataModel } from "../../data";
3
3
  import { ExpressionEvaluator } from "../../expressions";
4
4
  import { BindingParser } from "../../binding";
5
5
  import { SchemaController } from "../../schema";
6
- import { ApplicabilityPlugin, StringResolverPlugin, ViewInstance } from "..";
6
+ import {
7
+ ApplicabilityPlugin,
8
+ AssetPlugin,
9
+ StringResolverPlugin,
10
+ ViewInstance,
11
+ } from "..";
7
12
  import { NodeType } from "../parser";
8
13
 
9
14
  const parseBinding = new BindingParser().parse;
@@ -34,6 +39,7 @@ test("uses the exact same object if nothing changes", () => {
34
39
  );
35
40
 
36
41
  new StringResolverPlugin().apply(view);
42
+ new AssetPlugin().apply(view);
37
43
 
38
44
  view.hooks.resolver.tap("input", (resolver) => {
39
45
  resolver.hooks.resolve.tap("input", (value, astNode, options) => {
@@ -205,6 +211,7 @@ test("hardcore immutability", () => {
205
211
  );
206
212
 
207
213
  new StringResolverPlugin().apply(view);
214
+ new AssetPlugin().apply(view);
208
215
 
209
216
  const resolved = view.update();
210
217
 
@@ -4,6 +4,7 @@ import { ExpressionEvaluator } from "../../expressions";
4
4
  import { BindingParser } from "../../binding";
5
5
  import { SchemaController } from "../../schema";
6
6
  import {
7
+ MultiNodePlugin,
7
8
  StringResolverPlugin,
8
9
  SwitchPlugin,
9
10
  ViewInstance,
@@ -80,7 +81,7 @@ describe("view", () => {
80
81
  expect(updated).toBe(resolved);
81
82
  });
82
83
 
83
- test("works with no valid switch cases in an array", () => {
84
+ test("works with no valid static switch cases in an array", () => {
84
85
  const model = withParser(new LocalModel({}), parseBinding);
85
86
  const evaluator = new ExpressionEvaluator({ model });
86
87
  const schema = new SchemaController();
@@ -122,6 +123,7 @@ describe("view", () => {
122
123
 
123
124
  const pluginOptions = toNodeResolveOptions(view.resolverOptions);
124
125
  new SwitchPlugin(pluginOptions).apply(view);
126
+ new MultiNodePlugin().apply(view);
125
127
  new StringResolverPlugin().apply(view);
126
128
 
127
129
  const resolved = view.update();
@@ -132,6 +134,60 @@ describe("view", () => {
132
134
  });
133
135
  });
134
136
 
137
+ test("works with no valid dynamic switch cases in an array", () => {
138
+ const model = withParser(new LocalModel({}), parseBinding);
139
+ const evaluator = new ExpressionEvaluator({ model });
140
+ const schema = new SchemaController();
141
+
142
+ const view = new ViewInstance(
143
+ {
144
+ id: "test",
145
+ type: "view",
146
+ title: [
147
+ {
148
+ dynamicSwitch: [
149
+ {
150
+ case: false,
151
+ asset: {
152
+ id: "false-case",
153
+ type: "text",
154
+ value: "some text",
155
+ },
156
+ },
157
+ {
158
+ case: false,
159
+ asset: {
160
+ id: "false-case-2",
161
+ type: "text",
162
+ value: "some text",
163
+ },
164
+ },
165
+ ],
166
+ },
167
+ ],
168
+ },
169
+ {
170
+ model,
171
+ parseBinding,
172
+ evaluator,
173
+ schema,
174
+ },
175
+ );
176
+
177
+ const pluginOptions = toNodeResolveOptions(view.resolverOptions);
178
+ new SwitchPlugin(pluginOptions).apply(view);
179
+ new MultiNodePlugin().apply(view);
180
+ new StringResolverPlugin().apply(view);
181
+
182
+ const resolved = view.update();
183
+
184
+ expect(resolved).toStrictEqual({
185
+ id: "test",
186
+ title: [],
187
+ type: "view",
188
+ });
189
+ });
190
+
135
191
  it("does not return a field object if the case does not resolve an asset", () => {
136
192
  const model = withParser(
137
193
  new LocalModel({
@@ -381,6 +437,7 @@ describe("view", () => {
381
437
 
382
438
  const pluginOptions = toNodeResolveOptions(view.resolverOptions);
383
439
  new SwitchPlugin(pluginOptions).apply(view);
440
+ new MultiNodePlugin().apply(view);
384
441
  new StringResolverPlugin().apply(view);
385
442
 
386
443
  const resolved = view.update();
@@ -735,6 +792,7 @@ describe("view", () => {
735
792
 
736
793
  const pluginOptions = toNodeResolveOptions(view.resolverOptions);
737
794
  new SwitchPlugin(pluginOptions).apply(view);
795
+ new MultiNodePlugin().apply(view);
738
796
  new StringResolverPlugin().apply(view);
739
797
 
740
798
  const resolved = view.update();
@@ -3,7 +3,13 @@ import { BindingParser } from "../../../binding";
3
3
  import { LocalModel, withParser } from "../../../data";
4
4
  import { SchemaController } from "../../../schema";
5
5
  import { NodeType, Parser } from "../index";
6
- import { SwitchPlugin, ApplicabilityPlugin, TemplatePlugin } from "../..";
6
+ import {
7
+ SwitchPlugin,
8
+ ApplicabilityPlugin,
9
+ TemplatePlugin,
10
+ MultiNodePlugin,
11
+ AssetPlugin,
12
+ } from "../..";
7
13
  import type { Options } from "../../plugins/options";
8
14
  import { ExpressionEvaluator } from "../../../expressions";
9
15
  import type { DataModelWithParser } from "../../../data";
@@ -30,9 +36,11 @@ describe("generates the correct AST", () => {
30
36
  model,
31
37
  },
32
38
  };
39
+ new AssetPlugin().applyParser(parser);
33
40
  new TemplatePlugin(options).applyParser(parser);
34
41
  new ApplicabilityPlugin().applyParser(parser);
35
42
  new SwitchPlugin(options).applyParser(parser);
43
+ new MultiNodePlugin().applyParser(parser);
36
44
  });
37
45
 
38
46
  test("works with basic objects", () => {
@@ -154,8 +162,10 @@ describe("parseView", () => {
154
162
  model,
155
163
  },
156
164
  };
165
+ new AssetPlugin().applyParser(parser);
157
166
  new TemplatePlugin(options).applyParser(parser);
158
167
  new ApplicabilityPlugin().applyParser(parser);
168
+ new MultiNodePlugin().applyParser(parser);
159
169
  new SwitchPlugin(options).applyParser(parser);
160
170
  });
161
171
 
@@ -249,8 +259,13 @@ describe("generates the correct AST when using switch plugin", () => {
249
259
  return true;
250
260
  },
251
261
  } as any);
262
+ const multiNodePlugin = new MultiNodePlugin();
263
+ const assetPlugin = new AssetPlugin();
264
+
252
265
  const parser = new Parser();
253
266
  switchPlugin.applyParser(parser);
267
+ multiNodePlugin.applyParser(parser);
268
+ assetPlugin.applyParser(parser);
254
269
 
255
270
  test("works with asset wrapped objects", () => {
256
271
  expect(parser.parseObject(toughStaticSwitchView)).toMatchSnapshot();
@@ -1,9 +1,7 @@
1
- import { omit, setIn } from "timm";
1
+ import { setIn } from "timm";
2
2
  import { SyncBailHook, SyncWaterfallHook } from "tapable-ts";
3
- import type { Template } from "@player-ui/types";
4
3
  import type { AnyAssetType, Node } from "./types";
5
4
  import { NodeType } from "./types";
6
- import { getNodeID, hasAsync } from "./utils";
7
5
 
8
6
  export * from "./types";
9
7
  export * from "./utils";
@@ -17,6 +15,12 @@ export interface ParseObjectOptions {
17
15
  templateDepth?: number;
18
16
  }
19
17
 
18
+ export interface ParseObjectChildOptions {
19
+ key: string;
20
+ path: Node.PathSegment[];
21
+ parentObj: object;
22
+ }
23
+
20
24
  interface NestedObj {
21
25
  /** The values of a nested local object */
22
26
  children: Node.Child[];
@@ -52,16 +56,14 @@ export class Parser {
52
56
  [Node.Node | undefined | null, object]
53
57
  >(),
54
58
 
55
- determineNodeType: new SyncBailHook<[object | string], NodeType>(),
56
-
57
59
  parseNode: new SyncBailHook<
58
60
  [
59
61
  obj: object,
60
62
  nodeType: Node.ChildrenTypes,
61
63
  parseOptions: ParseObjectOptions,
62
- determinedNodeType: NodeType | null,
64
+ childOptions?: ParseObjectChildOptions,
63
65
  ],
64
- Node.Node
66
+ Node.Node | Node.Child[]
65
67
  >(),
66
68
  };
67
69
 
@@ -75,27 +77,6 @@ export class Parser {
75
77
  return viewNode as Node.View;
76
78
  }
77
79
 
78
- private parseAsync(
79
- obj: object,
80
- type: Node.ChildrenTypes,
81
- options: ParseObjectOptions,
82
- ): Node.Node | null {
83
- const parsedAsync = this.parseObject(omit(obj, "async"), type, options);
84
- const parsedNodeId = getNodeID(parsedAsync);
85
- if (parsedAsync !== null && parsedNodeId) {
86
- return this.createASTNode(
87
- {
88
- id: parsedNodeId,
89
- type: NodeType.Async,
90
- value: parsedAsync,
91
- },
92
- obj,
93
- );
94
- }
95
-
96
- return null;
97
- }
98
-
99
80
  public createASTNode(node: Node.Node | null, value: any): Node.Node | null {
100
81
  const tapped = this.hooks.onCreateASTNode.call(node, value);
101
82
 
@@ -106,47 +87,21 @@ export class Parser {
106
87
  return tapped;
107
88
  }
108
89
 
109
- /**
110
- * Checks if there are templated values in the object
111
- *
112
- * @param obj - The Parsed Object to check to see if we have a template array type for
113
- * @param localKey - The key being checked
114
- */
115
- private hasTemplateValues(obj: any, localKey: string) {
116
- return (
117
- Object.hasOwnProperty.call(obj, "template") &&
118
- Array.isArray(obj?.template) &&
119
- obj.template.length &&
120
- obj.template.find((tmpl: any) => tmpl.output === localKey)
121
- );
122
- }
123
-
124
- private hasSwitchKey(localKey: string) {
125
- return localKey === ("staticSwitch" || "dynamicSwitch");
126
- }
127
-
128
90
  public parseObject(
129
91
  obj: object,
130
92
  type: Node.ChildrenTypes = NodeType.Value,
131
93
  options: ParseObjectOptions = { templateDepth: 0 },
132
94
  ): Node.Node | null {
133
- const nodeType = this.hooks.determineNodeType.call(obj);
134
-
135
- if (nodeType !== undefined) {
136
- const parsedNode = this.hooks.parseNode.call(
137
- obj,
138
- type,
139
- options,
140
- nodeType,
141
- );
142
- if (parsedNode) {
143
- return parsedNode;
144
- }
95
+ const parsedNode = this.hooks.parseNode.call(
96
+ obj,
97
+ type,
98
+ options,
99
+ ) as Node.Node | null;
100
+
101
+ if (parsedNode || parsedNode === null) {
102
+ return parsedNode;
145
103
  }
146
104
 
147
- /**
148
- *
149
- */
150
105
  const parseLocalObject = (
151
106
  currentValue: any,
152
107
  objToParse: unknown,
@@ -178,209 +133,39 @@ export class Parser {
178
133
  };
179
134
 
180
135
  const newValue = objEntries.reduce((accumulation, current): NestedObj => {
181
- const { children, ...rest } = accumulation;
136
+ let { value } = accumulation;
137
+ const { children } = accumulation;
182
138
  const [localKey, localValue] = current;
183
- if (localKey === "asset" && typeof localValue === "object") {
184
- const assetAST = this.parseObject(
185
- localValue,
186
- NodeType.Asset,
187
- options,
188
- );
189
-
190
- if (assetAST) {
191
- return {
192
- ...rest,
193
- children: [
194
- ...children,
195
- {
196
- path: [...path, "asset"],
197
- value: assetAST,
198
- },
199
- ],
200
- };
201
- }
202
- } else if (
203
- this.hooks.determineNodeType.call(localKey) === NodeType.Template &&
204
- Array.isArray(localValue)
205
- ) {
206
- const templateChildren = localValue
207
- .map((template: Template) => {
208
- const templateAST = this.hooks.onCreateASTNode.call(
209
- {
210
- type: NodeType.Template,
211
- depth: options.templateDepth ?? 0,
212
- data: template.data,
213
- template: template.value,
214
- dynamic: template.dynamic ?? false,
215
- },
216
- template,
217
- );
218
-
219
- if (templateAST?.type === NodeType.MultiNode) {
220
- templateAST.values.forEach((v) => {
221
- // eslint-disable-next-line no-param-reassign
222
- v.parent = templateAST;
223
- });
224
- }
225
-
226
- if (templateAST) {
227
- return {
228
- path: [...path, template.output],
229
- value: templateAST,
230
- };
231
- }
232
-
233
- // eslint-disable-next-line no-useless-return
234
- return;
235
- })
236
- .filter((element) => !!element);
237
-
238
- return {
239
- ...rest,
240
- children: [...children, ...templateChildren],
241
- } as NestedObj;
242
- } else if (
243
- (localValue &&
244
- this.hooks.determineNodeType.call(localValue) ===
245
- NodeType.Switch) ||
246
- this.hasSwitchKey(localKey)
247
- ) {
248
- const localSwitch = this.hooks.parseNode.call(
249
- this.hasSwitchKey(localKey)
250
- ? { [localKey]: localValue }
251
- : localValue,
252
- NodeType.Value,
253
- options,
254
- NodeType.Switch,
255
- );
256
139
 
257
- if (
258
- localSwitch &&
259
- localSwitch.type === NodeType.Value &&
260
- localSwitch.children?.length === 1 &&
261
- localSwitch.value === undefined
262
- ) {
263
- const firstChild = localSwitch.children[0];
264
-
265
- return {
266
- ...rest,
267
- children: [
268
- ...children,
269
- {
270
- path: [...path, localKey, ...firstChild.path],
271
- value: firstChild.value,
272
- },
273
- ],
274
- };
275
- }
276
-
277
- if (localSwitch) {
278
- return {
279
- ...rest,
280
- children: [
281
- ...children,
282
- {
283
- path: [...path, localKey],
284
- value: localSwitch,
285
- },
286
- ],
287
- };
288
- }
289
- } else if (localValue && hasAsync(localValue)) {
290
- const localAsync = this.parseAsync(
291
- localValue,
292
- NodeType.Value,
293
- options,
294
- );
295
- if (localAsync) {
296
- children.push({
297
- path: [...path, localKey],
298
- value: localAsync,
299
- });
300
- }
301
- } else if (localValue && Array.isArray(localValue)) {
302
- const childValues = localValue
303
- .map((childVal) =>
304
- this.parseObject(childVal, NodeType.Value, options),
305
- )
306
- .filter((child): child is Node.Node => !!child);
307
-
308
- if (childValues.length > 0) {
309
- const multiNode = this.hooks.onCreateASTNode.call(
310
- {
311
- type: NodeType.MultiNode,
312
- override: !this.hasTemplateValues(localObj, localKey),
313
- values: childValues,
314
- },
315
- localValue,
316
- );
317
-
318
- if (multiNode?.type === NodeType.MultiNode) {
319
- multiNode.values.forEach((v) => {
320
- // eslint-disable-next-line no-param-reassign
321
- v.parent = multiNode;
322
- });
323
- }
324
- if (multiNode) {
325
- return {
326
- ...rest,
327
- children: [
328
- ...children,
329
- {
330
- path: [...path, localKey],
331
- value: multiNode,
332
- },
333
- ],
334
- };
335
- }
336
- }
140
+ const newChildren = this.hooks.parseNode.call(
141
+ localValue,
142
+ NodeType.Value,
143
+ options,
144
+ {
145
+ path,
146
+ key: localKey,
147
+ parentObj: localObj,
148
+ },
149
+ ) as Node.Child[];
150
+
151
+ if (newChildren) {
152
+ children.push(...newChildren);
337
153
  } else if (localValue && typeof localValue === "object") {
338
- const determineNodeType =
339
- this.hooks.determineNodeType.call(localValue);
154
+ const result = parseLocalObject(accumulation.value, localValue, [
155
+ ...path,
156
+ localKey,
157
+ ]);
340
158
 
341
- if (determineNodeType === NodeType.Applicability) {
342
- const parsedNode = this.hooks.parseNode.call(
343
- localValue,
344
- NodeType.Value,
345
- options,
346
- determineNodeType,
347
- );
348
- if (parsedNode) {
349
- return {
350
- ...rest,
351
- children: [
352
- ...children,
353
- {
354
- path: [...path, localKey],
355
- value: parsedNode,
356
- },
357
- ],
358
- };
359
- }
360
- } else {
361
- const result = parseLocalObject(accumulation.value, localValue, [
362
- ...path,
363
- localKey,
364
- ]);
365
- return {
366
- value: result.value,
367
- children: [...children, ...result.children],
368
- };
369
- }
159
+ value = result.value;
160
+ children.push(...result.children);
370
161
  } else {
371
- const value = setIn(
372
- accumulation.value,
373
- [...path, localKey],
374
- localValue,
375
- );
376
-
377
- return {
378
- children,
379
- value,
380
- };
162
+ value = setIn(accumulation.value, [...path, localKey], localValue);
381
163
  }
382
164
 
383
- return accumulation;
165
+ return {
166
+ value,
167
+ children,
168
+ };
384
169
  }, defaultValue);
385
170
 
386
171
  return newValue;
@@ -389,18 +174,17 @@ export class Parser {
389
174
  const { value, children } = parseLocalObject(undefined, obj);
390
175
 
391
176
  const baseAst =
392
- value === undefined && children.length === 0
177
+ value === undefined && !children.length
393
178
  ? undefined
394
179
  : {
395
180
  type,
396
181
  value,
397
182
  };
398
183
 
399
- if (baseAst !== undefined && children.length > 0) {
400
- const parent = baseAst as Node.BaseWithChildren<any>;
184
+ if (baseAst && children.length) {
185
+ const parent: Node.BaseWithChildren<any> = baseAst;
401
186
  parent.children = children;
402
187
  children.forEach((child) => {
403
- // eslint-disable-next-line no-param-reassign
404
188
  child.value.parent = parent;
405
189
  });
406
190
  }
@@ -1,8 +1,28 @@
1
1
  import type { Node } from "./types";
2
2
 
3
- /** Check to see if the object contains async */
4
- export function hasAsync(obj: object): boolean {
5
- return Object.prototype.hasOwnProperty.call(obj, "async");
3
+ /**
4
+ * Checks if there are templated values in the object
5
+ *
6
+ * @param obj - The Parsed Object to check to see if we have a template array type for
7
+ * @param localKey - The key being checked
8
+ */
9
+ export function hasTemplateValues(obj: any, localKey: string) {
10
+ return (
11
+ Object.hasOwnProperty.call(obj, "template") &&
12
+ Array.isArray(obj?.template) &&
13
+ obj.template.length &&
14
+ obj.template.find((tmpl: any) => tmpl.output === localKey)
15
+ );
16
+ }
17
+
18
+ /** Check to see if the string is a valid switch key */
19
+ export function hasSwitchKey(localKey: string) {
20
+ return localKey === "staticSwitch" || localKey === "dynamicSwitch";
21
+ }
22
+
23
+ /** Check to see if the string is a valid template key */
24
+ export function hasTemplateKey(localKey: string) {
25
+ return localKey === "template";
6
26
  }
7
27
 
8
28
  /** Get the ID of the Node if there is one */