Skip to content

Commit

Permalink
refactor(ecs): restructure pkg, extract AComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 10, 2019
1 parent bcd1e64 commit 9a01b4e
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 330 deletions.
37 changes: 28 additions & 9 deletions packages/ecs/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ArrayLikeIterable,
Fn0,
IID,
ILogger,
INotify,
IRelease,
Type,
Expand Down Expand Up @@ -30,25 +31,39 @@ export interface ComponentInfo<SPEC, K extends ComponentID<SPEC>> {
stride: number;
}

export interface IComponent<K extends string, T, V> extends IID<K>, INotify {
export interface IComponent<K extends string, VALUES, GET, SET>
extends IID<K>,
INotify {
dense: UIntArray;
sparse: UIntArray;
vals: ArrayLike<any>;
size: number;
stride: number;
vals: VALUES;
readonly size: number;
readonly stride: number;

owner?: IID<string>;

has(id: number): boolean;
add(id: number, val?: V): boolean;
add(id: number, val?: SET): boolean;
delete(id: number): boolean;
get(id: number): T | undefined;
getIndex(i: number): T | undefined;
get(id: number): GET | undefined;
set(i: number, val: SET): boolean;
getIndex(i: number): GET | undefined;
setIndex(i: number, val: SET): boolean;
setIndexUnsafe(i: number, val: SET, notify?: boolean): void;

keys(): ArrayLikeIterable<number>;
values(): IterableIterator<T>;
values(): IterableIterator<GET>;

swapIndices(a: number, b: number): boolean;
/**
* Swaps slots of `src` & `dest` indices. The given args are NOT
* entity IDs, but indices in the `dense` array. The corresponding
* sparse & value slots are swapped too. Returns true if swap
* happened (false, if `src` and `dest` are equal)
*
* @param src
* @param dest
*/
swapIndices(src: number, dest: number): boolean;
}

export interface MemMappedComponentOpts<ID extends string> {
Expand Down Expand Up @@ -79,3 +94,7 @@ export interface ICache<T> extends IRelease {
getSet(key: number, notFound: Fn0<T>): T;
delete(key: number): boolean;
}

export let LOGGER: ILogger | null = null;

export const setLogger = (logger: ILogger) => (LOGGER = logger);
2 changes: 1 addition & 1 deletion packages/ecs/src/lru.ts → packages/ecs/src/caches/lru.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Fn0 } from "@thi.ng/api";
import { ConsCell, DCons } from "@thi.ng/dcons";
import { ICache } from "./api";
import { ICache } from "../api";

type LRUEntry<T> = { k: number; v: T };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fn0 } from "@thi.ng/api";
import { ICache } from "./api";
import { ICache } from "../api";

export class NullCache<T> implements ICache<T> {
release() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fn0 } from "@thi.ng/api";
import { ICache } from "./api";
import { ICache } from "../api";

export class UnboundedCache<T> implements ICache<T> {
index: Map<number, T>;
Expand Down
207 changes: 0 additions & 207 deletions packages/ecs/src/component-mm.ts

This file was deleted.

Loading

0 comments on commit 9a01b4e

Please sign in to comment.