2020-11-03 00:41:28 +01:00
|
|
|
function delay(time) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return new Promise((resolve) => setTimeout(resolve, time));
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function loadFromMock3Box(key) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const res = await window.fetch(`http://localhost:8889?key=${key}`);
|
|
|
|
const text = await res.text();
|
|
|
|
return text.length ? JSON.parse(text) : null;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function saveToMock3Box(key, newDataAtKey) {
|
2020-04-15 19:23:27 +02:00
|
|
|
const res = await window.fetch('http://localhost:8889', {
|
2019-12-06 16:32:36 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
key,
|
|
|
|
data: newDataAtKey,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-12-06 16:32:36 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return res.text();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Mock3Box {
|
2020-11-03 00:41:28 +01:00
|
|
|
static openBox(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.address = address;
|
2019-09-16 19:11:01 +02:00
|
|
|
return Promise.resolve({
|
2020-02-15 21:34:12 +01:00
|
|
|
onSyncDone: (cb) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setTimeout(cb, 200);
|
2019-11-20 01:03:20 +01:00
|
|
|
},
|
2019-09-16 19:11:01 +02:00
|
|
|
openSpace: async (spaceName, config) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { onSyncDone } = config;
|
|
|
|
this.spaceName = spaceName;
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
setTimeout(onSyncDone, 150);
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await delay(50);
|
2019-09-16 19:11:01 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
private: {
|
|
|
|
get: async (key) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await delay(50);
|
2020-11-03 00:41:28 +01:00
|
|
|
const res = await loadFromMock3Box(
|
|
|
|
`${this.address}-${this.spaceName}-${key}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return res;
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
|
|
|
set: async (key, data) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
await saveToMock3Box(
|
|
|
|
`${this.address}-${this.spaceName}-${key}`,
|
|
|
|
data,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
await delay(50);
|
|
|
|
return null;
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
2020-08-14 13:47:02 +02:00
|
|
|
logout: () => undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
2019-09-26 09:24:52 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
static async getConfig(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const backup = await loadFromMock3Box(`${address}-metamask-metamaskBackup`);
|
|
|
|
return backup ? { spaces: { metamask: {} } } : {};
|
2019-09-26 09:24:52 +02:00
|
|
|
}
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
module.exports = Mock3Box;
|