@noya-app/noya-multiplayer-react 0.1.39 → 0.1.41

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/dist/index.d.mts CHANGED
@@ -164,9 +164,9 @@ declare function useNoyaState<S, M = void, E = void, O extends UseNoyaStateOptio
164
164
  schema: TSchema;
165
165
  }]): UseNoyaStateResult<O["schema"] extends TSchema ? Static<O["schema"]> : S, M, E>;
166
166
 
167
- interface NoyaStateProviderProps<Schema extends TSchema, M, E> extends Omit<UseNoyaStateOptions<Static<Schema>, M, E>, "schema"> {
167
+ interface NoyaStateProviderProps<S, M, E> extends Omit<UseNoyaStateOptions<S, M, E>, "schema" | "mergeHistoryEntries"> {
168
168
  children: React.ReactNode;
169
- initialState?: Static<Schema>;
169
+ initialState?: S;
170
170
  }
171
171
  declare function useAssets(): _noya_app_state_manager.Asset[];
172
172
  declare function useAsset(id: string | undefined): _noya_app_state_manager.Asset | undefined;
@@ -174,14 +174,15 @@ declare function useAssetManager(): {
174
174
  create: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
175
175
  delete: (id: string) => Promise<void>;
176
176
  };
177
- declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({ schema, }: {
177
+ declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({ schema, mergeHistoryEntries, }: {
178
178
  schema: Schema;
179
+ mergeHistoryEntries?: StateManagerOptions<Static<Schema>, M>["mergeHistoryEntries"];
179
180
  }): {
180
181
  Context: React.Context<{
181
182
  ms: MultiplayerStateManager<Static<Schema>, M>;
182
183
  assetManager: AssetManager;
183
184
  } | undefined>;
184
- Provider: ({ children, initialState, ...options }: NoyaStateProviderProps<Schema, M, E>) => React.JSX.Element;
185
+ Provider: ({ children, initialState, ...options }: NoyaStateProviderProps<Static<Schema>, M, E>) => React.JSX.Element;
185
186
  useValue: {
186
187
  (): Static<Schema>;
187
188
  <A>(selector: (value: Static<Schema>) => A, options?: Pick<ObservableOptions, "isEqual">): A;
@@ -196,6 +197,7 @@ declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({
196
197
  <A>(selector: (value: Static<Schema>) => A, options?: ObservableOptions): [A, M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void];
197
198
  <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
+ useStateManager: () => MultiplayerStateManager<Static<Schema>, M>;
199
201
  };
200
202
 
201
203
  declare function useObservable<T>(observable: Observable<T>): T;
package/dist/index.d.ts CHANGED
@@ -164,9 +164,9 @@ declare function useNoyaState<S, M = void, E = void, O extends UseNoyaStateOptio
164
164
  schema: TSchema;
165
165
  }]): UseNoyaStateResult<O["schema"] extends TSchema ? Static<O["schema"]> : S, M, E>;
166
166
 
167
- interface NoyaStateProviderProps<Schema extends TSchema, M, E> extends Omit<UseNoyaStateOptions<Static<Schema>, M, E>, "schema"> {
167
+ interface NoyaStateProviderProps<S, M, E> extends Omit<UseNoyaStateOptions<S, M, E>, "schema" | "mergeHistoryEntries"> {
168
168
  children: React.ReactNode;
169
- initialState?: Static<Schema>;
169
+ initialState?: S;
170
170
  }
171
171
  declare function useAssets(): _noya_app_state_manager.Asset[];
172
172
  declare function useAsset(id: string | undefined): _noya_app_state_manager.Asset | undefined;
@@ -174,14 +174,15 @@ declare function useAssetManager(): {
174
174
  create: (options: _noya_app_state_manager.CreateAssetOptions | Uint8Array | File | Blob) => Promise<_noya_app_state_manager.Asset>;
175
175
  delete: (id: string) => Promise<void>;
176
176
  };
177
- declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({ schema, }: {
177
+ declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({ schema, mergeHistoryEntries, }: {
178
178
  schema: Schema;
179
+ mergeHistoryEntries?: StateManagerOptions<Static<Schema>, M>["mergeHistoryEntries"];
179
180
  }): {
180
181
  Context: React.Context<{
181
182
  ms: MultiplayerStateManager<Static<Schema>, M>;
182
183
  assetManager: AssetManager;
183
184
  } | undefined>;
184
- Provider: ({ children, initialState, ...options }: NoyaStateProviderProps<Schema, M, E>) => React.JSX.Element;
185
+ Provider: ({ children, initialState, ...options }: NoyaStateProviderProps<Static<Schema>, M, E>) => React.JSX.Element;
185
186
  useValue: {
186
187
  (): Static<Schema>;
187
188
  <A>(selector: (value: Static<Schema>) => A, options?: Pick<ObservableOptions, "isEqual">): A;
@@ -196,6 +197,7 @@ declare function createNoyaContext<Schema extends TSchema, M = void, E = void>({
196
197
  <A>(selector: (value: Static<Schema>) => A, options?: ObservableOptions): [A, M extends {} ? (metadata: M, value: SetStateAction<Static<Schema>>) => void : (value: SetStateAction<Static<Schema>>) => void];
197
198
  <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
+ useStateManager: () => MultiplayerStateManager<Static<Schema>, M>;
199
201
  };
200
202
 
201
203
  declare function useObservable<T>(observable: Observable<T>): T;
package/dist/index.js CHANGED
@@ -139,16 +139,14 @@ var UserPointer_ = function UserPointer({
139
139
  const [, setForceUpdate] = (0, import_react2.useState)(0);
140
140
  const updatedAt = metadata?.updatedAt ?? 0;
141
141
  (0, import_react2.useEffect)(() => {
142
- if (!shouldShow(hideAfter, updatedAt))
143
- return;
142
+ if (!shouldShow(hideAfter, updatedAt)) return;
144
143
  const timeoutId = setTimeout(() => {
145
144
  setForceUpdate((prev) => prev + 1);
146
145
  }, hideAfter);
147
146
  return () => clearTimeout(timeoutId);
148
147
  }, [hideAfter, updatedAt]);
149
148
  const show = shouldShow(hideAfter, updatedAt);
150
- if (!data || !data.pointer)
151
- return null;
149
+ if (!data || !data.pointer) return null;
152
150
  return /* @__PURE__ */ import_react2.default.createElement(UserPointerContainer, { key: user.id, point: data.pointer, show }, /* @__PURE__ */ import_react2.default.createElement(UserPointerIcon, { color: avatarStyle.backgroundColor }), /* @__PURE__ */ import_react2.default.createElement(UserNameTag, { background: avatarStyle.backgroundColor }, user.name));
153
151
  };
154
152
  var UserPointer2 = (0, import_react2.memo)(UserPointer_);
@@ -204,8 +202,7 @@ var UserNameTag = (0, import_react2.memo)(function UserNameTag2({
204
202
  var UserPointersOverlay = (0, import_react2.memo)(function UserPointers({ connectedUsers, ephemeralUserData }) {
205
203
  const currentUserId = useObservable(ephemeralUserData.currentUserId$);
206
204
  return /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, connectedUsers.map((user) => {
207
- if (user.id === currentUserId)
208
- return null;
205
+ if (user.id === currentUserId) return null;
209
206
  return /* @__PURE__ */ import_react2.default.createElement(
210
207
  UserPointer2,
211
208
  {
@@ -622,8 +619,7 @@ var StateInspector = (0, import_react5.memo)(function StateInspector2({
622
619
  }
623
620
  }, [historySnapshot]);
624
621
  (0, import_react5.useEffect)(() => {
625
- if (!historyContainerRef.current)
626
- return;
622
+ if (!historyContainerRef.current) return;
627
623
  const historyEntry = historyContainerRef.current.querySelector(
628
624
  `#${HISTORY_ELEMENT_PREFIX}${historySnapshot.historyIndex}`
629
625
  );
@@ -655,8 +651,7 @@ var StateInspector = (0, import_react5.memo)(function StateInspector2({
655
651
  zIndex: 9999,
656
652
  lineHeight: "13px"
657
653
  };
658
- if (!didMount)
659
- return null;
654
+ if (!didMount) return null;
660
655
  if (!showInspector) {
661
656
  return /* @__PURE__ */ import_react5.default.createElement(
662
657
  "div",
@@ -1134,8 +1129,7 @@ function pathToString(extendedPath) {
1134
1129
  ).join("");
1135
1130
  }
1136
1131
  function ellipsis(str, maxLength, position = "end") {
1137
- if (str.length <= maxLength)
1138
- return str;
1132
+ if (str.length <= maxLength) return str;
1139
1133
  switch (position) {
1140
1134
  case "start":
1141
1135
  return `\u2026${str.slice(str.length - maxLength)}`;
@@ -1208,8 +1202,7 @@ function useStateInspector({
1208
1202
  }) {
1209
1203
  const [root, setRoot] = import_react6.default.useState(null);
1210
1204
  (0, import_react6.useLayoutEffect)(() => {
1211
- if (disabled)
1212
- return;
1205
+ if (disabled) return;
1213
1206
  const { host, container } = createShadowRootElement();
1214
1207
  const root2 = (0, import_client.createRoot)(container);
1215
1208
  document.body.appendChild(host);
@@ -1219,8 +1212,7 @@ function useStateInspector({
1219
1212
  };
1220
1213
  }, [disabled]);
1221
1214
  (0, import_react6.useLayoutEffect)(() => {
1222
- if (!root)
1223
- return;
1215
+ if (!root) return;
1224
1216
  root.render(
1225
1217
  /* @__PURE__ */ import_react6.default.createElement(
1226
1218
  StateInspector,
@@ -1327,8 +1319,7 @@ function throttle(fn, ms) {
1327
1319
  return (...args) => {
1328
1320
  const now = Date.now();
1329
1321
  if (now - lastCall < ms) {
1330
- if (timeoutId)
1331
- clearTimeout(timeoutId);
1322
+ if (timeoutId) clearTimeout(timeoutId);
1332
1323
  timeoutId = setTimeout(
1333
1324
  () => {
1334
1325
  lastCall = now;
@@ -1467,8 +1458,7 @@ function useMultiplayerState(initialState, options) {
1467
1458
  });
1468
1459
  }, [multiplayerStateManager]);
1469
1460
  (0, import_react7.useEffect)(() => {
1470
- if (!unrecoverableError)
1471
- return;
1461
+ if (!unrecoverableError) return;
1472
1462
  const el = document.createElement("div");
1473
1463
  el.id = "noya-multiplayer-react-error-overlay";
1474
1464
  document.body.appendChild(el);
@@ -1516,13 +1506,11 @@ function enforceSchema(initialState, schema) {
1516
1506
  function parseAppDataParameter(options) {
1517
1507
  const window2 = options?.window ?? globalThis;
1518
1508
  const location = window2.location;
1519
- if (!location)
1520
- return null;
1509
+ if (!location) return null;
1521
1510
  const urlHash = (location.hash || "").replace(/^#/, "");
1522
1511
  const params = new URLSearchParams(urlHash);
1523
1512
  const data = params.get("data");
1524
- if (!data)
1525
- return null;
1513
+ if (!data) return null;
1526
1514
  try {
1527
1515
  return JSON.parse(data);
1528
1516
  } catch (e) {
@@ -1540,8 +1528,7 @@ function getAppData(initialState, schema, options) {
1540
1528
  }
1541
1529
  function useBroadcastConnectedUsers(connectedUsers) {
1542
1530
  (0, import_react8.useEffect)(() => {
1543
- if (!isEmbeddedApp())
1544
- return;
1531
+ if (!isEmbeddedApp()) return;
1545
1532
  const message = {
1546
1533
  type: "multiplayer.setUsers",
1547
1534
  payload: { users: connectedUsers }
@@ -1551,8 +1538,7 @@ function useBroadcastConnectedUsers(connectedUsers) {
1551
1538
  }
1552
1539
  function useBroadcastMenuItems(menuItems, handleSelectMenuItem) {
1553
1540
  (0, import_react8.useEffect)(() => {
1554
- if (!isEmbeddedApp())
1555
- return;
1541
+ if (!isEmbeddedApp()) return;
1556
1542
  const message = {
1557
1543
  type: "application.setMenuItems",
1558
1544
  payload: { menuItems }
@@ -1638,7 +1624,8 @@ function useAssetManager() {
1638
1624
  );
1639
1625
  }
1640
1626
  function createNoyaContext({
1641
- schema
1627
+ schema,
1628
+ mergeHistoryEntries
1642
1629
  }) {
1643
1630
  const NoyaStateContext = AnyNoyaStateContext;
1644
1631
  function NoyaStateProvider({
@@ -1650,7 +1637,8 @@ function createNoyaContext({
1650
1637
  initialState,
1651
1638
  {
1652
1639
  ...options,
1653
- schema
1640
+ schema,
1641
+ mergeHistoryEntries
1654
1642
  }
1655
1643
  );
1656
1644
  const value = (0, import_react9.useMemo)(
@@ -1692,12 +1680,17 @@ function createNoyaContext({
1692
1680
  const setValue = useSetValue(path ?? "");
1693
1681
  return [value, setValue];
1694
1682
  }
1683
+ function useStateManager() {
1684
+ const { ms } = useAnyNoyaStateContext();
1685
+ return ms;
1686
+ }
1695
1687
  return {
1696
1688
  Context: NoyaStateContext,
1697
1689
  Provider: NoyaStateProvider,
1698
1690
  useValue,
1699
1691
  useSetValue,
1700
- useValueState
1692
+ useValueState,
1693
+ useStateManager
1701
1694
  // useAssets,
1702
1695
  // useAssetManager,
1703
1696
  // useAsset,