Skip to content

Commit f55affd

Browse files
Merge pull request #235 from dojoengine/clause-sub
feat: subscription clauses
2 parents 58751dc + 1ac2903 commit f55affd

File tree

92 files changed

+3066
-5923
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+3066
-5923
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: git submodule update --init --recursive
2121

2222
- run: curl -L https://install.dojoengine.org | bash
23-
- run: /home/runner/.config/.dojo/bin/dojoup -v v0.7.2
23+
- run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.2
2424
- run: |
2525
cd examples/dojo-starter
2626
/home/runner/.config/.dojo/bin/sozo build

examples/dojo-starter

Submodule dojo-starter updated 43 files

examples/node/torii-bot/dojoConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import manifest from "../../dojo-starter/manifests/dev/manifest.json" assert { type: "json" };
1+
import manifest from "../../dojo-starter/manifests/dev/deployment/manifest.json" assert { type: "json" };
22

33
import { createDojoConfig } from "@dojoengine/core";
44

examples/node/torii-bot/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"jsx": "preserve",
1414
"allowSyntheticDefaultImports": true,
1515
"forceConsistentCasingInFileNames": true,
16-
"rootDir": "/Users/os/Documents/code/dojo.js/examples",
1716
"allowJs": true,
1817
"outDir": "dist",
1918
"types": ["bun-types"]

examples/react/react-app/dojoConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import manifest from "../../dojo-starter/manifests/dev/manifest.json";
1+
import manifest from "../../dojo-starter/manifests/dev/deployment/manifest.json";
2+
23
import { createDojoConfig } from "@dojoengine/core";
34

45
export const dojoConfig = createDojoConfig({

examples/react/react-app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "tsc && vite build",
99
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1010
"preview": "vite preview",
11-
"create-components": "npx @dojoengine/core ../../dojo-starter/manifests/dev/manifest.json src/dojo/generated/contractComponents.ts http://localhost:5050 0xb4079627ebab1cd3cf9fd075dda1ad2454a7a448bf659591f259efa2519b18"
11+
"create-components": "npx /Users/os/Documents/code/dojo/dojo.js/packages/core/bin/generateComponents.cjs ../../dojo-starter/manifests/dev/deployment/manifest.json src/dojo/generated/contractComponents.ts http://localhost:5050 0x3fc79ccfd72c1450d2ccb73c5c521a7ec68b6c6af0caf96a0f1c39ce58876c8"
1212
},
1313
"dependencies": {
1414
"@dojoengine/core": "workspace:*",
@@ -26,7 +26,7 @@
2626
"react": "^18.2.0",
2727
"react-dom": "^18.2.0",
2828
"rxjs": "^7.8.1",
29-
"starknet": "6.1.5",
29+
"starknet": "6.11.0",
3030
"vite-plugin-top-level-await": "^1.3.1",
3131
"vite-plugin-wasm": "^3.2.2"
3232
},

examples/react/react-app/src/App.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useComponentValue } from "@dojoengine/react";
1+
import "./App.css";
2+
import { useComponentValue, useQuerySync } from "@dojoengine/react";
23
import { Entity } from "@dojoengine/recs";
34
import { useEffect, useState } from "react";
4-
import "./App.css";
55
import { Direction } from "./utils";
66
import { getEntityIdFromKeys } from "@dojoengine/utils";
77
import { useDojo } from "./dojo/useDojo";
@@ -11,10 +11,22 @@ function App() {
1111
setup: {
1212
systemCalls: { spawn, move },
1313
clientComponents: { Position, Moves, DirectionsAvailable },
14+
toriiClient,
15+
contractComponents,
1416
},
1517
account,
1618
} = useDojo();
1719

