Skip to content
forked from OPIUM66/ctxjs

a simple context manager for javascript

Notifications You must be signed in to change notification settings

arefirasti/ctxjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Context Manager

This project demonstrates a simple context management system using JavaScript. It allows you to create contexts, bind values to keys within those contexts, and retrieve those values within a handler function.

Overview

The main components of this example are:

  • Context Creation: You can create a new context using Context.createContext().
  • Binding Values: Values can be bound to keys in the context using the bind method.
  • Retrieving Values: Values can be retrieved using the get method within a handler function that has access to the context.
  • Context Isolation: The context is isolated to the handler, meaning that accessing the context outside of the handler will return undefined.

How to Use

  1. Import the Context Module:

    import Context from 'the-context-js';
    // OR ||
    const { default: Context } = require("the-context-js");
  2. Create a Context: Use Context.createContext() to create a new context instance.

    const theContext = Context.createContext();
  3. Bind Values: Use the bind method to associate keys with values in the context.

    theContext.bind("key1", "value1");
    theContext.bind("key2", "value2");
  4. Define a Handler: Create a function that retrieves values from the context.

    const theHandler = function() {
    const theContext = Context.getContext();
    const key2 = theContext.get("key2");
    console.log(key2); // Outputs: value2
    };
  5. Create a New Handler with Context: Use containerWithContext to create a new handler that has access to the context.

    const newHandler = theContext.containerWithContext(theHandler);
    newHandler(); // Call the handler with context
  6. Call the Handler Without Context: If you call the handler directly without context, it will not have access to the bound values.

    theHandler(); // Outputs: undefined
  7. Global Context Access: Attempting to access the context globally will return undefined.

    const anotherContext = Context.getContext(); // This will be undefined
    console.log('GLOBAL: anotherContext', anotherContext); // Outputs: undefined
  8. Access To Value By Property: Attempting to access the context globally will return undefined.

    const context = Context.createContext(); 
    context.bind('key1', 'value1'); 
    console.log(context.getByProperty.key1);

Benchmark

  • Run a benchmark: node --expose-gc benchmark.js
Last Reports:
Context.createContext: 1.625 ms, 1000 iterations, 615498.25 ops, Total Memory: 42544.00 KB
Context.getContext: 0.581 ms, 1000 iterations, 1720282.13 ops, Total Memory: 43412.88 KB
Context.bind: 0.845 ms, 1000 iterations, 1183431.95 ops, Total Memory: 43998.81 KB
Context.get: 0.253 ms, 1000 iterations, 3955696.20 ops, Total Memory: 44259.15 KB
Context.unbind: 0.342 ms, 1000 iterations, 2924831.82 ops, Total Memory: 44528.48 KB

Debugging

  • Monitor Contexts: node debug.js
Commands:

1. create sampleContext --> ID: 18
2. bind key value 18
3. get all
4. get 18

About

a simple context manager for javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%