@inflector/aura 0.3.5 → 0.3.6
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/dist/hooks/react.d.ts.map +1 -1
- package/dist/hooks/react.js +40 -19
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/hooks/react.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAIpC,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAA;AAkB5B,wBAAgB,QAAQ,CACtB,IAAI,SAAS,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAElF,KAAK,EAAE,IAAI,EACX,OAAO,CAAC,EAAE;IACR,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;CACnE,
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/hooks/react.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAIpC,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAA;AAkB5B,wBAAgB,QAAQ,CACtB,IAAI,SAAS,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAElF,KAAK,EAAE,IAAI,EACX,OAAO,CAAC,EAAE;IACR,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;CACnE,GAsEc,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAChD;AAED,wBAAgB,SAAS,CACvB,IAAI,SAAS,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAElF,KAAK,EAAE,IAAI,EACX,OAAO,EAAE;IACP,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;CACnE,GA0Dc,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CACnD;AAED,eAAO,MAAM,OAAO,GAAI,UAAU,OAAO,CAAC,UAAU,CAAC,OAAO,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;;;;;;;;;aAGjF,CAAA;AAED,eAAO,MAAM,UAAU,GAAI,UAAU,GAAG,QAGvC,CAAA"}
|
package/dist/hooks/react.js
CHANGED
|
@@ -19,38 +19,48 @@ function deepEqual(a, b) {
|
|
|
19
19
|
}
|
|
20
20
|
export function useTable(table, Options) {
|
|
21
21
|
const [data, setData] = useState([]);
|
|
22
|
-
// 1. Keep the ref logic
|
|
23
22
|
const whereString = JSON.stringify(Options?.where);
|
|
24
23
|
const whereRef = useRef(whereString);
|
|
25
|
-
// 2. Cleanup: If the filter changed, clear the state immediately
|
|
26
|
-
// This happens during render to prevent showing old data
|
|
27
24
|
if (whereRef.current !== whereString) {
|
|
28
25
|
whereRef.current = whereString;
|
|
29
26
|
setData([]);
|
|
30
27
|
}
|
|
31
28
|
useEffect(() => {
|
|
32
29
|
const unsubscribe = table.Subscribe((event) => {
|
|
30
|
+
// Normalize op to uppercase to handle both old and new formats
|
|
31
|
+
const op = event.data.op;
|
|
33
32
|
setData((prev) => {
|
|
34
|
-
// Helper to prevent duplicates (
|
|
33
|
+
// Helper to prevent duplicates (Upsert logic)
|
|
35
34
|
const merge = (existing, incoming) => {
|
|
36
35
|
const incomingItems = Array.isArray(incoming) ? incoming : [incoming];
|
|
37
36
|
const filtered = existing.filter((ex) => !incomingItems.some((inc) => deepEqual(ex, inc)));
|
|
38
37
|
return [...filtered, ...incomingItems];
|
|
39
38
|
};
|
|
40
|
-
switch (
|
|
41
|
-
case 'Get':
|
|
42
|
-
|
|
43
|
-
case 'AddMany':
|
|
39
|
+
switch (op) {
|
|
40
|
+
case 'Get': {
|
|
41
|
+
// data is the record directly (no new/old wrapper)
|
|
44
42
|
return merge(prev, event.data);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
}
|
|
44
|
+
case 'INSERT': {
|
|
45
|
+
// data shape: { new: record, old: null }
|
|
46
|
+
const rows = Array.isArray(event.data) ? event.data : [event.data];
|
|
47
|
+
const newRows = rows.map((r) => r.new ?? r);
|
|
48
|
+
return merge(prev, newRows);
|
|
49
|
+
}
|
|
50
|
+
case 'DELETE': {
|
|
51
|
+
// data shape: { new: null, old: record }
|
|
52
|
+
const rows = Array.isArray(event.data) ? event.data : [event.data];
|
|
53
|
+
const oldRows = rows.map((r) => r.old ?? r);
|
|
54
|
+
return prev.filter((x) => !oldRows.some((d) => deepEqual(d, x)));
|
|
55
|
+
}
|
|
56
|
+
case 'UPDATE': {
|
|
57
|
+
// data shape: { new: record, old: record }
|
|
49
58
|
const updates = Array.isArray(event.data) ? event.data : [event.data];
|
|
50
59
|
return prev.map((item) => {
|
|
51
60
|
const found = updates.find((d) => deepEqual(d.old, item));
|
|
52
61
|
return found ? found.new : item;
|
|
53
62
|
});
|
|
63
|
+
}
|
|
54
64
|
default:
|
|
55
65
|
return prev;
|
|
56
66
|
}
|
|
@@ -62,7 +72,6 @@ export function useTable(table, Options) {
|
|
|
62
72
|
}
|
|
63
73
|
export function useRecord(table, Options) {
|
|
64
74
|
const [data, setData] = useState(undefined);
|
|
65
|
-
// Ref logic for single record
|
|
66
75
|
const whereString = JSON.stringify(Options.where);
|
|
67
76
|
const whereRef = useRef(whereString);
|
|
68
77
|
if (whereRef.current !== whereString) {
|
|
@@ -71,19 +80,31 @@ export function useRecord(table, Options) {
|
|
|
71
80
|
}
|
|
72
81
|
useEffect(() => {
|
|
73
82
|
const unsubscribe = table.Subscribe((event) => {
|
|
83
|
+
// Normalize op to uppercase to handle both old and new formats
|
|
84
|
+
const op = event.data.op;
|
|
74
85
|
setData((prev) => {
|
|
75
|
-
switch (
|
|
76
|
-
case 'Get':
|
|
77
|
-
|
|
86
|
+
switch (op) {
|
|
87
|
+
case 'Get': {
|
|
88
|
+
// data is the record directly (no new/old wrapper)
|
|
78
89
|
return Array.isArray(event.data) ? event.data[0] : event.data;
|
|
79
|
-
|
|
90
|
+
}
|
|
91
|
+
case 'INSERT': {
|
|
92
|
+
// data shape: { new: record, old: null }
|
|
93
|
+
const row = Array.isArray(event.data) ? event.data[0] : event.data;
|
|
94
|
+
return row?.new ?? row;
|
|
95
|
+
}
|
|
96
|
+
case 'DELETE': {
|
|
97
|
+
// data shape: { new: null, old: record }
|
|
80
98
|
const deleteList = Array.isArray(event.data) ? event.data : [event.data];
|
|
81
|
-
const isMatch = deleteList.some(d => deepEqual(d
|
|
99
|
+
const isMatch = deleteList.some((d) => deepEqual(d.old ?? d, prev));
|
|
82
100
|
return isMatch ? undefined : prev;
|
|
83
|
-
|
|
101
|
+
}
|
|
102
|
+
case 'UPDATE': {
|
|
103
|
+
// data shape: { new: record, old: record }
|
|
84
104
|
const updates = Array.isArray(event.data) ? event.data : [event.data];
|
|
85
105
|
const match = updates.find((d) => deepEqual(d.old, prev));
|
|
86
106
|
return match ? match.new : prev;
|
|
107
|
+
}
|
|
87
108
|
default:
|
|
88
109
|
return prev;
|
|
89
110
|
}
|