@noya-app/noya-multiplayer-react 0.1.22 → 0.1.24

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,6 +1,15 @@
1
1
  /* eslint-disable no-restricted-globals */
2
- import { createValue, MultiplayerUser, TSchema } from "@noya-app/state-manager";
3
- import { useEffect, useMemo } from "react";
2
+ import {
3
+ createValue,
4
+ localStorageSync,
5
+ MultiplayerUser,
6
+ SyncAdapter,
7
+ TSchema,
8
+ webSocketSync,
9
+ } from "@noya-app/state-manager";
10
+ import { useEffect, useMemo, useState } from "react";
11
+ import { useMultiplayerState, UseMultiplayerStateOptions } from "./hooks";
12
+ import { StateInspectorOptions } from "./inspector/StateInspector";
4
13
 
5
14
  export type AppViewType = "editable" | "readOnly" | "preview";
6
15
 
@@ -11,7 +20,7 @@ export type AppData<State> = {
11
20
  theme: AppTheme;
12
21
  viewType: AppViewType;
13
22
  initialState: State;
14
- inspector: boolean;
23
+ inspector: boolean | StateInspectorOptions;
15
24
  };
16
25
 
17
26
  export function createDefaultAppData<State>(
@@ -47,16 +56,16 @@ export function getAppData<State>(
47
56
  }
48
57
  }
49
58
 
50
- type NoyaAppOptions = {
59
+ type AppDataOptions = {
51
60
  schema?: TSchema;
52
61
  };
53
62
 
54
63
  /**
55
64
  * Get data from host app. Fall back to default data.
56
65
  */
57
- export function useNoyaApp<State>(
58
- initialState: State,
59
- { schema }: NoyaAppOptions = {}
66
+ export function useAppData<S extends object>(
67
+ initialState: S,
68
+ { schema }: AppDataOptions = {}
60
69
  ) {
61
70
  const appData = useMemo(() => {
62
71
  return getAppData(
@@ -132,3 +141,45 @@ export function useBroadcastMenuItems<T>(
132
141
  export function isEmbeddedApp() {
133
142
  return window.self !== window.top;
134
143
  }
144
+
145
+ export function useNoyaAppState<S extends object, M = void, E = void>(
146
+ initialState: S | (() => S),
147
+ options: UseMultiplayerStateOptions<S, M, E> & {
148
+ localStorageKey?: string;
149
+ }
150
+ ) {
151
+ // Resolve state to a value
152
+ const [state] = useState(initialState);
153
+
154
+ const {
155
+ multiplayerUrl,
156
+ inspector,
157
+ initialState: noyaAppInitialState,
158
+ theme,
159
+ viewType,
160
+ } = useAppData(state, {
161
+ schema: options.schema,
162
+ });
163
+
164
+ const sync = useMemo((): SyncAdapter<S, M, E> => {
165
+ return multiplayerUrl
166
+ ? webSocketSync(multiplayerUrl)
167
+ : localStorageSync({ key: options.localStorageKey || "noya-app" });
168
+ }, [multiplayerUrl, options.localStorageKey]);
169
+
170
+ const result = useMultiplayerState(noyaAppInitialState, {
171
+ ...options,
172
+ inspector: options.inspector ?? inspector,
173
+ sync,
174
+ });
175
+
176
+ const [, , extras] = result;
177
+
178
+ useBroadcastConnectedUsers(extras.connectedUsers);
179
+
180
+ const mergedExtras = useMemo(() => {
181
+ return { ...extras, theme, viewType } as const;
182
+ }, [extras, theme, viewType]);
183
+
184
+ return [result[0], result[1], mergedExtras] as const;
185
+ }