Skip to content

fix: recs remove component on empty values #427

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

Merged
merged 1 commit into from
Apr 3, 2025
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
5 changes: 5 additions & 0 deletions .changeset/blue-points-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dojoengine/state": patch
---

fix: remove empty components when no values
2 changes: 0 additions & 2 deletions examples/example-vite-grpc-playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
StandardizedQueryResult,
SchemaType,
type init,
ToriiQueryBuilder,
ClauseBuilder,
} from "@dojoengine/sdk";
import { evaluateUserInput } from "./components/ts-executor";

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"release": "pnpm build:deps && pnpm -F './packages/**' publish -r --force --no-git-checks",
"release:dry-run": "pnpm -F './packages/**' publish -r --force --dry-run",
"dev": "turbo watch dev --concurrency 20",
"dev:deps": "turbo watch build:deps --concurrency 20",
"docs": "turbo run docs --ui stream"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/react/hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { deepEqual, sleep } from "./utils";
*/
export function createSubscriptionHook<
Schema extends SchemaType,
Historical extends boolean = false,
Historical extends boolean = false
>(config: {
subscribeMethod: (
options: SubscribeParams<Schema, Historical>
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/react/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export * from "./state";
*/
export function useDojoSDK<
Client extends (...args: any) => any,
Schema extends SchemaType,
Schema extends SchemaType
>(): DojoContextType<Client, Schema> {
return useContext<DojoContextType<Client, Schema>>(DojoContext);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/react/hooks/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useModel<
N extends keyof SchemaType,
M extends keyof SchemaType[N] & string,
Client extends (...args: any) => any,
Schema extends SchemaType,
Schema extends SchemaType
>(entityId: BigNumberish, model: `${N}-${M}`): SchemaType[N][M] | undefined {
const [namespace, modelName] = model.split("-") as [N, M];
const { useDojoStore } =
Expand All @@ -57,7 +57,7 @@ export function useModels<
N extends keyof SchemaType,
M extends keyof SchemaType[N] & string,
Client extends (...args: any) => any,
Schema extends SchemaType,
Schema extends SchemaType
>(model: `${N}-${M}`): { [entityId: string]: SchemaType[N][M] | undefined } {
const [namespace, modelName] = model.split("-") as [N, M];
const { useDojoStore } =
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/react/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type DojoStoreHook<T extends SchemaType> = <U>(
*/
export interface DojoContextType<
Client extends (...args: any) => any,
Schema extends SchemaType,
Schema extends SchemaType
> {
/** The Dojo client instance */
config: DojoConfig;
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,14 @@ export type UnionOfModelData<T extends SchemaType> = {

export type ToriiResponse<
T extends SchemaType,
Historical extends boolean,
Historical extends boolean
> = Historical extends true
? StandardizedQueryResult<T>[]
: StandardizedQueryResult<T>;

export type SubscribeResponse<
T extends SchemaType,
Historical extends boolean,
Historical extends boolean
> = [ToriiResponse<T, Historical>, torii.Subscription];

export interface GetTokenRequest {
Expand Down Expand Up @@ -523,7 +523,7 @@ export interface SDKFunctionOptions {

export interface SubscribeParams<
T extends SchemaType,
Historical extends boolean = false,
Historical extends boolean = false
> {
// Query object used to filter entities.
query: ToriiQueryBuilder<T>;
Expand All @@ -535,7 +535,7 @@ export interface SubscribeParams<

export interface GetParams<
T extends SchemaType,
Historical extends boolean = false,
Historical extends boolean = false
> {
// The query object used to filter entities.
query: ToriiQueryBuilder<T>;
Expand Down
111 changes: 62 additions & 49 deletions packages/state/src/recs/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
Component,
ComponentValue,
Entity,
type Component,
type ComponentValue,
type Entity,
type Metadata,
type Schema,
getComponentValue,
hasComponent,
Metadata,
Schema,
removeComponent,
setComponent,
updateComponent,
} from "@dojoengine/recs";
import {
import type {
Clause,
Entities,
EntityKeysClause,
Expand Down Expand Up @@ -411,16 +412,17 @@ export const setEntities = async <S extends Schema>(
console.warn("No entities to set");
return;
}

if (logging) console.log("Entities to set:", entities);

for (const key in entities) {
if (!Object.hasOwn(entities, key)) {
console.log("Did not found key", key);
continue;
}

for (const componentName in entities[key]) {
if (!Object.hasOwn(entities[key], componentName)) {
console.log("Did not found componentName", componentName);
continue;
}
const recsComponent = Object.values(components).find(
Expand All @@ -429,63 +431,74 @@ export const setEntities = async <S extends Schema>(
componentName
);

if (recsComponent) {
try {
const rawValue = entities[key][componentName];
if (!recsComponent) {
if (logging)
console.warn(
`Component ${componentName} not found in provided components.`
);
continue;
}

try {
const rawValue = entities[key][componentName];
if (Object.keys(rawValue).length === 0) {
removeComponent(recsComponent, key as Entity, {
skipUpdateStream: false,
});
if (logging)
console.log(
`Raw value for ${componentName} on ${key}:`,
rawValue
`Removed component ${recsComponent.metadata?.name} on ${key}`
);
continue;
}

const convertedValue = convertValues(
recsComponent.schema,
if (logging)
console.log(
`Raw value for ${componentName} on ${key}:`,
rawValue
) as ComponentValue;
);

if (logging)
console.log(
`Converted value for ${componentName} on ${key}:`,
convertedValue
);
const convertedValue = convertValues(
recsComponent.schema,
rawValue
) as ComponentValue;

if (!convertedValue) {
console.error(
`convertValues returned undefined or invalid for ${componentName} on ${key}`
);
}

if (hasComponent(recsComponent, key as Entity)) {
updateComponent(
recsComponent,
key as Entity,
convertedValue as Partial<ComponentValue>,
getComponentValue(recsComponent, key as Entity)
);
if (logging)
console.log(
`Update component ${recsComponent.metadata?.name} on ${key}`
);
continue;
}
setComponent(recsComponent, key as Entity, convertedValue);
if (logging)
console.log(
`Converted value for ${componentName} on ${key}:`,
convertedValue
);

if (!convertedValue) {
console.error(
`convertValues returned undefined or invalid for ${componentName} on ${key}`
);
}

if (hasComponent(recsComponent, key as Entity)) {
updateComponent(
recsComponent,
key as Entity,
convertedValue as Partial<ComponentValue>,
getComponentValue(recsComponent, key as Entity)
);
if (logging)
console.log(
`Set component ${recsComponent.metadata?.name} on ${key}`
`Update component ${recsComponent.metadata?.name} on ${key}`
);
} catch (error) {
console.warn(
`Failed to set component ${recsComponent.metadata?.name} on ${key}`,
error
);
continue;
}
} else {
setComponent(recsComponent, key as Entity, convertedValue);

if (logging)
console.warn(
`Component ${componentName} not found in provided components.`
console.log(
`Set component ${recsComponent.metadata?.name} on ${key}`
);
} catch (error) {
console.warn(
`Failed to set component ${recsComponent.metadata?.name} on ${key}`,
error
);
}
}
}
Expand Down
Loading