@inflector/aura 0.1.12 → 0.1.13

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.
@@ -1,4 +1,4 @@
1
- import type { CreateAura } from '../index';
1
+ import type { CreateAura } from '..';
2
2
  type ValueOf<T> = T[keyof T];
3
3
  export declare function useTable<TDef extends ValueOf<Awaited<ReturnType<typeof CreateAura<any, any>>>['Database']>>(table: TDef, Options?: {
4
4
  where: Parameters<Awaited<ReturnType<TDef['GetOne']>['where']>>[0];
@@ -1 +1 @@
1
- {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/hooks/react.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAS,MAAM,UAAU,CAAA;AAIjD,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,GAgDc,4tBAAS,GAAG,SAAS,CACrC;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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AAO,CAAC,UAAU,CAAC,OAAO,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;;;;;;;;;aAGjF,CAAA;AAED,eAAO,MAAM,UAAU,GAAI,UAAU,GAAG,QAGvC,CAAA"}
@@ -19,30 +19,35 @@ function deepEqual(a, b) {
19
19
  }
20
20
  export function useTable(table, Options) {
21
21
  const [data, setData] = useState([]);
22
+ // Dummy state to force rerun when session changes
23
+ const [sessionVersion, setSessionVersion] = useState(0);
22
24
  useEffect(() => {
25
+ const handler = () => {
26
+ setSessionVersion((v) => v + 1);
27
+ };
28
+ window.addEventListener('__AuraSession', handler);
29
+ return () => {
30
+ window.removeEventListener('__AuraSession', handler);
31
+ };
32
+ }, []);
33
+ useEffect(() => {
34
+ // Subscribe to table
23
35
  const unsubscribe = table.Subscribe((event) => {
24
36
  setData((prev) => {
25
37
  if (!prev)
26
38
  return prev;
27
39
  switch (event.action) {
28
40
  case 'Get':
29
- return [...prev, event.data];
30
41
  case 'Add':
31
42
  return [...prev, event.data];
32
43
  case 'AddMany':
33
44
  return [...prev, ...event.data];
34
45
  case 'Delete':
35
- // Delete by matching old items
36
46
  return prev.filter((x) => !event.data.old.some((d) => deepEqual(d, x)));
37
47
  case 'Update':
38
- // Update each item by matching old and replacing with new
39
48
  return prev.map((item) => {
40
- // Find if the item matches any "old" in event.data array
41
49
  const update = event.data.find((d) => deepEqual(d.old, item));
42
- if (update) {
43
- return update.new;
44
- }
45
- return item;
50
+ return update ? update.new : item;
46
51
  });
47
52
  default:
48
53
  return prev;
@@ -52,11 +57,23 @@ export function useTable(table, Options) {
52
57
  return () => {
53
58
  unsubscribe();
54
59
  };
55
- }, [table, JSON.stringify(Options?.where)]);
60
+ }, [table, JSON.stringify(Options?.where), sessionVersion]); // <--- include sessionVersion here
56
61
  return data;
57
62
  }
58
63
  export function useRecord(table, Options) {
59
64
  const [data, setData] = useState();
65
+ // Dummy state to trigger rerun on session change
66
+ const [sessionVersion, setSessionVersion] = useState(0);
67
+ // Subscribe to global session changes
68
+ useEffect(() => {
69
+ const handler = () => {
70
+ setSessionVersion((v) => v + 1);
71
+ };
72
+ window.addEventListener('__AuraSession', handler);
73
+ return () => {
74
+ window.removeEventListener('__AuraSession', handler);
75
+ };
76
+ }, []);
60
77
  useEffect(() => {
61
78
  const unsubscribe = table.Subscribe((event) => {
62
79
  setData((prev) => {
@@ -67,12 +84,12 @@ export function useRecord(table, Options) {
67
84
  case 'Add':
68
85
  return event.data;
69
86
  case 'Delete':
70
- // If deleted record matches the current one, remove it
87
+ // Remove current record if it matches
71
88
  if (event.data.old && deepEqual(event.data.old, prev))
72
89
  return undefined;
73
90
  return prev;
74
91
  case 'Update':
75
- // If the current record matches any "old", replace with "new"
92
+ // Replace current record if it matches
76
93
  const update = event.data.find((d) => deepEqual(d.old, prev));
77
94
  if (update)
78
95
  return update.new;
@@ -83,7 +100,7 @@ export function useRecord(table, Options) {
83
100
  });
84
101
  }, Options.where, true);
85
102
  return () => unsubscribe();
86
- }, [table, JSON.stringify(Options.where)]);
103
+ }, [table, JSON.stringify(Options.where), sessionVersion]); // <--- include sessionVersion
87
104
  return data;
88
105
  }
89
106
  export const useUser = (provider) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflector/aura",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",