generated from MatrixAI/TypeScript-Demo-Lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
24 lines (21 loc) · 885 Bytes
/
types.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
/**
* Resource acquisition function
* This returns a tuple of resource release function and the acquired resource
* When implementing this type, make sure to use arrow functions or
* if you are passing a method, make sure to bind the `this` context
*/
type ResourceAcquire<Resource> = (
resources?: readonly any[],
) => Promise<readonly [ResourceRelease, Resource?]>;
/**
* Resource release function
* Pass any error during resource usage into the function
* The function should not rethrow the error, it only uses it to change
* its releasing behaviour, the `withF` and `withG` utilities will rethrow
* the error
*/
type ResourceRelease = (e?: Error) => Promise<void>;
type Resources<T extends readonly ResourceAcquire<unknown>[]> = {
[K in keyof T]: T[K] extends ResourceAcquire<infer R> ? R : never;
};
export type { ResourceAcquire, ResourceRelease, Resources };