2020-03-09 10:18:02 -04:00
|
|
|
import { openPlugin as openPluginOp } from "./ops/plugins.ts";
|
2019-12-05 15:30:20 -05:00
|
|
|
import { core } from "./core.ts";
|
|
|
|
|
|
|
|
export interface AsyncHandler {
|
|
|
|
(msg: Uint8Array): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PluginOp {
|
|
|
|
dispatch(
|
|
|
|
control: Uint8Array,
|
|
|
|
zeroCopy?: ArrayBufferView | null
|
|
|
|
): Uint8Array | null;
|
|
|
|
setAsyncHandler(handler: AsyncHandler): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PluginOpImpl implements PluginOp {
|
2020-03-28 13:03:49 -04:00
|
|
|
readonly #opId: number;
|
|
|
|
|
|
|
|
constructor(opId: number) {
|
|
|
|
this.#opId = opId;
|
|
|
|
}
|
2019-12-05 15:30:20 -05:00
|
|
|
|
|
|
|
dispatch(
|
|
|
|
control: Uint8Array,
|
|
|
|
zeroCopy?: ArrayBufferView | null
|
|
|
|
): Uint8Array | null {
|
2020-03-28 13:03:49 -04:00
|
|
|
return core.dispatch(this.#opId, control, zeroCopy);
|
2019-12-05 15:30:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setAsyncHandler(handler: AsyncHandler): void {
|
2020-03-28 13:03:49 -04:00
|
|
|
core.setAsyncHandler(this.#opId, handler);
|
2019-12-05 15:30:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(afinch7): add close method.
|
|
|
|
|
|
|
|
interface Plugin {
|
|
|
|
ops: {
|
|
|
|
[name: string]: PluginOp;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class PluginImpl implements Plugin {
|
2020-03-28 13:03:49 -04:00
|
|
|
#ops: { [name: string]: PluginOp } = {};
|
2019-12-05 15:30:20 -05:00
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
constructor(_rid: number, ops: { [name: string]: number }) {
|
2019-12-05 15:30:20 -05:00
|
|
|
for (const op in ops) {
|
2020-03-28 13:03:49 -04:00
|
|
|
this.#ops[op] = new PluginOpImpl(ops[op]);
|
2019-12-05 15:30:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get ops(): { [name: string]: PluginOp } {
|
2020-03-28 13:03:49 -04:00
|
|
|
return Object.assign({}, this.#ops);
|
2019-12-05 15:30:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function openPlugin(filename: string): Plugin {
|
2020-03-09 10:18:02 -04:00
|
|
|
const response = openPluginOp(filename);
|
2019-12-05 15:30:20 -05:00
|
|
|
return new PluginImpl(response.rid, response.ops);
|
|
|
|
}
|