UNPKG

@visactor/vrender-core

Version:

```typescript import { xxx } from '@visactor/vrender-core'; ```

83 lines (79 loc) 4.3 kB
const FULL_DEFINITION_KEYS = new Set([ "name", "patch", "priority", "exclude", "suppress", "resolver", "declaredAffectedKeys" ]); function isPlainObject(value) { return null != value && "object" == typeof value && !Array.isArray(value); } function normalizePatch(value) { return null == value ? void 0 : value; } export class StateDefinitionCompiler { compile(definitions = {}) { const normalizedEntries = Object.keys(definitions).map((name => [ name, this.normalizeDefinition(name, definitions[name]) ])), rankOrder = normalizedEntries.map((([, definition]) => definition)).sort(((left, right) => { var _a, _b; const leftPriority = null !== (_a = left.priority) && void 0 !== _a ? _a : 0, rightPriority = null !== (_b = right.priority) && void 0 !== _b ? _b : 0; return leftPriority !== rightPriority ? leftPriority - rightPriority : left.name.localeCompare(right.name); })), rankMap = new Map; rankOrder.forEach(((definition, index) => { rankMap.set(definition.name, index); })); const compiled = new Map; return normalizedEntries.forEach((([, definition]) => { var _a, _b, _c, _d; const declaredAffectedKeys = definition.declaredAffectedKeys ? new Set(Array.from(definition.declaredAffectedKeys)) : void 0, staticAffectedKeys = definition.patch ? new Set(Object.keys(definition.patch)) : new Set, affectedKeys = new Set(staticAffectedKeys); null == declaredAffectedKeys || declaredAffectedKeys.forEach((key => affectedKeys.add(key))), compiled.set(definition.name, { name: definition.name, priority: null !== (_a = definition.priority) && void 0 !== _a ? _a : 0, rank: null !== (_b = rankMap.get(definition.name)) && void 0 !== _b ? _b : 0, patch: normalizePatch(definition.patch), resolver: definition.resolver, declaredAffectedKeys: declaredAffectedKeys, exclude: new Set(null !== (_c = definition.exclude) && void 0 !== _c ? _c : []), suppress: new Set(null !== (_d = definition.suppress) && void 0 !== _d ? _d : []), hasResolver: "function" == typeof definition.resolver, affectedKeys: affectedKeys }); })), this.expandRelationClosure(compiled, "exclude"), this.expandRelationClosure(compiled, "suppress"), compiled; } normalizeDefinition(name, value) { var _a, _b; if (null == value) return { name: name, priority: 0, patch: void 0 }; if ((isPlainObject(value) ? Object.keys(value) : []).some((key => FULL_DEFINITION_KEYS.has(key)))) { const definition = value; return { name: null !== (_a = definition.name) && void 0 !== _a ? _a : name, priority: null !== (_b = definition.priority) && void 0 !== _b ? _b : 0, patch: normalizePatch(definition.patch), resolver: definition.resolver, declaredAffectedKeys: definition.declaredAffectedKeys, exclude: definition.exclude, suppress: definition.suppress }; } return { name: name, priority: 0, patch: value }; } expandRelationClosure(compiled, relation) { const rawRelationMap = new Map; compiled.forEach(((definition, name) => { rawRelationMap.set(name, new Set(definition[relation])); })), compiled.forEach(((definition, origin) => { const closure = new Set, walk = (stateName, visiting) => { const nextStates = rawRelationMap.get(stateName); nextStates && nextStates.size && nextStates.forEach((nextState => { nextState === origin || visiting.has(nextState) || closure.has(nextState) || (closure.add(nextState), visiting.add(nextState), walk(nextState, visiting), visiting.delete(nextState)); })); }; walk(origin, new Set([ origin ])), definition[relation] = closure; })); } } //# sourceMappingURL=state-definition-compiler.js.map