@marceloraineri/async-context 1.0.0
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 +1 -0
- package/index.js +31 -0
- package/index.ts +38 -0
- package/package.json +23 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# SyncContext
|
package/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Context = void 0;
|
|
4
|
+
var node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
+
var Context = /** @class */ (function () {
|
|
6
|
+
function Context() {
|
|
7
|
+
Context.asyncLocalStorageInstance = new node_async_hooks_1.AsyncLocalStorage();
|
|
8
|
+
}
|
|
9
|
+
Context.getInstance = function () {
|
|
10
|
+
if (!Context.asyncLocalStorageInstance) {
|
|
11
|
+
new Context();
|
|
12
|
+
}
|
|
13
|
+
return Context.asyncLocalStorageInstance;
|
|
14
|
+
};
|
|
15
|
+
Context.addValue = function (key, value) {
|
|
16
|
+
var contextObject = Context.getInstance().getStore();
|
|
17
|
+
if (!contextObject)
|
|
18
|
+
throw new Error('Nenhum contexto ativo encontrado. Use Context.getInstance().run().');
|
|
19
|
+
contextObject[key] = value;
|
|
20
|
+
return contextObject;
|
|
21
|
+
};
|
|
22
|
+
Context.addObjectValue = function (object) {
|
|
23
|
+
var contextObject = Context.getInstance().getStore();
|
|
24
|
+
if (!contextObject)
|
|
25
|
+
throw new Error('Nenhum contexto ativo encontrado. Use Context.getInstance().run().');
|
|
26
|
+
var merged = Object.assign(contextObject, object);
|
|
27
|
+
return merged;
|
|
28
|
+
};
|
|
29
|
+
return Context;
|
|
30
|
+
}());
|
|
31
|
+
exports.Context = Context;
|
package/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
type objectStorageType = {
|
|
4
|
+
[key: string] : unknown
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class Context {
|
|
8
|
+
static asyncLocalStorageInstance: AsyncLocalStorage<unknown>;
|
|
9
|
+
|
|
10
|
+
private constructor() {
|
|
11
|
+
Context.asyncLocalStorageInstance = new AsyncLocalStorage();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static getInstance() {
|
|
15
|
+
if (!Context.asyncLocalStorageInstance) {
|
|
16
|
+
new Context();
|
|
17
|
+
}
|
|
18
|
+
return Context.asyncLocalStorageInstance;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static addValue(key: string, value: any) {
|
|
22
|
+
const contextObject = Context.getInstance().getStore() as Record<string, any>;
|
|
23
|
+
if (!contextObject)
|
|
24
|
+
throw new Error('Nenhum contexto ativo encontrado. Use Context.getInstance().run().');
|
|
25
|
+
|
|
26
|
+
contextObject[key] = value;
|
|
27
|
+
return contextObject;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static addObjectValue(object: Record<string, any>) {
|
|
31
|
+
const contextObject = Context.getInstance().getStore();
|
|
32
|
+
if (!contextObject)
|
|
33
|
+
throw new Error('Nenhum contexto ativo encontrado. Use Context.getInstance().run().');
|
|
34
|
+
|
|
35
|
+
const merged = Object.assign(contextObject, object);
|
|
36
|
+
return merged;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@marceloraineri/async-context",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/ohraineri/AsyncContext.git"
|
|
12
|
+
},
|
|
13
|
+
"author": "Marcelo Raineri",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/ohraineri/AsyncContext/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/ohraineri/AsyncContext#readme",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^24.10.1",
|
|
21
|
+
"typescript": "^5.9.3"
|
|
22
|
+
}
|
|
23
|
+
}
|