20+
useQuerySync(toriiClient, contractComponents as any, [
21+
{
22+
Keys: {
23+
keys: [BigInt(account?.account.address).toString()],
24+
models: ["Position", "Moves", "DirectionsAvailable"],
25+
pattern_matching: "FixedLen",
26+
},
27+
},
28+
]);
29+
1830
const [clipboardStatus, setClipboardStatus] = useState({
1931
message: "",
2032
isError: false,

examples/react/react-app/src/Loading.tsx

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/react/react-app/src/dojo/createSystemCalls.ts

Lines changed: 96 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,78 @@
11
import { AccountInterface } from "starknet";
2-
import { Entity, getComponentValue } from "@dojoengine/recs";
2+
import {
3+
Entity,
4+
Has,
5+
HasValue,
6+
World,
7+
defineSystem,
8+
getComponentValue,
9+
} from "@dojoengine/recs";
310
import { uuid } from "@latticexyz/utils";
411
import { ClientComponents } from "./createClientComponents";
512
import { Direction, updatePositionWithDirection } from "../utils";
613
import { getEntityIdFromKeys } from "@dojoengine/utils";
7-
import { ContractComponents } from "./generated/contractComponents";
814
import type { IWorld } from "./generated/generated";
915

1016
export type SystemCalls = ReturnType<typeof createSystemCalls>;
1117

1218
export function createSystemCalls(
1319
{ client }: { client: IWorld },
14-
_contractComponents: ContractComponents,
15-
{ Position, Moves }: ClientComponents
20+
{ Position, Moves }: ClientComponents,
21+
world: World
1622
) {
1723
const spawn = async (account: AccountInterface) => {
24+
const entityId = getEntityIdFromKeys([
25+
BigInt(account.address),
26+
]) as Entity;
27+
28+
const movesId = uuid();
29+
Moves.addOverride(movesId, {
30+
entity: entityId,
31+
value: {
32+
player: BigInt(entityId),
33+
remaining:
34+
(getComponentValue(Moves, entityId)?.remaining || 0) + 100,
35+
},
36+
});
37+
38+
const positionId = uuid();
39+
Position.addOverride(positionId, {
40+
entity: entityId,
41+
value: {
42+
player: BigInt(entityId),
43+
vec: {
44+
x: 10 + (getComponentValue(Position, entityId)?.vec.x || 0),
45+
y: 10 + (getComponentValue(Position, entityId)?.vec.y || 0),
46+
},
47+
},
48+
});
49+
1850
try {
19-
const { transaction_hash } = await client.actions.spawn({
51+
await client.actions.spawn({
2052
account,
2153
});
2254

23-
console.log(
24-
await account.waitForTransaction(transaction_hash, {
25-
retryInterval: 100,
26-
})
27-
);
28-
29-
await new Promise((resolve) => setTimeout(resolve, 1000));
55+
// Wait for the indexer to update the entity
56+
// By doing this we keep the optimistic UI in sync with the actual state
57+
await new Promise<void>((resolve) => {
58+
defineSystem(
59+
world,
60+
[
61+
Has(Moves),
62+
HasValue(Moves, { player: BigInt(account.address) }),
63+
],
64+
() => {
65+
resolve();
66+
}
67+
);
68+
});
3069
} catch (e) {
3170
console.log(e);
71+
Position.removeOverride(positionId);
72+
Moves.removeOverride(movesId);
73+
} finally {
74+
Position.removeOverride(positionId);
75+
Moves.removeOverride(movesId);
3276
}
3377
};
3478

@@ -37,52 +81,57 @@ export function createSystemCalls(
3781
BigInt(account.address),
3882
]) as Entity;
3983

40-
// const positionId = uuid();
41-
// Position.addOverride(positionId, {
42-
// entity: entityId,
43-
// value: {
44-
// player: BigInt(entityId),
45-
// vec: updatePositionWithDirection(
46-
// direction,
47-
// getComponentValue(Position, entityId) as any
48-
// ).vec,
49-
// },
50-
// });
84+
// Update the state before the transaction
85+
const positionId = uuid();
86+
Position.addOverride(positionId, {
87+
entity: entityId,
88+
value: {
89+
player: BigInt(entityId),
90+
vec: updatePositionWithDirection(
91+
direction,
92+
getComponentValue(Position, entityId) as any
93+
).vec,
94+
},
95+
});
5196

52-
// const movesId = uuid();
53-
// Moves.addOverride(movesId, {
54-
// entity: entityId,
55-
// value: {
56-
// player: BigInt(entityId),
57-
// remaining:
58-
// (getComponentValue(Moves, entityId)?.remaining || 0) - 1,
59-
// },
60-
// });
97+
// Update the state before the transaction
98+
const movesId = uuid();
99+
Moves.addOverride(movesId, {
100+
entity: entityId,
101+
value: {
102+
player: BigInt(entityId),
103+
remaining:
104+
(getComponentValue(Moves, entityId)?.remaining || 0) - 1,
105+
},
106+
});
61107

62108
try {
63-
const { transaction_hash } = await client.actions.move({
109+
await client.actions.move({
64110
account,
65111
direction,
66112
});
67113

68-
await account.waitForTransaction(transaction_hash, {
69-
retryInterval: 100,
114+
// Wait for the indexer to update the entity
115+
// By doing this we keep the optimistic UI in sync with the actual state
116+
await new Promise<void>((resolve) => {
117+
defineSystem(
118+
world,
119+
[
120+
Has(Moves),
121+
HasValue(Moves, { player: BigInt(account.address) }),
122+
],
123+
() => {
124+
resolve();
125+
}
126+
);
70127
});
71-
72-
// console.log(
73-
// await account.waitForTransaction(transaction_hash, {
74-
// retryInterval: 100,
75-
// })
76-
// );
77-
78-
await new Promise((resolve) => setTimeout(resolve, 1000));
79128
} catch (e) {
80129
console.log(e);
81-
// Position.removeOverride(positionId);
82-
// Moves.removeOverride(movesId);
130+
Position.removeOverride(positionId);
131+
Moves.removeOverride(movesId);
83132
} finally {
84-
// Position.removeOverride(positionId);
85-
// Moves.removeOverride(movesId);
133+
Position.removeOverride(positionId);
134+
Moves.removeOverride(movesId);
86135
}
87136
};
88137

examples/react/react-app/src/dojo/generated/contractComponents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function defineContractComponents(world: World) {
1818
{ player: RecsType.BigInt, directions: RecsType.StringArray },
1919
{
2020
metadata: {
21-
name: "DirectionsAvailable",
21+
name: "dojo_starter-DirectionsAvailable",
2222
types: ["contractaddress"],
2323
customTypes: ["Direction"],
2424
},
@@ -36,7 +36,7 @@ export function defineContractComponents(world: World) {
3636
},
3737
{
3838
metadata: {
39-
name: "Moves",
39+
name: "dojo_starter-Moves",
4040
types: ["contractaddress", "u8", "enum", "bool"],
4141
customTypes: ["Direction"],
4242
},
@@ -52,7 +52,7 @@ export function defineContractComponents(world: World) {
5252
},
5353
{
5454
metadata: {
55-
name: "Position",
55+
name: "dojo_starter-Position",
5656
types: ["contractaddress", "u32", "u32"],
5757
customTypes: ["Vec2"],
5858
},

0 commit comments

Comments
 (0)