Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 11, 2024
1 parent 237d571 commit 540fec9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import '../../packages/wasm_exec.js';
import { JqParams, TokenizeParams } from '@core/background';
import { jq } from '@packages/jq';
import { tokenize } from '@packages/tokenizer';
import { is } from './helpres';

// eslint-disable-next-line @typescript-eslint/no-deprecated
chrome.runtime.onMessage.addListener(function (message: TokenizeParams | JqParams | object, _, sendResponse) {
if (is(message, 'tokenize')) {
tokenize(message.json)
.then(sendResponse)
.catch((err: unknown) => console.error(err));

chrome.runtime.onMessage.addListener(function (message: TokenizeParams | JqParams, _, sendResponse) {
if (message.action == 'tokenize') {
tokenize(message.json).then(sendResponse);
return true;
}

if (message.action == 'jq') {
jq(message.json, message.query).then(sendResponse);
if (is(message, 'jq')) {
jq(message.json, message.query)
.then(sendResponse)
.catch((err: unknown) => console.error(err));

return true;
}
});
11 changes: 11 additions & 0 deletions src/background/helpres.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { JqParams, Message, TokenizeParams } from '@core/background';


interface Types {
'jq': JqParams,
'tokenize': TokenizeParams,
}

export const is = <T extends Message['action']>(message: object, type: T): message is Types[T] => {
return 'action' in message && message.action == type;
};
2 changes: 2 additions & 0 deletions src/content-script/dom/build-dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ describe('buildDom', () => {
{
type: 'string',
value: 'https://www.youtube.com/watch?v=QH2-TGUlwu4',
variant: 'url',
},
{
type: 'string',
value: '/json-formatter/examples/2-array-root.json',
variant: 'url',
},
],
},
Expand Down
2 changes: 2 additions & 0 deletions src/core/background/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export interface JqParams {
query: string;
}

export type Message = TokenizeParams | JqParams;

export { TokenizerResponse };
4 changes: 2 additions & 2 deletions src/core/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getURL = chrome.runtime.getURL;
const sendMessage = chrome.runtime.sendMessage;
// eslint-disable-next-line @typescript-eslint/no-deprecated
const { getURL, sendMessage } = chrome.runtime;

export { getURL, sendMessage };

0 comments on commit 540fec9

Please sign in to comment.