Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fangxq/async-context

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fangxq/async-context - npm Package Compare versions

Comparing version 0.0.5 to 1.0.0

4

index.d.ts
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

@@ -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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc