@adventurelabs/scout-core 1.4.59 → 1.4.60
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/useScoutRealtimeDevices.js +27 -32
- package/dist/store/scout.js +20 -3
- package/package.json +1 -1
|
@@ -6,26 +6,26 @@ export function useScoutRealtimeDevices(scoutSupabase) {
|
|
|
6
6
|
const channels = useRef([]);
|
|
7
7
|
const [latestDeviceUpdate, setLatestDeviceUpdate] = useState(null);
|
|
8
8
|
const activeHerdId = useSelector((state) => state.scout.active_herd_id);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
const handleDeviceBroadcast = useCallback((message) => {
|
|
10
|
+
const { payload: data } = message;
|
|
11
|
+
if (data.table !== "devices" || data.schema !== "public") {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const deviceData = data.operation === "DELETE"
|
|
15
|
+
? data.old_record
|
|
16
|
+
: data.record ?? data.old_record;
|
|
14
17
|
if (!deviceData)
|
|
15
18
|
return;
|
|
16
19
|
let operation;
|
|
17
20
|
switch (data.operation) {
|
|
18
21
|
case "INSERT":
|
|
19
22
|
operation = EnumRealtimeOperation.INSERT;
|
|
20
|
-
console.log("[Devices] New device received:", data.record);
|
|
21
23
|
break;
|
|
22
24
|
case "UPDATE":
|
|
23
25
|
operation = EnumRealtimeOperation.UPDATE;
|
|
24
|
-
console.log("[Devices] Device updated:", data.record);
|
|
25
26
|
break;
|
|
26
27
|
case "DELETE":
|
|
27
28
|
operation = EnumRealtimeOperation.DELETE;
|
|
28
|
-
console.log("[Devices] Device deleted:", data.old_record);
|
|
29
29
|
break;
|
|
30
30
|
default:
|
|
31
31
|
return;
|
|
@@ -34,40 +34,35 @@ export function useScoutRealtimeDevices(scoutSupabase) {
|
|
|
34
34
|
data: deviceData,
|
|
35
35
|
operation,
|
|
36
36
|
};
|
|
37
|
-
console.log(`[scout-core realtime] DEVICE ${data.operation} received:`, JSON.stringify(realtimeData));
|
|
38
37
|
setLatestDeviceUpdate(realtimeData);
|
|
39
38
|
}, []);
|
|
40
|
-
// Clear latest update
|
|
41
39
|
const clearLatestUpdate = useCallback(() => {
|
|
42
40
|
setLatestDeviceUpdate(null);
|
|
43
41
|
}, []);
|
|
44
|
-
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (!scoutSupabase)
|
|
44
|
+
return;
|
|
45
45
|
channels.current.forEach((channel) => scoutSupabase.removeChannel(channel));
|
|
46
46
|
channels.current = [];
|
|
47
|
-
};
|
|
48
|
-
const createDevicesChannel = (herdId) => {
|
|
49
|
-
return scoutSupabase
|
|
50
|
-
.channel(`${herdId}-devices`, { config: { private: true } })
|
|
51
|
-
.on("broadcast", { event: "*" }, handleDeviceBroadcast)
|
|
52
|
-
.subscribe((status) => {
|
|
53
|
-
if (status === "SUBSCRIBED") {
|
|
54
|
-
console.log(`[Devices] ✅ Connected to herd ${herdId}`);
|
|
55
|
-
}
|
|
56
|
-
else if (status === "CHANNEL_ERROR") {
|
|
57
|
-
console.warn(`[Devices] 🟡 Failed to connect to herd ${herdId}`);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
cleanupChannels();
|
|
63
|
-
// Clear previous update when switching herds
|
|
64
47
|
clearLatestUpdate();
|
|
65
|
-
// Create devices channel for active herd
|
|
66
48
|
if (activeHerdId) {
|
|
67
|
-
const channel =
|
|
49
|
+
const channel = scoutSupabase
|
|
50
|
+
.channel(`${activeHerdId}-devices`, { config: { private: true } })
|
|
51
|
+
.on("broadcast", { event: "*" }, handleDeviceBroadcast)
|
|
52
|
+
.subscribe((status) => {
|
|
53
|
+
if (status === "SUBSCRIBED") {
|
|
54
|
+
console.log(`[Devices] ✅ Connected to herd ${activeHerdId}`);
|
|
55
|
+
}
|
|
56
|
+
else if (status === "CHANNEL_ERROR") {
|
|
57
|
+
console.warn(`[Devices] 🟡 Failed to connect to herd ${activeHerdId}`);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
68
60
|
channels.current.push(channel);
|
|
69
61
|
}
|
|
70
|
-
return
|
|
71
|
-
|
|
62
|
+
return () => {
|
|
63
|
+
channels.current.forEach((ch) => scoutSupabase.removeChannel(ch));
|
|
64
|
+
channels.current = [];
|
|
65
|
+
};
|
|
66
|
+
}, [scoutSupabase, activeHerdId, handleDeviceBroadcast, clearLatestUpdate]);
|
|
72
67
|
return [latestDeviceUpdate, clearLatestUpdate];
|
|
73
68
|
}
|
package/dist/store/scout.js
CHANGED
|
@@ -96,9 +96,26 @@ export const scoutSlice = createSlice({
|
|
|
96
96
|
},
|
|
97
97
|
updateDeviceForHerdModule: (state, action) => {
|
|
98
98
|
const { herd_id, device } = action.payload;
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
const targetHerdKey = device.herd_id != null
|
|
100
|
+
? String(device.herd_id)
|
|
101
|
+
: herd_id;
|
|
102
|
+
if (device.herd_id != null) {
|
|
103
|
+
for (const hm of state.herd_modules) {
|
|
104
|
+
if (hm.herd.id?.toString() !== targetHerdKey) {
|
|
105
|
+
hm.devices = hm.devices.filter((d) => d.id !== device.id);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const herd_module = state.herd_modules.find((hm) => hm.herd.id?.toString() === targetHerdKey);
|
|
110
|
+
if (!herd_module) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const i = herd_module.devices.findIndex((d) => d.id === device.id);
|
|
114
|
+
if (i >= 0) {
|
|
115
|
+
herd_module.devices[i] = device;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
herd_module.devices.push(device);
|
|
102
119
|
}
|
|
103
120
|
},
|
|
104
121
|
// append device to herd module
|