Skip to content

Commit

Permalink
batch sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Dec 15, 2024
1 parent 9e61aa4 commit 4da4b30
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
65 changes: 41 additions & 24 deletions client/src/dojo/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,52 @@ export const syncEntitiesEternum = async <S extends Schema>(
export const addToSubscription = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityID: string,
position?: { x: number; y: number },
entityID: string[],
position?: { x: number; y: number }[],
) => {
const positionClause: EntityKeysClause = {
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
};

position &&
(await getEntities(
client,
{
...positionClause,
},
components,
30_000,
false,
));
// position &&
// (await getEntities(
// client,
// {
// Composite: {
// operator: "Or",
// clauses: position.map((position) => ({
// Keys: {
// keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
// pattern_matching: "FixedLen" as PatternMatching,
// models: [],
// },
// })),
// },
// },
// components,
// 30_000,
// false,
// ));

await getEntities(
client,
{
Keys: {
keys: [entityID],
pattern_matching: "VariableLen",
models: [],
Composite: {
operator: "Or",
clauses: [
...entityID.map((id) => ({
Keys: {
keys: [id],
pattern_matching: "VariableLen" as PatternMatching,
models: [],
},
})),
...(position
? position.map((position) => ({
Keys: {
keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined],
pattern_matching: "FixedLen" as PatternMatching,
models: [],
},
}))
: []),
],
},
},
components,
Expand Down
6 changes: 2 additions & 4 deletions client/src/ui/components/entities/Entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
setIsSyncing(true);
const fetch = async () => {
try {
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
await addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, [
arrival.entityId.toString(),
);
]);
localStorage.setItem(cacheKey, now.toString());
} catch (error) {
console.error("Fetch failed", error);
Expand Down
4 changes: 3 additions & 1 deletion client/src/ui/components/resources/InventoryResources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export const InventoryResources = ({

setIsSyncing(true);
try {
await addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, entityId.toString());
await addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, [
entityId.toString(),
]);
localStorage.setItem(cacheKey, now.toString());
} catch (error) {
console.error("Fetch failed", error);
Expand Down
24 changes: 9 additions & 15 deletions client/src/ui/layouts/World.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,13 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => {
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
structureEntityId.toString(),
{ x: position?.x || 0, y: position?.y || 0 },
[structureEntityId.toString()],
[{ x: position?.x || 0, y: position?.y || 0 }],
);

await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
await addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, [
ADMIN_BANK_ENTITY_ID.toString(),
);
]);
} catch (error) {
console.error("Fetch failed", error);
} finally {
Expand Down Expand Up @@ -180,15 +178,11 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => {
}));
const fetch = async () => {
try {
await Promise.all(
filteredStructures.map((structure: PlayerStructure) =>
addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
structure.entity_id.toString(),
{ x: structure.position.x, y: structure.position.y },
),
),
await addToSubscription(
dojo.network.toriiClient,
dojo.network.contractComponents as any,
[...filteredStructures.map((structure) => structure.entity_id.toString())],
[...filteredStructures.map((structure) => ({ x: structure.position.x, y: structure.position.y }))],
);
} catch (error) {
console.error("Fetch failed", error);
Expand Down

0 comments on commit 4da4b30

Please sign in to comment.