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>) => voidParameters
| Parameter | Type | Description |
|---|---|---|
fn | T extends Function | The 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 outputExample: 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 listenersExample: 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 createdExample: 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); // 15Example: 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 outputExample: 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 warningGood to know: The wrapped function ignores all parameters on subsequent calls. Only the first invocation receives and uses the arguments. Use
oncefor initialization, setup, or operations that should never repeat.