New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

call-context

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

call-context

A context provision tool.

  • 0.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

This package provides a simple call-stack context provision.

Usage

import { context } from "call-context";

const ctx = context("my context"); // name is optional
const fn = () => {
  console.log(ctx.require()); // we'll provide 42, so this will log 42.
};

const contextProvidedFn = ctx.provide(42)(fn);
contextProvidedFn();

Context's require() will throw an error if the context has not been provided. Name your context at construction time so that you get a meaningful error message.

Providing several contexts

Use provide.

import { provide } from "call-context";

const contextProvidedFn =
  provide(context1, value1)(context2, value2)(context3, value3)(fn);

Optional provision

Use get to consume the context optionally. This returns undefined if the context has not been provided.

import { context } from "call-context";

const ctx = context("my context"); // name is optional
const fn = () => {
  console.log(ctx.get() ?? 42); // This will log 42 without provision.
};
fn();

Use cases

Using call-stack context allows dependencies to be provided deep in the call hierarchy. This idea is inspired from React's conext, which allows dependencies to be provided deep in the component hierarchy. Context allows for more liberal use of dependency inversion, because it reduces the amount of pumbling required to send the dependencies to the use site.

The alternative, i.e. passing dependencies down the call hierarchy, has better traceability.

Keywords

FAQs

Package last updated on 11 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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