plug-code 1.1.10 → 1.1.12

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 +19 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plug-code",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
4
4
  "description": "",
5
5
  "main": "src/plug-code.tsx",
6
6
  "types": "types/plug-code.d.ts",
@@ -63,33 +63,32 @@ 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
69
72
 
70
73
  const runUpdate = () => {
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 });
84
- };
74
+ const result = calculator()
75
+ lastValue = result
76
+ this.replace("root" as any, { [outputKey]: result })
77
+ }
78
+
79
+ // cálculo inicial inmediato
80
+ runUpdate()
85
81
 
82
+ // suscripción a dependencias
86
83
  dependencies.forEach(dep => {
87
- this.store.subscribe(dep as any, () => runUpdate());
88
- });
84
+ this.store.subscribe(dep as any, runUpdate)
85
+ })
89
86
 
90
- runUpdate();
87
+ return () => lastValue
91
88
  }
92
89
 
90
+
91
+
93
92
  register(slot: SlotKey, node: () => React.ReactNode): void;
94
93
  register<K extends string>(slot: SlotKey, node: (data: any) => React.ReactNode, dependencyKey: K): void;
95
94
  register(slot: SlotKey, node: (data?: any) => React.ReactNode, dependencyKey?: string) {