Skip to content

Commit

Permalink
refactor(ecs): update type usage
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 19, 2024
1 parent 86be0c3 commit f3e8c7a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
7 changes: 4 additions & 3 deletions packages/ecs/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IID,
INotify,
IRelease,
Maybe,
Type,
TypedArray,
UIntArray,
Expand Down Expand Up @@ -44,9 +45,9 @@ export interface IComponent<K extends string, VALUES, GET, SET>
has(id: number): boolean;
add(id: number, val?: SET): boolean;
delete(id: number): boolean;
get(id: number): GET | undefined;
get(id: number): Maybe<GET>;
set(i: number, val: SET): boolean;
getIndex(i: number): GET | undefined;
getIndex(i: number): Maybe<GET>;
setIndex(i: number, val: SET): boolean;
setIndexUnsafe(i: number, val: SET, notify?: boolean): void;

Expand Down Expand Up @@ -89,7 +90,7 @@ export interface GroupOpts {
export interface ICache<T> extends IClear, IRelease {
keys(): Iterable<number>;
set(key: number, val: T): T;
get(key: number): T | undefined;
get(key: number): Maybe<T>;
getSet(key: number, notFound: Fn0<T>): T;
delete(key: number): boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ecs/src/caches/null.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn0 } from "@thi.ng/api";
import type { Fn0, Maybe } from "@thi.ng/api";
import type { ICache } from "../api.js";

export class NullCache<T> implements ICache<T> {
Expand All @@ -14,7 +14,7 @@ export class NullCache<T> implements ICache<T> {
return val;
}

get(_: number): T | undefined {
get(_: number): Maybe<T> {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ecs/src/caches/unbounded.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn0 } from "@thi.ng/api";
import type { Fn0, Maybe } from "@thi.ng/api";
import type { ICache } from "../api.js";

export class UnboundedCache<T> implements ICache<T> {
Expand Down Expand Up @@ -26,7 +26,7 @@ export class UnboundedCache<T> implements ICache<T> {
return val;
}

get(key: number): T | undefined {
get(key: number): Maybe<T> {
return this.index.get(key);
}

Expand Down
13 changes: 10 additions & 3 deletions packages/ecs/src/components/acomponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { Event, IID, INotify, Listener, UIntArray } from "@thi.ng/api";
import type {
Event,
IID,
INotify,
Listener,
Maybe,
UIntArray,
} from "@thi.ng/api";
import { INotifyMixin } from "@thi.ng/api/mixins/inotify";
import { isFunction } from "@thi.ng/checks/is-function";
import type { IMemPoolArray } from "@thi.ng/malloc";
Expand Down Expand Up @@ -52,9 +59,9 @@ export abstract class AComponent<K extends string, VALUES, GET, SET>
return i < this.n && this.dense[i] === id;
}

abstract get(id: number): GET | undefined;
abstract get(id: number): Maybe<GET>;

abstract getIndex(i: number): GET | undefined;
abstract getIndex(i: number): Maybe<GET>;

valueIndexForID(id: number) {
const i = this.sparse[id];
Expand Down
6 changes: 3 additions & 3 deletions packages/ecs/src/ecs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Event, INotify, Listener } from "@thi.ng/api";
import type { Event, INotify, Listener, Maybe } from "@thi.ng/api";
import { INotifyMixin } from "@thi.ng/api/mixins/inotify";
import { uintTypeForSize } from "@thi.ng/api/typedarray";
import { bitSize } from "@thi.ng/binary/count";
Expand Down Expand Up @@ -70,10 +70,10 @@ export class ECS<SPEC> implements INotify<ECSEventType> {

defComponent<K extends ComponentID<SPEC>>(
opts: MemMappedComponentOpts<K>
): MemMappedComponent<K> | undefined;
): Maybe<MemMappedComponent<K>>;
defComponent<K extends ComponentID<SPEC>>(
opts: ObjectComponentOpts<K, SPEC[K]>
): ObjectComponent<K, SPEC[K]> | undefined;
): Maybe<ObjectComponent<K, SPEC[K]>>;
defComponent<K extends ComponentID<SPEC>>(opts: any) {
assert(
!this.components.has(opts.id),
Expand Down

0 comments on commit f3e8c7a

Please sign in to comment.