Skip to content

Commit

Permalink
⚡️ perf: reduce server service bundle size by refactoring with consta…
Browse files Browse the repository at this point in the history
…nt fold (lobehub#4641)

* refactor

* clean code
  • Loading branch information
arvinxx authored Nov 8, 2024
1 parent 6727431 commit 5a542b4
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/services/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const fileService = isServerMode ? new ServerService() : new ClientService();
export const fileService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
5 changes: 2 additions & 3 deletions src/services/import/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const importService = isServerMode ? new ServerService() : new ClientService();
export const importService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
5 changes: 2 additions & 3 deletions src/services/message/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const messageService = isServerMode ? new ServerService() : new ClientService();
export const messageService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
5 changes: 2 additions & 3 deletions src/services/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const pluginService = isServerMode ? new ServerService() : new ClientService();
export const pluginService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
5 changes: 2 additions & 3 deletions src/services/session/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const sessionService = isServerMode ? new ServerService() : new ClientService();
export const sessionService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
4 changes: 2 additions & 2 deletions src/services/topic/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const topicService = isServerMode ? new ServerService() : new ClientService();
export const topicService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
2 changes: 1 addition & 1 deletion src/services/upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fileEnv } from '@/config/file';
import { FileModel } from '@/database/client/models/file';
import { edgeClient } from '@/libs/trpc/client';
import { API_ENDPOINTS } from '@/services/_url';
import { FileMetadata, UploadFileParams } from '@/types/files';
Expand Down Expand Up @@ -68,6 +67,7 @@ class UploadService {
};

uploadToClientDB = async (params: UploadFileParams, file: File) => {
const { FileModel } = await import('@/database/client/models/file');
const fileArrayBuffer = await file.arrayBuffer();

// save to local storage
Expand Down
5 changes: 2 additions & 3 deletions src/services/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isServerMode } from '@/const/version';

import { ClientService } from './client';
import { ServerService } from './server';

export const userService = isServerMode ? new ServerService() : new ClientService();
export const userService =
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
3 changes: 2 additions & 1 deletion src/store/user/slices/common/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { StateCreator } from 'zustand/vanilla';
import { DEFAULT_PREFERENCE } from '@/const/user';
import { useOnlyFetchOnceSWR } from '@/libs/swr';
import { userService } from '@/services/user';
import { ClientService } from '@/services/user/client';
import type { UserStore } from '@/store/user';
import type { GlobalServerConfig } from '@/types/serverConfig';
import { UserInitializationState } from '@/types/user';
Expand Down Expand Up @@ -47,6 +46,8 @@ export const createCommonSlice: StateCreator<
await mutate(GET_USER_STATE_KEY);
},
updateAvatar: async (avatar) => {
const { ClientService } = await import('@/services/user/client');

const clientService = new ClientService();

await clientService.updateAvatar(avatar);
Expand Down
8 changes: 6 additions & 2 deletions src/store/user/slices/sync/action.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import useSWR, { SWRResponse } from 'swr';
import type { StateCreator } from 'zustand/vanilla';

import { syncService } from '@/services/sync';
import type { UserStore } from '@/store/user';
import { OnSyncEvent, PeerSyncStatus } from '@/types/sync';
import { browserInfo } from '@/utils/platform';
Expand Down Expand Up @@ -53,6 +52,8 @@ export const createSyncSlice: StateCreator<
const defaultUserName = `My ${browserInfo.browser} (${browserInfo.os})`;

set({ syncStatus: PeerSyncStatus.Connecting });
const { syncService } = await import('@/services/sync');

return syncService.enabledSync({
channel: {
name: sync.channelName,
Expand Down Expand Up @@ -83,7 +84,10 @@ export const createSyncSlice: StateCreator<
if (!userId) return false;

// if user don't enable sync, stop sync
if (!userEnableSync) return syncService.disableSync();
if (!userEnableSync) {
const { syncService } = await import('@/services/sync');
return syncService.disableSync();
}

return get().triggerEnableSync(userId, onEvent);
},
Expand Down

0 comments on commit 5a542b4

Please sign in to comment.