plug-code 1.1.7 → 1.1.9
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 +16 -5
- package/types/plug-code.d.ts +3 -1
package/package.json
CHANGED
package/src/core/plcAPI.tsx
CHANGED
|
@@ -64,13 +64,24 @@ 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
|
+
|
|
67
69
|
const runUpdate = () => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
if (isRunning) return;
|
|
71
|
+
|
|
72
|
+
isRunning = true;
|
|
73
|
+
try {
|
|
74
|
+
const result = calculator();
|
|
75
|
+
this.replace("root" as any, { [outputKey]: result });
|
|
76
|
+
} finally {
|
|
77
|
+
isRunning = false;
|
|
78
|
+
}
|
|
70
79
|
};
|
|
71
80
|
|
|
72
81
|
dependencies.forEach(dep => {
|
|
73
|
-
this.store.subscribe(dep as any, () =>
|
|
82
|
+
this.store.subscribe(dep as any, () => {
|
|
83
|
+
runUpdate();
|
|
84
|
+
});
|
|
74
85
|
});
|
|
75
86
|
|
|
76
87
|
runUpdate();
|
|
@@ -104,7 +115,7 @@ export class PlcAPI<S extends ObjectType> {
|
|
|
104
115
|
}
|
|
105
116
|
}
|
|
106
117
|
|
|
107
|
-
scope<T = any>(key: string
|
|
118
|
+
scope<T = any>(key: string | "root"): {
|
|
108
119
|
get: () => T;
|
|
109
120
|
update: (updater: (draft: T) => void) => void;
|
|
110
121
|
connect: (renderer: (data: T) => React.ReactNode) => React.FC;
|
|
@@ -116,7 +127,7 @@ export class PlcAPI<S extends ObjectType> {
|
|
|
116
127
|
get: (): T => this.getData(key),
|
|
117
128
|
|
|
118
129
|
update: (updater: (draft: T) => void) => {
|
|
119
|
-
this.update(key, updater);
|
|
130
|
+
this.update(key as any, updater);
|
|
120
131
|
},
|
|
121
132
|
|
|
122
133
|
connect: (renderer: (data: T) => React.ReactNode) => {
|
package/types/plug-code.d.ts
CHANGED
|
@@ -76,8 +76,10 @@ export declare class PlcAPI<S extends ObjectType> {
|
|
|
76
76
|
createData<K extends string, T>(key: K, initialState: T): void;
|
|
77
77
|
|
|
78
78
|
getData(key: string): any;
|
|
79
|
+
|
|
80
|
+
derive<K extends string>(outputKey: K, dependencies: string[], calculator: () => any): void
|
|
79
81
|
|
|
80
|
-
update
|
|
82
|
+
update(key: string | "root", updater: (draft: any) => void, slot?: string): void;
|
|
81
83
|
|
|
82
84
|
subscribe(listener: () => void): () => void;
|
|
83
85
|
|