Skip to content

Commit

Permalink
refactor: storage 抽为同构方案 & httpClient 重构支持流式 & configManager 抽离
Browse files Browse the repository at this point in the history
  • Loading branch information
moshangqi committed Dec 26, 2023
1 parent 5bcc146 commit 7ee85a0
Show file tree
Hide file tree
Showing 70 changed files with 1,362 additions and 1,400 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const tslintConfig = {
},
rules: {
...eslintConfig.rules,
'no-unused-vars': 1,
'no-unused-vars': 0,
'no-undef': 'off',
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': 'off',
Expand Down
35 changes: 0 additions & 35 deletions src/background/actionListener/configManager.ts

This file was deleted.

19 changes: 5 additions & 14 deletions src/background/actionListener/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Chrome from '@/background/core/chromeExtension';
import chromeExtension from '@/background/core/chromeExtension';
import { BackgroundEvents } from '@/isomorphic/background';
import { createStorageActionListener } from './storage';
import { createUserActionListener } from './user';
import { createClipActionListener } from './clip';
import { createTabActionListener } from './tab';
import { createSidePanelActionListener } from './sidePanel';
import { createRequestActionListener } from './request';
import { createManagerConfigActionListener } from './configManager';
import HttpClient from '../core/httpClient';

type SendResponse = (response: any) => void;

Check warning on line 10 in src/background/actionListener/index.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type

Expand All @@ -15,12 +14,12 @@ export interface RequestMessage<T> {
data: T;
}

export const initBackGroundActionListener = () => {
Chrome.runtime.onMessage.addListener(
export const initBackGroundActionListener = (httpClient: HttpClient) => {
chromeExtension.runtime.onMessage.addListener(
(request: RequestMessage<any>, _sender: chrome.runtime.MessageSender, sendResponse: SendResponse) => {

Check warning on line 19 in src/background/actionListener/index.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
switch (request.action) {
case BackgroundEvents.OperateUser: {
createUserActionListener(request, sendResponse, _sender);
createUserActionListener(request, sendResponse, _sender, httpClient);
break;
}
case BackgroundEvents.OperateStorage: {
Expand All @@ -39,14 +38,6 @@ export const initBackGroundActionListener = () => {
createSidePanelActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateRequest: {
createRequestActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateManagerConfig: {
createManagerConfigActionListener(request, sendResponse, _sender);
break;
}
default: {
break;
}
Expand Down
26 changes: 0 additions & 26 deletions src/background/actionListener/request.ts

This file was deleted.

17 changes: 9 additions & 8 deletions src/background/actionListener/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { pick } from 'lodash';
import Chrome from '@/background/core/chromeExtension';
import chromeExtension from '@/background/core/chromeExtension';
import { IOperateUserData, OperateUserEnum } from '@/isomorphic/background/user';
import { IUser } from '@/isomorphic/interface';
import { storage } from '@/isomorphic/storage';
import requestFn from '@/background/core/request';
import { STORAGE_KEYS, YUQUE_DOMAIN } from '@/config';
import HttpClient from '../core/httpClient';
import { RequestMessage } from './index';

export const SERVER_URLS = {
Expand All @@ -15,7 +15,7 @@ export const SERVER_URLS = {

const createLoginWindow = (): Promise<number> => {
return new Promise(resolve => {
Chrome.windows.create(
chromeExtension.windows.create(
{
focused: true,
width: 500,
Expand All @@ -32,7 +32,7 @@ const createLoginWindow = (): Promise<number> => {

const waitForWindowLogined = (windowId: number) => {
return new Promise<void>(resolve => {
Chrome.webRequest.onCompleted.addListener(
chromeExtension.webRequest.onCompleted.addListener(
data => {
if ([SERVER_URLS.DASHBOARD, SERVER_URLS.LOGIN].includes(data.url)) {
if (data.statusCode === 200) {
Expand All @@ -50,26 +50,27 @@ const waitForWindowLogined = (windowId: number) => {

const removeWindow = (windowId: number) => {
return new Promise(resolve => {
Chrome.windows.remove(windowId, resolve);
chromeExtension.windows.remove(windowId, resolve);
});
};

export async function createUserActionListener(
request: RequestMessage<IOperateUserData>,
callback: (params: any) => void,

Check warning on line 59 in src/background/actionListener/user.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
sender: chrome.runtime.MessageSender,
httpClient: HttpClient,
) {
const { type } = request.data;
switch (type) {
case OperateUserEnum.login: {
try {
await requestFn('/api/accounts/logout', {
await httpClient.handleRequest('/api/accounts/logout', {
method: 'DELETE',
});
const windowId = await createLoginWindow();
await waitForWindowLogined(windowId);
await removeWindow(windowId);
const { data, status } = await requestFn('/api/mine', {
const { data, status } = await httpClient.handleRequest('/api/mine', {
method: 'GET',
});
if (status === 200) {
Expand All @@ -89,7 +90,7 @@ export async function createUserActionListener(
break;
}
case OperateUserEnum.getUserShortCut: {
const result = await Chrome.commands.getAll();
const result = await chromeExtension.commands.getAll();
callback(result);
break;
}
Expand Down
10 changes: 10 additions & 0 deletions src/background/core/chromeExtension.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMsgId } from '@/isomorphic/util';

const chromeExtension = {
action: chrome.action,
cookies: chrome.cookies,
Expand Down Expand Up @@ -28,6 +30,14 @@ const chromeExtension = {
}
}
},
async sendToContent(tab: chrome.tabs.Tab, type: string, data: any, id?: string) {
const params = {
type,
data,
id: id || getMsgId({ type }),
};
chrome.tabs.sendMessage(tab.id as number, params);
},
},
webRequest: chrome.webRequest,
declarativeNetRequest: chrome.declarativeNetRequest,
Expand Down
36 changes: 0 additions & 36 deletions src/background/core/configManager/clip.ts

This file was deleted.

52 changes: 0 additions & 52 deletions src/background/core/configManager/levitate.ts

This file was deleted.

96 changes: 0 additions & 96 deletions src/background/core/configManager/wordMark.ts

This file was deleted.

Loading

0 comments on commit 7ee85a0

Please sign in to comment.