|
| 1 | +import { queryTorii } from "@dojoengine/sdk/sql"; |
1 | 2 | import type { App, AppStore } from "@pixelaw/core"; |
2 | 3 | import type { DojoStuff } from "./DojoEngine.init.ts"; |
3 | | -import { queryTorii } from "@dojoengine/sdk/sql"; |
4 | 4 | import { convertFullHexString } from "./utils/utils.ts"; |
5 | 5 |
|
6 | 6 | type State = { [key: string]: App | undefined }; |
7 | 7 |
|
8 | 8 | export class DojoAppStore implements AppStore { |
9 | | - private dojoStuff; |
10 | | - private state: State = {}; |
11 | | - |
12 | | - constructor(dojoStuff: DojoStuff) { |
13 | | - this.dojoStuff = dojoStuff; |
14 | | - } |
15 | | - |
16 | | - public static async getInstance(dojoStuff: DojoStuff): Promise<DojoAppStore> { |
17 | | - const dojoAppStore = new DojoAppStore(dojoStuff); |
18 | | - |
19 | | - await dojoAppStore.initialize(); |
20 | | - // await dojoAppStore.subscribe() |
21 | | - return dojoAppStore; |
22 | | - } |
23 | | - |
24 | | - private async initialize() { |
25 | | - try { |
26 | | - const items = await queryTorii( |
27 | | - this.dojoStuff.toriiUrl, |
28 | | - `SELECT * |
| 9 | + private dojoStuff; |
| 10 | + private state: State = {}; |
| 11 | + |
| 12 | + constructor(dojoStuff: DojoStuff) { |
| 13 | + this.dojoStuff = dojoStuff; |
| 14 | + } |
| 15 | + |
| 16 | + public static async getInstance(dojoStuff: DojoStuff): Promise<DojoAppStore> { |
| 17 | + const dojoAppStore = new DojoAppStore(dojoStuff); |
| 18 | + |
| 19 | + await dojoAppStore.initialize(); |
| 20 | + // await dojoAppStore.subscribe() |
| 21 | + return dojoAppStore; |
| 22 | + } |
| 23 | + |
| 24 | + private async initialize() { |
| 25 | + try { |
| 26 | + const items = await queryTorii( |
| 27 | + this.dojoStuff.toriiUrl, |
| 28 | + `SELECT * |
29 | 29 | FROM "pixelaw-App";`, |
30 | | - (rows: any[]) => { |
31 | | - return rows; |
32 | | - }, |
33 | | - ); |
34 | | - for (const item of items) { |
35 | | - const app: App = { |
36 | | - action: convertFullHexString(item.action), |
37 | | - icon: convertFullHexString(item.icon), |
38 | | - name: convertFullHexString(item.name), |
39 | | - plugin: item.plugin, |
40 | | - system: item.system, |
41 | | - entity: { id: "" }, // TODO |
42 | | - }; |
43 | | - this.state[app.name] = app; |
44 | | - console.log("APP", app); |
45 | | - } |
46 | | - // console.log({ items }) |
47 | | - } catch (e) { |
48 | | - console.error(e); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - getAll(): App[] { |
53 | | - return Object.values(this.state); |
54 | | - } |
55 | | - |
56 | | - getByName(name: string): App | undefined { |
57 | | - return this.state[name]; |
58 | | - } |
59 | | - |
60 | | - getBySystem(system: string): App | undefined { |
61 | | - return Object.values(this.state).find((app) => app?.system === system); |
62 | | - } |
| 30 | + (rows: any[]) => { |
| 31 | + return rows; |
| 32 | + }, |
| 33 | + ); |
| 34 | + for (const item of items) { |
| 35 | + const app: App = { |
| 36 | + action: convertFullHexString(item.action), |
| 37 | + icon: convertFullHexString(item.icon), |
| 38 | + name: convertFullHexString(item.name), |
| 39 | + plugin: item.plugin, |
| 40 | + system: item.system, |
| 41 | + entity: { id: "" }, // TODO |
| 42 | + }; |
| 43 | + this.state[app.name] = app; |
| 44 | + console.log("APP", app); |
| 45 | + } |
| 46 | + // console.log({ items }) |
| 47 | + } catch (e) { |
| 48 | + console.error(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + getAll(): App[] { |
| 53 | + return Object.values(this.state); |
| 54 | + } |
| 55 | + |
| 56 | + getByName(name: string): App | undefined { |
| 57 | + return this.state[name]; |
| 58 | + } |
| 59 | + |
| 60 | + getBySystem(system: string): App | undefined { |
| 61 | + return Object.values(this.state).find((app) => app?.system === system); |
| 62 | + } |
63 | 63 | } |
0 commit comments