@fangxq/async-context
Advanced tools
Comparing version 0.0.5 to 1.0.0
export const asyncContext: { | ||
definePrivate(path: string, proName: string, defaultValue: any): void | ||
definePublic(path: string, proName: string, defaultValue: any): void | ||
definePrivate(path: string, ctr:Function): void | ||
definePublic(path: string, ctr:Function): void | ||
get(path: string, proName: string): any | ||
@@ -5,0 +5,0 @@ set(path: string, proName: string, value: any): void |
97
index.js
@@ -1,2 +0,1 @@ | ||
const cloneDeep = require('lodash').cloneDeep | ||
const AsyncLocalStorage = require('async_hooks').AsyncLocalStorage | ||
@@ -6,82 +5,50 @@ | ||
class contextObject { | ||
defaultPrivateValues = {} // : { [key in string]: any } | ||
defaultPublicValues = {} //: { [key in string]: any } | ||
const publicConstructor = {} | ||
const publicContext = {} | ||
publicValues = {} //: { [key in string]: any } | ||
privateValues = {} //: { [key in string]: { [key in string]: any } } | ||
const privateConstructor = {} | ||
const privateContext = {} | ||
clearData() { | ||
this.publicValues = cloneDeep(this.defaultPublicValues) | ||
this.privateValues = {} | ||
} | ||
let syncToken = '' | ||
clearPrivateData(token) { | ||
delete this.privateValues[token] | ||
} | ||
function getCurrentContext() { | ||
return store.getStore() ?? syncToken | ||
} | ||
definePublicValue(proName, defaultValue) { | ||
this.defaultPublicValues[proName] = defaultValue | ||
this.publicValues[proName] = defaultValue | ||
Object.defineProperty(this, proName, { | ||
get: () => { | ||
return this.publicValues[proName] | ||
}, | ||
set: (nv) => { | ||
this.publicValues[proName] = nv | ||
} | ||
}) | ||
} | ||
definePrivateValue(proName, defaultValue) { | ||
const self = this | ||
function getPrivateValues() { | ||
const token = store.getStore() ?? syncToken | ||
if (!self.privateValues[token]) { | ||
self.privateValues[token] = cloneDeep(self.defaultPrivateValues) | ||
} | ||
return self.privateValues[token] | ||
} | ||
function getModule(path) { | ||
if (!publicConstructor[path] && !privateConstructor[path]) throw '找不到模块:' + path | ||
this.defaultPrivateValues[proName] = defaultValue | ||
Object.defineProperty(this, proName, { | ||
get: () => { | ||
return getPrivateValues()[proName] | ||
}, | ||
set: (nv) => { | ||
getPrivateValues()[proName] = nv | ||
} | ||
}) | ||
if (publicConstructor[path]) { | ||
if (!publicContext[path]) publicContext[path] = new publicConstructor[path]() | ||
return publicContext[path] | ||
} else { | ||
const token = getCurrentContext() | ||
if (!privateContext[token]) privateContext[token] = {} | ||
if (!privateContext[token][path]) privateContext[token][path] = new privateConstructor[path]() | ||
return privateContext[token][path] | ||
} | ||
} | ||
const dataContext = {} | ||
let syncToken = '' | ||
module.exports.asyncContext = { | ||
definePrivate(path, proName, defaultValue) { | ||
if (!dataContext[path]) dataContext[path] = new contextObject() | ||
dataContext[path].definePrivateValue(proName, defaultValue) | ||
definePrivate(path, ctr) { | ||
privateConstructor[path] = ctr | ||
}, | ||
definePublic(path, proName, defaultValue) { | ||
if (!dataContext[path]) dataContext[path] = new contextObject() | ||
dataContext[path].definePublicValue(proName, defaultValue) | ||
definePublic(path, ctr) { | ||
publicConstructor[path] = ctr | ||
}, | ||
get(path, proName) { | ||
return dataContext[path][proName] | ||
return getModule(path)[proName] | ||
}, | ||
set(path, proName, value) { | ||
dataContext[path][proName] = value | ||
getModule(path)[proName] = value | ||
}, | ||
clear(token) { | ||
Object.keys(dataContext).forEach(k => { | ||
dataContext[k].clearPrivateData(token) | ||
}) | ||
delete privateContext[token] | ||
}, | ||
clearAll() { | ||
Object.keys(dataContext).forEach(k => { | ||
dataContext[k].clearData() | ||
}) | ||
privateContext = {} | ||
publicContext = {} | ||
}, | ||
@@ -107,5 +74,3 @@ useAsync(token, fn) { | ||
}, | ||
getCurrentContext() { | ||
return store.getStore() ?? syncToken | ||
} | ||
getCurrentContext | ||
} |
{ | ||
"name": "@fangxq/async-context", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
2521
72