@noya-app/noya-multiplayer-react 0.1.44 → 0.1.46
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/.turbo/turbo-build.log +11 -11
- package/CHANGELOG.md +16 -0
- package/dist/index.bundle.js +10 -10
- package/dist/index.d.mts +81 -86
- package/dist/index.d.ts +81 -86
- package/dist/index.js +371 -418
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +268 -312
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/NoyaStateContext.tsx +96 -95
- package/src/WebSocketConnection.ts +153 -0
- package/src/components/ErrorOverlay.tsx +3 -3
- package/src/hooks.ts +74 -191
- package/src/index.ts +2 -0
- package/src/inspector/StateInspector.tsx +83 -45
- package/src/inspector/useStateInspector.tsx +11 -55
- package/src/noyaApp.ts +36 -95
- package/src/globals.ts +0 -4
package/src/noyaApp.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-globals */
|
|
2
2
|
import {
|
|
3
|
-
ConnectedUsersManager,
|
|
4
3
|
createOrCastValue,
|
|
5
4
|
findAllDefs,
|
|
5
|
+
isEmbeddedApp,
|
|
6
6
|
localStorageSync,
|
|
7
|
+
parentFrameSync,
|
|
7
8
|
Static,
|
|
8
9
|
stubSync,
|
|
9
10
|
SyncAdapter,
|
|
@@ -11,7 +12,7 @@ import {
|
|
|
11
12
|
TypeGuard,
|
|
12
13
|
webSocketSync,
|
|
13
14
|
} from "@noya-app/state-manager";
|
|
14
|
-
import {
|
|
15
|
+
import { useMemo, useState } from "react";
|
|
15
16
|
import { useMultiplayerState, UseMultiplayerStateOptions } from "./hooks";
|
|
16
17
|
import { StateInspectorOptions } from "./inspector/StateInspector";
|
|
17
18
|
|
|
@@ -94,92 +95,24 @@ export function getAppData<State>(
|
|
|
94
95
|
return appData as AppData<State>;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
export type IframeToHostMessage =
|
|
98
|
-
| {
|
|
99
|
-
type: "application.setMenuItems";
|
|
100
|
-
payload: {
|
|
101
|
-
menuItems: any[];
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
| {
|
|
105
|
-
type: "multiplayer.setUsers";
|
|
106
|
-
payload: {
|
|
107
|
-
users: any[];
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export type HostToIframeMessage = {
|
|
112
|
-
type: "application.selectMenuItem";
|
|
113
|
-
payload: {
|
|
114
|
-
value: string;
|
|
115
|
-
};
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export function useBroadcastConnectedUsers(
|
|
119
|
-
connectedUsersManager: ConnectedUsersManager
|
|
120
|
-
) {
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
if (!isEmbeddedApp()) return;
|
|
123
|
-
|
|
124
|
-
const unsubscribe = connectedUsersManager.connectedUsers$.subscribe(
|
|
125
|
-
(users) => {
|
|
126
|
-
const message: IframeToHostMessage = {
|
|
127
|
-
type: "multiplayer.setUsers",
|
|
128
|
-
payload: { users },
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
parent?.postMessage(message, "*");
|
|
132
|
-
}
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
return unsubscribe;
|
|
136
|
-
}, [connectedUsersManager]);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function useBroadcastMenuItems<T>(
|
|
140
|
-
menuItems: any[],
|
|
141
|
-
handleSelectMenuItem: (value: T) => void
|
|
142
|
-
) {
|
|
143
|
-
useEffect(() => {
|
|
144
|
-
if (!isEmbeddedApp()) return;
|
|
145
|
-
|
|
146
|
-
const message: IframeToHostMessage = {
|
|
147
|
-
type: "application.setMenuItems",
|
|
148
|
-
payload: { menuItems },
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
parent?.postMessage(message, "*");
|
|
152
|
-
|
|
153
|
-
const listener = (event: MessageEvent<HostToIframeMessage>) => {
|
|
154
|
-
if (event.data.type === "application.selectMenuItem") {
|
|
155
|
-
handleSelectMenuItem(event.data.payload.value as T);
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
window.addEventListener("message", listener);
|
|
160
|
-
|
|
161
|
-
return () => {
|
|
162
|
-
window.removeEventListener("message", listener);
|
|
163
|
-
};
|
|
164
|
-
}, [handleSelectMenuItem, menuItems]);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export function isEmbeddedApp() {
|
|
168
|
-
return window.self !== window.top;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
98
|
export type UseNoyaStateOptions<
|
|
172
99
|
S,
|
|
173
|
-
M =
|
|
100
|
+
M extends object = object,
|
|
174
101
|
E extends object = object,
|
|
175
|
-
|
|
102
|
+
MenuT extends string = string,
|
|
103
|
+
> = UseMultiplayerStateOptions<S, M, E, MenuT> & {
|
|
176
104
|
offlineStorageKey?: string;
|
|
177
105
|
};
|
|
178
106
|
|
|
179
|
-
export type UseNoyaStateResult<
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
107
|
+
export type UseNoyaStateResult<
|
|
108
|
+
S,
|
|
109
|
+
M extends object = object,
|
|
110
|
+
E extends object = object,
|
|
111
|
+
MenuT extends string = string,
|
|
112
|
+
> = [
|
|
113
|
+
ReturnType<typeof useMultiplayerState<S, M, E, MenuT>>[0],
|
|
114
|
+
ReturnType<typeof useMultiplayerState<S, M, E, MenuT>>[1],
|
|
115
|
+
ReturnType<typeof useMultiplayerState<S, M, E, MenuT>>[2] & {
|
|
183
116
|
theme: AppTheme;
|
|
184
117
|
viewType: AppViewType;
|
|
185
118
|
},
|
|
@@ -196,9 +129,15 @@ export type UseNoyaStateResult<S, M = void, E extends object = object> = [
|
|
|
196
129
|
*/
|
|
197
130
|
export function useNoyaState<
|
|
198
131
|
S,
|
|
199
|
-
M =
|
|
132
|
+
M extends object = object,
|
|
200
133
|
E extends object = object,
|
|
201
|
-
|
|
134
|
+
MenuT extends string = string,
|
|
135
|
+
O extends UseNoyaStateOptions<S, M, E, MenuT> = UseNoyaStateOptions<
|
|
136
|
+
S,
|
|
137
|
+
M,
|
|
138
|
+
E,
|
|
139
|
+
MenuT
|
|
140
|
+
>,
|
|
202
141
|
>(
|
|
203
142
|
...args:
|
|
204
143
|
| [
|
|
@@ -211,7 +150,8 @@ export function useNoyaState<
|
|
|
211
150
|
): UseNoyaStateResult<
|
|
212
151
|
O["schema"] extends TSchema ? Static<O["schema"]> : S,
|
|
213
152
|
M,
|
|
214
|
-
E
|
|
153
|
+
E,
|
|
154
|
+
MenuT
|
|
215
155
|
> {
|
|
216
156
|
const [initialState, options] =
|
|
217
157
|
typeof args[0] === "object" &&
|
|
@@ -236,13 +176,15 @@ export function useNoyaState<
|
|
|
236
176
|
});
|
|
237
177
|
});
|
|
238
178
|
|
|
239
|
-
const sync = useMemo((): SyncAdapter<S, M, E> => {
|
|
240
|
-
return
|
|
241
|
-
?
|
|
242
|
-
:
|
|
243
|
-
?
|
|
244
|
-
:
|
|
245
|
-
|
|
179
|
+
const sync = useMemo((): SyncAdapter<S, M, E, MenuT> => {
|
|
180
|
+
return isEmbeddedApp()
|
|
181
|
+
? parentFrameSync<S, M, E, MenuT>({ debug: options?.debug })
|
|
182
|
+
: multiplayerUrl
|
|
183
|
+
? webSocketSync<S, M, E, MenuT>(multiplayerUrl)
|
|
184
|
+
: options?.offlineStorageKey
|
|
185
|
+
? localStorageSync<S, M, E, MenuT>({ key: options.offlineStorageKey })
|
|
186
|
+
: stubSync<S, M, E, MenuT>();
|
|
187
|
+
}, [multiplayerUrl, options?.offlineStorageKey, options?.debug]);
|
|
246
188
|
|
|
247
189
|
const result = useMultiplayerState(noyaAppInitialState as S, {
|
|
248
190
|
...options,
|
|
@@ -252,8 +194,6 @@ export function useNoyaState<
|
|
|
252
194
|
|
|
253
195
|
const [, , extras] = result;
|
|
254
196
|
|
|
255
|
-
useBroadcastConnectedUsers(extras.connectedUsersManager);
|
|
256
|
-
|
|
257
197
|
const mergedExtras = useMemo(() => {
|
|
258
198
|
return { ...extras, theme, viewType } as const;
|
|
259
199
|
}, [extras, theme, viewType]);
|
|
@@ -261,7 +201,8 @@ export function useNoyaState<
|
|
|
261
201
|
return [result[0], result[1], mergedExtras] as UseNoyaStateResult<
|
|
262
202
|
O["schema"] extends TSchema ? Static<O["schema"]> : S,
|
|
263
203
|
M,
|
|
264
|
-
E
|
|
204
|
+
E,
|
|
205
|
+
MenuT
|
|
265
206
|
>;
|
|
266
207
|
}
|
|
267
208
|
|
package/src/globals.ts
DELETED