Skip to content
Snippets Groups Projects
base.ts 3.31 KiB
Newer Older
David Sehnal's avatar
David Sehnal committed
/**
 * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author David Sehnal <david.sehnal@gmail.com>
 */

import { PluginContext } from '../context';
import { UUID } from '../../mol-util';
David Sehnal's avatar
David Sehnal committed

export { PluginCommand }

interface PluginCommand<T = unknown> {
    readonly id: UUID,
    dispatch(ctx: PluginContext, params: T): Promise<void>,
    subscribe(ctx: PluginContext, action: PluginCommand.Action<T>): PluginCommand.Subscription
David Sehnal's avatar
David Sehnal committed
}

/** namespace.id must a globally unique identifier */
function PluginCommand<T>(): PluginCommand<T> {
    return new Impl();
David Sehnal's avatar
David Sehnal committed
}

class Impl<T> implements PluginCommand<T> {
    dispatch(ctx: PluginContext, params: T): Promise<void> {
        return ctx.commands.dispatch(this, params);
David Sehnal's avatar
David Sehnal committed
    }
    subscribe(ctx: PluginContext, action: PluginCommand.Action<T>): PluginCommand.Subscription {
        return ctx.commands.subscribe(this, action);
    }
    id = UUID.create22();
David Sehnal's avatar
David Sehnal committed
    }
}

namespace PluginCommand {
    export type Id = string & { '@type': 'plugin-command-id' }

    export interface Subscription {
        unsubscribe(): void
    }

    export type Action<T> = (params: T) => unknown | Promise<unknown>
    type Instance = { cmd: PluginCommand<any>, params: any, resolve: () => void, reject: (e: any) => void }
David Sehnal's avatar
David Sehnal committed

    export class Manager {
        private subs = new Map<string, Action<any>[]>();
        private disposing = false;

        subscribe<T>(cmd: PluginCommand<T>, action: Action<T>): Subscription {
            let actions = this.subs.get(cmd.id);
            if (!actions) {
                actions = [];
                this.subs.set(cmd.id, actions);
            }
            actions.push(action);

            return {
                unsubscribe: () => {
                    const actions = this.subs.get(cmd.id);
                    if (!actions) return;
                    const idx = actions.indexOf(action);
                    if (idx < 0) return;
                    for (let i = idx + 1; i < actions.length; i++) {
                        actions[i - 1] = actions[i];
                    }
                    actions.pop();
                }
            }
        }


        /** Resolves after all actions have completed */
        dispatch<T>(cmd: PluginCommand<T>, params: T) {
David Sehnal's avatar
David Sehnal committed
            return new Promise<void>((resolve, reject) => {
David Sehnal's avatar
David Sehnal committed
                if (this.disposing) {
David Sehnal's avatar
David Sehnal committed
                    reject('disposed');
                    return;
                }

                const actions = this.subs.get(cmd.id);
David Sehnal's avatar
David Sehnal committed
                if (!actions) {
                    resolve();
                    return;
                }

                this.resolve({ cmd, params, resolve, reject });
David Sehnal's avatar
David Sehnal committed
            });
        }

        dispose() {
            this.subs.clear();
        }

        private async resolve(instance: Instance) {
            const actions = this.subs.get(instance.cmd.id);
David Sehnal's avatar
David Sehnal committed
            if (!actions) {
David Sehnal's avatar
David Sehnal committed
                return;
            }
David Sehnal's avatar
David Sehnal committed

            try {
                // TODO: should actions be called "asynchronously" ("setImmediate") instead?
                for (const a of actions) {
                    await a(instance.params);
David Sehnal's avatar
David Sehnal committed
                }
                instance.resolve();
David Sehnal's avatar
David Sehnal committed
            } catch (e) {
                instance.reject(e);
David Sehnal's avatar
David Sehnal committed
            }
        }
    }
}