Small module to create a stateful context tree object to extend through decorators.
It provides a way to build a context object that you can share, extend and be sure that only the root can modify it.
Features:
- Creates light secure copies of the root context to share.
$ npm install nanocontext
import { nanocontext } from 'nanocontext'
const ctxRoot = nanocontext({ name: 'alice' })
ctxRoot.hello = () => 'hello from root'
console.log(ctxRoot.hello()) // hello from root
const ctxChild = ctxRoot.clone()
console.log(ctxChild.hello()) // hello from root (by inheritance)
ctxChild.hello = () => 'hello from child'
console.log(ctxRoot.hello()) // hello from root (it doesn't change)
console.log(ctxChild.hello()) // hello from child
It creates a new nanocontext instance based on an initial object.
source = {}
: The initial source context object.
Options can be:
builtInMethods = true
: Defines a set of built-in methods to work with the context. You can disabled and access to these methods from generic functions.parent = null
: Parent object to inherit.
Return the root context.
Alternative: getRoot(ctx)
Return the parent context.
Alternative: getParent(ctx)
Return a new context inherit from the current context (ctx
) or from a new object.
const child = ctx.clone()
Alternative: getClone(ctx, source)
๐ If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.
๐ฅ Ideas and contributions to the project are welcome. You must follow this guideline.
MIT ยฉ A GEUT project