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

callbackpreserver

v0.1.1

Published

Reusable function contexts

Downloads

17

Readme

CallbackPreserver

A utility class for preserving JavaScript callback function context so that the same callback can be invoked multiple times.

Installation

npm install --save callbackpreserver

Usage

  1. Instantiate CallbackPreserver.
import CallbackPreserver from 'callbackpreserver';
const preserver = new CallbackPreserver();
  1. Use CallbackPreserver -> preserve() in place of a callback function to hold callback context for later.
const methodThatAcceptsCallback = (callback) => {
  const context = "temporary context";
  callback(context);
}
methodThatAcceptsCallback(preserver.preserve);
  1. Use CallbackPreserver -> run() to invoke the preserved original callback on demand.
// invoke preserved callback
preserver.run((context) => {
  console.log(context); // "temporary context"
});

// invoke preserved callback again - idempotence
preserver.run((context) => {
  console.log(context); // "temporary context"
});

Use-Cases

CallbackPreserver provides a simple mechanism for code cleanliness. However, this pattern shines when used in conjuction with certain APIs that require batched context-aware callbacks. An example of this is Microsoft's Excel.run API used by O365 Addins to interact with Excel documents - https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-core-concepts.

Typical usage of Excel.run:

Excel.run(function (context) {
  // load the selected range
  const selectedRange = context.workbook.getSelectedRange();
  selectedRange.load('address');
  context.sync()
    .then(function () {
      console.log('The selected range is: ' + selectedRange.address);
  });

  // load sheet names
  var sheets = context.workbook.worksheets;
  sheets.load("items/name");
  return context.sync()
    .then(function () {
      for (var i in sheets.items) {
        console.log(sheets.items[i].name);
      }
    });
})

In the above example, the callback accepted by Excel.run is invoked with a proxy context object which is run asynchronously as a batch of operations. This makes it difficult to run your own JavaScript alongside Excel operations. This is where CallbackPreserver can come in.

const preserver = new CallbackPreserver();
Excel.run(preserver.preserve);
await preserver.run((context) => // perform Excel action)
// run your own code
await preserver.run((context) => // perform Excel action)
// run your own code
preserver.close() // allow context to be garbage collected

With CallbackPreserver the context can be reused across multiple invocations. In this example Excel operations that share a context can be pushed one at a time instead of in a batch.

API Interface

interface ICallbackPreserver {
  preserve: (...args: any[]) => void;
  close: () => void;
  run: (
    callable: (...args: any[]) => Promise<any>,
  ) => Promise<any> | Promise<never>;
}

Notes

CallbackPreserver is an experimental code cleanup mechanism that utilizes ES6 generators under the hood. Use at your own risk!