@react-remote-state/client 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -81,7 +81,7 @@ function useRemoteReducer(uri, reducer, gameId) {
|
|
81
81
|
};
|
82
82
|
(0, import_react.useEffect)(() => {
|
83
83
|
if (!meta.client) {
|
84
|
-
meta.client = (0, import_socket.connect)(uri);
|
84
|
+
meta.client = (0, import_socket.connect)(uri, { transports: ["websocket"] });
|
85
85
|
meta.client.on("error", console.error);
|
86
86
|
meta.client.on("connect", () => {
|
87
87
|
var _a, _b;
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Game } from \"@react-remote-state/types\";\nimport { useEffect, useReducer, useState } from \"react\";\nimport { connect, Socket as Client } from \"socket.io-client\";\n\ntype Meta = {\n client?: Client;\n isHostReady: boolean;\n localPlayerId?: string;\n};\n\nexport function getPlayer<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n if (!game || !playerId) {\n return undefined;\n }\n\n return game.players.find((player) => player.id == playerId);\n}\n\nexport function isHost<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n let player = getPlayer(playerId, game);\n\n return player?.host;\n}\n\ntype Reducer<GameCustom, PlayerCustom, Action> = (\n game: Game<GameCustom, PlayerCustom>,\n action: Action,\n playerId: string\n) => Game<GameCustom, PlayerCustom>;\n\nenum InternalActionType {\n Reduce,\n Update,\n}\n\ntype InternalReduceAction<Action> = {\n type: InternalActionType.Reduce;\n client: Client;\n playerId: string;\n action: Action;\n};\n\ntype InternalUpdateAction<GameCustom, PlayerCustom> = {\n type: InternalActionType.Update;\n game: Game<GameCustom, PlayerCustom>;\n};\n\ntype InternalAction<GameCustom, PlayerCustom, Action> =\n | InternalReduceAction<Action>\n | InternalUpdateAction<GameCustom, PlayerCustom>;\n\nfunction wrapReducer<GameCustom, PlayerCustom, Action>(\n reducer: Reducer<GameCustom, PlayerCustom, Action>\n) {\n return (\n game: Game<GameCustom, PlayerCustom> | undefined,\n internalAction: InternalAction<GameCustom, PlayerCustom, Action>\n ) => {\n switch (internalAction.type) {\n case InternalActionType.Update:\n return internalAction.game;\n case InternalActionType.Reduce:\n if (!!game) {\n internalAction.client.emit(\"update\", {\n game: reducer(game, internalAction.action, internalAction.playerId),\n });\n }\n\n return game;\n }\n };\n}\n\nexport function useRemoteReducer<GameCustom, PlayerCustom, Action>(\n uri: string,\n reducer: Reducer<GameCustom, PlayerCustom, Action>,\n gameId?: string\n): [\n Game<GameCustom, PlayerCustom> | undefined,\n string | undefined,\n (action: Action) => void\n] {\n let [meta, setMeta] = useState<Meta>({\n isHostReady: false,\n });\n\n let [game, internalDispatch] = useReducer(wrapReducer(reducer), undefined);\n\n let dispatch = (action: Action) => {\n if (!!meta.client && !!game) {\n meta.client.emit(\"notify\", { gameId: game.id, action });\n }\n };\n\n useEffect(() => {\n if (!meta.client) {\n meta.client = connect(uri);\n meta.client.on(\"error\", console.error);\n meta.client.on(\"connect\", () => {\n if (gameId == undefined) {\n meta.client?.emit(\"create\");\n } else {\n meta.client?.emit(\"join\", { gameId });\n }\n });\n\n meta.client.on(\"assign\", (assign) => {\n setMeta({ ...meta, localPlayerId: assign.playerId });\n });\n\n meta.client.on(\"update\", (update) =>\n internalDispatch({ type: InternalActionType.Update, game: update.game })\n );\n } else if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {\n meta.client.on(\"join\", (join) => {\n meta.client?.emit(\"accept\", {\n gameId: game?.id,\n playerId: join.playerId,\n });\n });\n\n meta.client.on(\"notify\", (notify) => {\n if (!!game && !!meta.client) {\n internalDispatch({\n type: InternalActionType.Reduce,\n client: meta.client,\n playerId: notify.playerId,\n action: notify.action,\n });\n }\n });\n\n setMeta({ ...meta, isHostReady: true });\n }\n });\n\n return [game, meta.localPlayerId, dispatch];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAgD;AAChD,oBAA0C;AAQnC,SAAS,UACd,UACA,MACA;AACA,MAAI,CAAC,QAAQ,CAAC,UAAU;AACtB,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,QAAQ,KAAK,CAAC,WAAW,OAAO,MAAM,QAAQ;AAC5D;AAEO,SAAS,OACd,UACA,MACA;AACA,MAAI,SAAS,UAAU,UAAU,IAAI;AAErC,SAAO,iCAAQ;AACjB;AA6BA,SAAS,YACP,SACA;AACA,SAAO,CACL,MACA,mBACG;AACH,YAAQ,eAAe,MAAM;AAAA,MAC3B,KAAK;AACH,eAAO,eAAe;AAAA,MACxB,KAAK;AACH,YAAI,CAAC,CAAC,MAAM;AACV,yBAAe,OAAO,KAAK,UAAU;AAAA,YACnC,MAAM,QAAQ,MAAM,eAAe,QAAQ,eAAe,QAAQ;AAAA,UACpE,CAAC;AAAA,QACH;AAEA,eAAO;AAAA,IACX;AAAA,EACF;AACF;AAEO,SAAS,iBACd,KACA,SACA,QAKA;AACA,MAAI,CAAC,MAAM,OAAO,QAAI,uBAAe;AAAA,IACnC,aAAa;AAAA,EACf,CAAC;AAED,MAAI,CAAC,MAAM,gBAAgB,QAAI,yBAAW,YAAY,OAAO,GAAG,MAAS;AAEzE,MAAI,WAAW,CAAC,WAAmB;AACjC,QAAI,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,MAAM;AAC3B,WAAK,OAAO,KAAK,UAAU,EAAE,QAAQ,KAAK,IAAI,OAAO,CAAC;AAAA,IACxD;AAAA,EACF;AAEA,8BAAU,MAAM;AACd,QAAI,CAAC,KAAK,QAAQ;AAChB,WAAK,aAAS,uBAAQ,
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Game } from \"@react-remote-state/types\";\nimport { useEffect, useReducer, useState } from \"react\";\nimport { connect, Socket as Client } from \"socket.io-client\";\n\ntype Meta = {\n client?: Client;\n isHostReady: boolean;\n localPlayerId?: string;\n};\n\nexport function getPlayer<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n if (!game || !playerId) {\n return undefined;\n }\n\n return game.players.find((player) => player.id == playerId);\n}\n\nexport function isHost<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n let player = getPlayer(playerId, game);\n\n return player?.host;\n}\n\ntype Reducer<GameCustom, PlayerCustom, Action> = (\n game: Game<GameCustom, PlayerCustom>,\n action: Action,\n playerId: string\n) => Game<GameCustom, PlayerCustom>;\n\nenum InternalActionType {\n Reduce,\n Update,\n}\n\ntype InternalReduceAction<Action> = {\n type: InternalActionType.Reduce;\n client: Client;\n playerId: string;\n action: Action;\n};\n\ntype InternalUpdateAction<GameCustom, PlayerCustom> = {\n type: InternalActionType.Update;\n game: Game<GameCustom, PlayerCustom>;\n};\n\ntype InternalAction<GameCustom, PlayerCustom, Action> =\n | InternalReduceAction<Action>\n | InternalUpdateAction<GameCustom, PlayerCustom>;\n\nfunction wrapReducer<GameCustom, PlayerCustom, Action>(\n reducer: Reducer<GameCustom, PlayerCustom, Action>\n) {\n return (\n game: Game<GameCustom, PlayerCustom> | undefined,\n internalAction: InternalAction<GameCustom, PlayerCustom, Action>\n ) => {\n switch (internalAction.type) {\n case InternalActionType.Update:\n return internalAction.game;\n case InternalActionType.Reduce:\n if (!!game) {\n internalAction.client.emit(\"update\", {\n game: reducer(game, internalAction.action, internalAction.playerId),\n });\n }\n\n return game;\n }\n };\n}\n\nexport function useRemoteReducer<GameCustom, PlayerCustom, Action>(\n uri: string,\n reducer: Reducer<GameCustom, PlayerCustom, Action>,\n gameId?: string\n): [\n Game<GameCustom, PlayerCustom> | undefined,\n string | undefined,\n (action: Action) => void\n] {\n let [meta, setMeta] = useState<Meta>({\n isHostReady: false,\n });\n\n let [game, internalDispatch] = useReducer(wrapReducer(reducer), undefined);\n\n let dispatch = (action: Action) => {\n if (!!meta.client && !!game) {\n meta.client.emit(\"notify\", { gameId: game.id, action });\n }\n };\n\n useEffect(() => {\n if (!meta.client) {\n meta.client = connect(uri, { transports: [\"websocket\"] });\n meta.client.on(\"error\", console.error);\n meta.client.on(\"connect\", () => {\n if (gameId == undefined) {\n meta.client?.emit(\"create\");\n } else {\n meta.client?.emit(\"join\", { gameId });\n }\n });\n\n meta.client.on(\"assign\", (assign) => {\n setMeta({ ...meta, localPlayerId: assign.playerId });\n });\n\n meta.client.on(\"update\", (update) =>\n internalDispatch({ type: InternalActionType.Update, game: update.game })\n );\n } else if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {\n meta.client.on(\"join\", (join) => {\n meta.client?.emit(\"accept\", {\n gameId: game?.id,\n playerId: join.playerId,\n });\n });\n\n meta.client.on(\"notify\", (notify) => {\n if (!!game && !!meta.client) {\n internalDispatch({\n type: InternalActionType.Reduce,\n client: meta.client,\n playerId: notify.playerId,\n action: notify.action,\n });\n }\n });\n\n setMeta({ ...meta, isHostReady: true });\n }\n });\n\n return [game, meta.localPlayerId, dispatch];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAgD;AAChD,oBAA0C;AAQnC,SAAS,UACd,UACA,MACA;AACA,MAAI,CAAC,QAAQ,CAAC,UAAU;AACtB,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,QAAQ,KAAK,CAAC,WAAW,OAAO,MAAM,QAAQ;AAC5D;AAEO,SAAS,OACd,UACA,MACA;AACA,MAAI,SAAS,UAAU,UAAU,IAAI;AAErC,SAAO,iCAAQ;AACjB;AA6BA,SAAS,YACP,SACA;AACA,SAAO,CACL,MACA,mBACG;AACH,YAAQ,eAAe,MAAM;AAAA,MAC3B,KAAK;AACH,eAAO,e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WAAK,OAAO,GAAG,QAAQ,CAAC,SAAS;AAxHvC;AAyHQ,mBAAK,WAAL,mBAAa,KAAK,UAAU;AAAA,UAC1B,QAAQ,6BAAM;AAAA,UACd,UAAU,KAAK;AAAA,QACjB;AAAA,MACF,CAAC;AAED,WAAK,OAAO,GAAG,UAAU,CAAC,WAAW;AACnC,YAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,QAAQ;AAC3B,2BAAiB;AAAA,YACf,MAAM;AAAA,YACN,QAAQ,KAAK;AAAA,YACb,UAAU,OAAO;AAAA,YACjB,QAAQ,OAAO;AAAA,UACjB,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAED,cAAQ,iCAAK,OAAL,EAAW,aAAa,KAAK,EAAC;AAAA,IACxC;AAAA,EACF,CAAC;AAED,SAAO,CAAC,MAAM,KAAK,eAAe,QAAQ;AAC5C;","names":[]}
|
package/dist/index.mjs
CHANGED
@@ -58,7 +58,7 @@ function useRemoteReducer(uri, reducer, gameId) {
|
|
58
58
|
};
|
59
59
|
useEffect(() => {
|
60
60
|
if (!meta.client) {
|
61
|
-
meta.client = connect(uri);
|
61
|
+
meta.client = connect(uri, { transports: ["websocket"] });
|
62
62
|
meta.client.on("error", console.error);
|
63
63
|
meta.client.on("connect", () => {
|
64
64
|
var _a, _b;
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Game } from \"@react-remote-state/types\";\nimport { useEffect, useReducer, useState } from \"react\";\nimport { connect, Socket as Client } from \"socket.io-client\";\n\ntype Meta = {\n client?: Client;\n isHostReady: boolean;\n localPlayerId?: string;\n};\n\nexport function getPlayer<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n if (!game || !playerId) {\n return undefined;\n }\n\n return game.players.find((player) => player.id == playerId);\n}\n\nexport function isHost<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n let player = getPlayer(playerId, game);\n\n return player?.host;\n}\n\ntype Reducer<GameCustom, PlayerCustom, Action> = (\n game: Game<GameCustom, PlayerCustom>,\n action: Action,\n playerId: string\n) => Game<GameCustom, PlayerCustom>;\n\nenum InternalActionType {\n Reduce,\n Update,\n}\n\ntype InternalReduceAction<Action> = {\n type: InternalActionType.Reduce;\n client: Client;\n playerId: string;\n action: Action;\n};\n\ntype InternalUpdateAction<GameCustom, PlayerCustom> = {\n type: InternalActionType.Update;\n game: Game<GameCustom, PlayerCustom>;\n};\n\ntype InternalAction<GameCustom, PlayerCustom, Action> =\n | InternalReduceAction<Action>\n | InternalUpdateAction<GameCustom, PlayerCustom>;\n\nfunction wrapReducer<GameCustom, PlayerCustom, Action>(\n reducer: Reducer<GameCustom, PlayerCustom, Action>\n) {\n return (\n game: Game<GameCustom, PlayerCustom> | undefined,\n internalAction: InternalAction<GameCustom, PlayerCustom, Action>\n ) => {\n switch (internalAction.type) {\n case InternalActionType.Update:\n return internalAction.game;\n case InternalActionType.Reduce:\n if (!!game) {\n internalAction.client.emit(\"update\", {\n game: reducer(game, internalAction.action, internalAction.playerId),\n });\n }\n\n return game;\n }\n };\n}\n\nexport function useRemoteReducer<GameCustom, PlayerCustom, Action>(\n uri: string,\n reducer: Reducer<GameCustom, PlayerCustom, Action>,\n gameId?: string\n): [\n Game<GameCustom, PlayerCustom> | undefined,\n string | undefined,\n (action: Action) => void\n] {\n let [meta, setMeta] = useState<Meta>({\n isHostReady: false,\n });\n\n let [game, internalDispatch] = useReducer(wrapReducer(reducer), undefined);\n\n let dispatch = (action: Action) => {\n if (!!meta.client && !!game) {\n meta.client.emit(\"notify\", { gameId: game.id, action });\n }\n };\n\n useEffect(() => {\n if (!meta.client) {\n meta.client = connect(uri);\n meta.client.on(\"error\", console.error);\n meta.client.on(\"connect\", () => {\n if (gameId == undefined) {\n meta.client?.emit(\"create\");\n } else {\n meta.client?.emit(\"join\", { gameId });\n }\n });\n\n meta.client.on(\"assign\", (assign) => {\n setMeta({ ...meta, localPlayerId: assign.playerId });\n });\n\n meta.client.on(\"update\", (update) =>\n internalDispatch({ type: InternalActionType.Update, game: update.game })\n );\n } else if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {\n meta.client.on(\"join\", (join) => {\n meta.client?.emit(\"accept\", {\n gameId: game?.id,\n playerId: join.playerId,\n });\n });\n\n meta.client.on(\"notify\", (notify) => {\n if (!!game && !!meta.client) {\n internalDispatch({\n type: InternalActionType.Reduce,\n client: meta.client,\n playerId: notify.playerId,\n action: notify.action,\n });\n }\n });\n\n setMeta({ ...meta, isHostReady: true });\n }\n });\n\n return [game, meta.localPlayerId, dispatch];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,SAAS,WAAW,YAAY,gBAAgB;AAChD,SAAS,eAAiC;AAQnC,SAAS,UACd,UACA,MACA;AACA,MAAI,CAAC,QAAQ,CAAC,UAAU;AACtB,WAAO;AAAA,EACT;AAEA,SAAO,KAAK,QAAQ,KAAK,CAAC,WAAW,OAAO,MAAM,QAAQ;AAC5D;AAEO,SAAS,OACd,UACA,MACA;AACA,MAAI,SAAS,UAAU,UAAU,IAAI;AAErC,SAAO,iCAAQ;AACjB;AA6BA,SAAS,YACP,SACA;AACA,SAAO,CACL,MACA,mBACG;AACH,YAAQ,eAAe,MAAM;AAAA,MAC3B,KAAK;AACH,eAAO,eAAe;AAAA,MACxB,KAAK;AACH,YAAI,CAAC,CAAC,MAAM;AACV,yBAAe,OAAO,KAAK,UAAU;AAAA,YACnC,MAAM,QAAQ,MAAM,eAAe,QAAQ,eAAe,QAAQ;AAAA,UACpE,CAAC;AAAA,QACH;AAEA,eAAO;AAAA,IACX;AAAA,EACF;AACF;AAEO,SAAS,iBACd,KACA,SACA,QAKA;AACA,MAAI,CAAC,MAAM,OAAO,IAAI,SAAe;AAAA,IACnC,aAAa;AAAA,EACf,CAAC;AAED,MAAI,CAAC,MAAM,gBAAgB,IAAI,WAAW,YAAY,OAAO,GAAG,MAAS;AAEzE,MAAI,WAAW,CAAC,WAAmB;AACjC,QAAI,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,MAAM;AAC3B,WAAK,OAAO,KAAK,UAAU,EAAE,QAAQ,KAAK,IAAI,OAAO,CAAC;AAAA,IACxD;AAAA,EACF;AAEA,YAAU,MAAM;AACd,QAAI,CAAC,KAAK,QAAQ;AAChB,WAAK,SAAS,QAAQ,
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Game } from \"@react-remote-state/types\";\nimport { useEffect, useReducer, useState } from \"react\";\nimport { connect, Socket as Client } from \"socket.io-client\";\n\ntype Meta = {\n client?: Client;\n isHostReady: boolean;\n localPlayerId?: string;\n};\n\nexport function getPlayer<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n if (!game || !playerId) {\n return undefined;\n }\n\n return game.players.find((player) => player.id == playerId);\n}\n\nexport function isHost<GameCustom, PlayerCustom>(\n playerId?: string,\n game?: Game<GameCustom, PlayerCustom>\n) {\n let player = getPlayer(playerId, game);\n\n return player?.host;\n}\n\ntype Reducer<GameCustom, PlayerCustom, Action> = (\n game: Game<GameCustom, PlayerCustom>,\n action: Action,\n playerId: string\n) => Game<GameCustom, PlayerCustom>;\n\nenum InternalActionType {\n Reduce,\n Update,\n}\n\ntype InternalReduceAction<Action> = {\n type: InternalActionType.Reduce;\n client: Client;\n playerId: string;\n action: Action;\n};\n\ntype InternalUpdateAction<GameCustom, PlayerCustom> = {\n type: InternalActionType.Update;\n game: Game<GameCustom, PlayerCustom>;\n};\n\ntype InternalAction<GameCustom, PlayerCustom, Action> =\n | InternalReduceAction<Action>\n | InternalUpdateAction<GameCustom, PlayerCustom>;\n\nfunction wrapReducer<GameCustom, PlayerCustom, Action>(\n reducer: Reducer<GameCustom, PlayerCustom, Action>\n) {\n return (\n game: Game<GameCustom, PlayerCustom> | undefined,\n internalAction: InternalAction<GameCustom, PlayerCustom, Action>\n ) => {\n switch (internalAction.type) {\n case InternalActionType.Update:\n return internalAction.game;\n case InternalActionType.Reduce:\n if (!!game) {\n internalAction.client.emit(\"update\", {\n game: reducer(game, internalAction.action, internalAction.playerId),\n });\n }\n\n return game;\n }\n };\n}\n\nexport function useRemoteReducer<GameCustom, PlayerCustom, Action>(\n uri: string,\n reducer: Reducer<GameCustom, PlayerCustom, Action>,\n gameId?: string\n): [\n Game<GameCustom, PlayerCustom> | undefined,\n string | undefined,\n (action: Action) => void\n] {\n let [meta, setMeta] = useState<Meta>({\n isHostReady: false,\n });\n\n let [game, internalDispatch] = useReducer(wrapReducer(reducer), undefined);\n\n let dispatch = (action: Action) => {\n if (!!meta.client && !!game) {\n meta.client.emit(\"notify\", { gameId: game.id, action });\n }\n };\n\n useEffect(() => {\n if (!meta.client) {\n meta.client = connect(uri, { transports: [\"websocket\"] });\n meta.client.on(\"error\", console.error);\n meta.client.on(\"connect\", () => {\n if (gameId == undefined) {\n meta.client?.emit(\"create\");\n } else {\n meta.client?.emit(\"join\", { gameId });\n }\n });\n\n meta.client.on(\"assign\", (assign) => {\n setMeta({ ...meta, localPlayerId: assign.playerId });\n });\n\n meta.client.on(\"update\", (update) =>\n internalDispatch({ type: InternalActionType.Update, game: update.game })\n );\n } else if (isHost(meta.localPlayerId, game) && !meta.isHostReady) {\n meta.client.on(\"join\", (join) => {\n meta.client?.emit(\"accept\", {\n gameId: game?.id,\n playerId: join.playerId,\n });\n });\n\n meta.client.on(\"notify\", (notify) => {\n if (!!game && !!meta.client) {\n internalDispatch({\n type: InternalActionType.Reduce,\n client: meta.client,\n playerId: notify.playerId,\n action: notify.action,\n });\n }\n });\n\n setMeta({ ...meta, isHostReady: true });\n }\n });\n\n return [game, meta.localPlayerId, dispatch];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,SAAS,WAAW,YAAY,gBAAgB;AAChD,SAAS,eAAiC;AAQnC,SAAS,UACd,UACA,MACA;AACA,MAAI,CAAC,QAAQ,CAAC,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