Untitled

 avatar
unknown
typescript
a year ago
1.1 kB
6
Indexable
let nextComponentId = 1;
class Component {
    static componentId: number;

    getComponentId() {
        return (this.constructor as unknown as typeof Component).componentId;
    }
}

class A extends Component {}
class B extends Component {}
class C extends Component {}

function registerComponents(...components: typeof Component[]) {
    const registered = new Set();
    components.forEach(c => {
        if (registered.has(c.name)) {
            console.log("Warning: Duplicate component in registerComponents");
            return;
        }
        c.componentId = nextComponentId;
        nextComponentId = nextComponentId << 1 >>> 0;
        registered.add(c.name);
    });
}

function getComponentId(...components: Component[]) {
    components.forEach(c => {
        console.log(c.getComponentId());
    });
}


registerComponents(A, B, A, C);
const a1 = new A();
const a2 = new A();
const b1 = new B();
const b2 = new B();
const c1 = new C();
const c2 = new C();
getComponentId(
    a1,
    a2,
    b1,
    b2,
    c1,
    c2,
);
Editor is loading...
Leave a Comment