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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plug-code",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "",
5
5
  "main": "src/plug-code.tsx",
6
6
  "types": "types/plug-code.d.ts",
@@ -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
- const result = calculator();
69
- this.replace("root" as any, { [outputKey]: result });
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 & "root"): {
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) => {
@@ -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<K extends keyof S>(key: string & "root", updater: (draft: any) => void, slot?: string): void;
82
+ update(key: string | "root", updater: (draft: any) => void, slot?: string): void;
83
83
 
84
84
  subscribe(listener: () => void): () => void;
85
85