Skip to content

Commit

Permalink
Fix dapp queue
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-plusplus committed Jul 12, 2021
1 parent c3c26bc commit a1005f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
11 changes: 8 additions & 3 deletions src/lib/temple/back/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@temple-wallet/dapp/dist/types";
import { browser, Runtime } from "webextension-polyfill-ts";

import { createQueue } from "lib/queue";
import { addLocalOperation } from "lib/temple/activity";
import {
getCurrentPermission,
Expand Down Expand Up @@ -46,6 +47,8 @@ const ACCOUNT_NAME_PATTERN = /^[a-zA-Z0-9 _-]{1,16}$/;
const AUTODECLINE_AFTER = 60_000;
const BEACON_ID = `temple_wallet_${browser.runtime.id}`;

const enqueueDApp = createQueue();

export async function init() {
const vaultExist = await Vault.isExist();
inited(vaultExist);
Expand Down Expand Up @@ -451,13 +454,15 @@ export async function processDApp(
return withInited(() => getCurrentPermission(origin));

case TempleDAppMessageType.PermissionRequest:
return withInited(() => requestPermission(origin, req));
return withInited(() =>
enqueueDApp(() => requestPermission(origin, req))
);

case TempleDAppMessageType.OperationRequest:
return withInited(() => requestOperation(origin, req));
return withInited(() => enqueueDApp(() => requestOperation(origin, req)));

case TempleDAppMessageType.SignRequest:
return withInited(() => requestSign(origin, req));
return withInited(() => enqueueDApp(() => requestSign(origin, req)));

case TempleDAppMessageType.BroadcastRequest:
return withInited(() => requestBroadcast(origin, req));
Expand Down
47 changes: 18 additions & 29 deletions src/lib/temple/back/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Runtime } from "webextension-polyfill-ts";

import { createQueue } from "lib/queue";
import * as Actions from "lib/temple/back/actions";
import { intercom } from "lib/temple/back/defaults";
import { store, toFront } from "lib/temple/back/store";
Expand All @@ -11,7 +10,6 @@ import {
} from "lib/temple/types";

const frontStore = store.map(toFront);
const enqueueDApp = createQueue();

export async function start() {
intercom.onRequest(processRequest);
Expand Down Expand Up @@ -194,33 +192,24 @@ async function processRequest(
};
}

return enqueueDApp(async () => {
if (!intercom.isConnected(port)) {
throw new Error("Disconnected");
}

if (!req.beacon) {
const resPayload = await Actions.processDApp(
req.origin,
req.payload
);
return {
type: TempleMessageType.PageResponse,
payload: resPayload ?? null,
};
} else {
const res = await Actions.processBeacon(
req.origin,
req.payload,
req.encrypted
);
return {
type: TempleMessageType.PageResponse,
payload: res?.payload ?? null,
encrypted: res?.encrypted,
};
}
});
if (!req.beacon) {
const resPayload = await Actions.processDApp(req.origin, req.payload);
return {
type: TempleMessageType.PageResponse,
payload: resPayload ?? null,
};
} else {
const res = await Actions.processBeacon(
req.origin,
req.payload,
req.encrypted
);
return {
type: TempleMessageType.PageResponse,
payload: res?.payload ?? null,
encrypted: res?.encrypted,
};
}
}
break;
}
Expand Down

0 comments on commit a1005f4

Please sign in to comment.