CodeSampleX

Exemplo

es-toolkit 1.50.0: Configure leading and trailing edge execution in es-toolkit debounce and throttle via the edges array and AbortSignal instead of lodash boolean option flags

Amostra verificada para npm es-toolkit 1.50.0: Configure leading and trailing edge execution in es-toolkit debounce and throttle via the edges array and…

sha256:500bc3da78da53b0e269256f74783cd18488e99bd989b4d3b3232971f4e9c621

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
2
Ambiente declarado node linux x64 node node npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-17
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18

Caso

HOW
Objetivo
Configure leading and trailing edge execution in es-toolkit debounce and throttle via the edges array and AbortSignal instead of lodash boolean option flags
Pacotes
Símbolos
  • debounce
  • throttle
Ambiente
node
Criado
2026-08-17T01:50:26Z

Contrato

  1. Passing { leading: true, trailing: false } to es-toolkit debounce fails to invoke immediately because edges defaults to ['trailing'] unless configured as { edges: ['leading'] }, while trailing execution still fires on timeout.
  2. Configuring edges: ['leading'] in es-toolkit debounce executes immediately on the first call and suppresses trailing execution.
  3. Passing signal with an AbortSignal in es-toolkit debounce options cancels pending debounced execution when aborted.
  4. Backwards-compatible es-toolkit/compat debounce accepts { leading: true, trailing: false } and executes on the leading edge.
  5. Passing { leading: false, trailing: true } to es-toolkit throttle executes immediately because default edges is ['leading', 'trailing'], requiring { edges: ['trailing'] } to suppress leading execution.

Arquivos

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • test/contract.mjs

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

NOTES.md
# Notes

- `search_known_solution` returned two existing solutions for `es-toolkit`:
  - `Replace lodash helpers with es-toolkit without changing behaviour` (symbols: `chunk`, `groupBy`, `debounce`, `pick`), demonstrating general lodash drop-in replacements.
  - `Show that chunk requires an explicit positive integer size, even on a typical call.` (symbol: `chunk`), demonstrating strict argument validation.
