plug-code 1.1.8 → 1.1.10
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 +1 -1
- package/src/core/plcAPI.tsx +18 -4
- package/types/plug-code.d.ts +1 -1
package/package.json
CHANGED
package/src/core/plcAPI.tsx
CHANGED
|
@@ -64,9 +64,23 @@ export class PlcAPI<S extends ObjectType> {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
derive<K extends string>(outputKey: K, dependencies: string[], calculator: () => any) {
|
|
67
|
+
let isRunning = false;
|
|
68
|
+
let idleHandle: number | null = null;
|
|
69
|
+
|
|
67
70
|
const runUpdate = () => {
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
if (isRunning) return;
|
|
72
|
+
if (idleHandle) cancelIdleCallback(idleHandle);
|
|
73
|
+
|
|
74
|
+
idleHandle = requestIdleCallback(() => {
|
|
75
|
+
isRunning = true;
|
|
76
|
+
try {
|
|
77
|
+
const result = calculator();
|
|
78
|
+
this.replace("root" as any, { [outputKey]: result });
|
|
79
|
+
} finally {
|
|
80
|
+
isRunning = false;
|
|
81
|
+
idleHandle = null;
|
|
82
|
+
}
|
|
83
|
+
}, { timeout: 100 });
|
|
70
84
|
};
|
|
71
85
|
|
|
72
86
|
dependencies.forEach(dep => {
|
|
@@ -104,7 +118,7 @@ export class PlcAPI<S extends ObjectType> {
|
|
|
104
118
|
}
|
|
105
119
|
}
|
|
106
120
|
|
|
107
|
-
scope<T = any>(key: string
|
|
121
|
+
scope<T = any>(key: string | "root"): {
|
|
108
122
|
get: () => T;
|
|
109
123
|
update: (updater: (draft: T) => void) => void;
|
|
110
124
|
connect: (renderer: (data: T) => React.ReactNode) => React.FC;
|
|
@@ -116,7 +130,7 @@ export class PlcAPI<S extends ObjectType> {
|
|
|
116
130
|
get: (): T => this.getData(key),
|
|
117
131
|
|
|
118
132
|
update: (updater: (draft: T) => void) => {
|
|
119
|
-
this.update(key, updater);
|
|
133
|
+
this.update(key as any, updater);
|
|
120
134
|
},
|
|
121
135
|
|
|
122
136
|
connect: (renderer: (data: T) => React.ReactNode) => {
|
package/types/plug-code.d.ts
CHANGED
|
@@ -79,7 +79,7 @@ export declare class PlcAPI<S extends ObjectType> {
|
|
|
79
79
|
|
|
80
80
|
derive<K extends string>(outputKey: K, dependencies: string[], calculator: () => any): void
|
|
81
81
|
|
|
82
|
-
update
|
|
82
|
+
update(key: string | "root", updater: (draft: any) => void, slot?: string): void;
|
|
83
83
|
|
|
84
84
|
subscribe(listener: () => void): () => void;
|
|
85
85
|
|