@salesforcedevs/docs-components 1.3.300-amf-fix-alpha → 1.3.300-async-fix-alpha2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/docs-components",
3
- "version": "1.3.300-amf-fix-alpha",
3
+ "version": "1.3.300-async-fix-alpha2",
4
4
  "description": "Docs Lightning web components for DSC",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -17,7 +17,8 @@
17
17
  "lodash.orderby": "^4.6.0",
18
18
  "lodash.uniqby": "^4.7.0",
19
19
  "query-string": "^7.1.3",
20
- "sentence-case": "^3.0.4"
20
+ "sentence-case": "^3.0.4",
21
+ "yieldable-json": "^2.0.1"
21
22
  },
22
23
  "devDependencies": {
23
24
  "@types/classnames": "^2.2.10",
@@ -9,12 +9,13 @@ import {
9
9
  } from "./utils";
10
10
  import type { TopicModel } from "./types";
11
11
  import { Json } from "typings/custom";
12
+
12
13
  const TABLE_SIZE_MATCH = "769px";
13
14
 
14
15
  export default class AmfTopic extends LightningElement {
15
16
  private _model: TopicModel | undefined;
16
- private amf: Json;
17
- private type: any;
17
+ private amf: Json | undefined;
18
+ private type: string | undefined;
18
19
 
19
20
  @api
20
21
  get model(): TopicModel | undefined {
@@ -22,70 +23,28 @@ export default class AmfTopic extends LightningElement {
22
23
  }
23
24
 
24
25
  set model(value: TopicModel) {
25
- if (
26
+ const amfClonePromise =
26
27
  !this.amf ||
27
28
  (value && this._model && value.amf !== this._model?.amf)
28
- ) {
29
- this.amf = value && this.clone(value.amf);
30
- }
31
- if (
29
+ ? clone(value.amf)
30
+ : Promise.resolve(this.amf);
31
+
32
+ const typeClonePromise =
32
33
  !this.type ||
33
34
  (value && this._model && value.type !== this._model?.type)
34
- ) {
35
- this.type = value && this.getTypeCloned(value.type);
36
- }
37
-
38
- this._model = value;
39
- if (this._model) {
40
- this.update();
41
- }
42
- // else { Remove child? No model, seems like no component should be shown. }
43
- }
44
-
45
- cloneWithWorker(value) {
46
- return new Promise((resolve, reject) => {
47
- const worker = new Worker("./cloneAmfObject.ts");
48
-
49
- worker.onmessage = (event) => {
50
- resolve(event.data);
51
- worker.terminate();
52
- };
53
-
54
- worker.onerror = (error) => {
55
- reject(error);
56
- worker.terminate();
57
- };
58
-
59
- worker.postMessage(value);
60
- });
61
- }
62
-
63
- /**
64
- * The underlying web components we use from api-console mutate their models we pass in.
65
- * Since LWC makes them Read Only, we need to copy them before passing to the Web Component.
66
- * @param value JSON Serializable object to clone.
67
- * @returns A copy of Value. One that has been serialized and parsed via JSON. (Functions, Regex, etc are not preserved.)
68
- */
69
- // function clone(value: any): any {
70
- // return JSON.parse(JSON.stringify(value));
71
- // }
72
-
73
- async clone(value: any) {
74
- try {
75
- const clonedObject = await this.cloneWithWorker(value);
76
- return clonedObject;
77
- } catch (error) {
78
- console.error("Error cloning value:", error);
79
- return null;
80
- }
81
- }
82
-
83
- async getTypeCloned(value: any) {
84
- const typeObj = await this.clone(value.type);
85
- if (typeObj) {
86
- return typeObj;
87
- }
88
- return "";
35
+ ? clone(value.type)
36
+ : Promise.resolve(this.type);
37
+
38
+ Promise.all([amfClonePromise, typeClonePromise])
39
+ .then(([clonedAmf, clonedType]) => {
40
+ this.amf = clonedAmf;
41
+ this.type = clonedType;
42
+ this._model = value;
43
+ this.update();
44
+ })
45
+ .catch((error) => {
46
+ console.error("Error cloning model:", error);
47
+ });
89
48
  }
90
49
 
91
50
  update(): void {
@@ -143,10 +102,62 @@ export default class AmfTopic extends LightningElement {
143
102
  }
144
103
 
145
104
  renderedCallback(): void {
146
- try {
147
- this.update();
148
- } catch (error) {
149
- console.error(error);
105
+ if (this._model) {
106
+ try {
107
+ this.update();
108
+ } catch (error) {
109
+ console.error(error);
110
+ }
150
111
  }
151
112
  }
152
113
  }
114
+
115
+ /**
116
+ * The underlying web components we use from api-console mutate their models we pass in.
117
+ * Since LWC makes them Read Only, we need to copy them before passing to the Web Component.
118
+ * @param value JSON Serializable object to clone.
119
+ * @returns A Promise that resolves with a copy of Value, serialized and parsed via JSON.
120
+ */
121
+ function clone(value: any): Promise<any> {
122
+ return new Promise((resolve, reject) => {
123
+ if (window.Worker) {
124
+ const workerCode = `
125
+ onmessage = function(e) {
126
+ try {
127
+ const jsonObject = JSON.parse(e.data);
128
+ postMessage({ success: true, data: jsonObject });
129
+ } catch (error) {
130
+ postMessage({ success: false, error: error.message });
131
+ }
132
+ };
133
+ `;
134
+
135
+ const blob = new Blob([workerCode], {
136
+ type: "application/javascript"
137
+ });
138
+ const workerURL = URL.createObjectURL(blob);
139
+ const worker = new Worker(workerURL);
140
+
141
+ try {
142
+ const jsonObj = JSON.stringify(value);
143
+ worker.postMessage(jsonObj);
144
+ } catch (err) {
145
+ reject(err);
146
+ }
147
+
148
+ worker.onmessage = function (e) {
149
+ if (e.data.success) {
150
+ resolve(e.data.data);
151
+ } else {
152
+ reject(new Error(e.data.error));
153
+ }
154
+ };
155
+
156
+ worker.onerror = function (e) {
157
+ reject(new Error(e.message));
158
+ };
159
+ } else {
160
+ reject(new Error("Your browser doesn't support web workers."));
161
+ }
162
+ });
163
+ }
@@ -0,0 +1,3 @@
1
+ onmessage = function (event) {
2
+ postMessage(JSON.parse(JSON.stringify(event.data)));
3
+ };
@@ -1,5 +0,0 @@
1
- onmessage = function (event) {
2
- const value = event.data;
3
- const clonedValue = JSON.parse(JSON.stringify(value));
4
- postMessage(clonedValue);
5
- };