feat: add infra code (#2718)

This commit is contained in:
Himself65
2023-06-08 09:41:20 +08:00
committed by GitHub
parent 4958d096b0
commit f3fd5ff76b
22 changed files with 283 additions and 16 deletions

View File

@@ -4,7 +4,6 @@
"main": "./src/index.ts",
"module": "./src/index.ts",
"devDependencies": {
"@affine/templates": "workspace:*",
"@blocksuite/global": "0.0.0-20230606130340-805f430b-nightly",
"next": "=13.4.2",
"react": "18.3.0-canary-16d053d59-20230506",
@@ -21,7 +20,9 @@
"./blocksuite": "./src/blocksuite/index.ts"
},
"peerDependencies": {
"@blocksuite/global": "0.0.0-20230409084303-221991d4-nightly"
"@affine/templates": "workspace:*",
"@blocksuite/global": "0.0.0-20230409084303-221991d4-nightly",
"@toeverything/infra": "workspace:*"
},
"dependencies": {
"lit": "^2.7.5"

View File

@@ -1,16 +1,45 @@
/// <reference types="@blocksuite/global" />
import { assertEquals } from '@blocksuite/global/utils';
import type {
DBHandlerManager,
DebugHandlerManager,
DialogHandlerManager,
ExportHandlerManager,
HandlerManager,
PrimitiveHandlers,
UIHandlerManager,
UpdaterHandlerManager,
WorkspaceHandlerManager,
} from '@toeverything/infra';
import getConfig from 'next/config';
import { z } from 'zod';
import { UaHelper } from './ua-helper';
type UnwrapManagerHandler<
Manager extends HandlerManager<string, Record<string, PrimitiveHandlers>>
> = {
[K in keyof Manager['handlers']]: Manager['handlers'][K] extends (
...args: infer Args
) => Promise<infer R>
? (...args: Args) => Promise<R>
: never;
};
declare global {
interface Window {
appInfo: {
electron: boolean;
};
apis: any;
apis: {
db: UnwrapManagerHandler<DBHandlerManager>;
debug: UnwrapManagerHandler<DebugHandlerManager>;
dialog: UnwrapManagerHandler<DialogHandlerManager>;
export: UnwrapManagerHandler<ExportHandlerManager>;
ui: UnwrapManagerHandler<UIHandlerManager>;
updater: UnwrapManagerHandler<UpdaterHandlerManager>;
workspace: UnwrapManagerHandler<WorkspaceHandlerManager>;
};
events: any;
}
}

View File

@@ -5,5 +5,10 @@
"composite": true,
"noEmit": false,
"outDir": "lib"
}
},
"references": [
{
"path": "../infra"
}
]
}

View File

@@ -0,0 +1,31 @@
{
"name": "@toeverything/infra",
"main": "./src/index.ts",
"module": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"publishConfig": {
"module": "./dist/index.mjs",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"files": [
"dist"
]
},
"scripts": {
"build": "vite build",
"dev": "vite build --watch"
},
"devDependencies": {
"vite": "^4.3.9",
"vite-plugin-dts": "^2.3.0"
}
}

View File

