plug-code 1.1.17 → 1.2.0

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.17",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "main": "src/plug-code.tsx",
6
6
  "types": "types/plug-code.d.ts",
@@ -6,6 +6,7 @@ import type { PlcStore } from "./plcStore";
6
6
  import type { CommandFn, transformerType } from "../types/api";
7
7
 
8
8
  import type { ChannelRegistry, CommandRegistry, SlotRegistry } from "../types/registry";
9
+ import { isEqual } from "../helpers/core";
9
10
 
10
11
  type ChannelKey = keyof ChannelRegistry | (string & {});
11
12
  type ChannelData<K> = K extends keyof ChannelRegistry ? ChannelRegistry[K] : any;
@@ -20,7 +21,6 @@ type CommandResult<K> = K extends keyof CommandRegistry
20
21
 
21
22
  type SlotKey = keyof SlotRegistry | (string & {});
22
23
 
23
-
24
24
  export class PlcAPI<S extends ObjectType> {
25
25
  private store: PlcStore<S>
26
26
  private pipeline: PlcPipeline<S>
@@ -34,6 +34,18 @@ export class PlcAPI<S extends ObjectType> {
34
34
  this.pipeline = new PlcPipeline(store)
35
35
  }
36
36
 
37
+ redraw(key: string): void {
38
+ const currentData = this.substores.get(key);
39
+ if (currentData === undefined) return;
40
+
41
+ const dataRef = typeof currentData === 'object' && currentData !== null
42
+ ? (Array.isArray(currentData) ? [...currentData] : { ...currentData })
43
+ : currentData;
44
+
45
+ this.substores.set(key, dataRef);
46
+ this.store.set(key as any, dataRef);
47
+ }
48
+
37
49
  watch<T>(
38
50
  storeKey: string,
39
51
  selector: (data: any) => T,
@@ -123,7 +135,7 @@ export class PlcAPI<S extends ObjectType> {
123
135
 
124
136
  register(slot: SlotKey, node: (props?: any) => React.ReactNode): void;
125
137
  register<K extends string>(slot: SlotKey, node: (data: any, props?: any) => React.ReactNode, dependencyKey: K): void;
126
- register(slot: SlotKey, node: (data?: any, props?: any) => React.ReactNode, dependencyKey?: string) {
138
+ register(slot: SlotKey, node: (arg1?: any, arg2?: any) => React.ReactNode, dependencyKey?: string) {
127
139
  if (dependencyKey) {
128
140
  const ConnectedWrapper = (props: any) => {
129
141
  const [storeData, setStoreData] = useState(() => this.substores.get(dependencyKey));
@@ -139,7 +151,7 @@ export class PlcAPI<S extends ObjectType> {
139
151
  };
140
152
 
141
153
  this.store.batch(() => {
142
- this.pipeline.register(slot as string, ConnectedWrapper);
154
+ this.pipeline.register(slot as string, (p: any) => <ConnectedWrapper {...p} />);
143
155
  });
144
156
  }
145
157
  else {
@@ -199,7 +211,7 @@ export class PlcAPI<S extends ObjectType> {
199
211
  ): (WrappedComponent: React.ComponentType<OwnProps & ResultProps>) => React.FC<OwnProps> {
200
212
  return (WrappedComponent: React.ComponentType<OwnProps & ResultProps>) => {
201
213
 
202
- const ConnectedComponent = (props: OwnProps) => {
214
+ const ConnectedComponent: React.FC<OwnProps> = (props) => {
203
215
  const propsRef = useRef(props);
204
216
  propsRef.current = props;
205
217
 
@@ -212,25 +224,26 @@ export class PlcAPI<S extends ObjectType> {
212
224
  const unsubscribe = this.store.subscribe(key as any, () => {
213
225
  const currentData = this.substores.get(key);
214
226
  const newSlice = selector(currentData, propsRef.current);
215
-
216
227
  setSlice(prev => {
217
- if (prev === newSlice) return prev;
218
-
219
- if (typeof prev === 'object' && prev !== null && typeof newSlice === 'object' && newSlice !== null) {
220
- const keysA = Object.keys(prev) as Array<keyof ResultProps>;
221
- const keysB = Object.keys(newSlice) as Array<keyof ResultProps>;
222
- if (keysA.length === keysB.length && keysA.every(k => prev[k] === newSlice[k])) {
223
- return prev;
224
- }
225
- }
226
-
228
+ if (isEqual(prev, newSlice)) return prev;
227
229
  return newSlice;
228
230
  });
229
231
  });
230
232
  return unsubscribe;
231
233
  }, []);
232
234
 
235
+ useEffect(() => {
236
+ const currentData = this.substores.get(key);
237
+ const newSlice = selector(currentData, props);
238
+
239
+ setSlice(prev => {
240
+ if (isEqual(prev, newSlice)) return prev;
241
+ return newSlice;
242
+ });
243
+ }, [props]);
244
+
233
245
  if (slice === undefined && this.substores.get(key) === undefined) return null;
246
+
234
247
  return <WrappedComponent {...props} {...slice} />;
235
248
  };
236
249
 
@@ -240,6 +253,7 @@ export class PlcAPI<S extends ObjectType> {
240
253
  return ConnectedComponent;
241
254
  };
242
255
  }
256
+
243
257
  wrap(slot: SlotKey, fn: (next: () => React.ReactNode) => () => React.ReactNode) {
244
258
  this.store.batch(() => {
245
259
  this.pipeline.wrap(slot as string, fn)
@@ -367,7 +381,6 @@ export class PlcAPI<S extends ObjectType> {
367
381
  }
368
382
 
369
383
  if (triggerKey && triggerKey !== key) {
370
-
371
384
  const triggerData = this.substores.get(triggerKey);
372
385
 
373
386
  if (triggerData) {
@@ -0,0 +1,10 @@
1
+ export const isEqual = (a: any, b: any) => {
2
+ if (a === b) return true;
3
+ if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null) {
4
+ const keysA = Object.keys(a);
5
+ const keysB = Object.keys(b);
6
+ if (keysA.length !== keysB.length) return false;
7
+ return keysA.every(k => a[k] === b[k]);
8
+ }
9
+ return false;
10
+ }
@@ -57,7 +57,8 @@ export declare class PlcAPI<S extends ObjectType> {
57
57
  watch<T>(storeKey: string, selector: (data: any) => T, callback: (newValue: T, oldValue: T) => void): () => void
58
58
  override<K extends string>(key: K & "root", data: any, slot?: string): void
59
59
  // --- Gestión de UI (Slots & Rendering) ---
60
-
60
+
61
+ redraw(key: string): void
61
62
  register(slot: string, node: (props?: any) => React.ReactNode): void;
62
63
  register<K extends string>(slot: string, node: (data: any, props?: any) => React.ReactNode, dependencyKey: K): void;
63
64