Function: createWarnOnce()
API / @xmachines/core / utils / createWarnOnce
function createWarnOnce(): (anchor, message) => void;Defined in: packages/core/src/utils/warn-once.ts:27
Builds a function that writes one warning for each anchor object, and no more.
A diagnostic that a hot path finds — a stale field of a static config, read on every event — floods the console at the rate of that path. The key of this function is the identity of an object that stays the same between two calls, such as the config itself. Each anchor therefore reports one time, and a new object reports again.
The function is a factory, and it keeps its memory in a WeakSet of its own. A module
that calls it one time holds one memory for its diagnostics, and an anchor that nothing
else holds is free for the garbage collector.
Returns
A function of (anchor, message). It calls console.warn(message) for the
first call with an anchor, and it does nothing for every later call with that anchor.
(anchor, message) => void
Example
import { createWarnOnce } from "@xmachines/core/utils";
const warnOnce = createWarnOnce();
function derive(config: Config): void { if (config.legacy) warnOnce(config, "config.legacy has no effect");}