@react-remote-state/client 1.1.4 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -8,10 +8,11 @@ declare function getPlayer<GameCustom, PlayerCustom>(playerId?: string, game?: G
8
8
  } | undefined;
9
9
  declare function isHost<GameCustom, PlayerCustom>(playerId?: string, game?: Game<GameCustom, PlayerCustom>): boolean | undefined;
10
10
  type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
11
- declare function useRemoteReducer<GameCustom, PlayerCustom, Action>(uri: string, reducer: Reducer<GameCustom, PlayerCustom, Action>, gameId?: string): [
12
- Game<GameCustom, PlayerCustom> | undefined,
13
- string | undefined,
14
- (action: Action) => void
11
+ 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
+ game: Game<GameCustom, PlayerCustom> | undefined,
13
+ localPlayerId: string | undefined,
14
+ dispatch: (action: Action) => void,
15
+ declined: boolean
15
16
  ];
16
17
 
17
18
  export { getPlayer, isHost, useRemoteReducer };
package/dist/index.d.ts CHANGED
@@ -8,10 +8,11 @@ declare function getPlayer<GameCustom, PlayerCustom>(playerId?: string, game?: G
8
8
  } | undefined;
9
9
  declare function isHost<GameCustom, PlayerCustom>(playerId?: string, game?: Game<GameCustom, PlayerCustom>): boolean | undefined;
10
10
  type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
11
- declare function useRemoteReducer<GameCustom, PlayerCustom, Action>(uri: string, reducer: Reducer<GameCustom, PlayerCustom, Action>, gameId?: string): [
12
- Game<GameCustom, PlayerCustom> | undefined,
13
- string | undefined,
14
- (action: Action) => void
11
+ 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
+ game: Game<GameCustom, PlayerCustom> | undefined,
13
+ localPlayerId: string | undefined,
14
+ dispatch: (action: Action) => void,
15
+ declined: boolean
15
16
  ];
16
17
 
17
18
  export { getPlayer, isHost, useRemoteReducer };
