@noya-app/noya-multiplayer-react 0.1.38 → 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/dist/index.mjs CHANGED
@@ -54,7 +54,7 @@ import React, { memo, useEffect, useMemo as useMemo2, useState } from "react";
54
54
  import { useMemo, useSyncExternalStore } from "react";
55
55
  function useObservable(observable, path) {
56
56
  const { get, listen } = useMemo(() => {
57
- const listen2 = path ? (callback) => observable.listen(path, callback) : observable.listen;
57
+ const listen2 = path ? (callback) => observable.subscribe(path, callback) : observable.subscribe;
58
58
  const get2 = path ? () => observable.get(path) : observable.get;
59
59
  return { get: get2, listen: listen2 };
60
60
  }, [observable, path]);
@@ -1349,6 +1349,7 @@ function useMultiplayerState(initialState, options) {
1349
1349
  ephemeralUserData,
1350
1350
  multiplayerStateManager,
1351
1351
  assets,
1352
+ assetManager,
1352
1353
  createAsset: assetManager.create,
1353
1354
  deleteAsset: assetManager.delete
1354
1355
  // evaluate: async (code: string) => {
@@ -1418,7 +1419,15 @@ function useMultiplayerState(initialState, options) {
1418
1419
  const [unrecoverableError, setUnrecoverableError] = useState2();
1419
1420
  useEffect3(() => {
1420
1421
  return multiplayerStateManager.errorEmitter.addListener((error) => {
1421
- setUnrecoverableError(error);
1422
+ switch (error.reason) {
1423
+ case "invalidDataType":
1424
+ case "schemaMismatch":
1425
+ case "outdatedSchema":
1426
+ case "dataMigrationFailure":
1427
+ case "schemaMigration":
1428
+ setUnrecoverableError(error);
1429
+ break;
1430
+ }
1422
1431
  });
1423
1432
  }, [multiplayerStateManager]);
1424
1433
  useEffect3(() => {
@@ -1564,6 +1573,119 @@ function useNoyaState(...args) {
1564
1573
  }, [extras, theme, viewType]);
1565
1574
  return [result[0], result[1], mergedExtras];
1566
1575
  }
1576
+
1577
+ // src/NoyaStateContext.tsx
1578
+ import React6, {
1579
+ createContext,
1580
+ useCallback as useCallback2,
1581
+ useContext,
1582
+ useMemo as useMemo5
1583
+ } from "react";
1584
+ var AnyNoyaStateContext = createContext(void 0);
1585
+ function useAnyNoyaStateContext() {
1586
+ const value = useContext(AnyNoyaStateContext);
1587
+ if (!value) {
1588
+ throw new Error(
1589
+ "useNoyaStateContext must be used within a NoyaStateProvider"
1590
+ );
1591
+ }
1592
+ return value;
1593
+ }
1594
+ function useAssets() {
1595
+ const { assetManager } = useAnyNoyaStateContext();
1596
+ return useObservable(assetManager.assets$);
1597
+ }
1598
+ function useAsset(id) {
1599
+ const { assetManager } = useAnyNoyaStateContext();
1600
+ const observable = useMemo5(
1601
+ () => assetManager.assets$.map((assets) => assets.find((a) => a.id === id)),
1602
+ [assetManager, id]
1603
+ );
1604
+ return useObservable(observable);
1605
+ }
1606
+ function useAssetManager() {
1607
+ const { assetManager } = useAnyNoyaStateContext();
1608
+ return useMemo5(
1609
+ () => ({
1610
+ create: assetManager.create,
1611
+ delete: assetManager.delete
1612
+ }),
1613
+ [assetManager]
1614
+ );
1615
+ }
1616
+ function createNoyaContext({
1617
+ schema,
1618
+ mergeHistoryEntries
1619
+ }) {
1620
+ const NoyaStateContext = AnyNoyaStateContext;
1621
+ function NoyaStateProvider({
1622
+ children,
1623
+ initialState,
1624
+ ...options
1625
+ }) {
1626
+ const [, , { multiplayerStateManager, assetManager }] = useNoyaState(
1627
+ initialState,
1628
+ {
1629
+ ...options,
1630
+ schema,
1631
+ mergeHistoryEntries
1632
+ }
1633
+ );
1634
+ const value = useMemo5(
1635
+ () => ({
1636
+ ms: multiplayerStateManager,
1637
+ assetManager
1638
+ }),
1639
+ [multiplayerStateManager, assetManager]
1640
+ );
1641
+ return /* @__PURE__ */ React6.createElement(AnyNoyaStateContext.Provider, { value }, children);
1642
+ }
1643
+ function useValue(path, options) {
1644
+ const { ms } = useAnyNoyaStateContext();
1645
+ let actualPath = typeof path === "function" || !path ? "" : path;
1646
+ const mappedObservable = useMemo5(() => {
1647
+ if (typeof path === "function") {
1648
+ return ms.optimisticState$.map(path, { isEqual: options?.isEqual });
1649
+ }
1650
+ return ms.optimisticState$;
1651
+ }, [ms, path, options?.isEqual]);
1652
+ const value = useObservable(mappedObservable, actualPath);
1653
+ return value;
1654
+ }
1655
+ function useSetValue(path) {
1656
+ const { ms } = useAnyNoyaStateContext();
1657
+ const setValue = useCallback2(
1658
+ (...args) => {
1659
+ const [metadata, value] = args.length === 2 ? args : [void 0, args[0]];
1660
+ const setStateAtPath = ms.setStateAtPath;
1661
+ setStateAtPath(metadata, path ?? "", value);
1662
+ },
1663
+ [ms, path]
1664
+ );
1665
+ return setValue;
1666
+ }
1667
+ function useValueState(...args) {
1668
+ const [selector, options, path] = args.length >= 1 && typeof args[0] === "function" ? [args[0], args[1], void 0] : [void 0, void 0, args[0]];
1669
+ const value = useValue(selector ?? path ?? "", options);
1670
+ const setValue = useSetValue(path ?? "");
1671
+ return [value, setValue];
1672
+ }
1673
+ function useStateManager() {
1674
+ const { ms } = useAnyNoyaStateContext();
1675
+ return ms;
1676
+ }
1677
+ return {
1678
+ Context: NoyaStateContext,
1679
+ Provider: NoyaStateProvider,
1680
+ useValue,
1681
+ useSetValue,
1682
+ useValueState,
1683
+ useStateManager
1684
+ // useAssets,
1685
+ // useAssetManager,
1686
+ // useAsset,
1687
+ };
1688
+ }
1567
1689
  export {
1568
1690
  StateInspector,
1569
1691
  UserNameTag,
@@ -1572,12 +1694,16 @@ export {
1572
1694
  UserPointerIcon,
1573
1695
  UserPointersOverlay,
1574
1696
  createDefaultAppData,
1697
+ createNoyaContext,
1575
1698
  enforceSchema,
1576
1699
  getAppData,
1577
1700
  getAvatarInitials,
1578
1701
  getAvatarStyle,
1579
1702
  isEmbeddedApp,
1580
1703
  parseAppDataParameter,
1704
+ useAsset,
1705
+ useAssetManager,
1706
+ useAssets,
1581
1707
  useBroadcastConnectedUsers,
1582
1708
  useBroadcastMenuItems,
1583
1709
  useEphemeralUserData,