Skip to content

Commit

Permalink
feat: revert changes to services
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron committed Apr 18, 2024
1 parent 08d8d14 commit 2a42b91
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 97 deletions.
12 changes: 6 additions & 6 deletions packages/app/src/systems/CRX/background/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import { DatabaseEvents } from './services/DatabaseEvents';
import { VaultService } from './services/VaultService';

errorBoundary(() => {
const communicationProtocol: CommunicationProtocol | undefined =
new CommunicationProtocol();
const communicationProtocol = new CommunicationProtocol();

BackgroundService.start(communicationProtocol);
VaultService.start(communicationProtocol);
DatabaseEvents.start(communicationProtocol);

chrome.runtime.onConnect.addListener((port) => {
// Only allow connections from the extension
Expand All @@ -23,10 +26,7 @@ errorBoundary(() => {
return;
}
if ([BACKGROUND_SCRIPT_NAME, VAULT_SCRIPT_NAME].includes(port.name)) {
communicationProtocol?.addConnection(port);
communicationProtocol.addConnection(port);
}
});
BackgroundService.start(communicationProtocol);
VaultService.start(communicationProtocol);
DatabaseEvents.start(communicationProtocol);
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { BACKGROUND_SCRIPT_NAME } from '@fuel-wallet/types';
import type { Connection } from '@fuel-wallet/types';
import {
CONTENT_SCRIPT_NAME,
MessageTypes,
type RequestMessage,
} from '@fuels/connectors';
import { CONTENT_SCRIPT_NAME, MessageTypes } from '@fuels/connectors';
import { Address } from 'fuels';
import type {
JSONRPCParams,
Expand Down Expand Up @@ -34,50 +30,37 @@ type EventOrigin = {
export class BackgroundService {
readonly server: JSONRPCServer<EventOrigin>;
readonly communicationProtocol: CommunicationProtocol;
readonly removeCommunicationListeners: () => void;
readonly methods = [
this.ping,
this.version,
this.isConnected,
this.accounts,
this.connect,
this.network,
this.disconnect,
this.signMessage,
this.sendTransaction,
this.currentAccount,
this.addAssets,
this.assets,
this.addNetwork,
this.addAbi,
this.getAbi,
// biome-ignore lint/complexity/noBannedTypes: Needed to imply that functions have the "name" property
] as Array<Function>;

constructor(communicationProtocol: CommunicationProtocol) {
this.communicationProtocol = communicationProtocol;
this.server = new JSONRPCServer<EventOrigin>();
this.server.applyMiddleware(this.connectionMiddleware.bind(this));
this.removeCommunicationListeners = this.setupListeners();
this.externalMethods();
this.setupListeners();
this.externalMethods([
this.ping,
this.version,
this.isConnected,
this.accounts,
this.connect,
this.network,
this.disconnect,
this.signMessage,
this.sendTransaction,
this.currentAccount,
this.addAssets,
this.assets,
this.addNetwork,
this.addAbi,
this.getAbi,
]);
}

static start(communicationProtocol: CommunicationProtocol) {
return new BackgroundService(communicationProtocol);
}

private stop() {
this.clearExternalMethods();
this.removeCommunicationListeners();
}

public restart(communicationProtocol: CommunicationProtocol) {
this.stop();
return new BackgroundService(communicationProtocol);
}

setupListeners() {
const handleRequest = async (event: RequestMessage) => {
this.communicationProtocol.on(MessageTypes.request, async (event) => {
if (event.target !== BACKGROUND_SCRIPT_NAME) return;
const origin = event.sender?.origin!;
const title = event.sender?.tab?.title!;
Expand All @@ -88,36 +71,27 @@ export class BackgroundService {
favIconUrl,
});
if (response) {
await new Promise((resolve) => setTimeout(resolve, 1500));
this.communicationProtocol.postMessage({
id: event.id,
type: MessageTypes.response,
target: CONTENT_SCRIPT_NAME,
response,
});
}
};

this.communicationProtocol.on(MessageTypes.request, handleRequest);
return () => {
this.communicationProtocol.off(MessageTypes.request, handleRequest);
};
}

externalMethods() {
for (let i = 0; i < this.methods.length; i++) {
const method = this.methods[i];
const methodName = String(method?.name || method);
this.server.addMethod(methodName, this[methodName].bind(this));
}
});
}

clearExternalMethods() {
for (let i = 0; i < this.methods.length; i++) {
const method = this.methods[i];
const methodName = String(method?.name || method);
this.server.removeMethod(methodName);
}
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
externalMethods(methods: Array<string | any>) {
// biome-ignore lint/complexity/noForEach: <explanation>
methods.forEach((method) => {
let methodName = method;
if (method.name) {
methodName = method.name;
}
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
this.server.addMethod(methodName, this[methodName].bind(this) as any);
});
}

async requireAccounts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ export class DatabaseEvents {
return new DatabaseEvents(communicationProtocol);
}

private stop() {
this.databaseObservable.removeAllListeners();
}

restart(communicationProtocol: CommunicationProtocol) {
this.stop();
return new DatabaseEvents(communicationProtocol);
}

createEvents(events: EventMessageEvents): EventMessage {
return {
target: CONTENT_SCRIPT_NAME,
Expand Down
25 changes: 1 addition & 24 deletions packages/app/src/systems/CRX/background/services/VaultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import type { CommunicationProtocol } from './CommunicationProtocol';
export class VaultService extends VaultServer {
readonly communicationProtocol: CommunicationProtocol;
private autoLockInterval?: NodeJS.Timeout;
private removeCommunicationListeners: () => void;

constructor(communicationProtocol: CommunicationProtocol) {
super();
this.communicationProtocol = communicationProtocol;
this.autoLock();
this.autoUnlock();
this.removeCommunicationListeners = this.setupListeners();
this.setupListeners();
}

async checkVaultIntegrity() {
Expand Down Expand Up @@ -90,24 +89,6 @@ export class VaultService extends VaultServer {
return new VaultService(communicationProtocol);
}

private async stop() {
if (this.autoLockInterval) {
clearInterval(this.autoLockInterval);
this.autoLockInterval = undefined;
}
if (await this.isLocked()) {
const secret = await loadSecret();
secret && (await this.unlock({ password: secret, shouldSave: false }));
}
this.removeCommunicationListeners();
this.removeAllListeners();
}

async restart(communicationProtocol: CommunicationProtocol) {
await this.stop();
return new VaultService(communicationProtocol);
}

setupListeners() {
const handleRequest = async (event: RequestMessage) => {
if (!event.sender?.origin?.includes(chrome.runtime.id)) return;
Expand Down Expand Up @@ -138,10 +119,6 @@ export class VaultService extends VaultServer {
};
chrome.runtime.onMessage.addListener(handleRestartEvent);
this.communicationProtocol.on(MessageTypes.request, handleRequest);
return () => {
this.communicationProtocol.off(MessageTypes.request, handleRequest);
chrome.runtime.onMessage.removeListener(handleRestartEvent);
};
}

emitLockEvent() {
Expand Down

0 comments on commit 2a42b91

Please sign in to comment.