@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.mjs CHANGED
@@ -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,7 +5550,96 @@ 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, acceptPlayer) {
5553
+
5554
+ // src/hooks.ts
5555
+ function useClientInitialization(meta, setMeta, uri) {
5556
+ useEffect(() => {
5557
+ if (!meta.client) {
5558
+ setMeta(__spreadProps(__spreadValues({}, meta), {
5559
+ client: socket_default.connect(uri, { transports: ["websocket"] })
5560
+ }));
5561
+ }
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
5582
+ };
5583
+ (_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
5584
+ } else {
5585
+ (_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
5586
+ }
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) {
5615
+ if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5616
+ meta.client.on("join", (join) => {
5617
+ if (!!meta.client) {
5618
+ internalDispatch({
5619
+ type: 2 /* Accept */,
5620
+ playerId: join.playerId,
5621
+ client: meta.client
5622
+ });
5623
+ }
5624
+ });
5625
+ meta.client.on("notify", (notify) => {
5626
+ if (!!game && !!meta.client) {
5627
+ internalDispatch({
5628
+ type: 0 /* Reduce */,
5629
+ client: meta.client,
5630
+ playerId: notify.playerId,
5631
+ action: notify.action
5632
+ });
5633
+ }
5634
+ });
5635
+ setMeta(__spreadProps(__spreadValues({}, meta), { isHostReady: true }));
5636
+ }
5637
+ }
5638
+ });
5639
+ }
5640
+
5641
+ // src/reducer.ts
5642
+ function createInternalReducer(reducer, acceptPlayer) {
5548
5643
  return (game, internalAction) => {
5549
5644
  var _a;
5550
5645
  switch (internalAction.type) {
@@ -5571,7 +5666,9 @@ function wrapReducer(reducer, acceptPlayer) {
5571
5666
  }
5572
5667
  };
5573
5668
  }
5574
- function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash.constant)(
5669
+
5670
+ // src/index.ts
5671
+ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash2.constant)(
5575
5672
  true
5576
5673
  ), storage = sessionStorage) {
5577
5674
  let [meta, setMeta] = useState({
@@ -5580,7 +5677,7 @@ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash
5580
5677
  declined: false
5581
5678
  });
5582
5679
  let [game, internalDispatch] = useReducer(
5583
- wrapReducer(reducer, acceptPlayer),
5680
+ createInternalReducer(reducer, acceptPlayer),
5584
5681
  void 0
5585
5682
  );
5586
5683
  let dispatch = (action) => {
@@ -5588,86 +5685,21 @@ function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash
5588
5685
  meta.client.emit("notify", { gameId: game.id, action });
5589
5686
  }
5590
5687
  };
5591
- useEffect(() => {
5592
- if (!meta.client) {
5593
- setMeta(__spreadProps(__spreadValues({}, meta), {
5594
- client: socket_default.connect(uri, { transports: ["websocket"] })
5595
- }));
5596
- }
5597
- if (!!meta.client) {
5598
- if (!meta.isGuestReady) {
5599
- meta.client.on("error", console.error);
5600
- meta.client.on("connect", () => {
5601
- var _a, _b, _c;
5602
- if (gameId == void 0 && !game) {
5603
- (_a = meta.client) == null ? void 0 : _a.emit("create");
5604
- } else if (!!gameId) {
5605
- let playerCache = (0, import_lodash.flow)(
5606
- () => !!gameId ? storage.getItem(gameId) : null,
5607
- (cache) => !!cache ? JSON.parse(cache) : null
5608
- )();
5609
- if (!!playerCache) {
5610
- let rejoin = {
5611
- playerId: playerCache.playerId,
5612
- socketId: playerCache.socketId,
5613
- gameId
5614
- };
5615
- (_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
5616
- } else {
5617
- (_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
5618
- }
5619
- }
5620
- });
5621
- meta.client.on("assign", (assign) => {
5622
- var _a, _b;
5623
- setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
5624
- if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
5625
- let playerCache = {
5626
- playerId: assign.playerId,
5627
- socketId: (_b = meta.client) == null ? void 0 : _b.id
5628
- };
5629
- storage.setItem(assign.gameId, JSON.stringify(playerCache));
5630
- }
5631
- });
5632
- meta.client.on(
5633
- "update",
5634
- (update) => internalDispatch({
5635
- type: 1 /* Update */,
5636
- game: update.game
5637
- })
5638
- );
5639
- meta.client.on("decline", () => setMeta(__spreadProps(__spreadValues({}, meta), { declined: true })));
5640
- setMeta(__spreadProps(__spreadValues({}, meta), { isGuestReady: true }));
5641
- }
5642
- if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
5643
- meta.client.on("join", (join) => {
5644
- if (!!meta.client) {
5645
- internalDispatch({
5646
- type: 2 /* Accept */,
5647
- playerId: join.playerId,
5648
- client: meta.client
5649
- });
5650
- }
5651
- });
5652
- meta.client.on("notify", (notify) => {
5653
- if (!!game && !!meta.client) {
5654
- internalDispatch({
5655
- type: 0 /* Reduce */,
5656
- client: meta.client,
5657
- playerId: notify.playerId,
5658
- action: notify.action
5659
- });
5660
- }
5661
- });
5662
- setMeta(__spreadProps(__spreadValues({}, meta), { isHostReady: true }));
5663
- }
5664
- }
5665
- });
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
+ }
5666
5700
  return [game, meta.localPlayerId, dispatch, meta.declined];
5667
5701
  }
5668
5702
  export {
5669
- getPlayer,
5670
- isHost,
5671
5703
  useRemoteReducer
5672
5704
  };
5673
5705
  /*! Bundled license information: