-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from dojoengine/fix/readme-1
feat: optimistic example
- Loading branch information
Showing
4 changed files
with
148 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { getEntityIdFromKeys } from "@dojoengine/utils"; | ||
import { useDojoStore } from "./App"; | ||
import { useDojo } from "./useDojo"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
export const useSystemCalls = () => { | ||
const state = useDojoStore((state) => state); | ||
|
||
const { | ||
setup: { client }, | ||
account: { account }, | ||
} = useDojo(); | ||
|
||
const generateEntityId = () => { | ||
return getEntityIdFromKeys([BigInt(account?.address)]); | ||
}; | ||
|
||
const spawn = async () => { | ||
// Generate a unique entity ID | ||
const entityId = generateEntityId(); | ||
|
||
// Generate a unique transaction ID | ||
const transactionId = uuidv4(); | ||
|
||
// The value to update the Moves model with | ||
const remainingMoves = 100; | ||
|
||
// Apply an optimistic update to the state | ||
// this uses immer drafts to update the state | ||
state.applyOptimisticUpdate( | ||
transactionId, | ||
(draft) => | ||
(draft.entities[entityId].models.dojo_starter.Moves!.remaining = | ||
remainingMoves) | ||
); | ||
|
||
try { | ||
// Execute the spawn action from the client | ||
await client.actions.spawn({ account }); | ||
|
||
// Wait for the entity to be updated with the new state | ||
await state.waitForEntityChange(entityId, (entity) => { | ||
return ( | ||
entity?.models?.dojo_starter?.Moves?.remaining === | ||
remainingMoves | ||
); | ||
}); | ||
} catch (error) { | ||
// Revert the optimistic update if an error occurs | ||
state.revertOptimisticUpdate(transactionId); | ||
console.error("Error executing spawn:", error); | ||
throw error; | ||
} finally { | ||
// Confirm the transaction if successful | ||
state.confirmTransaction(transactionId); | ||
} | ||
}; | ||
|
||
return { | ||
spawn, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters