Skip to content

Commit aa870bc

Browse files
committed
refactor(libraries): implement initialization validation and standardize nullable fields
1 parent cd1972a commit aa870bc

File tree

21 files changed

+62
-71
lines changed

21 files changed

+62
-71
lines changed

packages/asset-manager/src/asset-manager.library.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BaseAssetManagerLibrary, type InitContext, NfFile, NfNotFound } from "@nanoforge/common";
22

33
export class AssetManagerLibrary extends BaseAssetManagerLibrary {
4-
private _assets: Map<string, string>;
4+
private _assets?: Map<string, string>;
55

66
get __name(): string {
77
return "AssetManagerLibrary";
@@ -12,6 +12,7 @@ export class AssetManagerLibrary extends BaseAssetManagerLibrary {
1212
}
1313

1414
public getAsset(path: string): NfFile {
15+
if (!this._assets) this.throwNotInitializedError();
1516
const res = this._assets.get(this._parsePath(path));
1617
if (!res) throw new NfNotFound(path, "Asset");
1718
return new NfFile(res);

packages/common/src/context/contexts/library.context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export enum LibraryStatusEnum {
55
}
66

77
export class LibraryContext {
8-
protected _status: LibraryStatusEnum;
8+
protected _status: LibraryStatusEnum = LibraryStatusEnum.UNLOADED;
99

1010
get status() {
1111
return this._status;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NfException } from "../abstracts/exception.abstract";
2+
3+
export class NfNotInitializedException extends NfException {
4+
get code(): number {
5+
return 404;
6+
}
7+
8+
constructor(item: string, type?: string) {
9+
super(`${type ? `${type} - ` : ""}${item} not initialized.`);
10+
}
11+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { NfNotFound } from "./exceptions/not-found.exception";
21
export { NfFetchException } from "./exceptions/fetch.exception";
2+
export { NfNotFound } from "./exceptions/not-found.exception";
3+
export { NfNotInitializedException } from "./exceptions/not-initialized.exception";

packages/common/src/library/libraries/library.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type ClearContext, type InitContext } from "../../context";
2+
import { NfNotInitializedException } from "../../exception";
23
import { RelationshipHandler } from "../relationship/relationship-handler";
34
import { DEFAULT_LIBRARY_OPTIONS } from "./consts/library-options-default.const";
45
import { type ILibrary, type ILibraryOptions } from "./library.type";
@@ -30,4 +31,8 @@ export abstract class Library implements ILibrary {
3031

3132
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3233
public async __clear(_context: ClearContext): Promise<void> {}
34+
35+
protected throwNotInitializedError(): never {
36+
throw new NfNotInitializedException(this.__name, "Library");
37+
}
3338
}

packages/common/src/library/manager/managers/base-library.manager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { LibraryHandle } from "../handle/library.handle";
44

55
export class BaseLibraryManager {
66
protected _libraries: LibraryHandle[] = [];
7-
private _librariesIndex: {
8-
[sym: symbol]: number;
9-
};
7+
private _librariesIndex: Record<symbol, number> = {};
108

119
/**
1210
* @todo Add error management

packages/core/src/application/nanoforge-application.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type ILibrary,
55
type INetworkLibrary,
66
type IRunOptions,
7+
NfNotInitializedException,
78
} from "@nanoforge/common";
89

910
import { EditableApplicationContext } from "../common/context/contexts/application.editable-context";
@@ -13,7 +14,7 @@ import type { IApplicationOptions } from "./application-options.type";
1314

1415
export abstract class NanoforgeApplication {
1516
protected applicationConfig: ApplicationConfig;
16-
private _core: Core;
17+
private _core?: Core;
1718
private readonly _options: IApplicationOptions;
1819

1920
constructor(options?: Partial<IApplicationOptions>) {
@@ -51,6 +52,7 @@ export abstract class NanoforgeApplication {
5152
}
5253

5354
public run() {
54-
return this._core.run();
55+
if (!this._core) throw new NfNotInitializedException("Core");
56+
return this._core?.run();
5557
}
5658
}

packages/core/src/common/library/manager/library.manager.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import {
2424
import { EditableLibraryContext } from "../../context/contexts/library.editable-context";
2525
import { Relationship } from "../relationship-functions";
2626

27+
const hasMethod = (obj: any, method: string) => {
28+
return typeof obj[method] === "function";
29+
};
30+
2731
export class EditableLibraryManager extends LibraryManager {
2832
public set(sym: symbol, library: ILibrary) {
2933
this.setNewLibrary(sym, library, new EditableLibraryContext());
@@ -90,13 +94,13 @@ export class EditableLibraryManager extends LibraryManager {
9094

9195
public getMutableLibraries(): LibraryHandle<IMutableLibrary>[] {
9296
return this._libraries.filter(
93-
(handle) => handle && typeof handle.library["mute"] === "function",
97+
(handle) => handle && hasMethod(handle.library, "mute"),
9498
) as LibraryHandle<IMutableLibrary>[];
9599
}
96100

97101
private _getRunnerLibraries(): LibraryHandle<IRunnerLibrary>[] {
98102
return this._libraries.filter(
99-
(handle) => handle && typeof handle.library["__run"] === "function",
103+
(handle) => handle && hasMethod(handle.library, "__run"),
100104
) as LibraryHandle<IRunnerLibrary>[];
101105
}
102106
}

packages/core/src/core/core.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
InitContext,
99
type LibraryHandle,
1010
LibraryStatusEnum,
11+
NfNotInitializedException,
1112
} from "@nanoforge/common";
1213

1314
import { type ApplicationConfig } from "../application/application-config";
@@ -19,8 +20,8 @@ import { ConfigRegistry } from "../config/config-registry";
1920
export class Core {
2021
private readonly config: ApplicationConfig;
2122
private readonly context: ApplicationContext;
22-
private options: IApplicationOptions;
23-
private _configRegistry: ConfigRegistry;
23+
private options?: IApplicationOptions;
24+
private _configRegistry?: ConfigRegistry;
2425

2526
constructor(config: ApplicationConfig, context: ApplicationContext) {
2627
this.config = config;
@@ -34,6 +35,8 @@ export class Core {
3435
}
3536

3637
public async run(): Promise<void> {
38+
if (!this.options) throw new NfNotInitializedException("Core");
39+
3740
const context = this.getExecutionContext();
3841
const clientContext = this.getClientContext();
3942
const libraries = this.config.libraryManager.getExecutionLibraries();
@@ -58,6 +61,8 @@ export class Core {
5861
}
5962

6063
private getInitContext(options: IRunOptions): InitContext {
64+
if (!this._configRegistry) throw new NfNotInitializedException("Core");
65+
6166
return new InitContext(this.context, this.config.libraryManager, this._configRegistry, options);
6267
}
6368

packages/ecs/.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ package-lock.json
44
yarn.lock
55
bun.lock
66

7-
*.js
87
*.d.ts
98
build

0 commit comments

Comments
 (0)