---
# System prepended metadata

title: Untitled

---

```typescript=

type ComponentId = number;

type ComponentMask = number;

enum ComponentType {
    Data = "data",
    Tag = "tag"
}

type ComponentInfo = {
    id: ComponentId;
    mask: ComponentMask;
    component: Component;
    type: ComponentType;
}

type IsTag = {
    // impl
}

type ComponentInfos = Map<ComponentId, ComponentInfo>

type Worlds = Map<WorldId, World>

export function get_or_register(component: Component) {
    return runtime.components.get(get_component_id(component)) ?? register_component(component); 
}

type WithId<T> = T & { [COMPONENT_ID_PROP_KEY]: ComponentId };
export function get_component_id(component: WithId<Component>) {
    return component[COMPONENT_ID_PROP_KEY];
}

export function is_tag(component: Component) {
    return runtime.components.get(get_component_id(component)).type === ComponentType.Tag;
}

function is_tag_internal() {
    return // member of ComponentType
}

function register_component(component: Component) {
    const info = {
        id: ++runtime.component_counter,
        mask: 1 << runtime.component_counter,
        component,
        type: is_tag_internal()
    }
    runtime.component.set(runtime.component_counter, info);
    return info;
}

// component-flags
enum ComponentFlags {
    Empty = 0,
    Added = 1 << 1,
    Changed = 1 << 2,
    Removed = 1 << 3
}


```



```typescript=
// constants.ts

const SYMBOL_PREFIX = `$$_${package.name}__`;
const GLOBAL_CONTEXT_KEY = defsymbol("global_context");
const COMPONENT_ID_PROP_KEY = defsymbol("component_id");

const defsymbol = (name: string) => Symbol(`${SYMBOL_PREFIX}${name}`);

```

```typescript=

// global/index.ts

type Global = {
    runtime: Runtime;
}

globalThis[GLOBAL_CONTEXT_KEY] = { } as Partial<Global>;

export const global_context = globalThis[GLOBAL_CONTEXT_KEY] as Global;


// global/runtime.ts

type Runtime = {
    world: Worlds;
    current_world: WorldId;
    components: ComponentInfos;
    component_counter: ComponentId;
}

global_context.runtime = {
    worlds: new Map(),
    currentWorld: -1,
    components: new Map(),
    component_counter: ComponentId;
}

export const runtime = global_context.runtime;


```

```typescript=

type Disposable = {
     dispose(): unknown;
}


if (instance?.dispose) {
    instance.dispose()
}

```

```typescript=
type record = Record<string, any>

type ResourceId = number;
type Resource<T extends record = record> = Type<T>;
type ResourceInstance<T extends record = record> = InstanceType<Resource<T>>

type Resources = Map<Resource, ResourceInstance>

function add_resource(resource: Resource | ResourceInstance) {
    const [instance, type] = get_type_and_instance(resource)
    this.resources.set(type, instance);
}

function get_type_and_instance(target: record | Type<record>) {
    // If target constructor is Function, then target itself is a constructor
    if(target.constructor === Function) {
        return [new traget(), target]
    } else {
        return [target, target.constructor]
    }
}

```

```typescript=

type ComponentBundle = readonly (Component | ComponentInstance)[];

type World = {
    batch(size: number, components: ComponentBundle): Entity;
    spawn(...components: ComponentBundle): Entity;
    despawn(entity: Entity): boolean;
    
    add(...component: ComponentBundle): void;
    get<T extends Component>(component: T): T;
    remove(component: Component): void;
    has(component: Component): boolean;
    
    contains(entity: Entity): boolean;
    
    update(): void
    query<T extends QueryParams(...params: T): Query<T>;
    iter(): Iterable<Entity>;
}

```