@player-ui/computed-properties-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.
- package/dist/index.cjs.js +54 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.esm.js +50 -0
- package/package.json +19 -0
- package/src/index.ts +81 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
class ComputedPropertiesPlugin {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.name = "ComputedProperties";
|
|
8
|
+
}
|
|
9
|
+
apply(player) {
|
|
10
|
+
let schemaController;
|
|
11
|
+
let expressionEvaluator;
|
|
12
|
+
const getExpressionType = (binding) => {
|
|
13
|
+
const dataType = schemaController == null ? void 0 : schemaController.getType(binding);
|
|
14
|
+
if ((dataType == null ? void 0 : dataType.type) === "Expression") {
|
|
15
|
+
return dataType;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const computedPropertyMiddleware = {
|
|
19
|
+
name: this.name,
|
|
20
|
+
get(binding, options, next) {
|
|
21
|
+
const expType = getExpressionType(binding);
|
|
22
|
+
if (expType) {
|
|
23
|
+
const { exp } = expType;
|
|
24
|
+
const result = exp && (expressionEvaluator == null ? void 0 : expressionEvaluator.evaluate(exp, options == null ? void 0 : options.context));
|
|
25
|
+
return result != null ? result : expType.default;
|
|
26
|
+
}
|
|
27
|
+
return next == null ? void 0 : next.get(binding, options);
|
|
28
|
+
},
|
|
29
|
+
set(transaction, options, next) {
|
|
30
|
+
var _a;
|
|
31
|
+
for (const setOperation of transaction) {
|
|
32
|
+
if (getExpressionType(setOperation[0])) {
|
|
33
|
+
throw new Error(`Invalid 'set' operation on computed property: ${setOperation[0].asString()}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return (_a = next == null ? void 0 : next.set(transaction, options)) != null ? _a : [];
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
player.hooks.dataController.tap(this.name, (dataController) => {
|
|
40
|
+
dataController.hooks.resolveDataStages.tap(this.name, (dataPipeline) => {
|
|
41
|
+
return [...dataPipeline, computedPropertyMiddleware];
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
player.hooks.schema.tap(this.name, (schema) => {
|
|
45
|
+
schemaController = schema;
|
|
46
|
+
});
|
|
47
|
+
player.hooks.expressionEvaluator.tap(this.name, (evaluator) => {
|
|
48
|
+
expressionEvaluator = evaluator;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.ComputedPropertiesPlugin = ComputedPropertiesPlugin;
|
|
54
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PlayerPlugin, Player } from '@player-ui/player';
|
|
2
|
+
import { Schema, Expression } from '@player-ui/types';
|
|
3
|
+
|
|
4
|
+
interface ExpressionDataType extends Schema.DataType<'Expression'> {
|
|
5
|
+
/** The expression to evaluate to compute the value of this node */
|
|
6
|
+
exp?: Expression;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A player plugin to manage beacon events.
|
|
10
|
+
* It automatically keeps track of the current user's view, and adds additional metaData to each beacon event.
|
|
11
|
+
*/
|
|
12
|
+
declare class ComputedPropertiesPlugin implements PlayerPlugin {
|
|
13
|
+
name: string;
|
|
14
|
+
apply(player: Player): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { ComputedPropertiesPlugin, ExpressionDataType };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
class ComputedPropertiesPlugin {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.name = "ComputedProperties";
|
|
4
|
+
}
|
|
5
|
+
apply(player) {
|
|
6
|
+
let schemaController;
|
|
7
|
+
let expressionEvaluator;
|
|
8
|
+
const getExpressionType = (binding) => {
|
|
9
|
+
const dataType = schemaController == null ? void 0 : schemaController.getType(binding);
|
|
10
|
+
if ((dataType == null ? void 0 : dataType.type) === "Expression") {
|
|
11
|
+
return dataType;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const computedPropertyMiddleware = {
|
|
15
|
+
name: this.name,
|
|
16
|
+
get(binding, options, next) {
|
|
17
|
+
const expType = getExpressionType(binding);
|
|
18
|
+
if (expType) {
|
|
19
|
+
const { exp } = expType;
|
|
20
|
+
const result = exp && (expressionEvaluator == null ? void 0 : expressionEvaluator.evaluate(exp, options == null ? void 0 : options.context));
|
|
21
|
+
return result != null ? result : expType.default;
|
|
22
|
+
}
|
|
23
|
+
return next == null ? void 0 : next.get(binding, options);
|
|
24
|
+
},
|
|
25
|
+
set(transaction, options, next) {
|
|
26
|
+
var _a;
|
|
27
|
+
for (const setOperation of transaction) {
|
|
28
|
+
if (getExpressionType(setOperation[0])) {
|
|
29
|
+
throw new Error(`Invalid 'set' operation on computed property: ${setOperation[0].asString()}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return (_a = next == null ? void 0 : next.set(transaction, options)) != null ? _a : [];
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
player.hooks.dataController.tap(this.name, (dataController) => {
|
|
36
|
+
dataController.hooks.resolveDataStages.tap(this.name, (dataPipeline) => {
|
|
37
|
+
return [...dataPipeline, computedPropertyMiddleware];
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
player.hooks.schema.tap(this.name, (schema) => {
|
|
41
|
+
schemaController = schema;
|
|
42
|
+
});
|
|
43
|
+
player.hooks.expressionEvaluator.tap(this.name, (evaluator) => {
|
|
44
|
+
expressionEvaluator = evaluator;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { ComputedPropertiesPlugin };
|
|
50
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-ui/computed-properties-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
|
+
"@babel/runtime": "7.15.4"
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.cjs.js",
|
|
17
|
+
"module": "dist/index.esm.js",
|
|
18
|
+
"typings": "dist/index.d.ts"
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Player, PlayerPlugin } from '@player-ui/player';
|
|
2
|
+
import type { ExpressionEvaluator } from '@player-ui/expressions';
|
|
3
|
+
import type { Schema, Expression } from '@player-ui/types';
|
|
4
|
+
import type { DataModelMiddleware, Updates } from '@player-ui/data';
|
|
5
|
+
import type { SchemaController } from '@player-ui/schema';
|
|
6
|
+
import type { BindingInstance } from '@player-ui/binding';
|
|
7
|
+
|
|
8
|
+
export interface ExpressionDataType extends Schema.DataType<'Expression'> {
|
|
9
|
+
/** The expression to evaluate to compute the value of this node */
|
|
10
|
+
exp?: Expression;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A player plugin to manage beacon events.
|
|
15
|
+
* It automatically keeps track of the current user's view, and adds additional metaData to each beacon event.
|
|
16
|
+
*/
|
|
17
|
+
export class ComputedPropertiesPlugin implements PlayerPlugin {
|
|
18
|
+
name = 'ComputedProperties';
|
|
19
|
+
|
|
20
|
+
apply(player: Player) {
|
|
21
|
+
// Inject a middleware into the data-model to intercept any calls to an expression type in the schema
|
|
22
|
+
// Calls to `set` throw an error. Calls to `get` compute the value and return if or default
|
|
23
|
+
|
|
24
|
+
let schemaController: SchemaController | undefined;
|
|
25
|
+
let expressionEvaluator: ExpressionEvaluator | undefined;
|
|
26
|
+
|
|
27
|
+
/** Look up the data-type of the binding, and check for an Expression type */
|
|
28
|
+
const getExpressionType = (
|
|
29
|
+
binding: BindingInstance
|
|
30
|
+
): ExpressionDataType | undefined => {
|
|
31
|
+
// Check to see if the data-type of the binding is an `Expression`
|
|
32
|
+
const dataType = schemaController?.getType(binding);
|
|
33
|
+
|
|
34
|
+
if (dataType?.type === 'Expression') {
|
|
35
|
+
return dataType as ExpressionDataType;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const computedPropertyMiddleware: DataModelMiddleware = {
|
|
40
|
+
name: this.name,
|
|
41
|
+
get(binding, options, next) {
|
|
42
|
+
const expType = getExpressionType(binding);
|
|
43
|
+
|
|
44
|
+
if (expType) {
|
|
45
|
+
const { exp } = expType;
|
|
46
|
+
const result =
|
|
47
|
+
exp && expressionEvaluator?.evaluate(exp, options?.context);
|
|
48
|
+
|
|
49
|
+
return result ?? expType.default;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return next?.get(binding, options);
|
|
53
|
+
},
|
|
54
|
+
set(transaction, options, next): Updates {
|
|
55
|
+
for (const setOperation of transaction) {
|
|
56
|
+
if (getExpressionType(setOperation[0])) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Invalid 'set' operation on computed property: ${setOperation[0].asString()}`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return next?.set(transaction, options) ?? [];
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
player.hooks.dataController.tap(this.name, (dataController) => {
|
|
68
|
+
dataController.hooks.resolveDataStages.tap(this.name, (dataPipeline) => {
|
|
69
|
+
return [...dataPipeline, computedPropertyMiddleware];
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
player.hooks.schema.tap(this.name, (schema) => {
|
|
74
|
+
schemaController = schema;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
player.hooks.expressionEvaluator.tap(this.name, (evaluator) => {
|
|
78
|
+
expressionEvaluator = evaluator;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|