@noya-app/noya-multiplayer-react 0.1.27 → 0.1.28
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 +10 -10
- package/CHANGELOG.md +9 -0
- package/dist/index.d.mts +10 -9
- package/dist/index.d.ts +10 -9
- package/dist/index.js +41 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -30
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/noyaApp.test.ts +107 -0
- package/src/noyaApp.ts +55 -44
package/src/noyaApp.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-globals */
|
|
2
2
|
import {
|
|
3
3
|
createOrCastValue,
|
|
4
|
+
findAllDefs,
|
|
4
5
|
localStorageSync,
|
|
5
6
|
MultiplayerUser,
|
|
6
7
|
SyncAdapter,
|
|
@@ -34,46 +35,56 @@ export function createDefaultAppData<State>(
|
|
|
34
35
|
};
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
export
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
export type GetAppDataOptions = {
|
|
39
|
+
window?: { location: { hash: string } };
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function enforceSchema<State>(
|
|
43
|
+
initialState: State,
|
|
44
|
+
schema?: TSchema
|
|
45
|
+
): State {
|
|
46
|
+
return schema
|
|
47
|
+
? (createOrCastValue({
|
|
48
|
+
schema,
|
|
49
|
+
value: initialState,
|
|
50
|
+
defs: findAllDefs(schema),
|
|
51
|
+
}) as State)
|
|
52
|
+
: initialState;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function parseAppDataParameter(options?: GetAppDataOptions) {
|
|
56
|
+
const window = options?.window ?? globalThis;
|
|
57
|
+
const urlHash = (window.location.hash || "").replace(/^#/, "");
|
|
58
|
+
const params = new URLSearchParams(urlHash);
|
|
59
|
+
const data = params.get("data");
|
|
60
|
+
if (!data) return null;
|
|
42
61
|
try {
|
|
43
|
-
|
|
44
|
-
const params = new URLSearchParams(urlHash);
|
|
45
|
-
const data = params.get("data");
|
|
46
|
-
if (!data) {
|
|
47
|
-
return initialState ? createDefaultAppData(initialState) : undefined;
|
|
48
|
-
}
|
|
49
|
-
const parsed = JSON.parse(data);
|
|
50
|
-
if (!parsed.initialState) {
|
|
51
|
-
parsed.initialState = initialState;
|
|
52
|
-
}
|
|
53
|
-
return parsed;
|
|
62
|
+
return JSON.parse(data);
|
|
54
63
|
} catch (e) {
|
|
55
|
-
return
|
|
64
|
+
return null;
|
|
56
65
|
}
|
|
57
66
|
}
|
|
58
67
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
export function getAppData<State>(
|
|
69
|
+
initialState: State | (() => State),
|
|
70
|
+
schema?: TSchema,
|
|
71
|
+
options?: GetAppDataOptions
|
|
72
|
+
): AppData<State> {
|
|
73
|
+
const resolvedInitialState =
|
|
74
|
+
initialState instanceof Function ? initialState() : initialState;
|
|
62
75
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
initialState: S,
|
|
68
|
-
{ schema }: AppDataOptions = {}
|
|
69
|
-
) {
|
|
70
|
-
const appData = useMemo(() => {
|
|
71
|
-
return getAppData(
|
|
72
|
-
schema ? (createOrCastValue(schema, initialState) as S) : initialState
|
|
73
|
-
);
|
|
74
|
-
}, [initialState, schema]);
|
|
76
|
+
try {
|
|
77
|
+
const appData: AppData<unknown> =
|
|
78
|
+
parseAppDataParameter(options) ??
|
|
79
|
+
createDefaultAppData(resolvedInitialState);
|
|
75
80
|
|
|
76
|
-
|
|
81
|
+
return {
|
|
82
|
+
...appData,
|
|
83
|
+
initialState: enforceSchema(appData.initialState, schema) as State,
|
|
84
|
+
};
|
|
85
|
+
} catch (e) {
|
|
86
|
+
return createDefaultAppData(enforceSchema(resolvedInitialState, schema));
|
|
87
|
+
}
|
|
77
88
|
}
|
|
78
89
|
|
|
79
90
|
export type IframeToHostMessage =
|
|
@@ -148,17 +159,17 @@ export function useNoyaAppState<S, M = void, E = void>(
|
|
|
148
159
|
localStorageKey?: string;
|
|
149
160
|
}
|
|
150
161
|
) {
|
|
151
|
-
//
|
|
152
|
-
const [
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
+
// Keep these stable after initial render
|
|
163
|
+
const [
|
|
164
|
+
{
|
|
165
|
+
multiplayerUrl,
|
|
166
|
+
inspector,
|
|
167
|
+
initialState: noyaAppInitialState,
|
|
168
|
+
theme,
|
|
169
|
+
viewType,
|
|
170
|
+
},
|
|
171
|
+
] = useState(() => {
|
|
172
|
+
return getAppData(initialState, options.schema);
|
|
162
173
|
});
|
|
163
174
|
|
|
164
175
|
const sync = useMemo((): SyncAdapter<S, M, E> => {
|