@teambit/semantics.entities.semantic-schema 0.0.81 → 0.0.83

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.
@@ -1,9 +1,17 @@
1
1
  import { SchemaNodeConstructor } from './schema-node-constructor';
2
+ type GetSchemaFunc = () => SchemaNodeConstructor[];
2
3
  export declare class SchemaRegistry {
3
4
  private static instance;
4
5
  private _schemas;
6
+ private _getSchemas;
5
7
  private constructor();
8
+ static registerGetSchemas(getSchemas: GetSchemaFunc): void;
9
+ /**
10
+ * @deprecated use registerGetSchemas instead
11
+ * registerGetSchemas is better for performance as it lazy-loads the schemas.
12
+ */
6
13
  static register(schema: SchemaNodeConstructor): void;
7
14
  static schemas(): Record<string, SchemaNodeConstructor>;
8
15
  static fromObject(obj: Record<string, any>): any;
9
16
  }
17
+ export {};
@@ -13,21 +13,40 @@ function _unknownSchemaFactory() {
13
13
  }
14
14
  class SchemaRegistry {
15
15
  static instance = new SchemaRegistry();
16
+ _getSchemas = [];
16
17
  constructor() {
17
18
  this._schemas = {};
18
19
  }
20
+ static registerGetSchemas(getSchemas) {
21
+ this.instance._getSchemas.push(getSchemas);
22
+ }
23
+
24
+ /**
25
+ * @deprecated use registerGetSchemas instead
26
+ * registerGetSchemas is better for performance as it lazy-loads the schemas.
27
+ */
19
28
  static register(schema) {
20
29
  if (this.instance._schemas[schema.name]) return;
21
30
  this.instance._schemas[schema.name] = schema;
22
31
  }
23
32
  static schemas() {
33
+ // this happens only once. as soon as the function is called, it registers all the schemas.
34
+ this.instance._getSchemas.forEach(getSchemas => {
35
+ const schemas = getSchemas();
36
+ schemas.forEach(schema => {
37
+ if (this.instance._schemas[schema.name]) return;
38
+ this.instance._schemas[schema.name] = schema;
39
+ });
40
+ });
41
+ this.instance._getSchemas = [];
24
42
  return this.instance._schemas;
25
43
  }
26
44
  static fromObject(obj) {
27
45
  if (!obj.__schema) {
28
46
  throw new Error(`fatal: "__schema" is missing, make sure the transformer is set on SchemaNode type`);
29
47
  }
30
- const SchemaClass = this.instance._schemas[obj.__schema];
48
+ const schemas = this.schemas();
49
+ const SchemaClass = schemas[obj.__schema];
31
50
  if (!SchemaClass) {
32
51
  // for backward and forward compatibility, to not break the users, it's better to return an unknown schema than throwing.
33
52
  return _unknownSchemaFactory().UnknownSchemaFactory.create(obj.location || {
@@ -1 +1 @@
1
- {"version":3,"names":["_unknownSchemaFactory","data","require","SchemaRegistry","instance","constructor","_schemas","register","schema","name","schemas","fromObject","obj","__schema","Error","SchemaClass","UnknownSchemaFactory","create","location","path","line","character","exports"],"sources":["schema-registry.ts"],"sourcesContent":["import { SchemaNodeConstructor } from './schema-node-constructor';\nimport { UnknownSchemaFactory } from './unknown-schema-factory';\n\nexport class SchemaRegistry {\n private static instance = new SchemaRegistry();\n private _schemas: Record<string, SchemaNodeConstructor>;\n\n private constructor() {\n this._schemas = {};\n }\n\n static register(schema: SchemaNodeConstructor) {\n if (this.instance._schemas[schema.name]) return;\n this.instance._schemas[schema.name] = schema;\n }\n\n static schemas() {\n return this.instance._schemas;\n }\n\n static fromObject(obj: Record<string, any>) {\n if (!obj.__schema) {\n throw new Error(`fatal: \"__schema\" is missing, make sure the transformer is set on SchemaNode type`);\n }\n\n const SchemaClass = this.instance._schemas[obj.__schema];\n if (!SchemaClass) {\n // for backward and forward compatibility, to not break the users, it's better to return an unknown schema than throwing.\n return UnknownSchemaFactory.create(obj.location || { path: '', line: 0, character: 0 }, obj.__schema, obj);\n }\n return SchemaClass.fromObject(obj);\n }\n}\n"],"mappings":";;;;;;AACA,SAAAA,sBAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,qBAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAEO,MAAME,cAAc,CAAC;EAC1B,OAAeC,QAAQ,GAAG,IAAID,cAAc,CAAC,CAAC;EAGtCE,WAAWA,CAAA,EAAG;IACpB,IAAI,CAACC,QAAQ,GAAG,CAAC,CAAC;EACpB;EAEA,OAAOC,QAAQA,CAACC,MAA6B,EAAE;IAC7C,IAAI,IAAI,CAACJ,QAAQ,CAACE,QAAQ,CAACE,MAAM,CAACC,IAAI,CAAC,EAAE;IACzC,IAAI,CAACL,QAAQ,CAACE,QAAQ,CAACE,MAAM,CAACC,IAAI,CAAC,GAAGD,MAAM;EAC9C;EAEA,OAAOE,OAAOA,CAAA,EAAG;IACf,OAAO,IAAI,CAACN,QAAQ,CAACE,QAAQ;EAC/B;EAEA,OAAOK,UAAUA,CAACC,GAAwB,EAAE;IAC1C,IAAI,CAACA,GAAG,CAACC,QAAQ,EAAE;MACjB,MAAM,IAAIC,KAAK,CAAC,mFAAmF,CAAC;IACtG;IAEA,MAAMC,WAAW,GAAG,IAAI,CAACX,QAAQ,CAACE,QAAQ,CAACM,GAAG,CAACC,QAAQ,CAAC;IACxD,IAAI,CAACE,WAAW,EAAE;MAChB;MACA,OAAOC,4CAAoB,CAACC,MAAM,CAACL,GAAG,CAACM,QAAQ,IAAI;QAAEC,IAAI,EAAE,EAAE;QAAEC,IAAI,EAAE,CAAC;QAAEC,SAAS,EAAE;MAAE,CAAC,EAAET,GAAG,CAACC,QAAQ,EAAED,GAAG,CAAC;IAC5G;IACA,OAAOG,WAAW,CAACJ,UAAU,CAACC,GAAG,CAAC;EACpC;AACF;AAACU,OAAA,CAAAnB,cAAA,GAAAA,cAAA","ignoreList":[]}
1
+ {"version":3,"names":["_unknownSchemaFactory","data","require","SchemaRegistry","instance","_getSchemas","constructor","_schemas","registerGetSchemas","getSchemas","push","register","schema","name","schemas","forEach","fromObject","obj","__schema","Error","SchemaClass","UnknownSchemaFactory","create","location","path","line","character","exports"],"sources":["schema-registry.ts"],"sourcesContent":["import { SchemaNodeConstructor } from './schema-node-constructor';\nimport { UnknownSchemaFactory } from './unknown-schema-factory';\n\ntype GetSchemaFunc = () => SchemaNodeConstructor[];\n\nexport class SchemaRegistry {\n private static instance = new SchemaRegistry();\n private _schemas: Record<string, SchemaNodeConstructor>;\n private _getSchemas: GetSchemaFunc[] = [];\n\n private constructor() {\n this._schemas = {};\n }\n\n static registerGetSchemas(getSchemas: GetSchemaFunc) {\n this.instance._getSchemas.push(getSchemas);\n }\n\n /**\n * @deprecated use registerGetSchemas instead\n * registerGetSchemas is better for performance as it lazy-loads the schemas.\n */\n static register(schema: SchemaNodeConstructor) {\n if (this.instance._schemas[schema.name]) return;\n this.instance._schemas[schema.name] = schema;\n }\n\n static schemas() {\n // this happens only once. as soon as the function is called, it registers all the schemas.\n this.instance._getSchemas.forEach((getSchemas) => {\n const schemas = getSchemas();\n schemas.forEach((schema) => {\n if (this.instance._schemas[schema.name]) return;\n this.instance._schemas[schema.name] = schema;\n });\n });\n this.instance._getSchemas = [];\n return this.instance._schemas;\n }\n\n static fromObject(obj: Record<string, any>) {\n if (!obj.__schema) {\n throw new Error(`fatal: \"__schema\" is missing, make sure the transformer is set on SchemaNode type`);\n }\n const schemas = this.schemas();\n const SchemaClass = schemas[obj.__schema];\n if (!SchemaClass) {\n // for backward and forward compatibility, to not break the users, it's better to return an unknown schema than throwing.\n return UnknownSchemaFactory.create(obj.location || { path: '', line: 0, character: 0 }, obj.__schema, obj);\n }\n return SchemaClass.fromObject(obj);\n }\n}\n"],"mappings":";;;;;;AACA,SAAAA,sBAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,qBAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAIO,MAAME,cAAc,CAAC;EAC1B,OAAeC,QAAQ,GAAG,IAAID,cAAc,CAAC,CAAC;EAEtCE,WAAW,GAAoB,EAAE;EAEjCC,WAAWA,CAAA,EAAG;IACpB,IAAI,CAACC,QAAQ,GAAG,CAAC,CAAC;EACpB;EAEA,OAAOC,kBAAkBA,CAACC,UAAyB,EAAE;IACnD,IAAI,CAACL,QAAQ,CAACC,WAAW,CAACK,IAAI,CAACD,UAAU,CAAC;EAC5C;;EAEA;AACF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U,CAACC,GAAG,CAAC;EACpC;AACF;AAACU,OAAA,CAAAxB,cAAA,GAAAA,cAAA","ignoreList":[]}
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@teambit/semantics.entities.semantic-schema",
3
- "version": "0.0.81",
3
+ "version": "0.0.83",
4
4
  "homepage": "https://bit.cloud/teambit/semantics/entities/semantic-schema",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.semantics",
8
8
  "name": "entities/semantic-schema",
9
- "version": "0.0.81"
9
+ "version": "0.0.83"
10
10
  },
11
11
  "dependencies": {
12
- "chalk": "2.4.2",
12
+ "chalk": "4.1.2",
13
13
  "lodash": "4.17.21",
14
- "@teambit/component-id": "1.2.2"
14
+ "@teambit/component-id": "1.2.3"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@types/lodash": "4.14.165",
@@ -1,20 +1,40 @@
1
1
  import { SchemaNodeConstructor } from './schema-node-constructor';
2
2
  import { UnknownSchemaFactory } from './unknown-schema-factory';
3
3
 
4
+ type GetSchemaFunc = () => SchemaNodeConstructor[];
5
+
4
6
  export class SchemaRegistry {
5
7
  private static instance = new SchemaRegistry();
6
8
  private _schemas: Record<string, SchemaNodeConstructor>;
9
+ private _getSchemas: GetSchemaFunc[] = [];
7
10
 
8
11
  private constructor() {
9
12
  this._schemas = {};
10
13
  }
11
14
 
15
+ static registerGetSchemas(getSchemas: GetSchemaFunc) {
16
+ this.instance._getSchemas.push(getSchemas);
17
+ }
18
+
19
+ /**
20
+ * @deprecated use registerGetSchemas instead
21
+ * registerGetSchemas is better for performance as it lazy-loads the schemas.
22
+ */
12
23
  static register(schema: SchemaNodeConstructor) {
13
24
  if (this.instance._schemas[schema.name]) return;
14
25
  this.instance._schemas[schema.name] = schema;
15
26
  }
16
27
 
17
28
  static schemas() {
29
+ // this happens only once. as soon as the function is called, it registers all the schemas.
30
+ this.instance._getSchemas.forEach((getSchemas) => {
31
+ const schemas = getSchemas();
32
+ schemas.forEach((schema) => {
33
+ if (this.instance._schemas[schema.name]) return;
34
+ this.instance._schemas[schema.name] = schema;
35
+ });
36
+ });
37
+ this.instance._getSchemas = [];
18
38
  return this.instance._schemas;
19
39
  }
20
40
 
@@ -22,8 +42,8 @@ export class SchemaRegistry {
22
42
  if (!obj.__schema) {
23
43
  throw new Error(`fatal: "__schema" is missing, make sure the transformer is set on SchemaNode type`);
24
44
  }
25
-
26
- const SchemaClass = this.instance._schemas[obj.__schema];
45
+ const schemas = this.schemas();
46
+ const SchemaClass = schemas[obj.__schema];
27
47
  if (!SchemaClass) {
28
48
  // for backward and forward compatibility, to not break the users, it's better to return an unknown schema than throwing.
29
49
  return UnknownSchemaFactory.create(obj.location || { path: '', line: 0, character: 0 }, obj.__schema, obj);