package/dist/index.js CHANGED
@@ -1829,7 +1829,7 @@ var require_lodash = __commonJS({
1829
1829
  return defineProperty(func, "toString", {
1830
1830
  "configurable": true,
1831
1831
  "enumerable": false,
1832
- "value": constant(string),
1832
+ "value": constant2(string),
1833
1833
  "writable": true
1834
1834
  });
1835
1835
  };
@@ -4380,7 +4380,7 @@ var require_lodash = __commonJS({
4380
4380
  value = nativeObjectToString.call(value);
4381
4381
  }
4382
4382
  result2[value] = key;
4383
- }, constant(identity));
4383
+ }, constant2(identity));
4384
4384
  var invertBy = createInverter(function(result2, value, key) {
4385
4385
  if (value != null && typeof value.toString != "function") {
4386
4386
  value = nativeObjectToString.call(value);
@@ -4873,7 +4873,7 @@ var require_lodash = __commonJS({
4873
4873
  function conforms(source) {
4874
4874
  return baseConforms(baseClone(source, CLONE_DEEP_FLAG));
4875
4875
  }
4876
- function constant(value) {
4876
+ function constant2(value) {
4877
4877
  return function() {
4878
4878
  return value;
4879
4879
  };
@@ -5055,7 +5055,7 @@ var require_lodash = __commonJS({
5055
5055
  lodash.concat = concat;
5056
5056
  lodash.cond = cond;
5057
5057
  lodash.conforms = conforms;
5058
- lodash.constant = constant;
5058
+ lodash.constant = constant2;
5059
5059
  lodash.countBy = countBy;
5060
5060
  lodash.create = create;
5061
5061
  lodash.curry = curry;
@@ -5557,8 +5557,9 @@ function isHost(playerId, game) {
5557
5557
  let player = getPlayer(playerId, game);
5558
5558
  return player == null ? void 0 : player.host;
5559
5559
  }
5560
- function wrapReducer(reducer) {
5560
+ function wrapReducer(reducer, acceptPlayer) {
5561
5561
  return (game, internalAction) => {
5562
+ var _a;
5562
5563
  switch (internalAction.type) {
5563
5564
  case 1 /* Update */:
5564
5565
  return internalAction.game;
@@ -5569,15 +5570,32 @@ function wrapReducer(reducer) {
5569
5570
  });
5570
5571
  }
5571
5572
  return game;
5573
+ case 2 /* Accept */:
5574
+ if (!!game) {
5575
+ (_a = internalAction.client) == null ? void 0 : _a.emit(
5576
+ acceptPlayer(game) ? "accept" : "decline",
5577
+ {
5578
+ gameId: game.id,
5579
+ playerId: internalAction.playerId
5580
+ }
5581
+ );
5582
+ }
5583
+ return game;
5572
5584
  }
5573
5585
  };
5574
5586
  }
5575
- function useRemoteReducer(uri, reducer, gameId) {
5587
+ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash.constant)(
5588
+ true
5589
+ ), storage = sessionStorage) {
5576
5590
  let [meta, setMeta] = (0, import_react.useState)({
5577
5591
  isHostReady: false,
5578
- isGuestReady: false
5592
+ isGuestReady: false,
5593
+ declined: false
5579
5594
  });
5580
- let [game, internalDispatch] = (0, import_react.useReducer)(wrapReducer(reducer), void 0);
5595
+ let [game, internalDispatch] = (0, import_react.useReducer)(
5596
+ wrapReducer(reducer, acceptPlayer),
5597
+ void 0
5598
+ );
5581
5599
  let dispatch = (action) => {
5582
5600
  if (!!meta.client && !!game) {
5583
5601
  meta.client.emit("notify", { gameId: game.id, action });
@@ -5598,7 +5616,7 @@ function useRemoteReducer(uri, reducer, gameId) {
5598
5616
  (_a = meta.client) == null ? void 0 : _a.emit("create");
5599
5617
  } else if (!!gameId) {
5600
5618
  let playerCache = (0, import_lodash.flow)(
5601
- () => !!gameId ? sessionStorage.getItem(gameId) : null,
5619
+ () => !!gameId ? storage.getItem(gameId) : null,
5602
5620
  (cache) => !!cache ? JSON.parse(cache) : null
5603
5621
  )();
5604
5622
  if (!!playerCache) {
@@ -5616,12 +5634,12 @@ function useRemoteReducer(uri, reducer, gameId) {
5616
5634
  meta.client.on("assign", (assign) => {
5617
5635
  var _a, _b;
5618
5636
  setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
5619
- if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id) && !!(game == null ? void 0 : game.id)) {
5637
+ if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
5620
5638
  let playerCache = {
5621
5639
  playerId: assign.playerId,
5622
5640
  socketId: (_b = meta.client) == null ? void 0 : _b.id
5623
5641
  };
5624
- sessionStorage.setItem(game.id, JSON.stringify(playerCache));
5642
+ storage.setItem(assign.gameId, JSON.stringify(playerCache));
5625
5643
  }
5626
5644
  });
5627
5645
  meta.client.on(
@@ -5631,15 +5649,18 @@ function useRemoteReducer(uri, reducer, gameId) {
5631
5649
  game: update.game
5632
5650
  })
5633
5651
  );
5652
+ meta.client.on("decline", () => setMeta(__spreadProps(__spreadValues({}, meta), { declined: true })));
5634
5653
  setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5635
5654
  }
5636
5655
  if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5637
5656
  meta.client.on("join", (join) => {
5638
- var _a;
5639
- (_a = meta.client) == null ? void 0 : _a.emit("accept", {
5640
- gameId: game == null ? void 0 : game.id,
5641
- playerId: join.playerId
5642
- });
5657
+ if (!!meta.client) {
5658
+ internalDispatch({
5659
+ type: 2 /* Accept */,
5660
+ playerId: join.playerId,
5661
+ client: meta.client
5662
+ });
5663
+ }
5643
5664
  });
5644
5665
  meta.client.on("notify", (notify) => {
5645
5666
  if (!!game && !!meta.client) {
@@ -5655,7 +5676,7 @@ function useRemoteReducer(uri, reducer, gameId) {
5655
5676
  }
5656
5677
  }
5657
5678
  });
5658
- return [game, meta.localPlayerId, dispatch];
5679
+ return [game, meta.localPlayerId, dispatch, meta.declined];
5659
5680
  }
5660
5681
  // Annotate the CommonJS export names for ESM import in node:
5661
5682
  0 && (module.exports = {