Skip to content

Commit

Permalink
refactor: diService、eventManager
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed Jul 30, 2024
1 parent 9d0f178 commit 006b9b6
Show file tree
Hide file tree
Showing 69 changed files with 2,942 additions and 942 deletions.
8 changes: 4 additions & 4 deletions packages/engine-core/src/command/commandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Iterable,
} from '@alilc/lowcode-shared';
import { ICommand, ICommandHandler } from './command';
import { Extensions, Registry } from '../common/registry';
import { Extensions, Registry } from '../extension/registry';
import { ICommandService } from './commandService';

export type ICommandsMap = Map<string, ICommand>;
Expand All @@ -26,7 +26,7 @@ export interface ICommandRegistry {
getCommands(): ICommandsMap;
}

class CommandsRegistry implements ICommandRegistry {
class CommandsRegistryImpl implements ICommandRegistry {
private readonly _commands = new Map<string, LinkedList<ICommand>>();

private readonly _didRegisterCommandEmitter = new Emitter<string>();
Expand Down Expand Up @@ -111,6 +111,6 @@ class CommandsRegistry implements ICommandRegistry {
}
}

const commandsRegistry = new CommandsRegistry();
export const CommandsRegistry = new CommandsRegistryImpl();

Registry.add(Extensions.Command, commandsRegistry);
Registry.add(Extensions.Command, CommandsRegistry);
8 changes: 3 additions & 5 deletions packages/engine-core/src/command/commandService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { createDecorator, Provide, IInstantiationService } from '@alilc/lowcode-shared';
import { Registry, Extensions } from '../common/registry';
import { ICommandRegistry } from './commandRegistry';
import { createDecorator, IInstantiationService } from '@alilc/lowcode-shared';
import { CommandsRegistry } from './commandRegistry';

export interface ICommandService {
executeCommand<T = any>(commandId: string, ...args: any[]): Promise<T | undefined>;
}

export const ICommandService = createDecorator<ICommandService>('commandService');

@Provide(ICommandService)
export class CommandService implements ICommandService {
constructor(@IInstantiationService private instantiationService: IInstantiationService) {}

Expand All @@ -17,7 +15,7 @@ export class CommandService implements ICommandService {
}

private tryExecuteCommand(id: string, args: any[]): Promise<any> {
const command = Registry.as<ICommandRegistry>(Extensions.Command).getCommand(id);
const command = CommandsRegistry.getCommand(id);
if (!command) {
return Promise.reject(new Error(`command '${id}' not found`));
}
Expand Down
14 changes: 8 additions & 6 deletions packages/engine-core/src/configuration/configurationRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
types,
} from '@alilc/lowcode-shared';
import { isUndefined, isObject } from 'lodash-es';
import { Extensions, Registry } from '../common/registry';
import { Extensions, Registry } from '../extension/registry';
import { OVERRIDE_PROPERTY_REGEX, overrideIdentifiersFromKey } from './configuration';

export interface IConfigurationRegistry {
Expand Down Expand Up @@ -133,18 +133,18 @@ export const allSettings: {
patternProperties: StringDictionary<IConfigurationPropertySchema>;
} = { properties: {}, patternProperties: {} };

export class ConfigurationRegistry implements IConfigurationRegistry {
export class ConfigurationRegistryImpl implements IConfigurationRegistry {
private registeredConfigurationDefaults: IConfigurationDefaults[] = [];
private configurationDefaultsOverrides: Map<
private readonly configurationDefaultsOverrides: Map<
string,
{
configurationDefaultOverrides: IConfigurationDefaultOverride[];
configurationDefaultOverrideValue?: IConfigurationDefaultOverrideValue;
}
>;

private configurationProperties: StringDictionary<IRegisteredConfigurationPropertySchema>;
private excludedConfigurationProperties: StringDictionary<IRegisteredConfigurationPropertySchema>;
private readonly configurationProperties: StringDictionary<IRegisteredConfigurationPropertySchema>;
private readonly excludedConfigurationProperties: StringDictionary<IRegisteredConfigurationPropertySchema>;
private overrideIdentifiers = new Set<string>();

private propertiesChangeEmitter = new Emitter<{
Expand Down Expand Up @@ -645,4 +645,6 @@ function isSameExtension(a?: IExtensionInfo, b?: IExtensionInfo): boolean {
return a.id === b.id && a.version === b.version;
}

Registry.add(Extensions.Configuration, new ConfigurationRegistry());
export const ConfigurationRegistry = new ConfigurationRegistryImpl();

Registry.add(Extensions.Configuration, ConfigurationRegistry);
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
createDecorator,
Emitter,
Provide,
type Event,
type EventListener,
} from '@alilc/lowcode-shared';
import { createDecorator, Emitter, type Event, type EventListener } from '@alilc/lowcode-shared';
import {
Configuration,
DefaultConfiguration,
Expand Down Expand Up @@ -69,7 +63,6 @@ export interface IConfigurationService {

export const IConfigurationService = createDecorator<IConfigurationService>('configurationService');

@Provide(IConfigurationService)
export class ConfigurationService implements IConfigurationService {
private configuration: Configuration;
private readonly defaultConfiguration: DefaultConfiguration;
Expand Down
19 changes: 6 additions & 13 deletions packages/engine-core/src/configuration/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
type IOverrides,
} from './configurationModel';
import {
ConfigurationRegistry,
type IConfigurationPropertySchema,
type IConfigurationRegistry,
type IRegisteredConfigurationPropertySchema,
} from './configurationRegistry';
import { Registry, Extensions } from '../common/registry';
import { isEqual, isNil, isPlainObject, get as lodasgGet } from 'lodash-es';
import {
IInspectValue,
Expand All @@ -37,8 +37,8 @@ export class DefaultConfiguration {

initialize(): ConfigurationModel {
this.resetConfigurationModel();
Registry.as<IConfigurationRegistry>(Extensions.Configuration).onDidUpdateConfiguration(
({ properties }) => this.onDidUpdateConfiguration([...properties]),
ConfigurationRegistry.onDidUpdateConfiguration(({ properties }) =>
this.onDidUpdateConfiguration([...properties]),
);

return this.configurationModel;
Expand All @@ -56,19 +56,14 @@ export class DefaultConfiguration {
}

private onDidUpdateConfiguration(properties: string[]): void {
this.updateConfigurationModel(
properties,
Registry.as<IConfigurationRegistry>(Extensions.Configuration).getConfigurationProperties(),
);
this.updateConfigurationModel(properties, ConfigurationRegistry.getConfigurationProperties());
this.emitter.emit({ defaults: this.configurationModel, properties });
}

private resetConfigurationModel(): void {
this._configurationModel = ConfigurationModel.createEmptyModel();

const properties = Registry.as<IConfigurationRegistry>(
Extensions.Configuration,
).getConfigurationProperties();
const properties = ConfigurationRegistry.getConfigurationProperties();

this.updateConfigurationModel(Object.keys(properties), properties);
}
Expand Down Expand Up @@ -156,9 +151,7 @@ class ConfigurationModelParser {
raw: any,
options?: ConfigurationParseOptions,
): IConfigurationModel & { hasExcludedProperties?: boolean } {
const configurationProperties = Registry.as<IConfigurationRegistry>(
Extensions.Configuration,
).getConfigurationProperties();
const configurationProperties = ConfigurationRegistry.getConfigurationProperties();
const filtered = this.filter(raw, configurationProperties, true, options);

raw = filtered.raw;
Expand Down
6 changes: 2 additions & 4 deletions packages/engine-core/src/extension/extensionHost.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type IConfigurationRegistry, type IConfigurationNode } from '../configuration';
import { Registry, Extensions } from '../common/registry';
import { ConfigurationRegistry, type IConfigurationNode } from '../configuration';
import { type ExtensionInitializer, type IExtensionInstance } from './extension';
import { invariant } from '@alilc/lowcode-shared';

Expand All @@ -19,9 +18,8 @@ export class ExtensionHost {
initializer: ExtensionInitializer,
preferenceConfigurations: IConfigurationNode[],
) {
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
this.configurationProperties =
configurationRegistry.registerConfigurations(preferenceConfigurations);
ConfigurationRegistry.registerConfigurations(preferenceConfigurations);

this.instance = initializer({});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-core/src/extension/extensionManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ExtensionManagement {
): Promise<void> {
if (!this.validateExtension(extension, override)) return;

const metadata = extension.meta ?? {};
const metadata = extension.metadata ?? {};
const host = new ExtensionHost(
extension.name,
extension,
Expand Down
3 changes: 1 addition & 2 deletions packages/engine-core/src/extension/extensionService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDecorator, Provide } from '@alilc/lowcode-shared';
import { createDecorator } from '@alilc/lowcode-shared';
import { ExtensionManagement, type IExtensionRegisterOptions } from './extensionManagement';
import { type IFunctionExtension } from './extension';
import { ExtensionHost } from './extensionHost';
Expand All @@ -15,7 +15,6 @@ export interface IExtensionService {

export const IExtensionService = createDecorator<IExtensionService>('extensionService');

@Provide(IExtensionService)
export class ExtensionService implements IExtensionService {
private extensionManagement = new ExtensionManagement();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ export const Registry: IRegistry = new RegistryImpl();
export const Extensions = {
Configuration: 'base.contributions.configuration',
Command: 'base.contributions.command',
Keybinding: 'base.contributions.keybinding',
Widget: 'base.contributions.widget',
};
6 changes: 5 additions & 1 deletion packages/engine-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ export * from './resource';
export * from './command';

// test
export * from './common/registry';
export * from './extension/registry';
export * from './main';
export * from './keybinding/keybindingRegistry';
export * from './keybinding/keybindingParser';
export * from './keybinding/keybindingResolver';
export * from './keybinding/keybindings';
39 changes: 0 additions & 39 deletions packages/engine-core/src/keybinding/keybinding.ts
Original file line number Diff line number Diff line change
@@ -1,39 +0,0 @@
/**
* A keybinding is a sequence of chords.
*/
export class Keybinding {
public readonly chords: Chord[];

constructor(chords: Chord[]) {
if (chords.length === 0) {
throw illegalArgument(`chords`);
}
this.chords = chords;
}

public getHashCode(): string {
let result = '';
for (let i = 0, len = this.chords.length; i < len; i++) {
if (i !== 0) {
result += ';';
}
result += this.chords[i].getHashCode();
}
return result;
}

public equals(other: Keybinding | null): boolean {
if (other === null) {
return false;
}
if (this.chords.length !== other.chords.length) {
return false;
}
for (let i = 0; i < this.chords.length; i++) {
if (!this.chords[i].equals(other.chords[i])) {
return false;
}
}
return true;
}
}
99 changes: 99 additions & 0 deletions packages/engine-core/src/keybinding/keybindingParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { KeyCodeUtils, ScanCodeUtils } from '@alilc/lowcode-shared';
import { KeyCodeChord, ScanCodeChord, Keybinding, Chord } from './keybindings';

export class KeybindingParser {
private static _readModifiers(input: string) {
input = input.toLowerCase().trim();

let ctrl = false;
let shift = false;
let alt = false;
let meta = false;

let matchedModifier: boolean;

do {
matchedModifier = false;
if (/^ctrl(\+|-)/.test(input)) {
ctrl = true;
input = input.slice('ctrl-'.length);
matchedModifier = true;
}
if (/^shift(\+|-)/.test(input)) {
shift = true;
input = input.slice('shift-'.length);
matchedModifier = true;
}
if (/^alt(\+|-)/.test(input)) {
alt = true;
input = input.slice('alt-'.length);
matchedModifier = true;
}
if (/^meta(\+|-)/.test(input)) {
meta = true;
input = input.slice('meta-'.length);
matchedModifier = true;
}
if (/^win(\+|-)/.test(input)) {
meta = true;
input = input.slice('win-'.length);
matchedModifier = true;
}
if (/^cmd(\+|-)/.test(input)) {
meta = true;
input = input.slice('cmd-'.length);
matchedModifier = true;
}
} while (matchedModifier);

let key: string;

const firstSpaceIdx = input.indexOf(' ');
if (firstSpaceIdx > 0) {
key = input.substring(0, firstSpaceIdx);
input = input.substring(firstSpaceIdx);
} else {
key = input;
input = '';
}

return {
remains: input,
ctrl,
shift,
alt,
meta,
key,
};
}

private static parseChord(input: string): [Chord, string] {
const mods = this._readModifiers(input);
const scanCodeMatch = mods.key.match(/^\[([^\]]+)\]$/);
if (scanCodeMatch) {
const strScanCode = scanCodeMatch[1];
const scanCode = ScanCodeUtils.lowerCaseToEnum(strScanCode);
return [
new ScanCodeChord(mods.ctrl, mods.shift, mods.alt, mods.meta, scanCode),
mods.remains,
];
}
const keyCode = KeyCodeUtils.fromUserSettings(mods.key);
return [new KeyCodeChord(mods.ctrl, mods.shift, mods.alt, mods.meta, keyCode), mods.remains];
}

static parseKeybinding(input: string): Keybinding | null {
if (!input) {
return null;
}

const chords: Chord[] = [];
let chord: Chord;

while (input.length > 0) {
[chord, input] = this.parseChord(input);
chords.push(chord);
}
return chords.length > 0 ? new Keybinding(chords) : null;
}
}
Loading

0 comments on commit 006b9b6

Please sign in to comment.