Skip to content

feat: add order_by and entity_models to sdk #358

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 3 commits into from
Dec 13, 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
19 changes: 8 additions & 11 deletions examples/example-vite-react-app-recs/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,26 @@ export async function setup({ ...config }: DojoConfig) {
contractComponents as any,
undefined,
[],
[],
[],
3000,
Comment on lines 31 to 34
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider improving type safety and error handling.

  1. The contractComponents as any type assertion appears multiple times. Consider defining proper types to avoid using any.
  2. Error handling could be more robust for the sync operations, similar to how burner account initialization is handled.
+ try {
    const getSync = await getSyncEntities(
      toriiClient,
-     contractComponents as any,
+     contractComponents,
      // ... other parameters
    );
+ } catch (e) {
+   console.error("Failed to initialize entity sync:", e);
+   // Consider proper error recovery strategy
+ }

+ try {
    const eventSync = getSyncEvents(
      toriiClient,
-     contractComponents as any,
+     contractComponents,
      // ... other parameters
    );
+ } catch (e) {
+   console.error("Failed to initialize event sync:", e);
+   // Consider proper error recovery strategy
+ }

Also applies to: 49-52

true
);

// Create client-side components that mirror the contract components
const clientComponents = createClientComponents({ contractComponents });

// Initialize the Dojo provider with the manifest and RPC URL
// Initialize the Dojo provider with t
// he manifest and RPC URL
const dojoProvider = new DojoProvider(config.manifest, config.rpcUrl);

// Set up event synchronization between the client and the Dojo network
const eventSync = getSyncEvents(
toriiClient,
contractComponents as any,
undefined,
[],
[],
[]
);