@@ -0,0 +1,105 @@
export interface WorkspaceMeta {
id: string;
mainDBPath: string;
secondaryDBPath?: string; // assume there will be only one
}
export type PrimitiveHandlers = (...args: any[]) => Promise<any>;
type TODO = any;
export abstract class HandlerManager<
Namespace extends string,
Handlers extends Record<string, PrimitiveHandlers>
> {
abstract readonly app: TODO;
abstract readonly namespace: Namespace;
abstract readonly handlers: Handlers;
}
type DBHandlers = {
getDocAsUpdates: (id: string) => Promise<Uint8Array>;
applyDocUpdate: (id: string, update: Uint8Array) => Promise<void>;
addBlob: (
workspaceId: string,
key: string,
data: Uint8Array
) => Promise<void>;
getBlob: (workspaceId: string, key: string) => Promise<any>;
deleteBlob: (workspaceId: string, key: string) => Promise<void>;
getBlobKeys: (workspaceId: string) => Promise<any>;
getDefaultStorageLocation: () => Promise<string>;
};
export abstract class DBHandlerManager extends HandlerManager<
'db',
DBHandlers
> {}
type DebugHandlers = {
revealLogFile: () => Promise<string>;
logFilePath: () => Promise<string>;
};
export abstract class DebugHandlerManager extends HandlerManager<
'debug',
DebugHandlers
> {}
type DialogHandlers = {
revealDBFile: (workspaceId: string) => Promise<any>;
loadDBFile: () => Promise<any>;
saveDBFileAs: (workspaceId: string) => Promise<any>;
moveDBFile: (workspaceId: string, dbFileLocation?: string) => Promise<any>;
selectDBFileLocation: () => Promise<any>;
setFakeDialogResult: (result: any) => Promise<any>;
};
export abstract class DialogHandlerManager extends HandlerManager<
'dialog',
DialogHandlers
> {}
type UIHandlers = {
handleThemeChange: (theme: 'system' | 'light' | 'dark') => Promise<any>;
handleSidebarVisibilityChange: (visible: boolean) => Promise<any>;
handleMinimizeApp: () => Promise<any>;
handleMaximizeApp: () => Promise<any>;
handleCloseApp: () => Promise<any>;
getGoogleOauthCode: () => Promise<any>;
};
export abstract class UIHandlerManager extends HandlerManager<
'ui',
UIHandlers
> {}
type ExportHandlers = {
savePDFFileAs: (title: string) => Promise<any>;
};
export abstract class ExportHandlerManager extends HandlerManager<
'export',
ExportHandlers
> {}
type UpdaterHandlers = {
currentVersion: () => Promise<any>;
quitAndInstall: () => Promise<any>;
checkForUpdatesAndNotify: () => Promise<any>;
};
export abstract class UpdaterHandlerManager extends HandlerManager<
'updater',
UpdaterHandlers
> {}
type WorkspaceHandlers = {
list: () => Promise<[workspaceId: string, meta: WorkspaceMeta][]>;
delete: (id: string) => Promise<void>;
getMeta: (id: string) => Promise<WorkspaceMeta>;
};
export abstract class WorkspaceHandlerManager extends HandlerManager<
'workspace',
WorkspaceHandlers
> {}

View File

@@ -0,0 +1 @@
export * from './handler';

View File

@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"include": ["./src"],
"compilerOptions": {
"composite": true,
"noEmit": false,
"outDir": "lib"
},
"references": [
{
"path": "./tsconfig.node.json"
}
]
}

View File

@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"outDir": "lib",
"noEmit": false
},
"include": ["vite.config.ts"]
}

View File

@@ -0,0 +1,24 @@
import { resolve } from 'node:path';
import { fileURLToPath } from 'url';
import dts from 'vite-plugin-dts';
import { defineConfig } from 'vitest/config';
const root = fileURLToPath(new URL('.', import.meta.url));
export default defineConfig({
build: {
lib: {
entry: {
index: resolve(root, 'src/index.ts'),
},
formats: ['es', 'cjs'],
name: 'AffineInfra',
},
},
plugins: [
dts({
insertTypesEntry: true,
}),
],
});

View File

@@ -102,11 +102,9 @@ export const CRUD: WorkspaceCRUD<WorkspaceFlavour.LOCAL> = {
// workspaces in desktop
if (window.apis && environment.isDesktop) {
// @ts-expect-error
const desktopIds = (await window.apis.workspace.list()).map(([id]) => id);
// the ids maybe a subset of the local storage
const moreWorkspaces = desktopIds.filter(
// @ts-expect-error
id => !allWorkspaceIDs.includes(id)
);
allWorkspaceIDs = [...allWorkspaceIDs, ...moreWorkspaces];