refactor(core): use debug log replace console when command registry (#5237)

This commit is contained in:
Joooye_34
2023-12-09 07:42:04 +00:00
parent a066dfb108
commit c1bb467013
4 changed files with 12 additions and 4 deletions

View File

@@ -54,6 +54,7 @@
"dev": "vite build --watch" "dev": "vite build --watch"
}, },
"dependencies": { "dependencies": {
"@affine/debug": "workspace:*",
"@affine/env": "workspace:*", "@affine/env": "workspace:*",
"@affine/sdk": "workspace:*", "@affine/sdk": "workspace:*",
"@blocksuite/blocks": "0.11.0-nightly-202312070955-2b5bb47", "@blocksuite/blocks": "0.11.0-nightly-202312070955-2b5bb47",

View File

@@ -1,3 +1,4 @@
import { DebugLogger } from '@affine/debug';
// @ts-expect-error upstream type is wrong // @ts-expect-error upstream type is wrong
import { tinykeys } from 'tinykeys'; import { tinykeys } from 'tinykeys';
@@ -7,12 +8,14 @@ import {
createAffineCommand, createAffineCommand,
} from './command'; } from './command';
const commandLogger = new DebugLogger('command:registry');
export const AffineCommandRegistry = new (class { export const AffineCommandRegistry = new (class {
readonly commands: Map<string, AffineCommand> = new Map(); readonly commands: Map<string, AffineCommand> = new Map();
register(options: AffineCommandOptions) { register(options: AffineCommandOptions) {
if (this.commands.has(options.id)) { if (this.commands.has(options.id)) {
console.warn(`Command ${options.id} already registered.`); commandLogger.warn(`Command ${options.id} already registered.`);
return () => {}; return () => {};
} }
const command = createAffineCommand(options); const command = createAffineCommand(options);
@@ -38,17 +41,17 @@ export const AffineCommandRegistry = new (class {
}); });
} }
console.debug(`Registered command ${command.id}`); commandLogger.debug(`Registered command ${command.id}`);
return () => { return () => {
unsubKb?.(); unsubKb?.();
this.commands.delete(command.id); this.commands.delete(command.id);
console.debug(`Unregistered command ${command.id}`); commandLogger.debug(`Unregistered command ${command.id}`);
}; };
} }
get(id: string): AffineCommand | undefined { get(id: string): AffineCommand | undefined {
if (!this.commands.has(id)) { if (!this.commands.has(id)) {
console.warn(`Command ${id} not registered.`); commandLogger.warn(`Command ${id} not registered.`);
return undefined; return undefined;
} }
return this.commands.get(id); return this.commands.get(id);

View File

@@ -14,6 +14,9 @@
{ {
"path": "../env" "path": "../env"
}, },
{
"path": "../debug"
},
{ {
"path": "./tsconfig.node.json" "path": "./tsconfig.node.json"
} }

View File

@@ -13795,6 +13795,7 @@ __metadata:
resolution: "@toeverything/infra@workspace:packages/common/infra" resolution: "@toeverything/infra@workspace:packages/common/infra"
dependencies: dependencies:
"@affine-test/fixtures": "workspace:*" "@affine-test/fixtures": "workspace:*"
"@affine/debug": "workspace:*"
"@affine/env": "workspace:*" "@affine/env": "workspace:*"
"@affine/sdk": "workspace:*" "@affine/sdk": "workspace:*"
"@affine/templates": "workspace:*" "@affine/templates": "workspace:*"