@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.d.mts +8 -13
- package/dist/index.d.ts +8 -13
- package/dist/index.js +134 -83
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +135 -82
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
@@ -1,17 +1,12 @@
|
|
1
1
|
import { Game } from '@react-remote-state/types';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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 | null, storage?: Storage): [
|
12
|
-
Game<GameCustom, PlayerCustom> | undefined,
|
13
|
-
string | undefined,
|
14
|
-
(action: Action) => void
|
3
|
+
type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
|
4
|
+
|
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): [
|
6
|
+
game: Game<GameCustom, PlayerCustom> | undefined,
|
7
|
+
localPlayerId: string | undefined,
|
8
|
+
dispatch: (action: Action) => void,
|
9
|
+
declined: boolean
|
15
10
|
];
|
16
11
|
|
17
|
-
export {
|
12
|
+
export { useRemoteReducer };
|
package/dist/index.d.ts
CHANGED
@@ -1,17 +1,12 @@
|
|
1
1
|
import { Game } from '@react-remote-state/types';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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 | null, storage?: Storage): [
|
12
|
-
Game<GameCustom, PlayerCustom> | undefined,
|
13
|
-
string | undefined,
|
14
|
-
(action: Action) => void
|
3
|
+
type Reducer<GameCustom, PlayerCustom, Action> = (game: Game<GameCustom, PlayerCustom>, action: Action, playerId: string) => Game<GameCustom, PlayerCustom>;
|
4
|
+
|
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): [
|
6
|
+
game: Game<GameCustom, PlayerCustom> | undefined,
|
7
|
+
localPlayerId: string | undefined,
|
8
|
+
dispatch: (action: Action) => void,
|
9
|
+
declined: boolean
|
15
10
|
];
|
16
11
|
|
17
|
-
export {
|
12
|
+
export { 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":
|
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
|
-
},
|
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
|
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 =
|
5058
|
+
lodash.constant = constant2;
|
5059
5059
|
lodash.countBy = countBy;
|
5060
5060
|
lodash.create = create;
|
5061
5061
|
lodash.curry = curry;
|
@@ -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/
|
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,89 +5561,77 @@ function isHost(playerId, game) {
|
|
5557
5561
|
let player = getPlayer(playerId, game);
|
5558
5562
|
return player == null ? void 0 : player.host;
|
5559
5563
|
}
|
5560
|
-
|
5561
|
-
|
5562
|
-
|
5563
|
-
case 1 /* Update */:
|
5564
|
-
return internalAction.game;
|
5565
|
-
case 0 /* Reduce */:
|
5566
|
-
if (!!game) {
|
5567
|
-
internalAction.client.emit("update", {
|
5568
|
-
game: reducer(game, internalAction.action, internalAction.playerId)
|
5569
|
-
});
|
5570
|
-
}
|
5571
|
-
return game;
|
5572
|
-
}
|
5573
|
-
};
|
5574
|
-
}
|
5575
|
-
function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
|
5576
|
-
let [meta, setMeta] = (0, import_react.useState)({
|
5577
|
-
isHostReady: false,
|
5578
|
-
isGuestReady: false
|
5579
|
-
});
|
5580
|
-
let [game, internalDispatch] = (0, import_react.useReducer)(wrapReducer(reducer), void 0);
|
5581
|
-
let dispatch = (action) => {
|
5582
|
-
if (!!meta.client && !!game) {
|
5583
|
-
meta.client.emit("notify", { gameId: game.id, action });
|
5584
|
-
}
|
5585
|
-
};
|
5564
|
+
|
5565
|
+
// src/hooks.ts
|
5566
|
+
function useClientInitialization(meta, setMeta, uri) {
|
5586
5567
|
(0, import_react.useEffect)(() => {
|
5587
5568
|
if (!meta.client) {
|
5588
5569
|
setMeta(__spreadProps(__spreadValues({}, meta), {
|
5589
5570
|
client: socket_default.connect(uri, { transports: ["websocket"] })
|
5590
5571
|
}));
|
5591
5572
|
}
|
5592
|
-
|
5593
|
-
|
5594
|
-
|
5595
|
-
|
5596
|
-
|
5597
|
-
|
5598
|
-
|
5599
|
-
|
5600
|
-
|
5601
|
-
|
5602
|
-
|
5603
|
-
|
5604
|
-
|
5605
|
-
|
5606
|
-
|
5607
|
-
|
5608
|
-
|
5609
|
-
|
5610
|
-
|
5611
|
-
|
5612
|
-
(_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
|
5613
|
-
}
|
5614
|
-
}
|
5615
|
-
});
|
5616
|
-
meta.client.on("assign", (assign) => {
|
5617
|
-
var _a, _b;
|
5618
|
-
setMeta(__spreadProps(__spreadValues({}, meta), { localPlayerId: assign.playerId }));
|
5619
|
-
if (!!((_a = meta == null ? void 0 : meta.client) == null ? void 0 : _a.id)) {
|
5620
|
-
let playerCache = {
|
5621
|
-
playerId: assign.playerId,
|
5622
|
-
socketId: (_b = meta.client) == null ? void 0 : _b.id
|
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
|
5623
5593
|
};
|
5624
|
-
|
5594
|
+
(_b = meta.client) == null ? void 0 : _b.emit("rejoin", rejoin);
|
5595
|
+
} else {
|
5596
|
+
(_c = meta.client) == null ? void 0 : _c.emit("join", { gameId });
|
5625
5597
|
}
|
5626
|
-
}
|
5627
|
-
|
5628
|
-
|
5629
|
-
|
5630
|
-
|
5631
|
-
|
5632
|
-
|
5633
|
-
|
5634
|
-
|
5635
|
-
|
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) {
|
5636
5626
|
if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {
|
5637
5627
|
meta.client.on("join", (join) => {
|
5638
|
-
|
5639
|
-
|
5640
|
-
|
5641
|
-
|
5642
|
-
|
5628
|
+
if (!!meta.client) {
|
5629
|
+
internalDispatch({
|
5630
|
+
type: 2 /* Accept */,
|
5631
|
+
playerId: join.playerId,
|
5632
|
+
client: meta.client
|
5633
|
+
});
|
5634
|
+
}
|
5643
5635
|
});
|
5644
5636
|
meta.client.on("notify", (notify) => {
|
5645
5637
|
if (!!game && !!meta.client) {
|
@@ -5655,12 +5647,71 @@ function useRemoteReducer(uri, reducer, gameId, storage = sessionStorage) {
|
|
5655
5647
|
}
|
5656
5648
|
}
|
5657
5649
|
});
|
5658
|
-
|
5650
|
+
}
|
5651
|
+
|
5652
|
+
// src/reducer.ts
|
5653
|
+
function createInternalReducer(reducer, acceptPlayer) {
|
5654
|
+
return (game, internalAction) => {
|
5655
|
+
var _a;
|
5656
|
+
switch (internalAction.type) {
|
5657
|
+
case 1 /* Update */:
|
5658
|
+
return internalAction.game;
|
5659
|
+
case 0 /* Reduce */:
|
5660
|
+
if (!!game) {
|
5661
|
+
internalAction.client.emit("update", {
|
5662
|
+
game: reducer(game, internalAction.action, internalAction.playerId)
|
5663
|
+
});
|
5664
|
+
}
|
5665
|
+
return game;
|
5666
|
+
case 2 /* Accept */:
|
5667
|
+
if (!!game) {
|
5668
|
+
(_a = internalAction.client) == null ? void 0 : _a.emit(
|
5669
|
+
acceptPlayer(game) ? "accept" : "decline",
|
5670
|
+
{
|
5671
|
+
gameId: game.id,
|
5672
|
+
playerId: internalAction.playerId
|
5673
|
+
}
|
5674
|
+
);
|
5675
|
+
}
|
5676
|
+
return game;
|
5677
|
+
}
|
5678
|
+
};
|
5679
|
+
}
|
5680
|
+
|
5681
|
+
// src/index.ts
|
5682
|
+
function useRemoteReducer(uri, reducer, gameId, acceptPlayer = (0, import_lodash2.constant)(
|
5683
|
+
true
|
5684
|
+
), storage = sessionStorage) {
|
5685
|
+
let [meta, setMeta] = (0, import_react2.useState)({
|
5686
|
+
isHostReady: false,
|
5687
|
+
isGuestReady: false,
|
5688
|
+
declined: false
|
5689
|
+
});
|
5690
|
+
let [game, internalDispatch] = (0, import_react2.useReducer)(
|
5691
|
+
createInternalReducer(reducer, acceptPlayer),
|
5692
|
+
void 0
|
5693
|
+
);
|
5694
|
+
let dispatch = (action) => {
|
5695
|
+
if (!!meta.client && !!game) {
|
5696
|
+
meta.client.emit("notify", { gameId: game.id, action });
|
5697
|
+
}
|
5698
|
+
};
|
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
|
+
}
|
5711
|
+
return [game, meta.localPlayerId, dispatch, meta.declined];
|
5659
5712
|
}
|
5660
5713
|
// Annotate the CommonJS export names for ESM import in node:
|
5661
5714
|
0 && (module.exports = {
|
5662
|
-
getPlayer,
|
5663
|
-
isHost,
|
5664
5715
|
useRemoteReducer
|
5665
5716
|
});
|
5666
5717
|
/*! Bundled license information:
|