Standard Toolkit
Packages@accelint/coreUtility

once

Ensure a function is only called once

Usage

import { once } from '@accelint/core';

let count = 0;
const increment = () => { count++; };
const incrementOnce = once(increment);

incrementOnce(); // count = 1
incrementOnce(); // count = 1 (not called again)
incrementOnce(); // count = 1 (still not called)

Reference

function once<T extends (...args: any[]) => any>(
  fn: T
): (...args: Parameters<T>) => void

Parameters

ParameterTypeDescription
fnT extends FunctionThe function to call once

Returns

Returns a wrapped function that executes only on the first call. Subsequent calls do nothing.

Type Parameters

  • T - The type of the function to wrap

Examples

Example: Initialization function

import { once } from '@accelint/core';

const initializeDatabase = once(() => {
  console.log('Connecting to database...');
  // Expensive initialization
});

// First call executes
initializeDatabase(); // Logs: "Connecting to database..."

// Subsequent calls do nothing
initializeDatabase(); // No output
initializeDatabase(); // No output

Example: Event listener setup

import { once } from '@accelint/core';

const setupEventListeners = once(() => {
  window.addEventListener('resize', handleResize);
  window.addEventListener('scroll', handleScroll);
  console.log('Event listeners attached');
});

// Safe to call multiple times
setupEventListeners(); // Listeners attached
setupEventListeners(); // No duplicate listeners

Example: Singleton pattern

import { once } from '@accelint/core';

class Service {
  constructor() {
    console.log('Service initialized');
  }
}

let instance: Service;
const createInstance = once(() => {
  instance = new Service();
});

createInstance(); // Logs: "Service initialized"
createInstance(); // No output, instance already created

Example: With parameters

import { once } from '@accelint/core';

let globalValue = 10;

const addToGlobal = (n: number) => {
  globalValue += n;
  console.log(`Added ${n}, total: ${globalValue}`);
};

const addOnce = once(addToGlobal);

addOnce(5);  // Logs: "Added 5, total: 15"
addOnce(10); // No output (not called)
addOnce(20); // No output (not called)

console.log(globalValue); // 15

Example: Cleanup on unmount

import { once } from '@accelint/core';

function useEffect(cleanup: () => void) {
  // Ensure cleanup only runs once
  const cleanupOnce = once(cleanup);
  
  return () => {
    cleanupOnce();
  };
}

const unmount = useEffect(() => {
  console.log('Cleaning up resources');
  // Remove event listeners, close connections, etc.
});

unmount(); // Logs: "Cleaning up resources"
unmount(); // No output

Example: Logging warning once

import { once } from '@accelint/core';

const warnDeprecated = once((feature: string) => {
  console.warn(`DEPRECATED: ${feature} will be removed in the next version`);
});

function oldApi() {
  warnDeprecated('oldApi');
  // Implementation...
}

oldApi(); // Logs warning
oldApi(); // No warning
oldApi(); // No warning

Good to know: The wrapped function ignores all parameters on subsequent calls. Only the first invocation receives and uses the arguments. Use once for initialization, setup, or operations that should never repeat.

  • tap - Execute a function but return the original value
  • noop - No-operation function
  • identity - Returns its input unchanged

On this page