@colyseus/react 0.1.0 → 0.1.1
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/index.cjs +7 -6
- package/dist/index.mjs +8 -7
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -199,10 +199,12 @@ function useColyseusState(roomState, decoder, selector = (s) => s) {
|
|
|
199
199
|
(0, react.useEffect)(() => {
|
|
200
200
|
if (roomState && decoder) getOrCreateSubscription(roomState, decoder);
|
|
201
201
|
}, [roomState, decoder]);
|
|
202
|
-
const
|
|
202
|
+
const selectorRef = (0, react.useRef)(selector);
|
|
203
|
+
selectorRef.current = selector;
|
|
204
|
+
const getSnapshot = (0, react.useCallback)(() => {
|
|
203
205
|
if (!roomState || !decoder) return;
|
|
204
206
|
const subscription = getOrCreateSubscription(roomState, decoder);
|
|
205
|
-
const selectedState =
|
|
207
|
+
const selectedState = selectorRef.current(roomState);
|
|
206
208
|
const ctx = {
|
|
207
209
|
refs: decoder.root?.refs,
|
|
208
210
|
objectToRefId: subscription.objectToRefId,
|
|
@@ -224,14 +226,13 @@ function useColyseusState(roomState, decoder, selector = (s) => s) {
|
|
|
224
226
|
}
|
|
225
227
|
}
|
|
226
228
|
return result;
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
+
}, [roomState, decoder]);
|
|
230
|
+
return (0, react.useSyncExternalStore)((0, react.useCallback)((callback) => {
|
|
229
231
|
if (!roomState || !decoder) return () => {};
|
|
230
232
|
const subscription = getOrCreateSubscription(roomState, decoder);
|
|
231
233
|
subscription.listeners.add(callback);
|
|
232
234
|
return () => subscription.listeners.delete(callback);
|
|
233
|
-
};
|
|
234
|
-
return (0, react.useSyncExternalStore)(subscribe, getSnapshot);
|
|
235
|
+
}, [roomState, decoder]), getSnapshot);
|
|
235
236
|
}
|
|
236
237
|
|
|
237
238
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useSyncExternalStore } from "react";
|
|
1
|
+
import { useCallback, useEffect, useRef, useSyncExternalStore } from "react";
|
|
2
2
|
import { ArraySchema, MapSchema, Schema } from "@colyseus/schema";
|
|
3
3
|
|
|
4
4
|
//#region src/schema/createSnapshot.ts
|
|
@@ -199,10 +199,12 @@ function useColyseusState(roomState, decoder, selector = (s) => s) {
|
|
|
199
199
|
useEffect(() => {
|
|
200
200
|
if (roomState && decoder) getOrCreateSubscription(roomState, decoder);
|
|
201
201
|
}, [roomState, decoder]);
|
|
202
|
-
const
|
|
202
|
+
const selectorRef = useRef(selector);
|
|
203
|
+
selectorRef.current = selector;
|
|
204
|
+
const getSnapshot = useCallback(() => {
|
|
203
205
|
if (!roomState || !decoder) return;
|
|
204
206
|
const subscription = getOrCreateSubscription(roomState, decoder);
|
|
205
|
-
const selectedState =
|
|
207
|
+
const selectedState = selectorRef.current(roomState);
|
|
206
208
|
const ctx = {
|
|
207
209
|
refs: decoder.root?.refs,
|
|
208
210
|
objectToRefId: subscription.objectToRefId,
|
|
@@ -224,14 +226,13 @@ function useColyseusState(roomState, decoder, selector = (s) => s) {
|
|
|
224
226
|
}
|
|
225
227
|
}
|
|
226
228
|
return result;
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
+
}, [roomState, decoder]);
|
|
230
|
+
return useSyncExternalStore(useCallback((callback) => {
|
|
229
231
|
if (!roomState || !decoder) return () => {};
|
|
230
232
|
const subscription = getOrCreateSubscription(roomState, decoder);
|
|
231
233
|
subscription.listeners.add(callback);
|
|
232
234
|
return () => subscription.listeners.delete(callback);
|
|
233
|
-
};
|
|
234
|
-
return useSyncExternalStore(subscribe, getSnapshot);
|
|
235
|
+
}, [roomState, decoder]), getSnapshot);
|
|
235
236
|
}
|
|
236
237
|
|
|
237
238
|
//#endregion
|