Skip to content

Commit

Permalink
Refactoring enums
Browse files Browse the repository at this point in the history
  • Loading branch information
shoonia committed Apr 19, 2024
1 parent 244df56 commit 5213b77
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IState } from './popup/store/types';
import type {
IRequest,
IResponse,
RequestEvents,
REQUEST,
} from './transport';

export const getURL = (path: string) => {
Expand All @@ -13,7 +13,7 @@ export const to = (url: string) => {
return chrome.tabs.create({ url });
};

export const sendRequest = async (type: RequestEvents, state: IState): Promise<void> => {
export const sendRequest = async (type: REQUEST, state: IState): Promise<void> => {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
Expand Down
10 changes: 5 additions & 5 deletions src/module/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
RequestEvents,
ResponseEvents,
REQUEST,
RESPONSE,
addRequestListener,
dispatchResponce,
} from '../transport';
import { downloadFiles } from './downloadFiles';

addRequestListener((req) => {
switch (req?.type) {
case RequestEvents.onmout: {
case REQUEST.READY: {
return dispatchResponce({
type: ResponseEvents.content_loaded,
type: RESPONSE.LOADED,
});
}

case RequestEvents.download: {
case REQUEST.DOWNLOAD: {
return downloadFiles(req.state);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/popup/store/appModule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TMoudule } from './types';
import {
type IResponse,
RequestEvents,
ResponseEvents,
REQUEST,
RESPONSE,
} from '../../../src/transport';
import { onMessage, sendRequest } from '../../../src/chrome';

Expand All @@ -17,11 +17,11 @@ export const appModule: TMoudule = (store) => {
});

store.on('@ready', (state) => {
sendRequest(RequestEvents.onmout, state);
sendRequest(REQUEST.READY, state);
});

store.on('download/file', (state) => {
sendRequest(RequestEvents.download, state);
sendRequest(REQUEST.DOWNLOAD, state);
});

store.on('toggle/includePageId', (_, includePageId) => {
Expand All @@ -38,7 +38,7 @@ export const appModule: TMoudule = (store) => {

onMessage<IResponse>((res) => {
switch (res?.type) {
case ResponseEvents.content_loaded: {
case RESPONSE.LOADED: {
return store.set({ isEnable: true });
}
}
Expand Down
28 changes: 15 additions & 13 deletions src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import type { IState } from './popup/store/types';

export const CUSTOM_EVENT_REQUEST = '>_request::velo-filesystem';
export const CUSTOM_EVENT_RESPONSE = '<_response::velo-filesystem';
export const enum CUSTOM_EVENT {
REQUEST = '>_request::velo-filesystem',
RESPONSE = '<_response::velo-filesystem',
}

export const enum RequestEvents {
onmout = '>_onmout',
download = '>_download',
export const enum REQUEST {
READY = '>_ready',
DOWNLOAD = '>_download',
}

export const enum ResponseEvents {
content_loaded = '<_content_loaded',
export const enum RESPONSE {
LOADED = '<_loaded',
}

export interface IRequest {
readonly type: RequestEvents;
readonly type: REQUEST;
readonly state: IState;
}

export interface IResponse {
readonly type: ResponseEvents;
readonly type: RESPONSE;
}

type TDispatcer<T> = (detail: T) => boolean;
type TListener<T> = (callback: (detail: T) => void) => void

export const dispatchResponce: TDispatcer<IResponse> = (detail) =>
dispatchEvent(
new CustomEvent<IResponse>(CUSTOM_EVENT_RESPONSE, { detail }),
new CustomEvent<IResponse>(CUSTOM_EVENT.RESPONSE, { detail }),
);

export const dispatchRequest: TDispatcer<IRequest> = (detail) =>
dispatchEvent(
new CustomEvent<IRequest>(CUSTOM_EVENT_REQUEST, { detail }),
new CustomEvent<IRequest>(CUSTOM_EVENT.REQUEST, { detail }),
);

export const addResponseListener: TListener<IResponse> = (callback) =>
addEventListener(CUSTOM_EVENT_RESPONSE, (event) => callback(event.detail));
addEventListener(CUSTOM_EVENT.RESPONSE, (event) => callback(event.detail));


export const addRequestListener: TListener<IRequest> = (callback) =>
addEventListener(CUSTOM_EVENT_REQUEST, (event) => callback(event.detail));
addEventListener(CUSTOM_EVENT.REQUEST, (event) => callback(event.detail));
7 changes: 3 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type monaco from 'monaco-editor';
import type {
IRequest,
IResponse,
CUSTOM_EVENT_REQUEST,
CUSTOM_EVENT_RESPONSE,
CUSTOM_EVENT,
} from './transport';

interface IPage {
Expand All @@ -28,7 +27,7 @@ declare global {
}

interface WindowEventMap {
readonly [CUSTOM_EVENT_REQUEST]: CustomEvent<IRequest>;
readonly [CUSTOM_EVENT_RESPONSE]: CustomEvent<IResponse>;
readonly [CUSTOM_EVENT.REQUEST]: CustomEvent<IRequest>;
readonly [CUSTOM_EVENT.RESPONSE]: CustomEvent<IResponse>;
}
}

0 comments on commit 5213b77

Please sign in to comment.