@react-remote-state/client 1.1.5 → 1.1.8

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
@@ -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;
@@ -5523,7 +5523,11 @@ var require_lodash = __commonJS({
5523
5523
  });
5524
5524
 
5525
5525
  // src/index.ts
5526
- import { useEffect, useReducer, useState } from "react";
5526
+ var import_lodash2 = __toESM(require_lodash());
5527
+ import { useReducer, useState } from "react";
5528
+
5529
+ // src/hooks.ts
5530
+ import { useEffect } from "react";
5527
5531
 
5528
5532
  // src/socket.ts
5529
5533
  import { connect } from "socket.io-client";
@@ -5532,8 +5536,10 @@ var mod = {
5532
5536
  };
5533
5537
  var socket_default = mod;
5534
5538
 
5535
- // src/index.ts
5539
+ // src/hooks.ts
5536
5540
  var import_lodash = __toESM(require_lodash());
5541
+
5542
+ // src/player.ts
5537
5543
  function getPlayer(playerId, game) {
5538
5544
  if (!game || !playerId) {
5539
5545
  return void 0;
@@ -5544,89 +5550,77 @@ function isHost(playerId, game) {
5544
5550
  let player = getPlayer(playerId, game);
5545
5551
  return player == null ? void 0 : player.host;
5546
5552
  }
5547
- function wrapReducer(reducer) {
5548
- return (game, internalAction) => {
5549
- switch (internalAction.type) {
5550
- case 1 /* Update */:
5551
- return internalAction.game;
5552
- case 0 /* Reduce */:
5553
- if (!!game) {
5554
- internalAction.client.emit("update", {
5555
- game: reducer(game, internalAction.action, internalAction.playerId)
5556
- });
5557
- }
5558
- return game;
5559
- }
5560
- };
5561
- }
5562
- function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
5563
- let [meta, setMeta] = useState({
5564
- isHostReady: false,
5565
- isGuestReady: false
5566
- });
5567
- let [game, internalDispatch] = useReducer(wrapReducer(reducer), void 0);
5568
- let dispatch = (action) => {
5569
- if (!!meta.client && !!game) {
5570
- meta.client.emit("notify", { gameId: game.id, action });
5571
- }
5572
- };
5553
+
5554
+ // src/hooks.ts
5555
+ function useClientInitialization(meta, setMeta, uri) {
5573
5556
  useEffect(() => {
5574
5557
  if (!meta.client) {
5575
5558
  setMeta(__spreadProps(__spreadValues({}, meta), {
5576
5559
  client: socket_default.connect(uri, { transports: ["websocket"] })
5577
5560
  }));
5578
5561
  }
5579
- if (!!meta.client) {
5580
- if (!meta.isGuestReady) {
5581
- meta.client.on("error", console.error);
5582
- meta.client.on("connect", () => {
5583
- var _a, _b, _c;
5584
- if (gameId == void 0 && !game) {
5585
- (_a = meta.client) == null ? void 0 : _a.emit("create");
5586
- } else if (!!gameId) {
5587
- let playerCache = (0, import_lodash.flow)(
5588
- () => !!gameId ? storage.getItem(gameId) : null,
5589
- (cache) => !!cache ? JSON.parse(cache) : null
5590
- )();
5591
- if (!!playerCache) {
5592
- let rejoin = {
5593
- playerId: playerCache.playerId,
5594
- socketId: playerCache.socketId,
5595
- gameId
5596
- };
5597
- (_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
5598
- } else {
5599
- (_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
5600
- }
5601
- }
5602
- });
5603
- meta.client.on("assign", (assign) => {
5604
- var _a, _b;
5605
- setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
5606
- if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
5607
- let playerCache = {
5608
- playerId: assign.playerId,
5609
- socketId: (_b = meta.client) == null ? void 0 : _b.id
5562
+ }, [meta.client]);
5563
+ }
5564
+ function useGuestInitialization(meta, setMeta, storage, internalDispatch, gameId, game) {
5565
+ useEffect(() => {
5566
+ if (!!meta.client && !meta.isGuestReady) {
5567
+ meta.client.on("error", console.error);
5568
+ meta.client.on("connect", () => {
5569
+ var _a, _b, _c;
5570
+ if (gameId == void 0 && !game) {
5571
+ (_a = meta.client) == null ? void 0 : _a.emit("create");
5572
+ } else if (!!gameId) {
5573
+ let playerCache = (0, import_lodash.flow)(
5574
+ () => !!gameId ? storage.getItem(gameId) : null,
5575
+ (cache) => !!cache ? JSON.parse(cache) : null
5576
+ )();
5577
+ if (!!playerCache) {
5578
+ let rejoin = {
5579
+ playerId: playerCache.playerId,
5580
+ socketId: playerCache.socketId,
5581
+ gameId
5610
5582
  };
5611
- storage.setItem(assign.gameId, JSON.stringify(playerCache));
5583
+ (_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
5584
+ } else {
5585
+ (_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
5612
5586
  }
5613
- });
5614
- meta.client.on(
5615
- "update",
5616
- (update) => internalDispatch({
5617
- type: 1 /* Update */,
5618
- game: update.game
5619
- })
5620
- );
5621
- setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5622
- }
5587
+ }
5588
+ });
5589
+ meta.client.on("assign", (assign) => {
5590
+ var _a, _b;
5591
+ setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
5592
+ if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
5593
+ let playerCache = {
5594
+ playerId: assign.playerId,
5595
+ socketId: (_b = meta.client) == null ? void 0 : _b.id
5596
+ };
5597
+ storage.setItem(assign.gameId, JSON.stringify(playerCache));
5598
+ }
5599
+ });
5600
+ meta.client.on(
5601
+ "update",
5602
+ (update) => internalDispatch({
5603
+ type: 1 /* Update */,
5604
+ game: update.game
5605
+ })
5606
+ );
5607
+ meta.client.on("decline", () => setMeta(__spreadProps(__spreadValues({}, meta), { declined: true })));
5608
+ setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5609
+ }
5610
+ }, [meta.client, meta.isGuestReady]);
5611
+ }
5612
+ function useHostInitialization(meta, setMeta, internalDispatch, game) {
5613
+ useEffect(() => {
5614
+ if (!!meta.client) {
5623
5615
  if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5624
5616
  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
- });
5617
+ if (!!meta.client) {
5618
+ internalDispatch({
5619
+ type: 2 /* Accept */,
5620
+ playerId: join.playerId,
5621
+ client: meta.client
5622
+ });
5623
+ }
5630
5624
  });
5631
5625
  meta.client.on("notify", (notify) => {
5632
5626
  if (!!game && !!meta.client) {
@@ -5642,11 +5636,70 @@ function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
5642
5636
  }
5643
5637
  }
5644
5638
  });
5645
- return [game, meta.localPlayerId, dispatch];
5639
+ }
5640
+
5641
+ // src/reducer.ts
5642
+ function createInternalReducer(reducer, acceptPlayer) {
5643
+ return (game, internalAction) => {
5644
+ var _a;
5645
+ switch (internalAction.type) {
5646
+ case 1 /* Update */:
5647
+ return internalAction.game;
5648
+ case 0 /* Reduce */:
5649
+ if (!!game) {
5650
+ internalAction.client.emit("update", {
5651
+ game: reducer(game, internalAction.action, internalAction.playerId)
5652
+ });
5653
+ }
5654
+ return game;
5655
+ case 2 /* Accept */:
5656
+ if (!!game) {
5657
+ (_a = internalAction.client) == null ? void 0 : _a.emit(
5658
+ acceptPlayer(game) ? "accept" : "decline",
5659
+ {
5660
+ gameId: game.id,
5661
+ playerId: internalAction.playerId
5662
+ }
5663
+ );
5664
+ }
5665
+ return game;
5666
+ }
5667
+ };
5668
+ }
5669
+
5670
+ // src/index.ts
5671
+ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash2.constant)(
5672
+ true
5673
+ ), storage = sessionStorage) {
5674
+ let [meta, setMeta] = useState({
5675
+ isHostReady: false,
5676
+ isGuestReady: false,
5677
+ declined: false
5678
+ });
5679
+ let [game, internalDispatch] = useReducer(
5680
+ createInternalReducer(reducer, acceptPlayer),
5681
+ void 0
5682
+ );
5683
+ let dispatch = (action) => {
5684
+ if (!!meta.client && !!game) {
5685
+ meta.client.emit("notify", { gameId: game.id, action });
5686
+ }
5687
+ };
5688
+ useClientInitialization(meta, setMeta, uri);
5689
+ useGuestInitialization(
5690
+ meta,
5691
+ setMeta,
5692
+ storage,
5693
+ internalDispatch,
5694
+ gameId,
5695
+ game
5696
+ );
5697
+ if (!gameId) {
5698
+ useHostInitialization(meta, setMeta, internalDispatch, game);
5699
+ }
5700
+ return [game, meta.localPlayerId, dispatch, meta.declined];
5646
5701
  }
5647
5702
  export {
5648
- getPlayer,
5649
- isHost,
5650
5703
  useRemoteReducer
5651
5704
  };
5652
5705
  /*! Bundled license information: