Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 23, 2024
1 parent b1e4359 commit f181290
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 47 deletions.
57 changes: 10 additions & 47 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
import '../../packages/wasm_exec.js';
import { type Message } from '@core/background';
import { jq } from '@packages/jq';
import { format, tokenize } from '@packages/tokenizer';
import { is } from './helpres';
import { handler } from './handler';

// eslint-disable-next-line @typescript-eslint/no-deprecated
chrome.runtime.onMessage.addListener(function (message: Message | object, _, sendResponse) {
if (is(message, 'tokenize')) {
tokenize(message.json)
.then(sendResponse)
.catch((err: Error) =>
sendResponse({
type: 'error',
scope: 'worker',
stack: err.stack,
error: err.message,
}),
);
chrome.runtime.onMessage.addListener(function (message: Message, _, sendResponse): boolean {
handler(message, sendResponse)
.catch((err: Error) => sendResponse({
type: 'error',
scope: 'worker',
stack: err.stack,
error: err.message,
}));

return true;
}

if (is(message, 'format')) {
format(message.json)
.then(sendResponse)
.catch((err: Error) =>
sendResponse({
type: 'error',
scope: 'worker',
stack: err.stack,
error: err.message,
}),
);

return true;
}

if (is(message, 'jq')) {
jq(message.json, message.query)
.then(sendResponse)
.catch((err: Error) =>
sendResponse({
type: 'error',
scope: 'worker',
stack: err.stack,
error: err.message,
}),
);

return true;
}
return true;
});
18 changes: 18 additions & 0 deletions src/background/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Message } from '@core/background';
import { jq } from '@packages/jq';
import { format, tokenize } from '@packages/tokenizer';
import { is } from './helpres';

export const handler = async (message: Message, sendResponse: (resp: unknown) => void): Promise<void> => {
if (is(message, 'tokenize')) {
await tokenize(message.json)
.then(sendResponse);
} else if (is(message, 'format')) {
await format(message.json)
.then(sendResponse);
} else if (is(message, 'jq')) {
await jq(message.json, message.query)
.then(sendResponse);
}
};

0 comments on commit f181290

Please sign in to comment.