@syncbridge/common 0.5.10 → 0.5.11

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/constants.js CHANGED
@@ -1,4 +1,4 @@
1
- export const version = '0.5.10';
1
+ export const version = '0.5.11';
2
2
  export const OWN_ELEMENT_METADATA = Symbol.for('OWN_ELEMENT_METADATA');
3
3
  export const COMPONENT_OPTIONS = Symbol.for('COMPONENT_OPTIONS');
4
4
  export const PROCESSOR_OPTIONS = Symbol.for('PROCESSOR_OPTIONS');
@@ -9,6 +9,7 @@ export declare namespace DefineVariable {
9
9
  variables?: TVariable | Promise<TVariable>;
10
10
  enumValues?: TEnumValue | Promise<TEnumValue>;
11
11
  choice?: string[] | Promise<string[]>;
12
+ index?: number;
12
13
  }
13
14
  }
14
15
  export {};
@@ -10,47 +10,49 @@ export function DefineVariable(options) {
10
10
  const designType = Reflect.getMetadata('design:type', target, propertyKey);
11
11
  if (designType === Array)
12
12
  options.isArray = true;
13
+ const base = Object.getPrototypeOf(target);
14
+ const baseMetadata = Reflect.hasMetadata(VARIABLE_CONTAINER, base)
15
+ ? deepClone(Reflect.getMetadata(VARIABLE_CONTAINER, base))
16
+ : {};
17
+ let metadata = merge(options, baseMetadata[propertyKey] || {}, {
18
+ deep: 'full',
19
+ ignoreNulls: false,
20
+ ignoreUndefined: false,
21
+ keepExisting: true,
22
+ });
13
23
  if (typeof options.variables === 'function') {
14
- options.type = VariableType.Nested;
15
- options.variables = Reflect.getMetadata(VARIABLE_CONTAINER, options.variables.prototype);
24
+ metadata.type = VariableType.Nested;
25
+ metadata.variables = Reflect.getMetadata(VARIABLE_CONTAINER, options.variables.prototype);
16
26
  }
17
- if (options.enumValues && !options.type) {
18
- options.type = VariableType.Enum;
27
+ if (metadata.enumValues && !metadata.type) {
28
+ metadata.type = VariableType.Enum;
19
29
  }
20
- if (options.type !== VariableType.Enum)
21
- delete options.enumValues;
22
- if (!options.type) {
30
+ if (metadata.type !== VariableType.Enum)
31
+ delete metadata.enumValues;
32
+ if (!metadata.type) {
23
33
  if (designType === String)
24
- options.type = VariableType.String;
34
+ metadata.type = VariableType.String;
25
35
  else if (designType === Boolean)
26
- options.type = VariableType.Boolean;
36
+ metadata.type = VariableType.Boolean;
27
37
  else if (designType === Number)
28
- options.type = VariableType.Number;
38
+ metadata.type = VariableType.Number;
29
39
  else if (typeof designType === 'function' &&
30
40
  Reflect.hasMetadata(VARIABLE_CONTAINER, designType.prototype)) {
31
- options.type = VariableType.Nested;
32
- options.variables = Reflect.getMetadata(VARIABLE_CONTAINER, designType.prototype);
41
+ metadata.type = VariableType.Nested;
42
+ metadata.variables = Reflect.getMetadata(VARIABLE_CONTAINER, designType.prototype);
33
43
  }
34
44
  }
35
- if (options.isArray && !options.type)
45
+ if (metadata.isArray && !metadata.type)
36
46
  throw new TypeError('Array variables must have type defined. ' +
37
47
  (target.constructor.name +
38
48
  '.' +
39
49
  propertyKey +
40
50
  ' is an array variable'));
41
- const base = Object.getPrototypeOf(target);
42
- const baseMetadata = Reflect.hasMetadata(VARIABLE_CONTAINER, base)
43
- ? deepClone(Reflect.getMetadata(VARIABLE_CONTAINER, base))
44
- : {};
45
- let metadata = merge(baseMetadata, options, {
46
- deep: 'full',
47
- ignoreNulls: false,
48
- ignoreUndefined: false,
49
- });
50
51
  metadata = omitUndefined(metadata, 'full');
51
52
  const metadataRecord = Reflect.hasOwnMetadata(VARIABLE_CONTAINER, target)
52
53
  ? Reflect.getMetadata(VARIABLE_CONTAINER, target)
53
54
  : {};
55
+ merge(metadataRecord, baseMetadata);
54
56
  metadataRecord[propertyKey] = metadata;
55
57
  Reflect.defineMetadata(VARIABLE_CONTAINER, metadataRecord, target);
56
58
  };
@@ -11,6 +11,7 @@ export declare class VariableMetadataRecord {
11
11
  */
12
12
  export declare class VariableMetadata {
13
13
  ignored?: boolean;
14
+ index?: number;
14
15
  hotPlug?: boolean;
15
16
  label?: string;
16
17
  description?: string;
@@ -21,6 +21,12 @@ let VariableMetadata = class VariableMetadata {
21
21
  ignored;
22
22
  hotPlug;
23
23
  };
24
+ __decorate([
25
+ ApiField({
26
+ description: 'Index of the variable. Variables with higher index will be displayed first',
27
+ }),
28
+ __metadata("design:type", Number)
29
+ ], VariableMetadata.prototype, "index", void 0);
24
30
  __decorate([
25
31
  ApiField({
26
32
  description: 'Determine if the variable can be changed while worker running',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncbridge/common",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
4
4
  "description": "SyncBridge Common utilities",
5
5
  "author": "Panates Inc",
6
6
  "license": "MIT",
@@ -1,11 +1,12 @@
1
1
  import { deepClone, merge } from '@jsopen/objects';
2
- import { ComponentMetadata, ProcessorMetadata } from '../models/index.js';
2
+ import { ComponentMetadata, ProcessorMetadata, } from '../models/index.js';
3
3
  import { initializeModelsDocument } from '../models-document.js';
4
4
  import { resolvePromisesDeep } from '../utils/resolve-promises.js';
5
5
  export async function normalizeProcessorMetadata(metadata) {
6
6
  const decoder = await getProcMetadataTypeDecoder();
7
7
  let out = await resolvePromisesDeep(metadata);
8
- out = deepClone({
8
+ out = deepClone(out);
9
+ out = decoder({
9
10
  className: out.className,
10
11
  displayName: out.displayName,
11
12
  description: out.description,
@@ -13,35 +14,18 @@ export async function normalizeProcessorMetadata(metadata) {
13
14
  author: out.author,
14
15
  tags: out.tags,
15
16
  abstract: out.abstract,
16
- variables: out.variables,
17
- components: out.components,
17
+ variables: normalizeVariables(out.variables),
18
+ components: normalizeComponents(out.components),
18
19
  ...out,
19
20
  });
20
- if (out.variables) {
21
- /** Convert object enumValues to arrays */
22
- for (const v of Object.values(out.variables)) {
23
- if (v.enumValues &&
24
- typeof v.enumValues === 'object' &&
25
- !Array.isArray(v.enumValues)) {
26
- let values = v.enumValues;
27
- const keys = Object.keys(values).filter(k => !/^\d+$/.test(k));
28
- values = keys.reduce((a, k) => {
29
- if (values[k] != null)
30
- a.push(values[k]);
31
- return a;
32
- }, []);
33
- v.enumValues = values;
34
- }
35
- }
36
- }
37
- out = decoder(out);
38
21
  merge(out, metadata, { keepExisting: true, deep: 'full' });
39
22
  return out;
40
23
  }
41
24
  export async function normalizeComponentMetadata(metadata) {
42
25
  const decoder = await getCompMetadataTypeDecoder();
43
26
  let out = await resolvePromisesDeep(metadata);
44
- out = deepClone({
27
+ out = deepClone(out);
28
+ out = decoder({
45
29
  className: out.className,
46
30
  displayName: out.displayName,
47
31
  description: out.description,
@@ -50,32 +34,44 @@ export async function normalizeComponentMetadata(metadata) {
50
34
  interfaces: out.interfaces,
51
35
  tags: out.tags,
52
36
  abstract: out.abstract,
53
- variables: out.variables,
54
- components: out.components,
37
+ variables: normalizeVariables(out.variables),
38
+ components: normalizeComponents(out.components),
55
39
  ...out,
56
40
  });
57
- if (out.variables) {
58
- /** Convert object enumValues to arrays */
59
- for (const v of Object.values(out.variables)) {
60
- if (v.enumValues &&
61
- typeof v.enumValues === 'object' &&
62
- !Array.isArray(v.enumValues)) {
63
- let values = v.enumValues;
64
- const keys = Object.keys(values).filter(k => !/^\d+$/.test(k));
65
- values = keys.reduce((a, k) => {
66
- if (values[k] != null)
67
- a.push(values[k]);
68
- return a;
69
- // v => !(typeof v === 'number' && !keys.includes(String(v))),
70
- }, []);
71
- v.enumValues = values;
72
- }
73
- }
74
- }
75
- out = decoder(out);
76
41
  merge(out, metadata, { keepExisting: true, deep: 'full' });
77
42
  return out;
78
43
  }
44
+ function normalizeComponents(components) {
45
+ if (!components)
46
+ return;
47
+ for (const component of Object.values(components)) {
48
+ if (component.variables)
49
+ component.variables = normalizeVariables(component.variables);
50
+ if (component.components)
51
+ component.components = normalizeComponents(component.components);
52
+ }
53
+ return components;
54
+ }
55
+ function normalizeVariables(variables) {
56
+ if (!variables)
57
+ return;
58
+ /** Convert object enumValues to arrays */
59
+ for (const v of Object.values(variables)) {
60
+ if (v.enumValues &&
61
+ typeof v.enumValues === 'object' &&
62
+ !Array.isArray(v.enumValues)) {
63
+ let values = v.enumValues;
64
+ const keys = Object.keys(values).filter(k => !/^\d+$/.test(k));
65
+ values = keys.reduce((a, k) => {
66
+ if (values[k] != null)
67
+ a.push(values[k]);
68
+ return a;
69
+ }, []);
70
+ v.enumValues = values;
71
+ }
72
+ }
73
+ return variables;
74
+ }
79
75
  // ************************************************************************
80
76
  // ************************************************************************
81
77
  let _compMetadataTypeDecoder;
@@ -1,6 +1,12 @@
1
1
  import { StackExecutor } from '../classes/stack-executor.js';
2
2
  import { ProcessorMetadata, Profile } from '../models/index.js';
3
+ /**
4
+ * Materializes metadata from profile.
5
+ */
3
6
  export declare function materializeMetadata(profile: Profile): ProcessorMetadata;
7
+ /**
8
+ *
9
+ */
4
10
  export declare function materializeMetadataSilent(profile: Profile): {
5
11
  metadata: ProcessorMetadata;
6
12
  issues?: StackExecutor.Issue[];
@@ -1,7 +1,11 @@
1
1
  import { clone, merge, omitUndefined } from '@jsopen/objects';
2
2
  import { SbError } from '../classes/sb-error.js';
3
3
  import { StackExecutor } from '../classes/stack-executor.js';
4
+ import { VariableType, } from '../models/index.js';
4
5
  import { ExtensionRegistry } from '../registry/extension-registry.js';
6
+ /**
7
+ * Materializes metadata from profile.
8
+ */
5
9
  export function materializeMetadata(profile) {
6
10
  const { metadata, issues } = materializeMetadataSilent(profile);
7
11
  if (issues?.length) {
@@ -16,6 +20,9 @@ export function materializeMetadata(profile) {
16
20
  }
17
21
  return metadata;
18
22
  }
23
+ /**
24
+ *
25
+ */
19
26
  export function materializeMetadataSilent(profile) {
20
27
  const stackExecutor = new StackExecutor();
21
28
  const processorRec = ExtensionRegistry.getProcessor(profile.processorClass);
@@ -69,6 +76,7 @@ function _materializeComponents(stackExecutor, metadata, profile) {
69
76
  const reg = ExtensionRegistry.getComponent(profileComponent.className);
70
77
  materializedComponent.className = profileComponent.className;
71
78
  materializedComponent.variables = merge({}, [reg.metadata.variables || {}, metadataComponent.variables || {}], { deep: 'full' });
79
+ materializedComponent.variables = sortVariables(materializedComponent.variables);
72
80
  if (metadataComponent.className) {
73
81
  if (materializedComponent.className &&
74
82
  materializedComponent.className !== metadataComponent.className)
@@ -97,3 +105,29 @@ function _materializeComponents(stackExecutor, metadata, profile) {
97
105
  }
98
106
  return out;
99
107
  }
108
+ function sortVariables(variables) {
109
+ if (!variables)
110
+ return;
111
+ const keys = Object.keys(variables);
112
+ const sorted = [...keys]
113
+ .sort((a, b) => {
114
+ const va = variables[a];
115
+ const vb = variables[b];
116
+ const indexA = va.index ?? Number.MAX_SAFE_INTEGER;
117
+ const indexB = vb.index ?? Number.MAX_SAFE_INTEGER;
118
+ if (indexA > indexB)
119
+ return 1;
120
+ if (indexA < indexB)
121
+ return -1;
122
+ if (va.type === VariableType.Nested && vb.type !== VariableType.Nested)
123
+ return 1;
124
+ if (vb.type === VariableType.Nested && va.type !== VariableType.Nested)
125
+ return -1;
126
+ return keys.indexOf(a) > keys.indexOf(b) ? 1 : -1;
127
+ })
128
+ .reduce((a, k) => {
129
+ a[k] = variables[k];
130
+ return a;
131
+ }, {});
132
+ return sorted;
133
+ }