
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
@tootallnate/once
Advanced tools
The @tootallnate/once package is designed to ensure a given function can only be called once. It is particularly useful in scenarios where you want to prevent duplicate execution of event handlers or other functions that should only run a single time during the lifecycle of an application.
Ensuring a function is only called once
This feature allows you to wrap any function with `once` to ensure it can only be executed a single time. Subsequent calls to the wrapped function will have no effect, preventing duplicate execution.
const once = require('@tootallnate/once');
function myFunction() {
console.log('This will only be logged once.');
}
const wrappedFunction = once(myFunction);
wrappedFunction(); // Logs: 'This will only be logged once.'
wrappedFunction(); // Does nothing on subsequent calls.
The 'once' package offers similar functionality to @tootallnate/once by ensuring a function can only be executed once. It's a widely used package for handling one-time events or operations. The main difference lies in their implementation and possibly the API surface, but both aim to solve the same problem of preventing duplicate function execution.
Part of the Lodash library, lodash.once provides a utility function to ensure a given function can only be called once. While it offers similar functionality to @tootallnate/once, it comes as part of the larger Lodash utility library, which includes a wide range of functions for different purposes. This might be preferable for projects already using Lodash for other utilities.
Install with npm
:
$ npm install @tootallnate/once
Creates a Promise that waits for event name
to occur on emitter
, and resolves
the promise with an array of the values provided to the event handler. If an
error
event occurs before the event specified by name
, then the Promise is
rejected with the error argument.
import once from '@tootallnate/once';
import { EventEmitter } from 'events';
const emitter = new EventEmitter();
setTimeout(() => {
emitter.emit('foo', 'bar');
}, 100);
const [result] = await once(emitter, 'foo');
console.log({ result });
// { result: 'bar' }
The main feature that this module provides over other "once" implementations is that
the Promise that is returned is strongly typed based on the type of emitter
and the name
of the event. Some examples are shown below.
The process "exit" event contains a single number for exit code:
const [code] = await once(process, 'exit');
// ^ number
A child process "exit" event contains either an exit code or a signal:
const child = spawn('echo', []);
const [code, signal] = await once(child, 'exit');
// ^ number | null
// ^ string | null
A forked child process "message" event is type any
, so you can cast the Promise directly:
const child = fork('file.js');
// With `await`
const [message, _]: [WorkerPayload, unknown] = await once(child, 'message');
// With Promise
const messagePromise: Promise<[WorkerPayload, unknown]> = once(child, 'message');
// Better yet would be to leave it as `any`, and validate the payload
// at runtime with i.e. `ajv` + `json-schema-to-typescript`
If the TypeScript definition does not contain an overload for the specified event name, then the Promise will have type unknown[]
and your code will need to narrow the result manually:
interface CustomEmitter extends EventEmitter {
on(name: 'foo', listener: (a: string, b: number) => void): this;
}
const emitter: CustomEmitter = new EventEmitter();
// "foo" event is a defined overload, so it's properly typed
const fooPromise = once(emitter, 'foo');
// ^ Promise<[a: string, b: number]>
// "bar" event in not a defined overload, so it gets `unknown[]`
const barPromise = once(emitter, 'bar');
// ^ Promise<unknown[]>
signal
- AbortSignal
instance to unbind event handlers before the Promise has been fulfilled.FAQs
Creates a Promise that waits for a single event
The npm package @tootallnate/once receives a total of 22,885,547 weekly downloads. As such, @tootallnate/once popularity was classified as popular.
We found that @tootallnate/once demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.