plug-code 1.1.5 → 1.1.7

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.5",
3
+ "version": "1.1.7",
4
4
  "description": "",
5
5
  "main": "src/plug-code.tsx",
6
6
  "types": "types/plug-code.d.ts",
@@ -51,6 +51,31 @@ export class PlcAPI<S extends ObjectType> {
51
51
  return this;
52
52
  }
53
53
 
54
+ replace<K extends string>(key: K & "root", data: Partial<any>, slot?: string) {
55
+ const currentSub = this.substores.get(key) || {};
56
+ const newSub = { ...currentSub, ...data };
57
+
58
+ this.substores.set(key, newSub);
59
+ this.store.set(key as any, newSub);
60
+
61
+ if (slot) {
62
+ this.invalidate(slot);
63
+ }
64
+ }
65
+
66
+ derive<K extends string>(outputKey: K, dependencies: string[], calculator: () => any) {
67
+ const runUpdate = () => {
68
+ const result = calculator();
69
+ this.replace("root" as any, { [outputKey]: result });
70
+ };
71
+
72
+ dependencies.forEach(dep => {
73
+ this.store.subscribe(dep as any, () => runUpdate());
74
+ });
75
+
76
+ runUpdate();
77
+ }
78
+
54
79
  register(slot: SlotKey, node: () => React.ReactNode): void;
55
80
  register<K extends string>(slot: SlotKey, node: (data: any) => React.ReactNode, dependencyKey: K): void;
56
81
  register(slot: SlotKey, node: (data?: any) => React.ReactNode, dependencyKey?: string) {
package/src/plug-code.tsx CHANGED
@@ -39,9 +39,7 @@ export function createPlugAndCode<S extends ObjectType>(
39
39
  }, []);
40
40
 
41
41
  useEffect(() => {
42
- api.update("root", (draft: any) => {
43
- Object.assign(draft, initialProps);
44
- });
42
+ api.replace("root", initialProps)
45
43
  }, [initialProps, api]);
46
44
 
47
45
  const useSelector = <Result,>(selector: (state: any) => Result): Result => {