/
CtxLike.ts
30 lines (25 loc) 路 1.03 KB
/
CtxLike.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { typeGuard } from "https://raw.githubusercontent.com/garronej/tsafe/v0.10.1/deno_dist/typeGuard.ts";
import type { NonPostableEvtLike } from "./NonPostableEvtLike.ts";
import type { Handler } from "../Handler.ts";
/**
* Minimal interface that an object must implement to be a valid context argument
* ( for interop between mismatching EVT versions )
* */
export interface CtxLike<Result = any> {
done(result: Result): void;
abort(error: Error): void;
zz__addHandler<T>(handler: Handler<T, any, CtxLike<Result>>, evt: NonPostableEvtLike<T>): void;
zz__removeHandler<T>(handler: Handler<T, any, CtxLike<Result>>): void;
}
export namespace CtxLike {
export function match<T=any>(o: any): o is CtxLike<T> {
return (
typeGuard<CtxLike>(o, true) &&
o instanceof Object &&
typeof o.done === "function" &&
typeof o.abort === "function" &&
typeof o.zz__addHandler === "function" &&
typeof o.zz__removeHandler === "function"
);
}
}