Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Dojo store add entity #349

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
"starknet": "6.11.0"
},
"dependencies": {
"@dojoengine/core": "workspace:*",
"@dojoengine/create-burner": "workspace:*",
"@dojoengine/torii-client": "workspace:*",
"immer": "^10.1.1",
"zustand": "^4.5.5"
Expand Down
10 changes: 8 additions & 2 deletions packages/sdk/src/__tests__/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe("createDojoStore", () => {
initialGalaxy,
]);
const state = useStore.getState();
expect(state.getEntities()).toHaveLength(4);
expect(state.entities["player1"]).toEqual(initialPlayer);
expect(state.entities["game1"]).toEqual(initialGame);
expect(state.entities["item1"]).toEqual(initialItem);
Expand All @@ -124,12 +125,14 @@ describe("createDojoStore", () => {
},
});
const state = useStore.getState();
expect(state.getEntities()).toHaveLength(1);
expect(state.entities["player1"].models.world?.player?.name).toEqual(
"Bob"
);
});

test("updateEntity should not add a new entity if entityId does not exist", () => {
test("updateEntity should add a new entity if entityId does not exist", () => {
useStore.getState().setEntities([initialPlayer]);
useStore.getState().updateEntity({
entityId: "nonexistent",
models: {
Expand All @@ -142,7 +145,10 @@ describe("createDojoStore", () => {
},
});
const state = useStore.getState();
expect(state.entities["nonexistent"]).toBeUndefined();
expect(state.getEntities()).toHaveLength(2);
expect(
state.entities["nonexistent"].models.world?.player?.name
).toEqual("Charlie");
});

test("applyOptimisticUpdate should apply updates and add to pendingTransactions", () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/sdk/src/convertQueryToEntityKeyClauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ function createClauseFromWhere(
}
});

console.log("fieldOrder", keys);

return {
Keys: {
keys: keys,
Expand Down
68 changes: 35 additions & 33 deletions packages/sdk/src/state/zustand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,46 +72,48 @@ export function createDojoStore<T extends SchemaType>() {
},
updateEntity: (entity: Partial<ParsedEntity<T>>) => {
set((state: Draft<GameState<T>>) => {
if (
entity.entityId &&
state.entities[entity.entityId] &&
entity.models
) {
if (entity.entityId && entity.models) {
const existingEntity =
state.entities[entity.entityId];

// Create new models object without spread
const mergedModels: typeof existingEntity.models =
Object.assign({}, existingEntity.models);
if (existingEntity) {
// Create new models object without spread
const mergedModels: typeof existingEntity.models =
Object.assign({}, existingEntity.models);

// Iterate through each namespace in the new models
Object.entries(entity.models).forEach(
([namespace, namespaceModels]) => {
const typedNamespace =
namespace as keyof ParsedEntity<T>["models"];
if (!(typedNamespace in mergedModels)) {
mergedModels[
typedNamespace as keyof typeof mergedModels
] = {} as any;
}
// Iterate through each namespace in the new models
Object.entries(entity.models).forEach(
([namespace, namespaceModels]) => {
const typedNamespace =
namespace as keyof ParsedEntity<T>["models"];
if (!(typedNamespace in mergedModels)) {
mergedModels[
typedNamespace as keyof typeof mergedModels
] = {} as any;
}

mergedModels[
typedNamespace as keyof typeof mergedModels
] = Object.assign(
{},
mergedModels[
typedNamespace as keyof typeof mergedModels
],
namespaceModels
);
}
);
// Update the entity
state.entities[entity.entityId] = {
...existingEntity,
...entity,
models: mergedModels,
};
] = Object.assign(
{},
mergedModels[
typedNamespace as keyof typeof mergedModels
],
namespaceModels
);
}
);
// Update the entity
state.entities[entity.entityId] = {
...existingEntity,
...entity,
models: mergedModels,
};
} else {
// Set new entity
state.entities[entity.entityId] =
entity as WritableDraft<ParsedEntity<T>>;
}
}
});
},
Expand Down
Loading
Loading