@react-remote-state/client 1.1.7 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -1,13 +1,7 @@
1
1
  import { Game } from '@react-remote-state/types';
2
2
 
3
- declare function getPlayer<GameCustom, PlayerCustom>(playerId?: string, game?: Game<GameCustom, PlayerCustom>): {
4
- id: string;
5
- host: boolean;
6
- connected: boolean;
7
- custom: PlayerCustom | undefined;
8
- } | undefined;
9
- declare function isHost<GameCustom, PlayerCustom>(playerId?: string, game?: Game<GameCustom, PlayerCustom>): boolean | undefined;
10
- type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
3
+ type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
4
+
11
5
  declare function useRemoteReducer<GameCustom, PlayerCustom, Action>(uri: string, reducer: Reducer<GameCustom, PlayerCustom, Action>, gameId?: string | null, acceptPlayer?: (game: Game<GameCustom, PlayerCustom>) => boolean, storage?: Storage): [
12
6
  game: Game<GameCustom, PlayerCustom> | undefined,
13
7
  localPlayerId: string | undefined,
@@ -15,4 +9,4 @@ declare function useRemoteReducer<GameCustom, PlayerCustom, Action>(uri: string,
15
9
  declined: boolean
16
10
  ];
17
11
 
18
- export { getPlayer, isHost, useRemoteReducer };
12
+ export { useRemoteReducer };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,7 @@
1
1
  import { Game } from '@react-remote-state/types';
2
2
 
3
- declare function getPlayer<GameCustom, PlayerCustom>(playerId?: string, game?: Game<GameCustom, PlayerCustom>): {
4
- id: string;
5
- host: boolean;
6
- connected: boolean;
7
- custom: PlayerCustom | undefined;
8
- } | undefined;
9
- declare function isHost<GameCustom, PlayerCustom>(playerId?: string, game?: Game<GameCustom, PlayerCustom>): boolean | undefined;
10
- type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
3
+ type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
4
+
11
5
  declare function useRemoteReducer<GameCustom, PlayerCustom, Action>(uri: string, reducer: Reducer<GameCustom, PlayerCustom, Action>, gameId?: string | null, acceptPlayer?: (game: Game<GameCustom, PlayerCustom>) => boolean, storage?: Storage): [
12
6
  game: Game<GameCustom, PlayerCustom> | undefined,
13
7
  localPlayerId: string | undefined,
@@ -15,4 +9,4 @@ declare function useRemoteReducer<GameCustom, PlayerCustom, Action>(uri: string,
15
9
  declined: boolean
16
10
  ];
17
11
 
18
- export { getPlayer, isHost, useRemoteReducer };
12
+ export { useRemoteReducer };
package/dist/index.js CHANGED
@@ -5531,11 +5531,13 @@ var require_lodash = __commonJS({
5531
5531
  // src/index.ts
5532
5532
  var src_exports = {};
5533
5533
  __export(src_exports, {
5534
- getPlayer: () => getPlayer,
5535
- isHost: () => isHost,
5536
5534
  useRemoteReducer: () => useRemoteReducer
5537
5535
  });
5538
5536
  module.exports = __toCommonJS(src_exports);
5537
+ var import_react2 = require("react");
5538
+ var import_lodash2 = __toESM(require_lodash());
5539
+
5540
+ // src/hooks.ts
5539
5541
  var import_react = require("react");
5540
5542
 
5541
5543
  // src/socket.ts
@@ -5545,8 +5547,10 @@ var mod = {
5545
5547
  };
5546
5548
  var socket_default = mod;
5547
5549
 
5548
- // src/index.ts
5550
+ // src/hooks.ts
5549
5551
  var import_lodash = __toESM(require_lodash());
5552
+
5553
+ // src/player.ts
5550
5554
  function getPlayer(playerId, game) {
5551
5555
  if (!game || !playerId) {
5552
5556
  return void 0;
@@ -5557,7 +5561,96 @@ function isHost(playerId, game) {
5557
5561
  let player = getPlayer(playerId, game);
5558
5562
  return player == null ? void 0 : player.host;
5559
5563
  }
5560
- function wrapReducer(reducer, acceptPlayer) {
5564
+
5565
+ // src/hooks.ts
5566
+ function useClientInitialization(meta, setMeta, uri) {
5567
+ (0, import_react.useEffect)(() => {
5568
+ if (!meta.client) {
5569
+ setMeta(__spreadProps(__spreadValues({}, meta), {
5570
+ client: socket_default.connect(uri, { transports: ["websocket"] })
5571
+ }));
5572
+ }
5573
+ }, [meta.client]);
5574
+ }
5575
+ function useGuestInitialization(meta, setMeta, storage, internalDispatch, gameId, game) {
5576
+ (0, import_react.useEffect)(() => {
5577
+ if (!!meta.client && !meta.isGuestReady) {
5578
+ meta.client.on("error", console.error);
5579
+ meta.client.on("connect", () => {
5580
+ var _a, _b, _c;
5581
+ if (gameId == void 0 && !game) {
5582
+ (_a = meta.client) == null ? void 0 : _a.emit("create");
5583
+ } else if (!!gameId) {
5584
+ let playerCache = (0, import_lodash.flow)(
5585
+ () => !!gameId ? storage.getItem(gameId) : null,
5586
+ (cache) => !!cache ? JSON.parse(cache) : null
5587
+ )();
5588
+ if (!!playerCache) {
5589
+ let rejoin = {
5590
+ playerId: playerCache.playerId,
5591
+ socketId: playerCache.socketId,
5592
+ gameId
5593
+ };
5594
+ (_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
5595
+ } else {
5596
+ (_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
5597
+ }
5598
+ }
5599
+ });
5600
+ meta.client.on("assign", (assign) => {
5601
+ var _a, _b;
5602
+ setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
5603
+ if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
5604
+ let playerCache = {
5605
+ playerId: assign.playerId,
5606
+ socketId: (_b = meta.client) == null ? void 0 : _b.id
5607
+ };
5608
+ storage.setItem(assign.gameId, JSON.stringify(playerCache));
5609
+ }
5610
+ });
5611
+ meta.client.on(
5612
+ "update",
5613
+ (update) => internalDispatch({
5614
+ type: 1 /* Update */,
5615
+ game: update.game
5616
+ })
5617
+ );
5618
+ meta.client.on("decline", () => setMeta(__spreadProps(__spreadValues({}, meta), { declined: true })));
5619
+ setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5620
+ }
5621
+ }, [meta.client, meta.isGuestReady]);
5622
+ }
5623
+ function useHostInitialization(meta, setMeta, internalDispatch, game) {
5624
+ (0, import_react.useEffect)(() => {
5625
+ if (!!meta.client) {
5626
+ if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5627
+ meta.client.on("join", (join) => {
5628
+ if (!!meta.client) {
5629
+ internalDispatch({
5630
+ type: 2 /* Accept */,
5631
+ playerId: join.playerId,
5632
+ client: meta.client
5633
+ });
5634
+ }
5635
+ });
5636
+ meta.client.on("notify", (notify) => {
5637
+ if (!!game && !!meta.client) {
5638
+ internalDispatch({
5639
+ type: 0 /* Reduce */,
5640
+ client: meta.client,
5641
+ playerId: notify.playerId,
5642
+ action: notify.action
5643
+ });
5644
+ }
5645
+ });
5646
+ setMeta(__spreadProps(__spreadValues({}, meta), { isHostReady: true }));
5647
+ }
5648
+ }
5649
+ });
5650
+ }
5651
+
5652
+ // src/reducer.ts
5653
+ function createInternalReducer(reducer, acceptPlayer) {
5561
5654
  return (game, internalAction) => {
5562
5655
  var _a;
5563
5656
  switch (internalAction.type) {
@@ -5584,16 +5677,18 @@ function wrapReducer(reducer, acceptPlayer) {
5584
5677
  }
5585
5678
  };
5586
5679
  }
5587
- function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash.constant)(
5680
+
5681
+ // src/index.ts
5682
+ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash2.constant)(
5588
5683
  true
5589
5684
  ), storage = sessionStorage) {
5590
- let [meta, setMeta] = (0, import_react.useState)({
5685
+ let [meta, setMeta] = (0, import_react2.useState)({
5591
5686
  isHostReady: false,
5592
5687
  isGuestReady: false,
5593
5688
  declined: false
5594
5689
  });
5595
- let [game, internalDispatch] = (0, import_react.useReducer)(
5596
- wrapReducer(reducer, acceptPlayer),
5690
+ let [game, internalDispatch] = (0, import_react2.useReducer)(
5691
+ createInternalReducer(reducer, acceptPlayer),
5597
5692
  void 0
5598
5693
  );
5599
5694
  let dispatch = (action) => {
@@ -5601,87 +5696,22 @@ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash
5601
5696
  meta.client.emit("notify", { gameId: game.id, action });
5602
5697
  }
5603
5698
  };
5604
- (0, import_react.useEffect)(() => {
5605
- if (!meta.client) {
5606
- setMeta(__spreadProps(__spreadValues({}, meta), {
5607
- client: socket_default.connect(uri, { transports: ["websocket"] })
5608
- }));
5609
- }
5610
- if (!!meta.client) {
5611
- if (!meta.isGuestReady) {
5612
- meta.client.on("error", console.error);
5613
- meta.client.on("connect", () => {
5614
- var _a, _b, _c;
5615
- if (gameId == void 0 && !game) {
5616
- (_a = meta.client) == null ? void 0 : _a.emit("create");
5617
- } else if (!!gameId) {
5618
- let playerCache = (0, import_lodash.flow)(
5619
- () => !!gameId ? storage.getItem(gameId) : null,
5620
- (cache) => !!cache ? JSON.parse(cache) : null
5621
- )();
5622
- if (!!playerCache) {
5623
- let rejoin = {
5624
- playerId: playerCache.playerId,
5625
- socketId: playerCache.socketId,
5626
- gameId
5627
- };
5628
- (_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
5629
- } else {
5630
- (_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
5631
- }
5632
- }
5633
- });
5634
- meta.client.on("assign", (assign) => {
5635
- var _a, _b;
5636
- setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
5637
- if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
5638
- let playerCache = {
5639
- playerId: assign.playerId,
5640
- socketId: (_b = meta.client) == null ? void 0 : _b.id
5641
- };
5642
- storage.setItem(assign.gameId, JSON.stringify(playerCache));
5643
- }
5644
- });
5645
- meta.client.on(
5646
- "update",
5647
- (update) => internalDispatch({
5648
- type: 1 /* Update */,
5649
- game: update.game
5650
- })
5651
- );
5652
- meta.client.on("decline", () => setMeta(__spreadProps(__spreadValues({}, meta), { declined: true })));
5653
- setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5654
- }
5655
- if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5656
- meta.client.on("join", (join) => {
5657
- if (!!meta.client) {
5658
- internalDispatch({
5659
- type: 2 /* Accept */,
5660
- playerId: join.playerId,
5661
- client: meta.client
5662
- });
5663
- }
5664
- });
5665
- meta.client.on("notify", (notify) => {
5666
- if (!!game && !!meta.client) {
5667
- internalDispatch({
5668
- type: 0 /* Reduce */,
5669
- client: meta.client,
5670
- playerId: notify.playerId,
5671
- action: notify.action
5672
- });
5673
- }
5674
- });
5675
- setMeta(__spreadProps(__spreadValues({}, meta), { isHostReady: true }));
5676
- }
5677
- }
5678
- });
5699
+ useClientInitialization(meta, setMeta, uri);
5700
+ useGuestInitialization(
5701
+ meta,
5702
+ setMeta,
5703
+ storage,
5704
+ internalDispatch,
5705
+ gameId,
5706
+ game
5707
+ );
5708
+ if (!gameId) {
5709
+ useHostInitialization(meta, setMeta, internalDispatch, game);
5710
+ }
5679
5711
  return [game, meta.localPlayerId, dispatch, meta.declined];
5680
5712
  }
5681
5713
  // Annotate the CommonJS export names for ESM import in node:
5682
5714
  0 && (module.exports = {
5683
- getPlayer,
5684
- isHost,
5685
5715
  useRemoteReducer
5686
5716
  });
5687
5717
  /*! Bundled license information: