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

@loke/context

v0.0.1

Published

A typescript version of the go context package.

Downloads

1,512

Readme

@loke/context

A typescript version of the go context package.

Installation

npm install @loke/context

Usage

import * as context from "@loke/context";
import { Context } from "@loke/context";

async function getUser(cxt: Context, id: string): Promise<unknown> {
  const { ctx, abort } = context.withTimeout(cxt, 1000);

  try {
    const res = await fetch(`/users/${id}`, { signal: ctx.signal });

    if (res.ok) {
      throw new Error("not ok");
    }

    return await res.json();
  } finally {
    abort();
  }
}

API

Context

type Context = {
  readonly signal?: AbortSignal;
  readonly deadline?: number;
};

Abortable

type Abortable = {
  readonly ctx: Context;
  readonly abort: () => void;
};

background

const background: Context;

TODO

const TODO: Context;

withAbort(parent: Context): Abortable;

  • parent - The parent context to extend.

Extends the parent context with an abortable signal. The signal is aborted when the returned abort function is called or when the parent context is aborted.

The returned about function MUST be called to avoid memory leaks.

withTimeout(parent: Context, duration: number): Abortable;

  • parent - The parent context to extend.
  • duration - The timeout in milliseconds.

Extends the parent context with an abortable signal. The signal is aborted when the returned abort function is called, when the parent context is aborted or when the timeout is reached.

The returned about function MUST be called to avoid memory leaks.

withDeadline(parent: Context, deadline: Date | number): Abortable;

  • parent - The parent context to extend.
  • deadline - The deadline as a date or a timestamp in milliseconds.

Extends the parent context with an abortable signal. The signal is aborted when the returned abort function is called, when the parent context is aborted or when the deadline is reached.

The returned about function MUST be called to avoid memory leaks.

withValues(parent: Context, values: Record<symbol | string, unknown>): Context;

  • parent - The parent context to extend.
  • values - The values to add to the context.

Extends the parent context with a set of values. To help with type safety the keys should be symbols, and accessed using custom getters. You can also use a custom setter to ensure that the values are of the correct type, this may be overkill if you only set the values once.

Example

const localeKey = Symbol("locale");

export function withLocale(parent: Context, locale: string): Context {
  return context.withValues(parent, {
    [localeKey]: locale,
  });
}

export function getLocale(ctx: Context): string {
  if (localeKey in ctx) {
    return ctx[localeKey];
  } else {
    return "en";
  }
}

Well known values API

requestIdKey

const requestIdKey: symbol;

Example

const ctx = context.withValues(context.background, {
  [context.requestIdKey]: "1234",
});

getRequestId(ctx: Context): string | undefined;

Example

const requestId = getRequestId(ctx);