Skip to content

Commit

Permalink
🦄 refactor: add IApplication
Browse files Browse the repository at this point in the history
Add the IApplication interface and refactor the Application
  • Loading branch information
xiangechen committed Aug 17, 2023
1 parent 6b320bb commit e61774f
Show file tree
Hide file tree
Showing 78 changed files with 313 additions and 559 deletions.
58 changes: 7 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
"lint-staged": "^13.2.2",
"nanoid": "^4.0.2",
"prettier": "^2.8.8",
"reflect-metadata": "^0.1.13",
"simple-git-hooks": "^2.8.1",
"ts-jest": "^29.1.0",
"tsyringe": "^4.7.0",
"typescript": "^5.1.3",
"typescript-plugin-css-modules": "^5.0.1"
}
Expand Down
18 changes: 18 additions & 0 deletions packages/chili-core/src/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

import { IShapeFactory } from "chili-geo";
import { IVisualFactory } from "chili-vis";
import { IStorage, Serialized } from "./base";
import { IDocument } from "./document";
import { IService } from "./service";

export interface IApplication {
readonly visualFactory: IVisualFactory;
readonly shapeFactory: IShapeFactory;
readonly services: IService[];
readonly storage: IStorage;
activeDocument: IDocument | undefined;
newDocument(name: string): Promise<IDocument>;
openDocument(id: string): Promise<IDocument | undefined>;
loadDocument(data: Serialized): Promise<IDocument>;
}
1 change: 0 additions & 1 deletion packages/chili-core/src/base/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface PubSubEventMap {
executeCommand: (commandName: keyof Commands) => void;
nodeLinkedListChanged: (records: NodeRecord[]) => void;
activeDocumentChanged: (document: IDocument | undefined) => void;
openDocument: (id: string) => void;
documentClosed: (document: IDocument) => void;
modelUpdate: (model: IModel) => void;
visibleChanged: (model: IModel) => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/chili-core/src/command/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

import { Application } from "chili/src/application";
import { IApplication } from "../application";

export interface ICommand {
execute(application: Application): Promise<void>;
execute(application: IApplication): Promise<void>;
}
10 changes: 0 additions & 10 deletions packages/chili-core/src/command/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,4 @@ export class Commands {
Mirror = "Mirror";
Rotate = "Rotate";
Array = "Array";

private constructor() {}

private static _instance: Commands | undefined;
static get instance() {
if (this._instance === undefined) {
this._instance = new Commands();
}
return this._instance;
}
}
14 changes: 9 additions & 5 deletions packages/chili-core/src/decorators/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { I18n } from "../i18n";

const CommandMap = new Map<string, new (...args: any[]) => ICommand>();

export type CommandConstructor = new (...args: any[]) => ICommand;

export interface CommandData {
name: keyof Commands;
display: keyof I18n;
Expand All @@ -13,17 +15,15 @@ export interface CommandData {
helpUrl?: string;
}

export function command<T extends new (...args: any[]) => ICommand>(commandData: CommandData) {
export function command<T extends CommandConstructor>(commandData: CommandData) {
return (ctor: T) => {
CommandMap.set(commandData.name, ctor);
ctor.prototype.data = commandData;
};
}

export namespace CommandData {
export function get(
command: string | ICommand | (new (...args: any[]) => ICommand)
): CommandData | undefined {
export namespace Command {
export function getData(command: string | ICommand | CommandConstructor): CommandData | undefined {
if (typeof command === "string") {
let c = CommandMap.get(command);
return c?.prototype.data;
Expand All @@ -33,4 +33,8 @@ export namespace CommandData {
return Object.getPrototypeOf(command).data;
}
}

export function get(name: keyof Commands): CommandConstructor | undefined {
return CommandMap.get(name);
}
}
2 changes: 2 additions & 0 deletions packages/chili-core/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

import { IApplication } from "./application";
import { History, IDisposable, IPropertyChanged } from "./base";
import { INode, INodeLinkedList } from "./model/node";
import { SelectionManager } from "./selectionManager";
Expand All @@ -13,6 +14,7 @@ export interface IDocument extends IPropertyChanged, IDisposable {
readonly history: History;
readonly visual: IVisual;
readonly rootNode: INodeLinkedList;
readonly application: IApplication;
addNode(...nodes: INode[]): void;
save(): Promise<void>;
close(): Promise<void>;
Expand Down
3 changes: 1 addition & 2 deletions packages/chili-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

export * from "./application";
export * from "./base";
export * from "./command";
export * from "./config";
Expand All @@ -12,10 +13,8 @@ export * from "./editor";
export * from "./geometry";
export * from "./i18n";
export * from "./id";
export * from "./ioc";
export * from "./math";
export * from "./model";
export * from "./module";
export * from "./selectionManager";
export * from "./service";
export * from "./snapType";
Expand Down
44 changes: 0 additions & 44 deletions packages/chili-core/src/ioc/container.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/chili-core/src/ioc/index.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/chili-core/src/ioc/inject.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/chili-core/src/ioc/register.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/chili-core/src/ioc/resolve.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/chili-core/src/module.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/chili-core/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

import { Application } from "chili/src/application";
import { IApplication } from "./application";

export interface IService {
register(app: Application): void;
register(app: IApplication): void;
start(): void;
stop(): void;
}
3 changes: 1 addition & 2 deletions packages/chili-core/test/collection.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.
import "reflect-metadata";
import { CollectionAction, CollectionChangedArgs, ObservableCollection } from "../src";

describe("ObservableCollection test", () => {
Expand Down Expand Up @@ -44,7 +43,7 @@ describe("ObservableCollection test", () => {
expect(collection.items).toStrictEqual([1, 3, 2, 3]);
expect(arg.items).toStrictEqual([3, 2]);
expect(arg.items.length).toBe(2);
expect(arg.item).toBe(1);
expect(arg.item).toBe(2);
}
});
collection.replace(1, 3, 2);
Expand Down
2 changes: 0 additions & 2 deletions packages/chili-core/test/converter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

import "reflect-metadata";

import { XYZ } from "../src";
import { IConverter, NumberConverter, StringConverter, XYZConverter } from "../src/converter";

Expand Down
4 changes: 1 addition & 3 deletions packages/chili-core/test/history.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. MPL-2.0 license.

import "reflect-metadata";

import { ArrayRecord, PropertyHistoryRecord, History } from "chili-core";
import { ArrayRecord, History, PropertyHistoryRecord } from "chili-core";

describe("test history", () => {
class TestClass {
Expand Down
Loading

0 comments on commit e61774f

Please sign in to comment.