Skip to content

Commit f3d9aff

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

File tree

18 files changed

+66
-45
lines changed

18 files changed

+66
-45
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
@@ -5,6 +5,7 @@ import {
55
type ILibrary,
66
type INetworkLibrary,
77
type IRunOptions,
8+
NfNotInitializedException,
89
} from "@nanoforge/common";
910

1011
import { Core } from "../core/core";
@@ -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>) {
@@ -48,6 +49,7 @@ export abstract class NanoforgeApplication {
4849
}
4950

5051
public run() {
51-
return this._core.run();
52+
if (!this._core) throw new NfNotInitializedException("Core");
53+
return this._core?.run();
5254
}
5355
}

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();
@@ -65,6 +68,8 @@ export class Core {
6568
}
6669

6770
private getInitContext(options: IRunOptions): InitContext {
71+
if (!this._configRegistry) throw new NfNotInitializedException("Core");
72+
6873
return new InitContext(this.context, this.config.libraryManager, this._configRegistry, options);
6974
}
7075

packages/ecs/lib/libecs.d.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.
2-
declare namespace RuntimeExports {
3-
let HEAPF32: any;
4-
let HEAPF64: any;
5-
let HEAP_DATA_VIEW: any;
6-
let HEAP8: any;
7-
let HEAPU8: any;
8-
let HEAP16: any;
9-
let HEAPU16: any;
10-
let HEAP32: any;
11-
let HEAPU32: any;
12-
let HEAP64: any;
13-
let HEAPU64: any;
14-
}
152
interface WasmModule {
163
}
174

@@ -20,6 +7,8 @@ export interface ClassHandle {
207
delete(): void;
218
deleteLater(): this;
229
isDeleted(): boolean;
10+
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
11+
[Symbol.dispose](): void;
2312
clone(): this;
2413
}
2514
export interface container extends ClassHandle {
@@ -85,5 +74,5 @@ interface EmbindModule {
8574
};
8675
}
8776

88-
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;
77+
export type MainModule = WasmModule & EmbindModule;
8978
export default function MainModuleFactory (options?: unknown): Promise<MainModule>;

0 commit comments

Comments
 (0)