Expand Down Expand Up @@ -80,16 +85,8 @@ export async function setup({ ...config }: DojoConfig) {
clientComponents,
contractComponents,
systemCalls: createSystemCalls({ client }, clientComponents, world),
publish: (
typedData: string,
signature: ArraySignatureType,
isSessionSignature = false
) => {
toriiClient.publishMessage(
typedData,
signature,
isSessionSignature
);
publish: (typedData: string, signature: ArraySignatureType) => {
toriiClient.publishMessage(typedData, signature);
},
config,
dojoProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,8 @@ export async function setup({ ...config }: DojoConfig) {
contractComponents,
clientComponents
),
publish: (
typedData: string,
signature: ArraySignatureType,
isSessionSignature = false
) => {
toriiClient.publishMessage(
typedData,
signature,
isSessionSignature
);
publish: (typedData: string, signature: ArraySignatureType) => {
toriiClient.publishMessage(typedData, signature);
},
config,
sync,
Expand Down
4 changes: 2 additions & 2 deletions examples/example-vite-react-phaser-recs/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
export default {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Fix invalid plugin import syntax

The current changes introduce two issues:

  1. Using import() in the plugins array is incorrect as it returns a Promise, but Tailwind expects plugin instances
  2. Configuration files should use static imports for predictable initialization

Apply this fix:

+ import animate from "tailwindcss-animate";

/** @type {import('tailwindcss').Config} */
export default {
    // ... rest of config ...
-    plugins: [import("tailwindcss-animate")],
+    plugins: [animate],
};

Alternatively, if you need to maintain CommonJS compatibility:

/** @type {import('tailwindcss').Config} */
- export default {
+ module.exports = {
    // ... rest of config ...
-    plugins: [import("tailwindcss-animate")],
+    plugins: [require("tailwindcss-animate")],
};

Also applies to: 75-75

darkMode: ["class"],
content: [
"./pages/**/*.{ts,tsx}",
Expand Down Expand Up @@ -72,5 +72,5 @@ module.exports = {
},
},
},
plugins: [require("tailwindcss-animate")],
plugins: [import("tailwindcss-animate")],
};
12 changes: 2 additions & 10 deletions examples/example-vite-react-pwa-recs/src/dojo/generated/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,8 @@ export async function setup({ ...config }: DojoConfig) {
contractComponents,
clientComponents
),
publish: (
typedData: string,
signature: ArraySignatureType,
isSessionSignature = false
) => {
toriiClient.publishMessage(
typedData,
signature,
isSessionSignature
);
publish: (typedData: string, signature: ArraySignatureType) => {
toriiClient.publishMessage(typedData, signature);
},
config,
dojoProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,8 @@ export async function setup({ ...config }: DojoConfig) {
contractComponents,
clientComponents
),
publish: (
typedData: string,
signature: ArraySignatureType,
isSessionSignature: boolean = false
) => {
toriiClient.publishMessage(
typedData,
signature,
isSessionSignature
);
publish: (typedData: string, signature: ArraySignatureType) => {
toriiClient.publishMessage(typedData, signature);
},
config,
world,
Expand Down
4 changes: 2 additions & 2 deletions examples/example-vite-react-threejs-recs/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
export default {
darkMode: ["class"],
content: [
"./pages/**/*.{ts,tsx}",
Expand Down Expand Up @@ -73,5 +73,5 @@ module.exports = {
},
},
},
plugins: [require("tailwindcss-animate")],
plugins: [import("tailwindcss-animate")],
};
46 changes: 23 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "dojo.js",
"scripts": {
"build": "bash ./scripts/build-packages.sh",
"build-examples": "bash ./scripts/build-examples.sh",
"clean": "bash ./scripts/clean.sh",
"prettier-check": "npx prettier --check .",
"prettier": "npx prettier --write .",
"release": "pnpm build && pnpm prettier && npx lerna publish --no-private --force-publish",
"docs": "npx typedoc --out docs",
"prepare": "husky install"
},
"devDependencies": {
"husky": "^9.1.6",
"lerna": "^8.1.5",
"prettier": "^3.3.3",
"tsup": "^8.1.0",
"typedoc": "^0.26.7",
"@typhonjs-typedoc/typedoc-theme-dmt": "^0.2.1",
"typedoc-plugin-coverage": "^3.3.0",
"@commitlint/cli": "^18.4.4",
"@commitlint/config-conventional": "^18.4.4",
"@ianvs/prettier-plugin-sort-imports": "^4.3.1"
}
"name": "dojo.js",
"scripts": {
"build": "bash ./scripts/build-packages.sh",
"build-examples": "bash ./scripts/build-examples.sh",
"clean": "bash ./scripts/clean.sh",
"prettier-check": "npx prettier --check {packages,examples}",
"prettier": "npx prettier --write .",
"release": "pnpm build && pnpm prettier && npx lerna publish --no-private --force-publish",
"docs": "npx typedoc --out docs",
"prepare": "husky install"
},
"devDependencies": {
"husky": "^9.1.6",
"lerna": "^8.1.5",
"prettier": "^3.3.3",
"tsup": "^8.1.0",
"typedoc": "^0.26.7",
"@typhonjs-typedoc/typedoc-theme-dmt": "^0.2.1",
"typedoc-plugin-coverage": "^3.3.0",
"@commitlint/cli": "^18.4.4",
"@commitlint/config-conventional": "^18.4.4",
"@ianvs/prettier-plugin-sort-imports": "^4.3.1"
}
}
6 changes: 5 additions & 1 deletion packages/sdk/src/getEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export async function getEntities<T extends SchemaType>(
data?: StandardizedQueryResult<T>;
error?: Error;
}) => void,
orderBy: torii.OrderBy[] = [],
entityModels: string[] = [],
limit: number = 100, // Default limit
offset: number = 0, // Default offset
options?: { logging?: boolean } // Logging option
Expand All @@ -46,8 +48,10 @@ export async function getEntities<T extends SchemaType>(

while (continueFetching) {
const toriiQuery: torii.Query = {
limit: limit,
limit,
offset: cursor,
order_by: orderBy,
entity_models: entityModels,
clause,
dont_include_hashed_keys: false,
};
Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/getEventMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export async function getEventMessages<T extends SchemaType>(
data?: StandardizedQueryResult<T>;
error?: Error;
}) => void,
orderBy: torii.OrderBy[] = [],
entityModels: string[] = [],
limit: number = 100, // Default limit
offset: number = 0, // Default offset
options?: { logging?: boolean } // Logging option
Expand All @@ -46,8 +48,10 @@ export async function getEventMessages<T extends SchemaType>(

while (continueFetching) {
const toriiQuery: torii.Query = {
limit: limit,
limit,
offset: cursor,
order_by: orderBy,
entity_models: entityModels,
clause,
dont_include_hashed_keys: false,
};
Expand Down
24 changes: 22 additions & 2 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ export async function init<T extends SchemaType>(
* @param {GetParams<T>} params - Parameters object
* @returns {Promise<StandardizedQueryResult<T>>} - A promise that resolves to the standardized query result.
*/
getEntities: ({ query, callback, limit, offset, options }) =>
getEntities: ({
query,
callback,
orderBy,
entityModels,
limit,
offset,
options,
}) =>
getEntities(
client,
query,
schema,
callback,
orderBy,
entityModels,
limit,
offset,
options
Expand All @@ -77,12 +87,22 @@ export async function init<T extends SchemaType>(
* @param {GetParams<T>} params - Parameters object
* @returns {Promise<StandardizedQueryResult<T>>} - A promise that resolves to the standardized query result.
*/
getEventMessages: ({ query, callback, limit, offset, options }) =>
getEventMessages: ({
query,
callback,
orderBy,
entityModels,
limit,
offset,
options,
}) =>
getEventMessages(
client,
query,
schema,
callback,
orderBy,
entityModels,
limit,
offset,
options
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ export interface GetParams<T extends SchemaType> {
data?: StandardizedQueryResult<T>;
error?: Error;
}) => void;
// The order to sort the entities by.
orderBy?: torii.OrderBy[];
// The models to whitelist for fetching. Leave this empty to fetch all models.
entityModels?: string[];
// The maximum number of entities to fetch per request. Default is 100.
limit?: number;
// The offset to start fetching entities from. Default is 0.
Expand Down
2 changes: 1 addition & 1 deletion worlds/dojo-starter
Loading