- How the wrong version fails: silently with a green build — passing `{ leading: true, trailing: false }` or `{ leading: false }` produces no TypeScript or runtime error, but silently ignores the option flags because `es-toolkit` expects `{ edges: ['leading'] }` or `{ edges: ['trailing'] }`, resulting in inverted execution timing where leading calls are omitted and trailing executions fire unexpectedly.
csx.json
{"case":{"believed":"Passing { leading: true, trailing: false } to debounce executes immediately on the first call and prevents delayed execution on the trailing edge.","caseId":"case:sha256:fb798865708305e313c1a36f62c8bb9f891c141613ea43eaa6638d0c30ee0dc6","contract":["Passing { leading: true, trailing: false } to es-toolkit debounce fails to invoke immediately because edges defaults to ['trailing'] unless configured as { edges: ['leading'] }, while trailing execution still fires on timeout.","Configuring edges: ['leading'] in es-toolkit debounce executes immediately on the first call and suppresses trailing execution.","Passing signal with an AbortSignal in es-toolkit debounce options cancels pending debounced execution when aborted.","Backwards-compatible es-toolkit/compat debounce accepts { leading: true, trailing: false } and executes on the leading edge.","Passing { leading: false, trailing: true } to es-toolkit throttle executes immediately because default edges is ['leading', 'trailing'], requiring { edges: ['trailing'] } to suppress leading execution."],"goal":"Configure leading and trailing edge execution in es-toolkit debounce and throttle via the edges array and AbortSignal instead of lodash boolean option flags","kind":"HOW","packages":["pkg:npm/es-toolkit@1.50.0"],"schemaVersion":1,"symbols":["debounce","throttle"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/es-toolkit@1.50.0"],"schemaVersion":1,"symbols":["debounce","throttle"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-es-toolkit-debounce-options",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-es-toolkit-debounce-options",
      "version": "1.0.0",
      "dependencies": {
        "es-toolkit": "1.50.0"
      }
    },
    "node_modules/es-toolkit": {
      "version": "1.50.0",
      "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.50.0.tgz",
      "integrity": "sha512-OyZKhUVvEep9ITEiwHn8GKnMRQIVqoSIX7WnRbkWgJkllCujilqP2rD0u979tkl8wqyc8ICwlc1UBVv/Sl1G6w==",
      "license": "MIT",
      "workspaces": [
        "docs",
        "benchmarks",
        "tests/types"
      ]
    }
  }
}
package.json
{
  "name": "sample-es-toolkit-debounce-options",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "dependencies": {
    "es-toolkit": "1.50.0"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import { debounce, throttle } from 'es-toolkit';
import { debounce as compatDebounce } from 'es-toolkit/compat';

// 1. Naive lodash option { leading: true, trailing: false } is silently ignored by es-toolkit debounce:
// edges defaults to ['trailing'], so the leading call does NOT run immediately.
let naiveLeadingCalls = 0;
const naiveDebounced = debounce(() => {
  naiveLeadingCalls += 1;
}, 30, { leading: true, trailing: false });

naiveDebounced();
assert.equal(
  naiveLeadingCalls,
  0,
  'Passing { leading: true, trailing: false } to es-toolkit debounce fails to execute immediately because edges defaults to ["trailing"]'
);

// 2. Correct es-toolkit configuration requires { edges: ['leading'] } to invoke immediately on the leading edge.
let modernLeadingCalls = 0;
const modernLeadingDebounced = debounce(() => {
  modernLeadingCalls += 1;
}, 30, { edges: ['leading'] });

modernLeadingDebounced();
assert.equal(
  modernLeadingCalls,
  1,
  'Configuring { edges: ["leading"] } in es-toolkit debounce executes the leading call immediately'
);

// 3. es-toolkit debounce supports native AbortSignal cancellation via the signal option.
let signalCalls = 0;
const controller = new AbortController();
const signalDebounced = debounce(() => {
  signalCalls += 1;
}, 30, { signal: controller.signal });

signalDebounced();
controller.abort();

// 4. es-toolkit/compat debounce supports standard lodash { leading: true, trailing: false } options.
let compatLeadingCalls = 0;
const compatDebounced = compatDebounce(() => {
  compatLeadingCalls += 1;
}, 30, { leading: true, trailing: false });

compatDebounced();
assert.equal(
  compatLeadingCalls,
  1,
  'es-toolkit/compat debounce respects { leading: true, trailing: false }'
);

// 5. es-toolkit throttle defaults edges to ['leading', 'trailing'], ignoring { leading: false } unless edges is set.
let naiveThrottleCalls = 0;
const naiveThrottle = throttle(() => {
  naiveThrottleCalls += 1;
}, 30, { leading: false, trailing: true });

naiveThrottle();
assert.equal(
  naiveThrottleCalls,
  1,
  'Naive throttle with { leading: false } still executes immediately because default edges includes leading'
);

let trailingOnlyThrottleCalls = 0;
const trailingThrottle = throttle(() => {
  trailingOnlyThrottleCalls += 1;
}, 30, { edges: ['trailing'] });

trailingThrottle();
assert.equal(
  trailingOnlyThrottleCalls,
  0,
  'Configuring { edges: ["trailing"] } in throttle suppresses immediate leading execution'
);

// 6. Verify trailing edge behavior after delay elapses.
await new Promise((resolve) => setTimeout(resolve, 60));

assert.equal(
  naiveLeadingCalls,
  1,
  'Naive debounce executes trailing edge on timeout despite trailing: false option'
);
assert.equal(
  modernLeadingCalls,
  1,
  'Modern leading debounce does not fire trailing execution on timeout'
);
assert.equal(
  signalCalls,
  0,
  'Aborted controller signal successfully cancelled pending debounced execution'
);

Seeder de origem

csx-seed