nanocontext
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.
Install
$ npm install nanocontext
Usage
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
API
const ctx = nanocontext(source, options)
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.
ctx.root
Return the root context.
Alternative: getRoot(ctx)
ctx.parent
Return the parent context.
Alternative: getParent(ctx)
ctx.clone(source = ctx)
Return a new context inherit from the current context (ctx
) or from a new object.
const child = ctx.clone()
Alternative: getClone(ctx, source)
Issues
Contributing
License
MIT © A GEUT project