Skip to content

Commit 21129b8

Browse files
committed
removed robotomics instance wait
1 parent b1b6845 commit 21129b8

File tree

15 files changed

+379
-157
lines changed

15 files changed

+379
-157
lines changed

src/App.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
<template>
2-
<main-layout v-if="isReady" :pagetitle="title">
2+
<main-layout :pagetitle="title">
33
<router-view />
44
</main-layout>
5-
<loader-layout v-else :pagetitle="title">
6-
<robo-layout-section gcenter>
7-
<robo-loader size="2" />
8-
</robo-layout-section>
9-
</loader-layout>
105
</template>
116

127
<script>
13-
import LoaderLayout from "@/components/layouts/Loader.vue";
148
import MainLayout from "@/components/layouts/Main.vue";
159
import { inject, ref, watch } from "vue";
1610
import { useRoute, useRouter } from "vue-router";
@@ -19,7 +13,6 @@ import { useStore } from "vuex";
1913
export default {
2014
name: "App",
2115
components: {
22-
LoaderLayout,
2316
MainLayout
2417
},
2518
setup() {
@@ -76,7 +69,6 @@ export default {
7669
);
7770
7871
return {
79-
isReady: RobonomicsProvider.isReady,
8072
title
8173
};
8274
}

src/hooks/useAccount.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { useRobonomics } from "./useRobonomics";
33

44
export const useAccount = () => {
55
const account = ref(null);
6-
const robonomics = useRobonomics();
7-
if (robonomics.accountManager.account) {
8-
account.value = robonomics.accountManager.account.address;
6+
const { accountManager } = useRobonomics();
7+
if (accountManager.account) {
8+
account.value = accountManager.account.address;
99
}
10-
const unsubscribe = robonomics.accountManager.onChange((res) => {
10+
const unsubscribe = accountManager.onChange((res) => {
1111
account.value = res.address;
1212
});
1313
return { account, unsubscribe };

src/hooks/useBalance.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { useRobonomics } from "./useRobonomics";
33

44
export const useBalance = (account) => {
55
const balance = ref(null);
6-
const robonomics = useRobonomics();
6+
const { isReady, getInstance } = useRobonomics();
77
let unsubscribe;
8-
watch(account, async () => {
8+
watch([account, isReady], async () => {
99
if (unsubscribe) {
1010
unsubscribe();
1111
}
12+
if (!isReady.value) {
13+
return;
14+
}
15+
const robonomics = getInstance();
1216
unsubscribe = await robonomics.account.getBalance(account.value, (r) => {
1317
balance.value = r.free.sub(r.feeFrozen).toNumber();
1418
});

src/hooks/useDevices.js

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,70 @@ import { onUnmounted, ref, watch } from "vue";
33
import { useRobonomics } from "./useRobonomics";
44

55
export const useDevices = (initialOwner = null) => {
6-
const robonomics = useRobonomics();
6+
const { isReady, getInstance } = useRobonomics();
77
const owner = ref(initialOwner);
88
const devices = ref([]);
99

10+
const getDevices = async (owner) => {
11+
if (!isReady.value) {
12+
const data = localStorage.getItem(`hadevices:${owner}`);
13+
if (data) {
14+
try {
15+
const parsedData = JSON.parse(data);
16+
console.log("getDevices cache");
17+
return { data: parsedData.value, cache: true };
18+
} catch (error) {
19+
console.log("hadevices bad", error);
20+
}
21+
} else {
22+
return { data: [], cache: true };
23+
}
24+
} else {
25+
try {
26+
const result = await getInstance().rws.getDevices(owner);
27+
const list = result.map((item) => {
28+
return item.toHuman();
29+
});
30+
localStorage.setItem(
31+
`hadevices:${owner}`,
32+
JSON.stringify({ time: Date.now(), value: list })
33+
);
34+
console.log("getDevices chain");
35+
return { data: list, cache: false };
36+
} catch (error) {
37+
console.log(error);
38+
}
39+
}
40+
return { data: [], cache: false };
41+
};
42+
1043
const loadDevices = async () => {
1144
if (owner.value) {
1245
try {
1346
validateAddress(owner.value);
14-
const result = await robonomics.rws.getDevices(owner.value);
15-
devices.value = result.map((item) => {
16-
return item.toHuman();
17-
});
47+
const result = await getDevices(owner.value);
48+
devices.value = result.data;
49+
if (result.cache) {
50+
const stop = watch(
51+
isReady,
52+
async () => {
53+
if (isReady.value) {
54+
const result = await getDevices(owner.value);
55+
devices.value = result.data;
56+
stop();
57+
}
58+
},
59+
{ immediate: true }
60+
);
61+
}
1862
return;
1963
} catch (error) {
2064
console.log(error);
2165
}
2266
}
2367
devices.value = [];
2468
};
69+
2570
watch(
2671
owner,
2772
async () => {
@@ -32,19 +77,25 @@ export const useDevices = (initialOwner = null) => {
3277
}
3378
);
3479

35-
(async () => {
36-
const unsubscribe = await robonomics.events.on(
37-
{ section: "rws", method: "NewDevices" },
38-
async (result) => {
39-
for (const event of result) {
40-
if (event.data[0].toHuman() === owner.value) {
41-
await loadDevices();
80+
watch(
81+
isReady,
82+
async (isReady) => {
83+
if (isReady) {
84+
const unsubscribe = await getInstance().events.on(
85+
{ section: "rws", method: "NewDevices" },
86+
async (result) => {
87+
for (const event of result) {
88+
if (event.data[0].toHuman() === owner.value) {
89+
await loadDevices();
90+
}
91+
}
4292
}
43-
}
93+
);
94+
onUnmounted(unsubscribe);
4495
}
45-
);
46-
onUnmounted(unsubscribe);
47-
})();
96+
},
97+
{ immediate: true }
98+
);
4899

49100
return {
50101
owner,

src/hooks/useRobonomics.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { inject, toRaw } from "vue";
1+
import { inject } from "vue";
22

33
export function useRobonomics() {
4-
const { instance } = inject("RobonomicsProvider");
5-
return toRaw(instance).value;
4+
const provider = inject("RobonomicsProvider");
5+
return {
6+
accountManager: provider.accountManager,
7+
isReady: provider.isReady,
8+
robonomics: provider.instance,
9+
getInstance: () => provider.instance.value
10+
};
611
}

src/hooks/useSend.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { useRobonomics } from "./useRobonomics";
33
import { useSubscription } from "./useSubscription";
44

55
export const useSend = () => {
6-
const robonomics = useRobonomics();
6+
const { isReady, getInstance, accountManager } = useRobonomics();
77
const { getFreeWeightCalc } = useSubscription();
88

99
const getCallWeight = async (tx, signer) => {
1010
if (!signer) {
11-
if (robonomics.accountManager.account) {
12-
signer = robonomics.accountManager.account.address;
11+
if (accountManager.account) {
12+
signer = accountManager.account.address;
1313
} else {
1414
throw new Error("Signer required");
1515
}
@@ -41,6 +41,12 @@ export const useSend = () => {
4141
tx.result.value = null;
4242
tx.error.value = null;
4343
tx.process.value = true;
44+
if (!isReady.value) {
45+
tx.error.value = "Robonomics is not ready";
46+
tx.process.value = false;
47+
return;
48+
}
49+
const robonomics = getInstance();
4450
try {
4551
if (subscription) {
4652
await checkWeight(call, subscription);

src/hooks/useSubscription.js

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,86 @@
1+
import { createType, TypeRegistry } from "@polkadot/types";
12
import { validateAddress } from "@polkadot/util-crypto";
2-
import { computed, reactive, ref, toRaw, watch } from "vue";
3+
import { computed, ref, shallowRef, watch } from "vue";
34
import { useDevices } from "./useDevices";
45
import { useRobonomics } from "./useRobonomics";
56

7+
const getRegistry = () => {
8+
const registry = new TypeRegistry();
9+
const types = {
10+
PalletRobonomicsRwsSubscription: {
11+
_enum: {
12+
Lifetime: {
13+
tps: "Compact<u32>"
14+
},
15+
Daily: {
16+
days: "Compact<u32>"
17+
}
18+
}
19+
},
20+
PalletRobonomicsRwsSubscriptionLedger: {
21+
freeWeight: "Compact<u64>",
22+
issueTime: "Compact<u64>",
23+
lastUpdate: "Compact<u64>",
24+
kind: {
25+
_enum: {
26+
Lifetime: {
27+
tps: "Compact<u32>"
28+
},
29+
Daily: {
30+
days: "Compact<u32>"
31+
}
32+
}
33+
}
34+
}
35+
};
36+
registry.register(types);
37+
return registry;
38+
};
39+
640
export const useSubscription = (initialOwner = null) => {
741
const owner = ref(initialOwner);
8-
const dataRaw = reactive({ value: null });
42+
const dataRaw = shallowRef(null);
943

10-
const robonomics = useRobonomics();
44+
const { isReady, getInstance } = useRobonomics();
1145
const { devices, loadDevices } = useDevices(owner);
1246

1347
const getReferenceCallWeight = () => {
14-
return robonomics.api.consts.rws.referenceCallWeight;
48+
return getInstance().api.consts.rws.referenceCallWeight;
1549
};
1650

1751
const getLedger = async (owner) => {
18-
const res = await robonomics.rws.getLedger(owner);
19-
if (!res.isEmpty) {
20-
return res.value;
52+
if (!isReady.value) {
53+
const data = localStorage.getItem(`hasubscription:${owner}`);
54+
if (data) {
55+
try {
56+
const parsedData = JSON.parse(data);
57+
// if (parsedData.time + DAYS_TO_MS > Date.now()) {
58+
const res = createType(
59+
getRegistry(),
60+
"Option<PalletRobonomicsRwsSubscriptionLedger>",
61+
parsedData.value
62+
);
63+
console.log("getLedger cache");
64+
return { data: res.value, cache: true };
65+
// }
66+
} catch (error) {
67+
console.log("hasubscription bad", error);
68+
}
69+
} else {
70+
return { data: undefined, cache: true };
71+
}
72+
} else {
73+
const res = await getInstance().rws.getLedger(owner);
74+
if (!res.isEmpty) {
75+
localStorage.setItem(
76+
`hasubscription:${owner}`,
77+
JSON.stringify({ time: Date.now(), value: res.value.toJSON() })
78+
);
79+
console.log("getLedger chain");
80+
return { data: res.value, cache: false };
81+
}
2182
}
22-
return;
83+
return { data: undefined, cache: false };
2384
};
2485

2586
const DAYS_TO_MS = 24 * 60 * 60 * 1000;
@@ -63,14 +124,13 @@ export const useSubscription = (initialOwner = null) => {
63124
if (dataRaw.value === null) {
64125
return null;
65126
}
66-
const dataRawObject = toRaw(dataRaw);
67-
if (dataRawObject.value.kind.isLifetime) {
127+
if (dataRaw.value.kind.isLifetime) {
68128
return null;
69129
}
70-
const issue_time = dataRawObject.value.issueTime.toNumber();
130+
const issue_time = dataRaw.value.issueTime.toNumber();
71131
let days = 0;
72-
if (dataRawObject.value.kind.isDaily) {
73-
days = dataRawObject.value.kind.value.days.toNumber();
132+
if (dataRaw.value.kind.isDaily) {
133+
days = dataRaw.value.kind.value.days.toNumber();
74134
}
75135
return issue_time + days * DAYS_TO_MS;
76136
});
@@ -79,10 +139,9 @@ export const useSubscription = (initialOwner = null) => {
79139
if (dataRaw.value === null) {
80140
return 0;
81141
}
82-
const dataRawObject = toRaw(dataRaw);
83142
let days = 0;
84-
if (dataRawObject.value.kind.isDaily) {
85-
days = dataRawObject.value.kind.value.days.toNumber();
143+
if (dataRaw.value.kind.isDaily) {
144+
days = dataRaw.value.kind.value.days.toNumber();
86145
}
87146
return days / 30;
88147
});
@@ -105,10 +164,20 @@ export const useSubscription = (initialOwner = null) => {
105164
if (owner.value) {
106165
try {
107166
validateAddress(owner.value);
108-
const ledger = await getLedger(owner.value);
109-
if (ledger) {
110-
dataRaw.value = ledger;
111-
return;
167+
const result = await getLedger(owner.value);
168+
dataRaw.value = result.data;
169+
if (result.cache) {
170+
const stop = watch(
171+
isReady,
172+
async () => {
173+
if (isReady.value) {
174+
const result = await getLedger(owner.value);
175+
dataRaw.value = result.data;
176+
stop();
177+
}
178+
},
179+
{ immediate: true }
180+
);
112181
}
113182
// eslint-disable-next-line no-empty
114183
} catch (error) {

0 commit comments

Comments
 (0)