Skip to content
Snippets Groups Projects
api-impl.ts 1.21 KiB
Newer Older
Michal Malý's avatar
Michal Malý committed
import { ReDNATCOMsp } from './index';
import { ReDNATCOMspApi } from './api';

export class ReDNATCOMspApiImpl implements ReDNATCOMspApi.Object {
    private target: ReDNATCOMsp|undefined = undefined;
    private onEvent: ((evt: ReDNATCOMspApi.Event) => void)|undefined;

    private check() {
        if (!this.target)
            throw new Error('ReDNATCOMsp object not bound');
    }

    _bind(target: ReDNATCOMsp) {
        this.target = target;
    }

    async command(cmd: ReDNATCOMspApi.Command) {
Michal Malý's avatar
Michal Malý committed
        this.check();
        this.target!.command(cmd);
    }

    event(evt: ReDNATCOMspApi.Event) {
        if (this.onEvent)
            this.onEvent(evt);
    }

    init(elemId: string, onEvent?: (evt: ReDNATCOMspApi.Event) => void) {
Michal Malý's avatar
Michal Malý committed
        this.onEvent = onEvent;
        ReDNATCOMsp.init(elemId);
Michal Malý's avatar
Michal Malý committed
        return this;
    }

    isReady(): boolean {
        return !!this.target;
    }

    loadStructure(data: string, type: 'cif'|'pdb') {
        this.check();
        this.target!.loadStructure(data, type);
    }

    query<T extends ReDNATCOMspApi.Queries.Type>(type: T): ReDNATCOMspApi.ResponseTypes[T] {
Michal Malý's avatar
Michal Malý committed
        this.check();
        return this.target!.apiQuery(type) as ReDNATCOMspApi.ResponseTypes[T];