@noya-app/noya-multiplayer-react 0.1.38 → 0.1.39
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 -12
- package/CHANGELOG.md +9 -0
- package/dist/index.bundle.js +7 -7
- package/dist/index.d.mts +52 -16
- package/dist/index.d.ts +52 -16
- package/dist/index.js +120 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/NoyaStateContext.tsx +218 -0
- package/src/__tests__/NoyaStateContext.test.ts +43 -0
- package/src/hooks.ts +12 -3
- package/src/index.ts +1 -0
- package/src/useObservable.ts +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as _noya_app_state_manager from '@noya-app/state-manager';
|
|
2
|
-
import { MultiplayerUser, EphemeralUserData, ConnectionEvent, Task, MultiplayerStateManager, AssetManager, StateManager,
|
|
2
|
+
import { MultiplayerUser, EphemeralUserData, ConnectionEvent, Task, MultiplayerStateManager, AssetManager, StateManager, StateManagerOptions, MultiplayerStateManagerOptions, SyncAdapterOptions, TSchema, Static } from '@noya-app/state-manager';
|
|
3
3
|
export * from '@noya-app/state-manager';
|
|
4
|
-
import React, { SetStateAction } from 'react';
|
|
5
|
-
import {
|
|
4
|
+
import React, { ComponentPropsWithoutRef, SetStateAction } from 'react';
|
|
5
|
+
import { GetAtPath, ObservableOptions, PathKey, Observable } from '@noya-app/observable';
|
|
6
6
|
|
|
7
7
|
declare function getAvatarInitials(name: string): string;
|
|
8
8
|
/**
|
|
@@ -27,11 +27,12 @@ type UserPointerProps<E extends UserPointerData> = {
|
|
|
27
27
|
ephemeralUserData: EphemeralUserData<E>;
|
|
28
28
|
hideAfter?: number;
|
|
29
29
|
};
|
|
30
|
-
declare const
|
|
30
|
+
declare const UserPointer_: <E extends UserPointerData>({ user, ephemeralUserData, hideAfter, }: UserPointerProps<E>) => React.JSX.Element | null;
|
|
31
|
+
declare const UserPointer: typeof UserPointer_;
|
|
31
32
|
declare const UserPointerContainer: React.NamedExoticComponent<{
|
|
32
33
|
point: Point;
|
|
33
34
|
children: React.ReactNode;
|
|
34
|
-
show?: boolean
|
|
35
|
+
show?: boolean;
|
|
35
36
|
}>;
|
|
36
37
|
declare const UserNameTag: React.NamedExoticComponent<{
|
|
37
38
|
background: string;
|
|
@@ -43,7 +44,7 @@ type UserPointersOverlayProps<E extends UserPointerData> = {
|
|
|
43
44
|
};
|
|
44
45
|
declare const UserPointersOverlay: React.MemoExoticComponent<(<E extends UserPointerData>({ connectedUsers, ephemeralUserData }: UserPointersOverlayProps<E>) => React.JSX.Element)>;
|
|
45
46
|
declare const UserPointerIcon: React.NamedExoticComponent<{
|
|
46
|
-
color?: string
|
|
47
|
+
color?: string;
|
|
47
48
|
}>;
|
|
48
49
|
|
|
49
50
|
type StateInspectorAnchor = "left" | "right" | "bottom left" | "bottom right";
|
|
@@ -53,19 +54,19 @@ type StateInspectorOptions = {
|
|
|
53
54
|
};
|
|
54
55
|
declare const StateInspector: React.MemoExoticComponent<(<S, M, E>({ state, connectionEvents, connectedUsers, tasks, userId, unstyled, colorScheme, multiplayerStateManager, assetManager, ephemeralUserData, anchor, forceInit, ...props }: {
|
|
55
56
|
state: S;
|
|
56
|
-
connectionEvents?: ConnectionEvent<S>[]
|
|
57
|
-
connectedUsers?: MultiplayerUser[]
|
|
58
|
-
tasks?: Task[]
|
|
59
|
-
userId?: string
|
|
60
|
-
unstyled?: boolean
|
|
57
|
+
connectionEvents?: ConnectionEvent<S>[];
|
|
58
|
+
connectedUsers?: MultiplayerUser[];
|
|
59
|
+
tasks?: Task[];
|
|
60
|
+
userId?: string;
|
|
61
|
+
unstyled?: boolean;
|
|
61
62
|
multiplayerStateManager: MultiplayerStateManager<S, M>;
|
|
62
63
|
assetManager: AssetManager;
|
|
63
64
|
ephemeralUserData: EphemeralUserData<E>;
|
|
64
65
|
forceInit: () => void;
|
|
65
|
-
} & StateInspectorOptions &
|
|
66
|
+
} & StateInspectorOptions & ComponentPropsWithoutRef<"div">) => React.JSX.Element | null)>;
|
|
66
67
|
|
|
67
68
|
declare function useSyncStateManager<S, M>(stateManager: StateManager<S, M>): S;
|
|
68
|
-
declare function useSyncStateManager<S, M, P extends string>(stateManager: StateManager<S, M>, path: P):
|
|
69
|
+
declare function useSyncStateManager<S, M, P extends string>(stateManager: StateManager<S, M>, path: P): GetAtPath<S, P>;
|
|
69
70
|
declare function useManagedState<S, M = void>(createInitialState: () => S, options?: StateManagerOptions<S, M>): readonly [S, (...args: M extends {} ? [metadata: M, action: SetStateAction<S>] : [action: SetStateAction<S>]) => void, StateManager<S, M>];
|
|
70
71
|
declare function useManagedHistory<S, M = void>(stateManager: StateManager<S, M>): _noya_app_state_manager.HistorySnapshot<S, M>;
|
|
71
72
|
declare function useSyncMultiplayerStateManager<S, M = void>(multiplayerStateManager: MultiplayerStateManager<S, M>): S;
|
|
@@ -90,7 +91,8 @@ declare function useMultiplayerState<S, M = void, E = void>(initialState: S | ((
|
|
|
90
91
|
ephemeralUserData: EphemeralUserData<E>;
|
|
91
92
|
multiplayerStateManager: MultiplayerStateManager<S, M>;
|
|
92
93
|
assets: _noya_app_state_manager.Asset[];
|
|
93
|
-
|
|
94
|
+
assetManager: AssetManager;
|
|
95
|
+
createAsset: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
|
|
94
96
|
deleteAsset: (id: string) => Promise<void>;
|
|
95
97
|
}];
|
|
96
98
|
|
|
@@ -162,7 +164,41 @@ declare function useNoyaState<S, M = void, E = void, O extends UseNoyaStateOptio
|
|
|
162
164
|
schema: TSchema;
|
|
163
165
|
}]): UseNoyaStateResult<O["schema"] extends TSchema ? Static<O["schema"]> : S, M, E>;
|
|
164
166
|
|
|
167
|
+
interface NoyaStateProviderProps<Schema extends TSchema, M, E> extends Omit<UseNoyaStateOptions<Static<Schema>, M, E>, "schema"> {
|
|
168
|
+
children: React.ReactNode;
|
|
169
|
+
initialState?: Static<Schema>;
|
|
170
|
+
}
|
|
171
|
+
declare function useAssets(): _noya_app_state_manager.Asset[];
|
|
172
|
+
declare function useAsset(id: string | undefined): _noya_app_state_manager.Asset | undefined;
|
|
173
|
+
declare function useAssetManager(): {
|
|
174
|
+
create: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
|
|
175
|
+
delete: (id: string) => Promise<void>;
|
|
176
|
+
};
|
|
177
|
+
declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({ schema, }: {
|
|
178
|
+
schema: Schema;
|
|
179
|
+
}): {
|
|
180
|
+
Context: React.Context<{
|
|
181
|
+
ms: MultiplayerStateManager<Static<Schema>, M>;
|
|
182
|
+
assetManager: AssetManager;
|
|
183
|
+
} | undefined>;
|
|
184
|
+
Provider: ({ children, initialState, ...options }: NoyaStateProviderProps<Schema, M, E>) => React.JSX.Element;
|
|
185
|
+
useValue: {
|
|
186
|
+
(): Static<Schema>;
|
|
187
|
+
<A>(selector: (value: Static<Schema>) => A, options?: Pick<ObservableOptions, "isEqual">): A;
|
|
188
|
+
<P extends PathKey[] | string>(path: P): GetAtPath<Static<Schema>, P>;
|
|
189
|
+
};
|
|
190
|
+
useSetValue: {
|
|
191
|
+
(): M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void;
|
|
192
|
+
<P extends PathKey[] | string>(path: P): M extends {} ? (metadata: M, value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void : (value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void;
|
|
193
|
+
};
|
|
194
|
+
useValueState: {
|
|
195
|
+
(): [Static<Schema>, M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void];
|
|
196
|
+
<A>(selector: (value: Static<Schema>) => A, options?: ObservableOptions): [A, M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void];
|
|
197
|
+
<P extends PathKey[] | string>(path?: P): [GetAtPath<Static<Schema>, P>, M extends {} ? (metadata: M, value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void : (value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void];
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
|
|
165
201
|
declare function useObservable<T>(observable: Observable<T>): T;
|
|
166
|
-
declare function useObservable<T, P extends PathKey[]>(observable: Observable<T>, path: P):
|
|
202
|
+
declare function useObservable<T, P extends PathKey[] | string>(observable: Observable<T>, path: P): GetAtPath<T, P>;
|
|
167
203
|
|
|
168
|
-
export { type AppData, type AppTheme, type AppViewType, type GetAppDataOptions, type HostToIframeMessage, type IframeToHostMessage, StateInspector, type StateInspectorAnchor, type StateInspectorOptions, type UseMultiplayerStateOptions, type UseNoyaStateOptions, type UseNoyaStateResult, UserNameTag, UserPointer, UserPointerContainer, type UserPointerData, UserPointerIcon, type UserPointerProps, UserPointersOverlay, type UserPointersOverlayProps, createDefaultAppData, enforceSchema, getAppData, getAvatarInitials, getAvatarStyle, isEmbeddedApp, parseAppDataParameter, useBroadcastConnectedUsers, useBroadcastMenuItems, useEphemeralUserData, useManagedHistory, useManagedState, useMultiplayerState, useNoyaState, useObservable, useSyncMultiplayerStateManager, useSyncStateManager };
|
|
204
|
+
export { type AppData, type AppTheme, type AppViewType, type GetAppDataOptions, type HostToIframeMessage, type IframeToHostMessage, StateInspector, type StateInspectorAnchor, type StateInspectorOptions, type UseMultiplayerStateOptions, type UseNoyaStateOptions, type UseNoyaStateResult, UserNameTag, UserPointer, UserPointerContainer, type UserPointerData, UserPointerIcon, type UserPointerProps, UserPointersOverlay, type UserPointersOverlayProps, createDefaultAppData, createNoyaContext, enforceSchema, getAppData, getAvatarInitials, getAvatarStyle, isEmbeddedApp, parseAppDataParameter, useAsset, useAssetManager, useAssets, useBroadcastConnectedUsers, useBroadcastMenuItems, useEphemeralUserData, useManagedHistory, useManagedState, useMultiplayerState, useNoyaState, useObservable, useSyncMultiplayerStateManager, useSyncStateManager };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as _noya_app_state_manager from '@noya-app/state-manager';
|
|
2
|
-
import { MultiplayerUser, EphemeralUserData, ConnectionEvent, Task, MultiplayerStateManager, AssetManager, StateManager,
|
|
2
|
+
import { MultiplayerUser, EphemeralUserData, ConnectionEvent, Task, MultiplayerStateManager, AssetManager, StateManager, StateManagerOptions, MultiplayerStateManagerOptions, SyncAdapterOptions, TSchema, Static } from '@noya-app/state-manager';
|
|
3
3
|
export * from '@noya-app/state-manager';
|
|
4
|
-
import React, { SetStateAction } from 'react';
|
|
5
|
-
import {
|
|
4
|
+
import React, { ComponentPropsWithoutRef, SetStateAction } from 'react';
|
|
5
|
+
import { GetAtPath, ObservableOptions, PathKey, Observable } from '@noya-app/observable';
|
|
6
6
|
|
|
7
7
|
declare function getAvatarInitials(name: string): string;
|
|
8
8
|
/**
|
|
@@ -27,11 +27,12 @@ type UserPointerProps<E extends UserPointerData> = {
|
|
|
27
27
|
ephemeralUserData: EphemeralUserData<E>;
|
|
28
28
|
hideAfter?: number;
|
|
29
29
|
};
|
|
30
|
-
declare const
|
|
30
|
+
declare const UserPointer_: <E extends UserPointerData>({ user, ephemeralUserData, hideAfter, }: UserPointerProps<E>) => React.JSX.Element | null;
|
|
31
|
+
declare const UserPointer: typeof UserPointer_;
|
|
31
32
|
declare const UserPointerContainer: React.NamedExoticComponent<{
|
|
32
33
|
point: Point;
|
|
33
34
|
children: React.ReactNode;
|
|
34
|
-
show?: boolean
|
|
35
|
+
show?: boolean;
|
|
35
36
|
}>;
|
|
36
37
|
declare const UserNameTag: React.NamedExoticComponent<{
|
|
37
38
|
background: string;
|
|
@@ -43,7 +44,7 @@ type UserPointersOverlayProps<E extends UserPointerData> = {
|
|
|
43
44
|
};
|
|
44
45
|
declare const UserPointersOverlay: React.MemoExoticComponent<(<E extends UserPointerData>({ connectedUsers, ephemeralUserData }: UserPointersOverlayProps<E>) => React.JSX.Element)>;
|
|
45
46
|
declare const UserPointerIcon: React.NamedExoticComponent<{
|
|
46
|
-
color?: string
|
|
47
|
+
color?: string;
|
|
47
48
|
}>;
|
|
48
49
|
|
|
49
50
|
type StateInspectorAnchor = "left" | "right" | "bottom left" | "bottom right";
|
|
@@ -53,19 +54,19 @@ type StateInspectorOptions = {
|
|
|
53
54
|
};
|
|
54
55
|
declare const StateInspector: React.MemoExoticComponent<(<S, M, E>({ state, connectionEvents, connectedUsers, tasks, userId, unstyled, colorScheme, multiplayerStateManager, assetManager, ephemeralUserData, anchor, forceInit, ...props }: {
|
|
55
56
|
state: S;
|
|
56
|
-
connectionEvents?: ConnectionEvent<S>[]
|
|
57
|
-
connectedUsers?: MultiplayerUser[]
|
|
58
|
-
tasks?: Task[]
|
|
59
|
-
userId?: string
|
|
60
|
-
unstyled?: boolean
|
|
57
|
+
connectionEvents?: ConnectionEvent<S>[];
|
|
58
|
+
connectedUsers?: MultiplayerUser[];
|
|
59
|
+
tasks?: Task[];
|
|
60
|
+
userId?: string;
|
|
61
|
+
unstyled?: boolean;
|
|
61
62
|
multiplayerStateManager: MultiplayerStateManager<S, M>;
|
|
62
63
|
assetManager: AssetManager;
|
|
63
64
|
ephemeralUserData: EphemeralUserData<E>;
|
|
64
65
|
forceInit: () => void;
|
|
65
|
-
} & StateInspectorOptions &
|
|
66
|
+
} & StateInspectorOptions & ComponentPropsWithoutRef<"div">) => React.JSX.Element | null)>;
|
|
66
67
|
|
|
67
68
|
declare function useSyncStateManager<S, M>(stateManager: StateManager<S, M>): S;
|
|
68
|
-
declare function useSyncStateManager<S, M, P extends string>(stateManager: StateManager<S, M>, path: P):
|
|
69
|
+
declare function useSyncStateManager<S, M, P extends string>(stateManager: StateManager<S, M>, path: P): GetAtPath<S, P>;
|
|
69
70
|
declare function useManagedState<S, M = void>(createInitialState: () => S, options?: StateManagerOptions<S, M>): readonly [S, (...args: M extends {} ? [metadata: M, action: SetStateAction<S>] : [action: SetStateAction<S>]) => void, StateManager<S, M>];
|
|
70
71
|
declare function useManagedHistory<S, M = void>(stateManager: StateManager<S, M>): _noya_app_state_manager.HistorySnapshot<S, M>;
|
|
71
72
|
declare function useSyncMultiplayerStateManager<S, M = void>(multiplayerStateManager: MultiplayerStateManager<S, M>): S;
|
|
@@ -90,7 +91,8 @@ declare function useMultiplayerState<S, M = void, E = void>(initialState: S | ((
|
|
|
90
91
|
ephemeralUserData: EphemeralUserData<E>;
|
|
91
92
|
multiplayerStateManager: MultiplayerStateManager<S, M>;
|
|
92
93
|
assets: _noya_app_state_manager.Asset[];
|
|
93
|
-
|
|
94
|
+
assetManager: AssetManager;
|
|
95
|
+
createAsset: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
|
|
94
96
|
deleteAsset: (id: string) => Promise<void>;
|
|
95
97
|
}];
|
|
96
98
|
|
|
@@ -162,7 +164,41 @@ declare function useNoyaState<S, M = void, E = void, O extends UseNoyaStateOptio
|
|
|
162
164
|
schema: TSchema;
|
|
163
165
|
}]): UseNoyaStateResult<O["schema"] extends TSchema ? Static<O["schema"]> : S, M, E>;
|
|
164
166
|
|
|
167
|
+
interface NoyaStateProviderProps<Schema extends TSchema, M, E> extends Omit<UseNoyaStateOptions<Static<Schema>, M, E>, "schema"> {
|
|
168
|
+
children: React.ReactNode;
|
|
169
|
+
initialState?: Static<Schema>;
|
|
170
|
+
}
|
|
171
|
+
declare function useAssets(): _noya_app_state_manager.Asset[];
|
|
172
|
+
declare function useAsset(id: string | undefined): _noya_app_state_manager.Asset | undefined;
|
|
173
|
+
declare function useAssetManager(): {
|
|
174
|
+
create: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
|
|
175
|
+
delete: (id: string) => Promise<void>;
|
|
176
|
+
};
|
|
177
|
+
declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({ schema, }: {
|
|
178
|
+
schema: Schema;
|
|
179
|
+
}): {
|
|
180
|
+
Context: React.Context<{
|
|
181
|
+
ms: MultiplayerStateManager<Static<Schema>, M>;
|
|
182
|
+
assetManager: AssetManager;
|
|
183
|
+
} | undefined>;
|
|
184
|
+
Provider: ({ children, initialState, ...options }: NoyaStateProviderProps<Schema, M, E>) => React.JSX.Element;
|
|
185
|
+
useValue: {
|
|
186
|
+
(): Static<Schema>;
|
|
187
|
+
<A>(selector: (value: Static<Schema>) => A, options?: Pick<ObservableOptions, "isEqual">): A;
|
|
188
|
+
<P extends PathKey[] | string>(path: P): GetAtPath<Static<Schema>, P>;
|
|
189
|
+
};
|
|
190
|
+
useSetValue: {
|
|
191
|
+
(): M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void;
|
|
192
|
+
<P extends PathKey[] | string>(path: P): M extends {} ? (metadata: M, value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void : (value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void;
|
|
193
|
+
};
|
|
194
|
+
useValueState: {
|
|
195
|
+
(): [Static<Schema>, M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void];
|
|
196
|
+
<A>(selector: (value: Static<Schema>) => A, options?: ObservableOptions): [A, M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void];
|
|
197
|
+
<P extends PathKey[] | string>(path?: P): [GetAtPath<Static<Schema>, P>, M extends {} ? (metadata: M, value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void : (value: SetStateAction<GetAtPath<Static<Schema>, P>>) => void];
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
|
|
165
201
|
declare function useObservable<T>(observable: Observable<T>): T;
|
|
166
|
-
declare function useObservable<T, P extends PathKey[]>(observable: Observable<T>, path: P):
|
|
202
|
+
declare function useObservable<T, P extends PathKey[] | string>(observable: Observable<T>, path: P): GetAtPath<T, P>;
|
|
167
203
|
|
|
168
|
-
export { type AppData, type AppTheme, type AppViewType, type GetAppDataOptions, type HostToIframeMessage, type IframeToHostMessage, StateInspector, type StateInspectorAnchor, type StateInspectorOptions, type UseMultiplayerStateOptions, type UseNoyaStateOptions, type UseNoyaStateResult, UserNameTag, UserPointer, UserPointerContainer, type UserPointerData, UserPointerIcon, type UserPointerProps, UserPointersOverlay, type UserPointersOverlayProps, createDefaultAppData, enforceSchema, getAppData, getAvatarInitials, getAvatarStyle, isEmbeddedApp, parseAppDataParameter, useBroadcastConnectedUsers, useBroadcastMenuItems, useEphemeralUserData, useManagedHistory, useManagedState, useMultiplayerState, useNoyaState, useObservable, useSyncMultiplayerStateManager, useSyncStateManager };
|
|
204
|
+
export { type AppData, type AppTheme, type AppViewType, type GetAppDataOptions, type HostToIframeMessage, type IframeToHostMessage, StateInspector, type StateInspectorAnchor, type StateInspectorOptions, type UseMultiplayerStateOptions, type UseNoyaStateOptions, type UseNoyaStateResult, UserNameTag, UserPointer, UserPointerContainer, type UserPointerData, UserPointerIcon, type UserPointerProps, UserPointersOverlay, type UserPointersOverlayProps, createDefaultAppData, createNoyaContext, enforceSchema, getAppData, getAvatarInitials, getAvatarStyle, isEmbeddedApp, parseAppDataParameter, useAsset, useAssetManager, useAssets, useBroadcastConnectedUsers, useBroadcastMenuItems, useEphemeralUserData, useManagedHistory, useManagedState, useMultiplayerState, useNoyaState, useObservable, useSyncMultiplayerStateManager, useSyncStateManager };
|
package/dist/index.js
CHANGED
|
@@ -38,12 +38,16 @@ __export(src_exports, {
|
|
|
38
38
|
UserPointerIcon: () => UserPointerIcon,
|
|
39
39
|
UserPointersOverlay: () => UserPointersOverlay,
|
|
40
40
|
createDefaultAppData: () => createDefaultAppData,
|
|
41
|
+
createNoyaContext: () => createNoyaContext,
|
|
41
42
|
enforceSchema: () => enforceSchema,
|
|
42
43
|
getAppData: () => getAppData,
|
|
43
44
|
getAvatarInitials: () => getAvatarInitials,
|
|
44
45
|
getAvatarStyle: () => getAvatarStyle,
|
|
45
46
|
isEmbeddedApp: () => isEmbeddedApp,
|
|
46
47
|
parseAppDataParameter: () => parseAppDataParameter,
|
|
48
|
+
useAsset: () => useAsset,
|
|
49
|
+
useAssetManager: () => useAssetManager,
|
|
50
|
+
useAssets: () => useAssets,
|
|
47
51
|
useBroadcastConnectedUsers: () => useBroadcastConnectedUsers,
|
|
48
52
|
useBroadcastMenuItems: () => useBroadcastMenuItems,
|
|
49
53
|
useEphemeralUserData: () => useEphemeralUserData,
|
|
@@ -111,7 +115,7 @@ var import_react2 = __toESM(require("react"));
|
|
|
111
115
|
var import_react = require("react");
|
|
112
116
|
function useObservable(observable, path) {
|
|
113
117
|
const { get, listen } = (0, import_react.useMemo)(() => {
|
|
114
|
-
const listen2 = path ? (callback) => observable.
|
|
118
|
+
const listen2 = path ? (callback) => observable.subscribe(path, callback) : observable.subscribe;
|
|
115
119
|
const get2 = path ? () => observable.get(path) : observable.get;
|
|
116
120
|
return { get: get2, listen: listen2 };
|
|
117
121
|
}, [observable, path]);
|
|
@@ -1381,6 +1385,7 @@ function useMultiplayerState(initialState, options) {
|
|
|
1381
1385
|
ephemeralUserData,
|
|
1382
1386
|
multiplayerStateManager,
|
|
1383
1387
|
assets,
|
|
1388
|
+
assetManager,
|
|
1384
1389
|
createAsset: assetManager.create,
|
|
1385
1390
|
deleteAsset: assetManager.delete
|
|
1386
1391
|
// evaluate: async (code: string) => {
|
|
@@ -1450,7 +1455,15 @@ function useMultiplayerState(initialState, options) {
|
|
|
1450
1455
|
const [unrecoverableError, setUnrecoverableError] = (0, import_react7.useState)();
|
|
1451
1456
|
(0, import_react7.useEffect)(() => {
|
|
1452
1457
|
return multiplayerStateManager.errorEmitter.addListener((error) => {
|
|
1453
|
-
|
|
1458
|
+
switch (error.reason) {
|
|
1459
|
+
case "invalidDataType":
|
|
1460
|
+
case "schemaMismatch":
|
|
1461
|
+
case "outdatedSchema":
|
|
1462
|
+
case "dataMigrationFailure":
|
|
1463
|
+
case "schemaMigration":
|
|
1464
|
+
setUnrecoverableError(error);
|
|
1465
|
+
break;
|
|
1466
|
+
}
|
|
1454
1467
|
});
|
|
1455
1468
|
}, [multiplayerStateManager]);
|
|
1456
1469
|
(0, import_react7.useEffect)(() => {
|
|
@@ -1589,6 +1602,107 @@ function useNoyaState(...args) {
|
|
|
1589
1602
|
}, [extras, theme, viewType]);
|
|
1590
1603
|
return [result[0], result[1], mergedExtras];
|
|
1591
1604
|
}
|
|
1605
|
+
|
|
1606
|
+
// src/NoyaStateContext.tsx
|
|
1607
|
+
var import_react9 = __toESM(require("react"));
|
|
1608
|
+
var AnyNoyaStateContext = (0, import_react9.createContext)(void 0);
|
|
1609
|
+
function useAnyNoyaStateContext() {
|
|
1610
|
+
const value = (0, import_react9.useContext)(AnyNoyaStateContext);
|
|
1611
|
+
if (!value) {
|
|
1612
|
+
throw new Error(
|
|
1613
|
+
"useNoyaStateContext must be used within a NoyaStateProvider"
|
|
1614
|
+
);
|
|
1615
|
+
}
|
|
1616
|
+
return value;
|
|
1617
|
+
}
|
|
1618
|
+
function useAssets() {
|
|
1619
|
+
const { assetManager } = useAnyNoyaStateContext();
|
|
1620
|
+
return useObservable(assetManager.assets$);
|
|
1621
|
+
}
|
|
1622
|
+
function useAsset(id) {
|
|
1623
|
+
const { assetManager } = useAnyNoyaStateContext();
|
|
1624
|
+
const observable = (0, import_react9.useMemo)(
|
|
1625
|
+
() => assetManager.assets$.map((assets) => assets.find((a) => a.id === id)),
|
|
1626
|
+
[assetManager, id]
|
|
1627
|
+
);
|
|
1628
|
+
return useObservable(observable);
|
|
1629
|
+
}
|
|
1630
|
+
function useAssetManager() {
|
|
1631
|
+
const { assetManager } = useAnyNoyaStateContext();
|
|
1632
|
+
return (0, import_react9.useMemo)(
|
|
1633
|
+
() => ({
|
|
1634
|
+
create: assetManager.create,
|
|
1635
|
+
delete: assetManager.delete
|
|
1636
|
+
}),
|
|
1637
|
+
[assetManager]
|
|
1638
|
+
);
|
|
1639
|
+
}
|
|
1640
|
+
function createNoyaContext({
|
|
1641
|
+
schema
|
|
1642
|
+
}) {
|
|
1643
|
+
const NoyaStateContext = AnyNoyaStateContext;
|
|
1644
|
+
function NoyaStateProvider({
|
|
1645
|
+
children,
|
|
1646
|
+
initialState,
|
|
1647
|
+
...options
|
|
1648
|
+
}) {
|
|
1649
|
+
const [, , { multiplayerStateManager, assetManager }] = useNoyaState(
|
|
1650
|
+
initialState,
|
|
1651
|
+
{
|
|
1652
|
+
...options,
|
|
1653
|
+
schema
|
|
1654
|
+
}
|
|
1655
|
+
);
|
|
1656
|
+
const value = (0, import_react9.useMemo)(
|
|
1657
|
+
() => ({
|
|
1658
|
+
ms: multiplayerStateManager,
|
|
1659
|
+
assetManager
|
|
1660
|
+
}),
|
|
1661
|
+
[multiplayerStateManager, assetManager]
|
|
1662
|
+
);
|
|
1663
|
+
return /* @__PURE__ */ import_react9.default.createElement(AnyNoyaStateContext.Provider, { value }, children);
|
|
1664
|
+
}
|
|
1665
|
+
function useValue(path, options) {
|
|
1666
|
+
const { ms } = useAnyNoyaStateContext();
|
|
1667
|
+
let actualPath = typeof path === "function" || !path ? "" : path;
|
|
1668
|
+
const mappedObservable = (0, import_react9.useMemo)(() => {
|
|
1669
|
+
if (typeof path === "function") {
|
|
1670
|
+
return ms.optimisticState$.map(path, { isEqual: options?.isEqual });
|
|
1671
|
+
}
|
|
1672
|
+
return ms.optimisticState$;
|
|
1673
|
+
}, [ms, path, options?.isEqual]);
|
|
1674
|
+
const value = useObservable(mappedObservable, actualPath);
|
|
1675
|
+
return value;
|
|
1676
|
+
}
|
|
1677
|
+
function useSetValue(path) {
|
|
1678
|
+
const { ms } = useAnyNoyaStateContext();
|
|
1679
|
+
const setValue = (0, import_react9.useCallback)(
|
|
1680
|
+
(...args) => {
|
|
1681
|
+
const [metadata, value] = args.length === 2 ? args : [void 0, args[0]];
|
|
1682
|
+
const setStateAtPath = ms.setStateAtPath;
|
|
1683
|
+
setStateAtPath(metadata, path ?? "", value);
|
|
1684
|
+
},
|
|
1685
|
+
[ms, path]
|
|
1686
|
+
);
|
|
1687
|
+
return setValue;
|
|
1688
|
+
}
|
|
1689
|
+
function useValueState(...args) {
|
|
1690
|
+
const [selector, options, path] = args.length >= 1 && typeof args[0] === "function" ? [args[0], args[1], void 0] : [void 0, void 0, args[0]];
|
|
1691
|
+
const value = useValue(selector ?? path ?? "", options);
|
|
1692
|
+
const setValue = useSetValue(path ?? "");
|
|
1693
|
+
return [value, setValue];
|
|
1694
|
+
}
|
|
1695
|
+
return {
|
|
1696
|
+
Context: NoyaStateContext,
|
|
1697
|
+
Provider: NoyaStateProvider,
|
|
1698
|
+
useValue,
|
|
1699
|
+
useSetValue,
|
|
1700
|
+
useValueState
|
|
1701
|
+
// useAssets,
|
|
1702
|
+
// useAssetManager,
|
|
1703
|
+
// useAsset,
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1592
1706
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1593
1707
|
0 && (module.exports = {
|
|
1594
1708
|
StateInspector,
|
|
@@ -1598,12 +1712,16 @@ function useNoyaState(...args) {
|
|
|
1598
1712
|
UserPointerIcon,
|
|
1599
1713
|
UserPointersOverlay,
|
|
1600
1714
|
createDefaultAppData,
|
|
1715
|
+
createNoyaContext,
|
|
1601
1716
|
enforceSchema,
|
|
1602
1717
|
getAppData,
|
|
1603
1718
|
getAvatarInitials,
|
|
1604
1719
|
getAvatarStyle,
|
|
1605
1720
|
isEmbeddedApp,
|
|
1606
1721
|
parseAppDataParameter,
|
|
1722
|
+
useAsset,
|
|
1723
|
+
useAssetManager,
|
|
1724
|
+
useAssets,
|
|
1607
1725
|
useBroadcastConnectedUsers,
|
|
1608
1726
|
useBroadcastMenuItems,
|
|
1609
1727
|
useEphemeralUserData,
|