Giter VIP home page Giter VIP logo

eldc's Introduction

Event-Loop Durable Context

Simple, seamless user defined context that NEVER gets lost across event loop callbacks.

This is an alpha version, although quite capable. It's meant to demonstrate feasability and provide insight of performance impact if its approach.

Its first goal is to support the npm package new-newjs.

It can also be used as a robust 'continuation-local-storage' replacement.

It is being developed on node 10.11, but would likely work on previous versions, depending on the builtins called (for the moment). It would be targeted to work on node 6+ at release.

Examples use 'modern' javascript

This example demonstrates basic use and function.

import defineContext from 'eldc'
const log = console.log

// define a type of context that can be provided and consumed,
// specifying its contextual properties and their defaults.
// the type can simply be separately imported by providers
// and consumers as needed; literally 'no strings attached'
const MyContext = defineContext({givesMeaningTo: 'Zero'})

// the context at 'loop-global' level
setTimeout(_ => log(MyContext.givesMeaningTo === 'Zero'), 300)

// instantiate an instance of context, provide its value,
// consume in a later callback.
MyContext(() => {
  MyContext.givesMeaningTo = 'One'
  setTimeout(_ => log(MyContext.givesMeaningTo === 'One'), 200)
})

// instantiate an instance of context and provide its value.
// reuse context across multiple executions
const runInCtx = MyContext({givesMeaningTo: 'Two'})

runInCtx(_ => 
  setTimeout(_ => log(MyContext.givesMeaningTo === 'Two'), 100)
)

//... asyncronously
log('Two' === await runInCtx(async() => {
  await new Promise(_ => setTimeout(_, 10))
  return MyContext.givesMeaningTo
}))

my-context.js

import defineContext from 'eldc'

const MyContext = defineContext({contextualProperty: 'hello'})
export default MyContext

provider.js

import MyContext from './my-context'
import Consumer from './consumer'

MyContext(() => {
  true === (MyContext.contextualProperty === 'hello')
  con = new Consumer()
	

  MyContext.contextualProperty = 'world'
  true === (MyContext.contextualProperty === 'world')

  MyContext(() => {
    const con = new Consumer()
    true === (con.fromContext === 'hello')
  })
  true === (MyContext.contextualProperty === 'world')
  true === (con.fromContext === 'world')
})

eldc's People

Contributors

papercuptech avatar

Watchers

 avatar

eldc's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.