Skip to content

Commit

Permalink
feat: useQuerySync hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Jul 6, 2024
1 parent 911f4df commit 91a7e75
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
13 changes: 12 additions & 1 deletion examples/react/react-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { useComponentValue } from "@dojoengine/react";
import { useComponentValue, useQuerySync } from "@dojoengine/react";
import { Entity } from "@dojoengine/recs";
import { useEffect, useState } from "react";
import "./App.css";
import { Direction } from "./utils";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { useDojo } from "./dojo/useDojo";
import { getSyncEntities } from "@dojoengine/state";
import { Subscription } from "@dojoengine/torii-client";

function App() {
const {
setup: {
systemCalls: { spawn, move },
clientComponents: { Position, Moves, DirectionsAvailable },
toriiClient,
contractComponents,
},
account,
} = useDojo();

useQuerySync(
toriiClient,
contractComponents as any,
["Moves", "Position", "DirectionsAvailable"],
[account?.account.address.toString()]
);

const [clipboardStatus, setClipboardStatus] = useState({
message: "",
isError: false,
Expand Down
9 changes: 1 addition & 8 deletions examples/react/react-app/src/dojo/generated/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ export async function setup({ ...config }: DojoConfig) {
const clientComponents = createClientComponents({ contractComponents });

// fetch all existing entities from torii
const sync = await getSyncEntities(toriiClient, contractComponents as any, {
Keys: {
keys: ["player"],
models: ["Moves"],
pattern_matching: "VariableLen",
},
});

// create dojo provider
const dojoProvider = new DojoProvider(config.manifest, config.rpcUrl);
Expand Down Expand Up @@ -83,6 +76,6 @@ export async function setup({ ...config }: DojoConfig) {
dojoProvider,
burnerManager,
toriiClient,
sync,
// sync,
};
}
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./usePromise";
export * from "./usePromise";
export * from "./useEntityQuery";
export * from "./utils";
export * from "./useQuerySync";
// export * from "./useFindEntity";
51 changes: 51 additions & 0 deletions packages/react/src/useQuerySync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Component, Metadata, Schema } from "@dojoengine/recs";
import { useCallback, useEffect } from "react";
import {
Client,
PatternMatching,
Subscription,
} from "@dojoengine/torii-client";
import { getSyncEntities } from "@dojoengine/state";

export function useQuerySync<S extends Schema>(
toriiClient: Client,
components: Component<S, Metadata, undefined>[],
models: string[],
keys: string[],
patternMatching: PatternMatching = "VariableLen"
) {
const setupSync = useCallback(async () => {
try {
const sync = await getSyncEntities(toriiClient, components, {
Keys: {
keys,
models,
pattern_matching: patternMatching,
},
});

return sync;
} catch (error) {
throw error;
}
}, [toriiClient, components, keys, patternMatching]);

useEffect(() => {
let unsubscribe: Subscription | undefined;

setupSync()
.then((sync) => {
unsubscribe = sync;
})
.catch((error) => {
console.error("Error setting up entity sync:", error);
});

return () => {
if (unsubscribe) {
unsubscribe.cancel();
console.log("Sync unsubscribed");
}
};
}, [setupSync]);
}

0 comments on commit 91a7e75

Please sign in to comment.