@noya-app/noya-multiplayer-react 0.1.39 → 0.1.40
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 +14 -14
- package/CHANGELOG.md +6 -0
- package/dist/index.bundle.js +7 -7
- package/dist/index.d.ts +7 -5
- package/dist/index.js +10 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/NoyaStateContext.tsx +23 -7
- package/dist/index.d.mts +0 -204
package/src/NoyaStateContext.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import { GetAtPath, ObservableOptions, PathKey } from "@noya-app/observable";
|
|
|
2
2
|
import {
|
|
3
3
|
AssetManager,
|
|
4
4
|
MultiplayerStateManager,
|
|
5
|
+
StateManagerOptions,
|
|
5
6
|
Static,
|
|
6
7
|
TSchema,
|
|
7
8
|
} from "@noya-app/state-manager";
|
|
@@ -15,10 +16,10 @@ import React, {
|
|
|
15
16
|
import { useNoyaState, UseNoyaStateOptions } from "./noyaApp";
|
|
16
17
|
import { useObservable } from "./useObservable";
|
|
17
18
|
|
|
18
|
-
interface NoyaStateProviderProps<
|
|
19
|
-
extends Omit<UseNoyaStateOptions<
|
|
19
|
+
interface NoyaStateProviderProps<S, M, E>
|
|
20
|
+
extends Omit<UseNoyaStateOptions<S, M, E>, "schema" | "mergeHistoryEntries"> {
|
|
20
21
|
children: React.ReactNode;
|
|
21
|
-
initialState?:
|
|
22
|
+
initialState?: S;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
const AnyNoyaStateContext = createContext<
|
|
@@ -65,12 +66,19 @@ export function useAssetManager() {
|
|
|
65
66
|
|
|
66
67
|
export function createNoyaContext<Schema extends TSchema, M = void, E = void>({
|
|
67
68
|
schema,
|
|
69
|
+
mergeHistoryEntries,
|
|
68
70
|
}: {
|
|
69
71
|
schema: Schema;
|
|
72
|
+
mergeHistoryEntries?: StateManagerOptions<
|
|
73
|
+
Static<Schema>,
|
|
74
|
+
M
|
|
75
|
+
>["mergeHistoryEntries"];
|
|
70
76
|
}) {
|
|
77
|
+
type S = Static<Schema>;
|
|
78
|
+
|
|
71
79
|
const NoyaStateContext = AnyNoyaStateContext as React.Context<
|
|
72
80
|
| {
|
|
73
|
-
ms: MultiplayerStateManager<
|
|
81
|
+
ms: MultiplayerStateManager<S, M>;
|
|
74
82
|
assetManager: AssetManager;
|
|
75
83
|
}
|
|
76
84
|
| undefined
|
|
@@ -80,12 +88,13 @@ export function createNoyaContext<Schema extends TSchema, M = void, E = void>({
|
|
|
80
88
|
children,
|
|
81
89
|
initialState,
|
|
82
90
|
...options
|
|
83
|
-
}: NoyaStateProviderProps<
|
|
91
|
+
}: NoyaStateProviderProps<S, M, E>) {
|
|
84
92
|
const [, , { multiplayerStateManager, assetManager }] = useNoyaState(
|
|
85
93
|
initialState,
|
|
86
94
|
{
|
|
87
95
|
...(options as any),
|
|
88
96
|
schema,
|
|
97
|
+
mergeHistoryEntries,
|
|
89
98
|
}
|
|
90
99
|
);
|
|
91
100
|
|
|
@@ -104,8 +113,6 @@ export function createNoyaContext<Schema extends TSchema, M = void, E = void>({
|
|
|
104
113
|
);
|
|
105
114
|
}
|
|
106
115
|
|
|
107
|
-
type S = Static<Schema>;
|
|
108
|
-
|
|
109
116
|
function useValue(): S;
|
|
110
117
|
function useValue<A>(
|
|
111
118
|
selector: (value: S) => A,
|
|
@@ -205,12 +212,21 @@ export function createNoyaContext<Schema extends TSchema, M = void, E = void>({
|
|
|
205
212
|
return [value, setValue];
|
|
206
213
|
}
|
|
207
214
|
|
|
215
|
+
function useStateManager() {
|
|
216
|
+
const { ms } = useAnyNoyaStateContext() as {
|
|
217
|
+
ms: MultiplayerStateManager<S, M>;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
return ms;
|
|
221
|
+
}
|
|
222
|
+
|
|
208
223
|
return {
|
|
209
224
|
Context: NoyaStateContext,
|
|
210
225
|
Provider: NoyaStateProvider,
|
|
211
226
|
useValue,
|
|
212
227
|
useSetValue,
|
|
213
228
|
useValueState,
|
|
229
|
+
useStateManager,
|
|
214
230
|
// useAssets,
|
|
215
231
|
// useAssetManager,
|
|
216
232
|
// useAsset,
|
package/dist/index.d.mts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import * as _noya_app_state_manager from '@noya-app/state-manager';
|
|
2
|
-
import { MultiplayerUser, EphemeralUserData, ConnectionEvent, Task, MultiplayerStateManager, AssetManager, StateManager, StateManagerOptions, MultiplayerStateManagerOptions, SyncAdapterOptions, TSchema, Static } from '@noya-app/state-manager';
|
|
3
|
-
export * from '@noya-app/state-manager';
|
|
4
|
-
import React, { ComponentPropsWithoutRef, SetStateAction } from 'react';
|
|
5
|
-
import { GetAtPath, ObservableOptions, PathKey, Observable } from '@noya-app/observable';
|
|
6
|
-
|
|
7
|
-
declare function getAvatarInitials(name: string): string;
|
|
8
|
-
/**
|
|
9
|
-
* Create a background color and initials from a name
|
|
10
|
-
*
|
|
11
|
-
* Adapted from https://mui.com/material-ui/react-avatar/
|
|
12
|
-
*/
|
|
13
|
-
declare function getAvatarStyle(name: string): {
|
|
14
|
-
color: string;
|
|
15
|
-
backgroundColor: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type Point = {
|
|
19
|
-
x: number;
|
|
20
|
-
y: number;
|
|
21
|
-
};
|
|
22
|
-
type UserPointerData = {
|
|
23
|
-
pointer?: Point;
|
|
24
|
-
};
|
|
25
|
-
type UserPointerProps<E extends UserPointerData> = {
|
|
26
|
-
user: MultiplayerUser;
|
|
27
|
-
ephemeralUserData: EphemeralUserData<E>;
|
|
28
|
-
hideAfter?: number;
|
|
29
|
-
};
|
|
30
|
-
declare const UserPointer_: <E extends UserPointerData>({ user, ephemeralUserData, hideAfter, }: UserPointerProps<E>) => React.JSX.Element | null;
|
|
31
|
-
declare const UserPointer: typeof UserPointer_;
|
|
32
|
-
declare const UserPointerContainer: React.NamedExoticComponent<{
|
|
33
|
-
point: Point;
|
|
34
|
-
children: React.ReactNode;
|
|
35
|
-
show?: boolean;
|
|
36
|
-
}>;
|
|
37
|
-
declare const UserNameTag: React.NamedExoticComponent<{
|
|
38
|
-
background: string;
|
|
39
|
-
children: React.ReactNode;
|
|
40
|
-
}>;
|
|
41
|
-
type UserPointersOverlayProps<E extends UserPointerData> = {
|
|
42
|
-
connectedUsers: MultiplayerUser[];
|
|
43
|
-
ephemeralUserData: EphemeralUserData<E>;
|
|
44
|
-
};
|
|
45
|
-
declare const UserPointersOverlay: React.MemoExoticComponent<(<E extends UserPointerData>({ connectedUsers, ephemeralUserData }: UserPointersOverlayProps<E>) => React.JSX.Element)>;
|
|
46
|
-
declare const UserPointerIcon: React.NamedExoticComponent<{
|
|
47
|
-
color?: string;
|
|
48
|
-
}>;
|
|
49
|
-
|
|
50
|
-
type StateInspectorAnchor = "left" | "right" | "bottom left" | "bottom right";
|
|
51
|
-
type StateInspectorOptions = {
|
|
52
|
-
colorScheme?: "light" | "dark";
|
|
53
|
-
anchor?: StateInspectorAnchor;
|
|
54
|
-
};
|
|
55
|
-
declare const StateInspector: React.MemoExoticComponent<(<S, M, E>({ state, connectionEvents, connectedUsers, tasks, userId, unstyled, colorScheme, multiplayerStateManager, assetManager, ephemeralUserData, anchor, forceInit, ...props }: {
|
|
56
|
-
state: S;
|
|
57
|
-
connectionEvents?: ConnectionEvent<S>[];
|
|
58
|
-
connectedUsers?: MultiplayerUser[];
|
|
59
|
-
tasks?: Task[];
|
|
60
|
-
userId?: string;
|
|
61
|
-
unstyled?: boolean;
|
|
62
|
-
multiplayerStateManager: MultiplayerStateManager<S, M>;
|
|
63
|
-
assetManager: AssetManager;
|
|
64
|
-
ephemeralUserData: EphemeralUserData<E>;
|
|
65
|
-
forceInit: () => void;
|
|
66
|
-
} & StateInspectorOptions & ComponentPropsWithoutRef<"div">) => React.JSX.Element | null)>;
|
|
67
|
-
|
|
68
|
-
declare function useSyncStateManager<S, M>(stateManager: StateManager<S, M>): S;
|
|
69
|
-
declare function useSyncStateManager<S, M, P extends string>(stateManager: StateManager<S, M>, path: P): GetAtPath<S, P>;
|
|
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>];
|
|
71
|
-
declare function useManagedHistory<S, M = void>(stateManager: StateManager<S, M>): _noya_app_state_manager.HistorySnapshot<S, M>;
|
|
72
|
-
declare function useSyncMultiplayerStateManager<S, M = void>(multiplayerStateManager: MultiplayerStateManager<S, M>): S;
|
|
73
|
-
declare function useEphemeralUserData<E>(ephemeralUserData: EphemeralUserData<E>, options?: {
|
|
74
|
-
throttle?: number;
|
|
75
|
-
}): {
|
|
76
|
-
data: Record<string, E | undefined>;
|
|
77
|
-
metadata: Record<string, _noya_app_state_manager.EphemeralUserMetadata | undefined>;
|
|
78
|
-
currentUserId: string | undefined;
|
|
79
|
-
};
|
|
80
|
-
type UseMultiplayerStateOptions<S, M = void, E = void> = MultiplayerStateManagerOptions<S, M> & {
|
|
81
|
-
sync?: (options: Pick<SyncAdapterOptions<S, M, E>, "ms" | "ephemeralUserData" | "assetManager">) => void;
|
|
82
|
-
trackConnectionEvents?: boolean;
|
|
83
|
-
trackTasks?: boolean;
|
|
84
|
-
inspector?: boolean | StateInspectorOptions;
|
|
85
|
-
};
|
|
86
|
-
declare function useMultiplayerState<S, M = void, E = void>(initialState: S | (() => S), options?: UseMultiplayerStateOptions<S, M, E>): readonly [S, (...args: M extends {} ? [metadata: M, action: S | ((prevState: S) => S)] : [action: S | ((prevState: S) => S)]) => void, {
|
|
87
|
-
tasks: Task[];
|
|
88
|
-
userId: string | undefined;
|
|
89
|
-
connectionEvents: ConnectionEvent<S>[] | undefined;
|
|
90
|
-
connectedUsers: MultiplayerUser[];
|
|
91
|
-
ephemeralUserData: EphemeralUserData<E>;
|
|
92
|
-
multiplayerStateManager: MultiplayerStateManager<S, M>;
|
|
93
|
-
assets: _noya_app_state_manager.Asset[];
|
|
94
|
-
assetManager: AssetManager;
|
|
95
|
-
createAsset: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
|
|
96
|
-
deleteAsset: (id: string) => Promise<void>;
|
|
97
|
-
}];
|
|
98
|
-
|
|
99
|
-
type AppViewType = "editable" | "readOnly" | "preview";
|
|
100
|
-
type AppTheme = "light" | "dark";
|
|
101
|
-
type AppData<State> = {
|
|
102
|
-
multiplayerUrl?: string;
|
|
103
|
-
theme: AppTheme;
|
|
104
|
-
viewType: AppViewType;
|
|
105
|
-
initialState: State;
|
|
106
|
-
inspector: boolean | StateInspectorOptions;
|
|
107
|
-
};
|
|
108
|
-
declare function createDefaultAppData<State>(initialState: State): AppData<State>;
|
|
109
|
-
type GetAppDataOptions = {
|
|
110
|
-
window?: {
|
|
111
|
-
location: {
|
|
112
|
-
hash: string;
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
overrideExistingState?: boolean;
|
|
116
|
-
};
|
|
117
|
-
declare function enforceSchema<State>(initialState: State, schema?: TSchema): State;
|
|
118
|
-
declare function parseAppDataParameter(options?: GetAppDataOptions): AppData<unknown> | null;
|
|
119
|
-
declare function getAppData<State>(initialState: State | (() => State), schema?: TSchema, options?: GetAppDataOptions): AppData<State>;
|
|
120
|
-
type IframeToHostMessage = {
|
|
121
|
-
type: "application.setMenuItems";
|
|
122
|
-
payload: {
|
|
123
|
-
menuItems: any[];
|
|
124
|
-
};
|
|
125
|
-
} | {
|
|
126
|
-
type: "multiplayer.setUsers";
|
|
127
|
-
payload: {
|
|
128
|
-
users: any[];
|
|
129
|
-
};
|
|
130
|
-
};
|
|
131
|
-
type HostToIframeMessage = {
|
|
132
|
-
type: "application.selectMenuItem";
|
|
133
|
-
payload: {
|
|
134
|
-
value: string;
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
declare function useBroadcastConnectedUsers(connectedUsers: MultiplayerUser[]): void;
|
|
138
|
-
declare function useBroadcastMenuItems<T>(menuItems: any[], handleSelectMenuItem: (value: T) => void): void;
|
|
139
|
-
declare function isEmbeddedApp(): boolean;
|
|
140
|
-
type UseNoyaStateOptions<S, M = void, E = void> = UseMultiplayerStateOptions<S, M, E> & {
|
|
141
|
-
offlineStorageKey?: string;
|
|
142
|
-
};
|
|
143
|
-
type UseNoyaStateResult<S, M = void, E = void> = [
|
|
144
|
-
ReturnType<typeof useMultiplayerState<S, M, E>>[0],
|
|
145
|
-
ReturnType<typeof useMultiplayerState<S, M, E>>[1],
|
|
146
|
-
ReturnType<typeof useMultiplayerState<S, M, E>>[2] & {
|
|
147
|
-
theme: AppTheme;
|
|
148
|
-
viewType: AppViewType;
|
|
149
|
-
}
|
|
150
|
-
];
|
|
151
|
-
/**
|
|
152
|
-
* Sync data locally or to a multiplayer server.
|
|
153
|
-
*
|
|
154
|
-
* There are 3 ways to use this hook:
|
|
155
|
-
*
|
|
156
|
-
* 1. Pass an initial state: `useNoyaState(initialState)`
|
|
157
|
-
* 2. Pass an initial state and a schema: `useNoyaState(initialState, { schema })`
|
|
158
|
-
* 3. Pass only a schema, and a conforming initial state is created automatically: `useNoyaState({ schema })`
|
|
159
|
-
*/
|
|
160
|
-
declare function useNoyaState<S, M = void, E = void, O extends UseNoyaStateOptions<S, M, E> = UseNoyaStateOptions<S, M, E>>(...args: [
|
|
161
|
-
initialState: O["schema"] extends TSchema ? Static<O["schema"]> | (() => Static<O["schema"]>) : S | (() => S),
|
|
162
|
-
options?: O
|
|
163
|
-
] | [options: O & {
|
|
164
|
-
schema: TSchema;
|
|
165
|
-
}]): UseNoyaStateResult<O["schema"] extends TSchema ? Static<O["schema"]> : S, M, E>;
|
|
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
|
-
|
|
201
|
-
declare function useObservable<T>(observable: Observable<T>): T;
|
|
202
|
-
declare function useObservable<T, P extends PathKey[] | string>(observable: Observable<T>, path: P): GetAtPath<T, P>;
|
|
203
|
-
|
|
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 };
|