@react-remote-state/client 1.1.5 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1823,7 +1823,7 @@ var require_lodash = __commonJS({
1823
1823
  return defineProperty(func, "toString", {
1824
1824
  "configurable": true,
1825
1825
  "enumerable": false,
1826
- "value": constant(string),
1826
+ "value": constant2(string),
1827
1827
  "writable": true
1828
1828
  });
1829
1829
  };
@@ -4374,7 +4374,7 @@ var require_lodash = __commonJS({
4374
4374
  value = nativeObjectToString.call(value);
4375
4375
  }
4376
4376
  result2[value] = key;
4377
- }, constant(identity));
4377
+ }, constant2(identity));
4378
4378
  var invertBy = createInverter(function(result2, value, key) {
4379
4379
  if (value != null && typeof value.toString != "function") {
4380
4380
  value = nativeObjectToString.call(value);
@@ -4867,7 +4867,7 @@ var require_lodash = __commonJS({
4867
4867
  function conforms(source) {
4868
4868
  return baseConforms(baseClone(source, CLONE_DEEP_FLAG));
4869
4869
  }
4870
- function constant(value) {
4870
+ function constant2(value) {
4871
4871
  return function() {
4872
4872
  return value;
4873
4873
  };
@@ -5049,7 +5049,7 @@ var require_lodash = __commonJS({
5049
5049
  lodash.concat = concat;
5050
5050
  lodash.cond = cond;
5051
5051
  lodash.conforms = conforms;
5052
- lodash.constant = constant;
5052
+ lodash.constant = constant2;
5053
5053
  lodash.countBy = countBy;
5054
5054
  lodash.create = create;
5055
5055
  lodash.curry = curry;
@@ -5544,8 +5544,9 @@ function isHost(playerId, game) {
5544
5544
  let player = getPlayer(playerId, game);
5545
5545
  return player == null ? void 0 : player.host;
5546
5546
  }
5547
- function wrapReducer(reducer) {
5547
+ function wrapReducer(reducer, acceptPlayer) {
5548
5548
  return (game, internalAction) => {
5549
+ var _a;
5549
5550
  switch (internalAction.type) {
5550
5551
  case 1 /* Update */:
5551
5552
  return internalAction.game;
@@ -5556,15 +5557,32 @@ function wrapReducer(reducer) {
5556
5557
  });
5557
5558
  }
5558
5559
  return game;
5560
+ case 2 /* Accept */:
5561
+ if (!!game) {
5562
+ (_a = internalAction.client) == null ? void 0 : _a.emit(
5563
+ acceptPlayer(game) ? "accept" : "decline",
5564
+ {
5565
+ gameId: game.id,
5566
+ playerId: internalAction.playerId
5567
+ }
5568
+ );
5569
+ }
5570
+ return game;
5559
5571
  }
5560
5572
  };
5561
5573
  }
5562
- function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
5574
+ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash.constant)(
5575
+ true
5576
+ ), storage = sessionStorage) {
5563
5577
  let [meta, setMeta] = useState({
5564
5578
  isHostReady: false,
5565
- isGuestReady: false
5579
+ isGuestReady: false,
5580
+ declined: false
5566
5581
  });
5567
- let [game, internalDispatch] = useReducer(wrapReducer(reducer), void 0);
5582
+ let [game, internalDispatch] = useReducer(
5583
+ wrapReducer(reducer, acceptPlayer),
5584
+ void 0
5585
+ );
5568
5586
  let dispatch = (action) => {
5569
5587
  if (!!meta.client && !!game) {
5570
5588
  meta.client.emit("notify", { gameId: game.id, action });
@@ -5618,15 +5636,18 @@ function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
5618
5636
  game: update.game
5619
5637
  })
5620
5638
  );
5639
+ meta.client.on("decline", () => setMeta(__spreadProps(__spreadValues({}, meta), { declined: true })));
5621
5640
  setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5622
5641
  }
5623
5642
  if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5624
5643
  meta.client.on("join", (join) => {
5625
- var _a;
5626
- (_a = meta.client) == null ? void 0 : _a.emit("accept", {
5627
- gameId: game == null ? void 0 : game.id,
5628
- playerId: join.playerId
5629
- });
5644
+ if (!!meta.client) {
5645
+ internalDispatch({
5646
+ type: 2 /* Accept */,
5647
+ playerId: join.playerId,
5648
+ client: meta.client
5649
+ });
5650
+ }
5630
5651
  });
5631
5652
  meta.client.on("notify", (notify) => {
5632
5653
  if (!!game && !!meta.client) {
@@ -5642,7 +5663,7 @@ function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
5642
5663
  }
5643
5664
  }
5644
5665
  });
5645
- return [game, meta.localPlayerId, dispatch];
5666
+ return [game, meta.localPlayerId, dispatch, meta.declined];
5646
5667
  }
5647
5668
  export {
5648
5669
  getPlayer,