npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thescottyjam/calling-context

v1.0.1

Published

Provide a context of values for functions to run in. Great alternative to global state.

Downloads

3

Readme

Calling Context

Make cleaner, less verbose function signatures while minimizing the usage of global state by providing your functions with some context.

It's as simple as

let context = require('@thescottyjam/calling-context')

// 1. Create a context
let configCtx = context.create()

// 2. Provide some useful values to your context
let dummyConfig = {verbose: true}
context.provide(configCtx, dummyConfig, () => {
  // Anything inside this callback has access to your context, including addThings()
  addThings(2, 3)
})

// 3. Use the provided context
function addThings(x, y) {
  let config = configCtx.get()
  if (config.verbose) {
    console.log('Adding', x, 'and', y)
  }
  return x + y
}

The common solution to providing configuration to your app, like the verbosity level, is to use global state (which is bad for testing), or to pass the config around your entire code base (which is just ugly).

Calling-Contexts serve the same purpose as React Contexts but in the non-UI world, and exist to help you implicitly pass parameters to nested functions without having to explicitly put them in every function signature.

Use Cases

  • Provide configuration to your whole app without having to rely on global state.
  • Provide request-related parameters (like req, res, a log object, etc) so these don't need to be part of every function signature anymore.
  • It is common for a single function to have a number of dedicated helper functions. In larger scenarios it can be convenient to use a calling context to pass the original parameters and other useful data to the helper functions.
  • Configuring your library to run differently when provided with different contexts (as apposed to a global configuration).
  • Whatever use cases you find

API Reference

callingContext.create() -> ContextHandle

Creates and returns a ContextHandle.

callingContext.provide(contextHandle, value, callback)

Calls the callback. While the callback is executing, the value will be provided to the contextHandle, and can be retrieved anywhere inside the callback.

If the provided contextHandle already has a value provided to it, it will be overwritten for the duration of the callback, then returned to normal.

Whatever is returned from the callback will be returned by provide().

NOTE: async callbacks are not supported.

callingContext.provide(contextHandleToValues, callback)

An alternative signature to provide() that lets you provide values to multiple context handles at once.

For example, if ctx1 and ctx2 are context handles returned by create(), then this will provide values for both:

provide({[ctx1]: value1, [ctx2]: value2}, () => { ... })
// NOTE: {[ctx]: value} is valid syntax.
// It makes the string representation of ctx into the key.

contextHandle.get() -> provided value

Retrieves the value currently being provided to this context handle. If no value is being provided, an error is thrown.

contextHandle.provided() -> boolean

Returns true if a value is currently being provided to this context handle.

Project Source

This project's github repository can be found here. Issues and feature requests are welcome, and can be submitted on github.