@noya-app/noya-multiplayer-react 0.1.18 → 0.1.19

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/src/globals.ts CHANGED
@@ -1,42 +1,4 @@
1
- import { Emitter } from "@noya-app/emitter";
2
- import { useEffect, useState } from "react";
3
-
4
- class Observable<T> {
5
- private value: T;
6
- private emitter = new Emitter<[T]>();
7
-
8
- constructor(value: T) {
9
- this.value = value;
10
- }
11
-
12
- get() {
13
- return this.value;
14
- }
15
-
16
- set(value: T) {
17
- if (this.value === value) return;
18
-
19
- this.value = value;
20
-
21
- this.emitter.emit(value);
22
- }
23
-
24
- listen(callback: (value: T) => void) {
25
- callback(this.value);
26
-
27
- return this.emitter.addListener(callback);
28
- }
29
- }
30
-
31
- export function useObservable<T>(observable: Observable<T>) {
32
- const [value, setValue] = useState(observable.get());
33
-
34
- useEffect(() => {
35
- return observable.listen(setValue);
36
- }, [observable]);
37
-
38
- return value;
39
- }
1
+ import { Observable } from "@noya-app/observable";
40
2
 
41
3
  export const shouldTrackEvents$ = new Observable(false);
42
4
  export const shouldTrackTasks$ = new Observable(false);
package/src/hooks.ts CHANGED
@@ -24,13 +24,10 @@ import {
24
24
  useState,
25
25
  useSyncExternalStore,
26
26
  } from "react";
27
- import {
28
- shouldTrackEvents$,
29
- shouldTrackTasks$,
30
- useObservable,
31
- } from "./globals";
27
+ import { shouldTrackEvents$, shouldTrackTasks$ } from "./globals";
32
28
  import { StateInspector } from "./inspector/StateInspector";
33
29
  import { useStateInspector } from "./inspector/useStateInspector";
30
+ import { useObservable } from "./useObservable";
34
31
 
35
32
  export function useSyncStateManager<S extends object, M>(
36
33
  stateManager: StateManager<S, M>
@@ -186,6 +183,7 @@ export function useMultiplayerState<S extends object, M = void, E = void>(
186
183
  const extras = useMemo(() => {
187
184
  return {
188
185
  tasks,
186
+ userId,
189
187
  connectionEvents,
190
188
  connectedUsers,
191
189
  ephemeralUserData,
@@ -197,7 +195,7 @@ export function useMultiplayerState<S extends object, M = void, E = void>(
197
195
  // }
198
196
  // },
199
197
  };
200
- }, [tasks, connectionEvents, connectedUsers, ephemeralUserData]);
198
+ }, [tasks, connectionEvents, connectedUsers, ephemeralUserData, userId]);
201
199
 
202
200
  const globalTrackEvents = useObservable(shouldTrackEvents$);
203
201
  const globalTrackTasks = useObservable(shouldTrackTasks$);
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "@noya-app/state-manager";
2
+ export * from "./avatarStyle";
3
+ export * from "./components/UserPointersOverlay";
2
4
  export * from "./hooks";
3
5
  export * from "./inspector/StateInspector";
@@ -686,7 +686,12 @@ export const StateInspector = memo(function StateInspector<
686
686
  <div style={styles.sectionInner}>
687
687
  {Object.entries(ephemeral).map(([key, value]) => (
688
688
  <InspectorRow key={key} colorScheme={colorScheme}>
689
- <ObjectInspector name={key} data={value} theme={theme} />
689
+ <ObjectInspector
690
+ name={key}
691
+ data={value}
692
+ theme={theme}
693
+ expandLevel={10}
694
+ />
690
695
  </InspectorRow>
691
696
  ))}
692
697
  </div>
@@ -0,0 +1,25 @@
1
+ import { GetWithPath, Observable, PathKey } from "@noya-app/observable";
2
+ import { useMemo, useSyncExternalStore } from "react";
3
+
4
+ export function useObservable<T>(observable: Observable<T>): T;
5
+ export function useObservable<T, P extends PathKey[]>(
6
+ observable: Observable<T>,
7
+ path: P
8
+ ): GetWithPath<T, P>;
9
+ export function useObservable<T, P extends PathKey[]>(
10
+ observable: Observable<T>,
11
+ path?: P
12
+ ) {
13
+ const { get, listen } = useMemo(() => {
14
+ const listen = path
15
+ ? (callback: (value: GetWithPath<T, P>) => void) =>
16
+ observable.listen(path, callback)
17
+ : observable.listen;
18
+
19
+ const get: () => void = path ? () => observable.get(path) : observable.get;
20
+
21
+ return { get, listen };
22
+ }, [observable, path]);
23
+
24
+ return useSyncExternalStore(listen, get, get);
25
+ }