@fangxq/async-context
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -8,4 +8,5 @@ export const asyncContext: { | ||
clearAll(): void | ||
use<T>(token: string, fn: () => Promise<T>): Promise<T> | ||
useAsync<T>(token: string, fn: () => Promise<T>): Promise<T> | ||
useSync<T>(token: string, fn: () => T): T | ||
getCurrentContext(): string | ||
} |
20
index.js
@@ -37,3 +37,3 @@ const cloneDeep = require('lodash').cloneDeep | ||
function getPrivateValues() { | ||
const token = store.getStore() | ||
const token = store.getStore() ?? syncToken | ||
if (!self.privateValues[token]) { | ||
@@ -59,5 +59,7 @@ self.privateValues[token] = cloneDeep(self.defaultPrivateValues) | ||
let syncToken = '' | ||
module.exports.asyncContext = { | ||
definePrivate(path, proName, defaultValue) { | ||
if(!dataContext[path]) dataContext[path] = new contextObject() | ||
if (!dataContext[path]) dataContext[path] = new contextObject() | ||
@@ -67,3 +69,3 @@ dataContext[path].definePrivateValue(proName, defaultValue) | ||
definePublic(path, proName, defaultValue) { | ||
if(!dataContext[path]) dataContext[path] = new contextObject() | ||
if (!dataContext[path]) dataContext[path] = new contextObject() | ||
@@ -88,3 +90,3 @@ dataContext[path].definePublicValue(proName, defaultValue) | ||
}, | ||
use(token, fn) { | ||
useAsync(token, fn) { | ||
return new Promise((resolve, reject) => { | ||
@@ -100,5 +102,13 @@ store.run(token, async () => { | ||
}, | ||
useSync(token, fn) { | ||
try { | ||
syncToken = token | ||
return fn() | ||
} finally { | ||
syncToken = '' | ||
} | ||
}, | ||
getCurrentContext() { | ||
return store.getStore() | ||
return store.getStore() ?? syncToken | ||
} | ||
} |
{ | ||
"name": "@fangxq/async-context", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3842
106