plug-code 1.1.10 → 1.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/core/plcAPI.tsx +24 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plug-code",
3
- "version": "1.1.10",
3
+ "version": "1.1.11",
4
4
  "description": "",
5
5
  "main": "src/plug-code.tsx",
6
6
  "types": "types/plug-code.d.ts",
@@ -63,31 +63,40 @@ export class PlcAPI<S extends ObjectType> {
63
63
  }
64
64
  }
65
65
 
66
- derive<K extends string>(outputKey: K, dependencies: string[], calculator: () => any) {
67
- let isRunning = false;
68
- let idleHandle: number | null = null;
66
+ derive<K extends string>(
67
+ outputKey: K,
68
+ dependencies: string[],
69
+ calculator: () => any
70
+ ) {
71
+ let lastValue: any
72
+
73
+ let isRunning = false
74
+ let idleHandle: number | null = null
69
75
 
70
76
  const runUpdate = () => {
71
- if (isRunning) return;
72
- if (idleHandle) cancelIdleCallback(idleHandle);
77
+ if (isRunning) return
78
+ if (idleHandle) cancelIdleCallback(idleHandle)
73
79
 
74
80
  idleHandle = requestIdleCallback(() => {
75
- isRunning = true;
81
+ isRunning = true
76
82
  try {
77
- const result = calculator();
78
- this.replace("root" as any, { [outputKey]: result });
83
+ const result = calculator()
84
+ lastValue = result
85
+ this.replace("root" as any, { [outputKey]: result })
79
86
  } finally {
80
- isRunning = false;
81
- idleHandle = null;
87
+ isRunning = false
88
+ idleHandle = null
82
89
  }
83
- }, { timeout: 100 });
84
- };
90
+ }, { timeout: 100 })
91
+ }
85
92
 
86
93
  dependencies.forEach(dep => {
87
- this.store.subscribe(dep as any, () => runUpdate());
88
- });
94
+ this.store.subscribe(dep as any, () => runUpdate())
95
+ })
96
+
97
+ runUpdate()
89
98
 
90
- runUpdate();
99
+ return () => lastValue
91
100
  }
92
101
 
93
102
  register(slot: SlotKey, node: () => React.ReactNode): void;