Skip to content

Commit 4335585

Browse files
authored
Merge pull request #349 from rsodre/dojo-store-add
fix: Dojo store add entity
2 parents db5ddfa + 090d2d6 commit 4335585

File tree

5 files changed

+15404
-12424
lines changed

5 files changed

+15404
-12424
lines changed

packages/sdk/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
"starknet": "6.11.0"
3939
},
4040
"dependencies": {
41-
"@dojoengine/core": "workspace:*",
42-
"@dojoengine/create-burner": "workspace:*",
4341
"@dojoengine/torii-client": "workspace:*",
4442
"immer": "^10.1.1",
4543
"zustand": "^4.5.5"

packages/sdk/src/__tests__/state.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ describe("createDojoStore", () => {
104104
initialGalaxy,
105105
]);
106106
const state = useStore.getState();
107+
expect(state.getEntities()).toHaveLength(4);
107108
expect(state.entities["player1"]).toEqual(initialPlayer);
108109
expect(state.entities["game1"]).toEqual(initialGame);
109110
expect(state.entities["item1"]).toEqual(initialItem);
@@ -124,12 +125,14 @@ describe("createDojoStore", () => {
124125
},
125126
});
126127
const state = useStore.getState();
128+
expect(state.getEntities()).toHaveLength(1);
127129
expect(state.entities["player1"].models.world?.player?.name).toEqual(
128130
"Bob"
129131
);
130132
});
131133

132-
test("updateEntity should not add a new entity if entityId does not exist", () => {
134+
test("updateEntity should add a new entity if entityId does not exist", () => {
135+
useStore.getState().setEntities([initialPlayer]);
133136
useStore.getState().updateEntity({
134137
entityId: "nonexistent",
135138
models: {
@@ -142,7 +145,10 @@ describe("createDojoStore", () => {
142145
},
143146
});
144147
const state = useStore.getState();
145-
expect(state.entities["nonexistent"]).toBeUndefined();
148+
expect(state.getEntities()).toHaveLength(2);
149+
expect(
150+
state.entities["nonexistent"].models.world?.player?.name
151+
).toEqual("Charlie");
146152
});
147153

148154
test("applyOptimisticUpdate should apply updates and add to pendingTransactions", () => {

packages/sdk/src/convertQueryToEntityKeyClauses.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ function createClauseFromWhere(
182182
}
183183
});
184184

185-
console.log("fieldOrder", keys);
186-
187185
return {
188186
Keys: {
189187
keys: keys,

packages/sdk/src/state/zustand.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,46 +72,48 @@ export function createDojoStore<T extends SchemaType>() {
7272
},
7373
updateEntity: (entity: Partial<ParsedEntity<T>>) => {
7474
set((state: Draft<GameState<T>>) => {
75-
if (
76-
entity.entityId &&
77-
state.entities[entity.entityId] &&
78-
entity.models
79-
) {
75+
if (entity.entityId && entity.models) {
8076
const existingEntity =
8177
state.entities[entity.entityId];
8278

83-
// Create new models object without spread
84-
const mergedModels: typeof existingEntity.models =
85-
Object.assign({}, existingEntity.models);
79+
if (existingEntity) {
80+
// Create new models object without spread
81+
const mergedModels: typeof existingEntity.models =
82+
Object.assign({}, existingEntity.models);
8683

87-
// Iterate through each namespace in the new models
88-
Object.entries(entity.models).forEach(
89-
([namespace, namespaceModels]) => {
90-
const typedNamespace =
91-
namespace as keyof ParsedEntity<T>["models"];
92-
if (!(typedNamespace in mergedModels)) {
93-
mergedModels[
94-
typedNamespace as keyof typeof mergedModels
95-
] = {} as any;
96-
}
84+
// Iterate through each namespace in the new models
85+
Object.entries(entity.models).forEach(
86+
([namespace, namespaceModels]) => {
87+
const typedNamespace =
88+
namespace as keyof ParsedEntity<T>["models"];
89+
if (!(typedNamespace in mergedModels)) {
90+
mergedModels[
91+
typedNamespace as keyof typeof mergedModels
92+
] = {} as any;
93+
}
9794

98-
mergedModels[
99-
typedNamespace as keyof typeof mergedModels
100-
] = Object.assign(
101-
{},
10295
mergedModels[
10396
typedNamespace as keyof typeof mergedModels
104-
],
105-
namespaceModels
106-
);
107-
}
108-
);
109-
// Update the entity
110-
state.entities[entity.entityId] = {
111-
...existingEntity,
112-
...entity,
113-
models: mergedModels,
114-
};
97+
] = Object.assign(
98+
{},
99+
mergedModels[
100+
typedNamespace as keyof typeof mergedModels
101+
],
102+
namespaceModels
103+
);
104+
}
105+
);
106+
// Update the entity
107+
state.entities[entity.entityId] = {
108+
...existingEntity,
109+
...entity,
110+
models: mergedModels,
111+
};
112+
} else {
113+
// Set new entity
114+
state.entities[entity.entityId] =
115+
entity as WritableDraft<ParsedEntity<T>>;
116+
}
115117
}
116118
});
117119
},

0 commit comments

Comments
 (0)