1
- import { useEffect , useState } from "react" ;
1
+ import { useEffect } from "react" ;
2
2
import "./App.css" ;
3
- import { ParsedEntity , SDK } from "@dojoengine/sdk" ;
3
+ import { SDK , createDojoStore } from "@dojoengine/sdk" ;
4
4
import { Schema } from "./bindings.ts" ;
5
5
6
+ import { v4 as uuidv4 } from "uuid" ;
7
+
8
+ export const useDojoStore = createDojoStore < Schema > ( ) ;
9
+
6
10
function App ( { db } : { db : SDK < Schema > } ) {
7
- const [ entities , setEntities ] = useState < ParsedEntity < Schema > [ ] > ( [ ] ) ;
11
+ const state = useDojoStore ( ( state ) => state ) ;
12
+ const entities = useDojoStore ( ( state ) => state . entities ) ;
8
13
9
14
useEffect ( ( ) => {
10
15
let unsubscribe : ( ( ) => void ) | undefined ;
@@ -28,15 +33,7 @@ function App({ db }: { db: SDK<Schema> }) {
28
33
response . data &&
29
34
response . data [ 0 ] . entityId !== "0x0"
30
35
) {
31
- console . log ( response . data ) ;
32
- setEntities ( ( prevEntities ) => {
33
- return prevEntities . map ( ( entity ) => {
34
- const newEntity = response . data ?. find (
35
- ( e ) => e . entityId === entity . entityId
36
- ) ;
37
- return newEntity ? newEntity : entity ;
38
- } ) ;
39
- } ) ;
36
+ state . setEntities ( response . data ) ;
40
37
}
41
38
} ,
42
39
{ logging : true }
@@ -54,8 +51,6 @@ function App({ db }: { db: SDK<Schema> }) {
54
51
} ;
55
52
} , [ db ] ) ;
56
53
57
- console . log ( "entities:" ) ;
58
-
59
54
useEffect ( ( ) => {
60
55
const fetchEntities = async ( ) => {
61
56
try {
@@ -76,23 +71,7 @@ function App({ db }: { db: SDK<Schema> }) {
76
71
return ;
77
72
}
78
73
if ( resp . data ) {
79
- console . log ( resp . data ) ;
80
- setEntities ( ( prevEntities ) => {
81
- const updatedEntities = [ ...prevEntities ] ;
82
- resp . data ?. forEach ( ( newEntity ) => {
83
- const index = updatedEntities . findIndex (
84
- ( entity ) =>
85
- entity . entityId ===
86
- newEntity . entityId
87
- ) ;
88
- if ( index !== - 1 ) {
89
- updatedEntities [ index ] = newEntity ;
90
- } else {
91
- updatedEntities . push ( newEntity ) ;
92
- }
93
- } ) ;
94
- return updatedEntities ;
95
- } ) ;
74
+ state . setEntities ( resp . data ) ;
96
75
}
97
76
}
98
77
) ;
@@ -104,20 +83,55 @@ function App({ db }: { db: SDK<Schema> }) {
104
83
fetchEntities ( ) ;
105
84
} , [ db ] ) ;
106
85
86
+ const optimisticUpdate = async ( ) => {
87
+ const entityId =
88
+ "0x571368d35c8fe136adf81eecf96a72859c43de7efd8fdd3d6f0d17e308df984" ;
89
+
90
+ const transactionId = uuidv4 ( ) ;
91
+
92
+ state . applyOptimisticUpdate ( transactionId , ( draft ) => {
93
+ draft . entities [ entityId ] . models . dojo_starter . Moves ! . remaining = 10 ;
94
+ } ) ;
95
+
96
+ try {
97
+ // Wait for the entity to be updated before full resolving the transaction. Reverts if the condition is not met.
98
+ const updatedEntity = await state . waitForEntityChange (
99
+ entityId ,
100
+ ( entity ) => {
101
+ // Define your specific condition here
102
+ return entity ?. models . dojo_starter . Moves ?. can_move === true ;
103
+ }
104
+ ) ;
105
+
106
+ console . log ( "Entity has been updated to active:" , updatedEntity ) ;
107
+
108
+ console . log ( "Updating entities..." ) ;
109
+ } catch ( error ) {
110
+ console . error ( "Error updating entities:" , error ) ;
111
+ state . revertOptimisticUpdate ( transactionId ) ;
112
+ } finally {
113
+ console . log ( "Updating entities..." ) ;
114
+ state . confirmTransaction ( transactionId ) ;
115
+ }
116
+ } ;
117
+
107
118
return (
108
119
< div >
109
120
< h1 > Game State</ h1 >
110
- { entities . map ( ( entity ) => (
111
- < div key = { entity . entityId } >
112
- < h2 > Entity { entity . entityId } </ h2 >
121
+ < button onClick = { optimisticUpdate } > update</ button >
122
+ { Object . entries ( entities ) . map ( ( [ entityId , entity ] ) => (
123
+ < div key = { entityId } >
124
+ < h2 > Entity { entityId } </ h2 >
113
125
< h3 > Position</ h3 >
114
126
< p >
115
127
Player:{ " " }
116
128
{ entity . models . dojo_starter . Position ?. player ?? "N/A" }
117
129
< br />
118
- X: { entity . models . dojo_starter . Position ?. vec . x ?? "N/A" }
130
+ X:{ " " }
131
+ { entity . models . dojo_starter . Position ?. vec ?. x ?? "N/A" }
119
132
< br />
120
- Y: { entity . models . dojo_starter . Position ?. vec . y ?? "N/A" }
133
+ Y:{ " " }
134
+ { entity . models . dojo_starter . Position ?. vec ?. y ?? "N/A" }
121
135
</ p >
122
136
< h3 > Moves</ h3 >
123
137
< p >
0 commit comments