Skip to content

Commit

Permalink
refactor(ecs): update INotify impls
Browse files Browse the repository at this point in the history
- update ECS & AComponent classes
  • Loading branch information
postspectacular committed Jul 25, 2023
1 parent 1ad68f6 commit a870653
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
8 changes: 8 additions & 0 deletions packages/ecs/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
ArrayLikeIterable,
EVENT_ALL,
Fn0,
IClear,
IID,
Expand Down Expand Up @@ -114,3 +115,10 @@ export interface ECSOpts {
export const EVENT_ADDED = "added";
export const EVENT_PRE_DELETE = "pre-delete";
export const EVENT_CHANGED = "changed";

export type ECSEventType =
| typeof EVENT_ADDED
| typeof EVENT_PRE_DELETE
| typeof EVENT_ALL;

export type ComponentEventType = ECSEventType | typeof EVENT_CHANGED;
10 changes: 6 additions & 4 deletions packages/ecs/src/components/acomponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
EVENT_CHANGED,
EVENT_PRE_DELETE,
type ComponentDefaultValue,
type ComponentEventType,
type IComponent,
} from "../api.js";

@INotifyMixin
export abstract class AComponent<K extends string, VALUES, GET, SET>
implements IComponent<K, VALUES, GET, SET>, INotify
implements IComponent<K, VALUES, GET, SET>, INotify<ComponentEventType>
{
readonly id: K;
abstract readonly size: number;
Expand Down Expand Up @@ -120,15 +121,16 @@ export abstract class AComponent<K extends string, VALUES, GET, SET>
}

// @ts-ignore: arguments
addListener(id: string, fn: Listener, scope?: any): boolean {}
addListener(id: ComponentEventType, fn: Listener, scope?: any): boolean {}

/** {@inheritDoc @thi.ng/api#INotify.removeListener} */
// prettier-ignore
// @ts-ignore: arguments
removeListener(id: string, fn: Listener, scope?: any): boolean {}
removeListener(id: ComponentEventType, fn: Listener, scope?: any): boolean {}

/** {@inheritDoc @thi.ng/api#INotify.notify} */
// @ts-ignore: arguments
notify(event: Event): boolean {}
notify(event: Event<ComponentEventType>): boolean {}

notifyChange(id: number): boolean {
return this.notify({ id: EVENT_CHANGED, target: this, value: id });
Expand Down
2 changes: 0 additions & 2 deletions packages/ecs/src/components/mem-component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { INotifyMixin } from "@thi.ng/api/mixins/inotify";
import {
typedArray,
type Type,
Expand All @@ -10,7 +9,6 @@ import type { IMemPoolArray } from "@thi.ng/malloc";
import type { ICache, MemMappedComponentOpts } from "../api.js";
import { AComponent } from "./acomponent.js";

@INotifyMixin
export class MemMappedComponent<K extends string> extends AComponent<
K,
TypedArray,
Expand Down
2 changes: 0 additions & 2 deletions packages/ecs/src/components/object-component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { UIntArray } from "@thi.ng/api";
import { INotifyMixin } from "@thi.ng/api/mixins/inotify";
import { assert } from "@thi.ng/errors/assert";
import type { IMemPoolArray } from "@thi.ng/malloc";
import type { ObjectComponentOpts } from "../api.js";
import { AComponent } from "./acomponent.js";

@INotifyMixin
export class ObjectComponent<K extends string, T> extends AComponent<
K,
T[],
Expand Down
9 changes: 5 additions & 4 deletions packages/ecs/src/ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EVENT_ADDED,
EVENT_PRE_DELETE,
type ComponentID,
type ECSEventType,
type ECSOpts,
type GroupOpts,
type IComponent,
Expand All @@ -26,7 +27,7 @@ import { Group } from "./groups/group.js";
let NEXT_GROUP_ID = 0;

@INotifyMixin
export class ECS<SPEC> implements INotify {
export class ECS<SPEC> implements INotify<ECSEventType> {
idgen: IDGen;
pool: IMemPoolArray;
components: Map<
Expand Down Expand Up @@ -142,13 +143,13 @@ export class ECS<SPEC> implements INotify {

/** {@inheritDoc @thi.ng/api#INotify.addListener} */
// @ts-ignore: mixin
addListener(id: string, fn: Listener, scope?: any): boolean {}
addListener(id: ECSEventType, fn: Listener, scope?: any): boolean {}

/** {@inheritDoc @thi.ng/api#INotify.removeListener} */
// @ts-ignore: mixin
removeListener(id: string, fn: Listener, scope?: any): boolean {}
removeListener(id: ECSEventType, fn: Listener, scope?: any): boolean {}

/** {@inheritDoc @thi.ng/api#INotify.notify} */
// @ts-ignore: mixin
notify(event: Event): boolean {}
notify(event: Event<ECSEventType>): boolean {}
}

0 comments on commit a870653

Please sign in to comment.