conode 0.1.23 → 0.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -7
- package/build/index.cjs +47 -57
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +7 -7
- package/build/index.js +26 -38
- package/build/index.js.map +1 -1
- package/package.json +12 -14
- package/src/index.ts +24 -38
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# CoNode
|
|
2
2
|
|
|
3
|
-
0-
|
|
3
|
+
0-deps async context management for Node.js
|
|
4
4
|
|
|
5
5
|
[![Build Status][github-image]][github-url]
|
|
6
6
|
[![NPM version][npm-image]][npm-url]
|
|
@@ -18,9 +18,10 @@
|
|
|
18
18
|
|
|
19
19
|
## Features
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
21
|
+
- Zero dependencies
|
|
22
|
+
- Lightweight wrappers around [AsyncLocalStorage](https://nodejs.org/api/async_hooks.html)
|
|
23
|
+
- Full TypeScript support
|
|
24
|
+
- Works in CommonJS and ES Module environments
|
|
24
25
|
|
|
25
26
|
## Installing
|
|
26
27
|
|
|
@@ -38,6 +39,22 @@ yarn add conode
|
|
|
38
39
|
|
|
39
40
|
## Examples
|
|
40
41
|
|
|
42
|
+
Quick start
|
|
43
|
+
```typescript
|
|
44
|
+
import { createContext } from 'conode';
|
|
45
|
+
|
|
46
|
+
interface RequestContext { userId: string }
|
|
47
|
+
|
|
48
|
+
const reqCtx = createContext<RequestContext>();
|
|
49
|
+
|
|
50
|
+
async function handleRequest() {
|
|
51
|
+
console.log(reqCtx.getContext()?.userId);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
await reqCtx.contextualize({ userId: 'abc123' }, handleRequest);
|
|
55
|
+
// Logs: 'abc123'
|
|
56
|
+
```
|
|
57
|
+
|
|
41
58
|
Functional approach
|
|
42
59
|
```typescript
|
|
43
60
|
import { createContext } from '../index';
|
|
@@ -51,7 +68,7 @@ const action = () => {
|
|
|
51
68
|
auth.contextualize(jwt, action);
|
|
52
69
|
```
|
|
53
70
|
|
|
54
|
-
Class approach
|
|
71
|
+
Class approach with decorator
|
|
55
72
|
```typescript
|
|
56
73
|
import { contextualize, Context } from '../index';
|
|
57
74
|
|
|
@@ -83,8 +100,8 @@ Copyright (c) 2022-present Ivan Zakharchanka
|
|
|
83
100
|
[npm-url]: https://www.npmjs.com/package/conode
|
|
84
101
|
[downloads-image]: https://img.shields.io/npm/dw/conode.svg?maxAge=43200
|
|
85
102
|
[npm-image]: https://img.shields.io/npm/v/conode.svg?maxAge=43200
|
|
86
|
-
[github-url]: https://github.com/3axap4eHko/conode/actions/workflows/
|
|
87
|
-
[github-image]: https://github.com/3axap4eHko/conode/actions/workflows/
|
|
103
|
+
[github-url]: https://github.com/3axap4eHko/conode/actions/workflows/build.yml
|
|
104
|
+
[github-image]: https://github.com/3axap4eHko/conode/actions/workflows/build.yml/badge.svg
|
|
88
105
|
[codecov-url]: https://codecov.io/gh/3axap4eHko/conode
|
|
89
106
|
[codecov-image]: https://codecov.io/gh/3axap4eHko/conode/branch/master/graph/badge.svg?token=JZ8QCGH6PI
|
|
90
107
|
[codeclimate-url]: https://codeclimate.com/github/3axap4eHko/conode/maintainability
|
package/build/index.cjs
CHANGED
|
@@ -1,70 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
9
|
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
Context: function() {
|
|
13
|
+
return Context;
|
|
14
|
+
},
|
|
15
|
+
contextualize: function() {
|
|
16
|
+
return contextualize;
|
|
17
|
+
},
|
|
18
|
+
createContext: function() {
|
|
19
|
+
return createContext;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _nodeasync_hooks = require("node:async_hooks");
|
|
23
|
+
const createContext = ()=>{
|
|
24
|
+
const storage = new _nodeasync_hooks.AsyncLocalStorage();
|
|
16
25
|
return {
|
|
17
|
-
contextualize(value, callback) {
|
|
18
|
-
return
|
|
19
|
-
storage.run(value, () => __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
try {
|
|
21
|
-
yield callback();
|
|
22
|
-
resolve();
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
reject(e);
|
|
26
|
-
}
|
|
27
|
-
}));
|
|
28
|
-
});
|
|
26
|
+
contextualize (value, callback) {
|
|
27
|
+
return storage.run(value, callback);
|
|
29
28
|
},
|
|
30
|
-
getContext() {
|
|
29
|
+
getContext () {
|
|
31
30
|
return storage.getStore();
|
|
32
31
|
}
|
|
33
32
|
};
|
|
34
33
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const context =
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return context.getContext();
|
|
42
|
-
}
|
|
43
|
-
contextualize(callback) {
|
|
44
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
return context.contextualize(this, callback);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
34
|
+
function Context(BaseClass = class {
|
|
35
|
+
}) {
|
|
36
|
+
const context = createContext();
|
|
37
|
+
return class Contextual extends BaseClass {
|
|
38
|
+
static get context() {
|
|
39
|
+
return context.getContext();
|
|
48
40
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
contextualize(callback) {
|
|
54
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
return context.contextualize(this, callback);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
};
|
|
41
|
+
async contextualize(callback) {
|
|
42
|
+
return context.contextualize(this, callback);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
59
45
|
}
|
|
60
|
-
exports.Context = Context;
|
|
61
46
|
function contextualize(target, _, descriptor) {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
47
|
+
const property = descriptor.value;
|
|
48
|
+
if (typeof property === 'function') {
|
|
49
|
+
const method = property?.bind(target);
|
|
50
|
+
if (method) {
|
|
51
|
+
function contextualized(...args) {
|
|
52
|
+
return target.contextualize(()=>method.apply(null, args));
|
|
53
|
+
}
|
|
54
|
+
;
|
|
55
|
+
descriptor.value = contextualized;
|
|
56
|
+
}
|
|
65
57
|
}
|
|
66
|
-
;
|
|
67
|
-
descriptor.value = contextualized;
|
|
68
58
|
}
|
|
69
|
-
|
|
70
|
-
//# sourceMappingURL=index.cjs.map
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=index.cjs.map
|
package/build/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<T>();\n\n return {\n contextualize(value: T, callback: Callback) {\n return storage.run(value, callback);\n },\n getContext() {\n return storage.getStore();\n }\n };\n};\n\ntype Constructor = new (...args: unknown[]) => object;\n\nexport function Context(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static get context() {\n return context.getContext();\n }\n\n async contextualize(callback: Callback) {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport interface Contextualized {\n contextualize(callback: Callback): Promise<any>;\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {\n const property = descriptor.value;\n if (typeof property === 'function') {\n const method = property?.bind(target);\n if (method) {\n function contextualized<A extends unknown[]>(...args: A) {\n return target.contextualize(() => method.apply(null, args));\n };\n descriptor.value = contextualized as T[K];\n }\n }\n}\n\n"],"names":["Context","contextualize","createContext","storage","AsyncLocalStorage","value","callback","run","getContext","getStore","BaseClass","context","Contextual","target","_","descriptor","property","method","bind","contextualized","args","apply"],"mappings":";;;;;;;;;;;IAmBgBA,OAAO;eAAPA;;IAiBAC,aAAa;eAAbA;;IAhCHC,aAAa;eAAbA;;;iCAJqB;AAI3B,MAAMA,gBAAgB;IAC3B,MAAMC,UAAU,IAAIC,kCAAiB;IAErC,OAAO;QACLH,eAAcI,KAAQ,EAAEC,QAAkB;YACxC,OAAOH,QAAQI,GAAG,CAACF,OAAOC;QAC5B;QACAE;YACE,OAAOL,QAAQM,QAAQ;QACzB;IACF;AACF;AAIO,SAAST,QAAQU,YAAyB;AAAQ,CAAC;IACxD,MAAMC,UAAUT;IAChB,OAAO,MAAMU,mBAAmBF;QAC9B,WAAWC,UAAU;YACnB,OAAOA,QAAQH,UAAU;QAC3B;QAEA,MAAMP,cAAcK,QAAkB,EAAE;YACtC,OAAOK,QAAQV,aAAa,CAAC,IAAI,EAAEK;QACrC;IACF;AACF;AAMO,SAASL,cAA2DY,MAAS,EAAEC,CAAI,EAAEC,UAA8B;IACxH,MAAMC,WAAWD,WAAWV,KAAK;IACjC,IAAI,OAAOW,aAAa,YAAY;QAClC,MAAMC,SAASD,UAAUE,KAAKL;QAC9B,IAAII,QAAQ;YACV,SAASE,eAAoC,GAAGC,IAAO;gBACrD,OAAOP,OAAOZ,aAAa,CAAC,IAAMgB,OAAOI,KAAK,CAAC,MAAMD;YACvD;;YACAL,WAAWV,KAAK,GAAGc;QACrB;IACF;AACF"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export type Callback = () =>
|
|
1
|
+
export type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;
|
|
2
2
|
export declare const createContext: <T>() => {
|
|
3
|
-
contextualize(value: T, callback: Callback):
|
|
3
|
+
contextualize(value: T, callback: Callback): unknown;
|
|
4
4
|
getContext(): T | undefined;
|
|
5
5
|
};
|
|
6
|
-
type Constructor = new (...args:
|
|
7
|
-
export declare function Context(
|
|
8
|
-
new (...args:
|
|
9
|
-
contextualize(callback: Callback): Promise<
|
|
6
|
+
type Constructor = new (...args: unknown[]) => object;
|
|
7
|
+
export declare function Context(BaseClass?: Constructor): {
|
|
8
|
+
new (...args: unknown[]): {
|
|
9
|
+
contextualize(callback: Callback): Promise<unknown>;
|
|
10
10
|
};
|
|
11
11
|
readonly context: unknown;
|
|
12
12
|
};
|
|
13
13
|
export interface Contextualized {
|
|
14
14
|
contextualize(callback: Callback): Promise<any>;
|
|
15
15
|
}
|
|
16
|
-
export declare function contextualize<T extends Contextualized>(target: T, _:
|
|
16
|
+
export declare function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor): void;
|
|
17
17
|
export {};
|
package/build/index.js
CHANGED
|
@@ -1,51 +1,39 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
-
export const createContext = ()
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
export const createContext = ()=>{
|
|
3
3
|
const storage = new AsyncLocalStorage();
|
|
4
4
|
return {
|
|
5
|
-
contextualize(value, callback) {
|
|
6
|
-
return
|
|
7
|
-
storage.run(value, async () => {
|
|
8
|
-
try {
|
|
9
|
-
await callback();
|
|
10
|
-
resolve();
|
|
11
|
-
}
|
|
12
|
-
catch (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
5
|
+
contextualize (value, callback) {
|
|
6
|
+
return storage.run(value, callback);
|
|
17
7
|
},
|
|
18
|
-
getContext() {
|
|
8
|
+
getContext () {
|
|
19
9
|
return storage.getStore();
|
|
20
10
|
}
|
|
21
11
|
};
|
|
22
12
|
};
|
|
23
|
-
export function Context(
|
|
13
|
+
export function Context(BaseClass = class {
|
|
14
|
+
}) {
|
|
24
15
|
const context = createContext();
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return context.getContext();
|
|
29
|
-
}
|
|
30
|
-
async contextualize(callback) {
|
|
31
|
-
return context.contextualize(this, callback);
|
|
32
|
-
}
|
|
16
|
+
return class Contextual extends BaseClass {
|
|
17
|
+
static get context() {
|
|
18
|
+
return context.getContext();
|
|
33
19
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
async contextualize(callback) {
|
|
39
|
-
return context.contextualize(this, callback);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
20
|
+
async contextualize(callback) {
|
|
21
|
+
return context.contextualize(this, callback);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
42
24
|
}
|
|
43
25
|
export function contextualize(target, _, descriptor) {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
26
|
+
const property = descriptor.value;
|
|
27
|
+
if (typeof property === 'function') {
|
|
28
|
+
const method = property?.bind(target);
|
|
29
|
+
if (method) {
|
|
30
|
+
function contextualized(...args) {
|
|
31
|
+
return target.contextualize(()=>method.apply(null, args));
|
|
32
|
+
}
|
|
33
|
+
;
|
|
34
|
+
descriptor.value = contextualized;
|
|
35
|
+
}
|
|
47
36
|
}
|
|
48
|
-
;
|
|
49
|
-
descriptor.value = contextualized;
|
|
50
37
|
}
|
|
51
|
-
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<T>();\n\n return {\n contextualize(value: T, callback: Callback) {\n return storage.run(value, callback);\n },\n getContext() {\n return storage.getStore();\n }\n };\n};\n\ntype Constructor = new (...args: unknown[]) => object;\n\nexport function Context(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static get context() {\n return context.getContext();\n }\n\n async contextualize(callback: Callback) {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport interface Contextualized {\n contextualize(callback: Callback): Promise<any>;\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {\n const property = descriptor.value;\n if (typeof property === 'function') {\n const method = property?.bind(target);\n if (method) {\n function contextualized<A extends unknown[]>(...args: A) {\n return target.contextualize(() => method.apply(null, args));\n };\n descriptor.value = contextualized as T[K];\n }\n }\n}\n\n"],"names":["AsyncLocalStorage","createContext","storage","contextualize","value","callback","run","getContext","getStore","Context","BaseClass","context","Contextual","target","_","descriptor","property","method","bind","contextualized","args","apply"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,mBAAmB;AAIrD,OAAO,MAAMC,gBAAgB;IAC3B,MAAMC,UAAU,IAAIF;IAEpB,OAAO;QACLG,eAAcC,KAAQ,EAAEC,QAAkB;YACxC,OAAOH,QAAQI,GAAG,CAACF,OAAOC;QAC5B;QACAE;YACE,OAAOL,QAAQM,QAAQ;QACzB;IACF;AACF,EAAE;AAIF,OAAO,SAASC,QAAQC,YAAyB;AAAQ,CAAC;IACxD,MAAMC,UAAUV;IAChB,OAAO,MAAMW,mBAAmBF;QAC9B,WAAWC,UAAU;YACnB,OAAOA,QAAQJ,UAAU;QAC3B;QAEA,MAAMJ,cAAcE,QAAkB,EAAE;YACtC,OAAOM,QAAQR,aAAa,CAAC,IAAI,EAAEE;QACrC;IACF;AACF;AAMA,OAAO,SAASF,cAA2DU,MAAS,EAAEC,CAAI,EAAEC,UAA8B;IACxH,MAAMC,WAAWD,WAAWX,KAAK;IACjC,IAAI,OAAOY,aAAa,YAAY;QAClC,MAAMC,SAASD,UAAUE,KAAKL;QAC9B,IAAII,QAAQ;YACV,SAASE,eAAoC,GAAGC,IAAO;gBACrD,OAAOP,OAAOV,aAAa,CAAC,IAAMc,OAAOI,KAAK,CAAC,MAAMD;YACvD;;YACAL,WAAWX,KAAK,GAAGe;QACrB;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "conode",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.24",
|
|
4
4
|
"description": "0-Deps, simple and fast context library for NodeJS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "build/index.cjs",
|
|
@@ -11,13 +11,6 @@
|
|
|
11
11
|
],
|
|
12
12
|
"module": "build/index.js",
|
|
13
13
|
"sideEffects": false,
|
|
14
|
-
"scripts": {
|
|
15
|
-
"convert-cjs": "node scripts/convert-cjs.js",
|
|
16
|
-
"build": "rm -rf build && yarn build:cjs && yarn build:mjs",
|
|
17
|
-
"build:cjs": "tsc -p tsconfig.cjs.json && yarn convert-cjs",
|
|
18
|
-
"build:mjs": "tsc -p tsconfig.mjs.json --declaration",
|
|
19
|
-
"test": "jest"
|
|
20
|
-
},
|
|
21
14
|
"repository": {
|
|
22
15
|
"type": "git",
|
|
23
16
|
"url": "git+https://github.com/3axap4eHko/conode.git"
|
|
@@ -39,10 +32,15 @@
|
|
|
39
32
|
},
|
|
40
33
|
"homepage": "https://github.com/3axap4eHko/conode#readme",
|
|
41
34
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"@types/
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
35
|
+
"@swc/jest": "^0.2.38",
|
|
36
|
+
"@types/jest": "^29.5.14",
|
|
37
|
+
"@types/node": "^22.15.18",
|
|
38
|
+
"inop": "^0.7.8",
|
|
39
|
+
"jest": "^29.7.0",
|
|
40
|
+
"typescript": "^5.8.3"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
|
|
44
|
+
"test": "jest"
|
|
47
45
|
}
|
|
48
|
-
}
|
|
46
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,22 +1,13 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
|
|
3
|
-
export type Callback = () =>
|
|
3
|
+
export type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;
|
|
4
4
|
|
|
5
5
|
export const createContext = <T>() => {
|
|
6
6
|
const storage = new AsyncLocalStorage<T>();
|
|
7
7
|
|
|
8
8
|
return {
|
|
9
9
|
contextualize(value: T, callback: Callback) {
|
|
10
|
-
return
|
|
11
|
-
storage.run(value, async () => {
|
|
12
|
-
try {
|
|
13
|
-
await callback();
|
|
14
|
-
resolve();
|
|
15
|
-
} catch (e) {
|
|
16
|
-
reject(e);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
});
|
|
10
|
+
return storage.run(value, callback);
|
|
20
11
|
},
|
|
21
12
|
getContext() {
|
|
22
13
|
return storage.getStore();
|
|
@@ -24,40 +15,35 @@ export const createContext = <T>() => {
|
|
|
24
15
|
};
|
|
25
16
|
};
|
|
26
17
|
|
|
27
|
-
type Constructor = new (...args:
|
|
18
|
+
type Constructor = new (...args: unknown[]) => object;
|
|
28
19
|
|
|
29
|
-
export function Context(
|
|
20
|
+
export function Context(BaseClass: Constructor = class { }) {
|
|
30
21
|
const context = createContext();
|
|
22
|
+
return class Contextual extends BaseClass {
|
|
23
|
+
static get context() {
|
|
24
|
+
return context.getContext();
|
|
25
|
+
}
|
|
31
26
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
static get context() {
|
|
35
|
-
return context.getContext();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async contextualize(callback: Callback) {
|
|
39
|
-
return context.contextualize(this, callback);
|
|
40
|
-
}
|
|
27
|
+
async contextualize(callback: Callback) {
|
|
28
|
+
return context.contextualize(this, callback);
|
|
41
29
|
}
|
|
42
|
-
|
|
43
|
-
static get context() {
|
|
44
|
-
return context.getContext();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async contextualize(callback: Callback) {
|
|
48
|
-
return context.contextualize(this, callback);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
30
|
+
};
|
|
51
31
|
}
|
|
52
32
|
|
|
53
33
|
export interface Contextualized {
|
|
54
34
|
contextualize(callback: Callback): Promise<any>;
|
|
55
35
|
}
|
|
56
36
|
|
|
57
|
-
export function contextualize<T extends Contextualized>(target: T, _:
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
37
|
+
export function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {
|
|
38
|
+
const property = descriptor.value;
|
|
39
|
+
if (typeof property === 'function') {
|
|
40
|
+
const method = property?.bind(target);
|
|
41
|
+
if (method) {
|
|
42
|
+
function contextualized<A extends unknown[]>(...args: A) {
|
|
43
|
+
return target.contextualize(() => method.apply(null, args));
|
|
44
|
+
};
|
|
45
|
+
descriptor.value = contextualized as T[K];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
63
48
|
}
|
|
49
|
+
|