CodeSampleX

サンプル

@tanstack/query-core 5.90.20: MutationCache

検証済みサンプル — npm @tanstack/query-core 5.90.20: MutationCache. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: assert MutationCache constructs an…

sha256:576e620254d3a7be77f740aaacde0038660e6aef005e49ba2836949b502abb80

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-03

ケース

HOW
ゴール
verify pkg:npm/%40tanstack/query-core@5.90.20
パッケージ
シンボル
  • MutationCache
作成日
2026-09-03T08:36:29Z

コントラクト

  1. assert MutationCache constructs an instance with cache lifecycle methods
  2. assert MutationCache find retrieves an active mutation by exact mutationKey or returns undefined
  3. assert MutationCache findAll matches mutations by key prefix or filter predicate
  4. assert MutationCache subscribe notifies listeners of mutation lifecycle events: added, updated, removed
  5. assert MutationCache remove removes a mutation and clear removes all cached mutations
  6. assert MutationCache unsubscribe stops listener notifications on subsequent cache events

ファイル

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:npm/%40tanstack/query-core@5.90.20
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40tanstack/query-core@5.90.20

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:eae957d8cfd8354e9f0608498dfaee5faa96b0311aee524cd86dd04ad1c9f17e","contract":["assert MutationCache constructs an instance with cache lifecycle methods","assert MutationCache find retrieves an active mutation by exact mutationKey or returns undefined","assert MutationCache findAll matches mutations by key prefix or filter predicate","assert MutationCache subscribe notifies listeners of mutation lifecycle events: added, updated, removed","assert MutationCache remove removes a mutation and clear removes all cached mutations","assert MutationCache unsubscribe stops listener notifications on subsequent cache events"],"goal":"verify pkg:npm/%40tanstack/query-core@5.90.20","kind":"HOW","packages":["pkg:npm/%40tanstack/query-core@5.90.20"],"schemaVersion":1,"symbols":["MutationCache"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40tanstack/query-core@5.90.20"],"schemaVersion":1,"subject":"pkg:npm/%40tanstack/query-core@5.90.20","symbols":["MutationCache"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { MutationCache, QueryClient } from '@tanstack/query-core';

/**
 * Creates a new MutationCache instance with optional configuration.
 *
 * @param {object} [config]
 * @returns {MutationCache}
 */
export function createMutationCache(config) {
  return new MutationCache(config);
}

/**
 * Subscribes a listener to MutationCache lifecycle events.
 *
 * @param {MutationCache} cache
 * @param {Function} listener
 * @returns {Function} Unsubscribe function
 */
export function subscribeMutationCache(cache, listener) {
  return cache.subscribe(listener);
}

/**
 * Finds a mutation within a MutationCache instance.
 *
 * @param {MutationCache} cache
 * @param {object} filters
 * @returns {object | undefined}
 */
export function findMutation(cache, filters) {
  return cache.find(filters);
}

export { MutationCache, QueryClient };
export default MutationCache;
package-lock.json
{
  "name": "sample-query-core-verify",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-query-core-verify",
      "version": "1.0.0",
      "dependencies": {
        "@tanstack/query-core": "5.90.20"
      }
    },
    "node_modules/@tanstack/query-core": {
      "version": "5.90.20",
      "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.90.20.tgz",
      "integrity": "sha512-OMD2HLpNouXEfZJWcKeVKUgQ5n+n3A2JFmBaScpNDUqSrQSjiveC7dKMe53uJUg1nDG16ttFPz2xfilz6i2uVg==",
      "license": "MIT",
      "funding": {
        "type": "github",
        "url": "https://github.com/sponsors/tannerlinsley"
      }
    }
  }
}
package.json
{
  "name": "sample-query-core-verify",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "dependencies": {
    "@tanstack/query-core": "5.90.20"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40tanstack/query-core@5.90.20",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40tanstack/query-core@5.90.20"
  ],
  "symbols": [
    "MutationCache"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { MutationCache, QueryClient, createMutationCache, subscribeMutationCache, findMutation } from '../index.mjs';

// 1. assert MutationCache constructs an instance with cache lifecycle methods
{
  const cache = createMutationCache();
  assert.ok(cache instanceof MutationCache);
  assert.strictEqual(typeof cache.build, 'function');
  assert.strictEqual(typeof cache.find, 'function');
  assert.strictEqual(typeof cache.findAll, 'function');
  assert.strictEqual(typeof cache.subscribe, 'function');
  assert.strictEqual(typeof cache.clear, 'function');
  assert.strictEqual(typeof cache.getAll, 'function');
  assert.strictEqual(typeof cache.remove, 'function');
}

// 2. assert MutationCache find retrieves an active mutation by exact mutationKey or returns undefined
{
  const cache = new MutationCache();
  const client = new QueryClient({ mutationCache: cache });

  const mut = cache.build(client, {
    mutationKey: ['users', 1],
    mutationFn: async (vars) => vars,
  });

  const found = findMutation(cache, { mutationKey: ['users', 1], exact: true });
  assert.ok(found);
  assert.strictEqual(found, mut);

  const notFound = findMutation(cache, { mutationKey: ['users', 2], exact: true });
  assert.strictEqual(notFound, undefined);
}

// 3. assert MutationCache findAll matches mutations by key prefix or filter predicate
{
  const cache = new MutationCache();
  const client = new QueryClient({ mutationCache: cache });

  cache.build(client, { mutationKey: ['todos', 1] });
  cache.build(client, { mutationKey: ['todos', 2] });
  const postsMut = cache.build(client, { mutationKey: ['posts', 1] });

  const todos = cache.findAll({ mutationKey: ['todos'] });
  assert.strictEqual(todos.length, 2);

  const matchedKeys = todos.map(m => m.options.mutationKey).sort();
  assert.deepStrictEqual(matchedKeys, [['todos', 1], ['todos', 2]]);

  const posts = cache.findAll({ predicate: (m) => m.options.mutationKey?.[0] === 'posts' });
  assert.strictEqual(posts.length, 1);
  assert.strictEqual(posts[0], postsMut);
}

// 4. assert MutationCache subscribe notifies listeners of mutation lifecycle events: added, updated, removed
{
  const cache = new MutationCache();
  const client = new QueryClient({ mutationCache: cache });

  const eventTypes = [];
  const unsubscribe = subscribeMutationCache(cache, (event) => {
    eventTypes.push(event.type);
  });

  const mut = cache.build(client, {
    mutationKey: ['counter'],
    mutationFn: async (val) => val,
  });

  await mut.execute(1);
  cache.remove(mut);

  assert.ok(eventTypes.includes('added'));
  assert.ok(eventTypes.includes('updated'));
  assert.ok(eventTypes.includes('removed'));

  unsubscribe();
}

// 5. assert MutationCache remove removes a mutation and clear removes all cached mutations
{
  const cache = new MutationCache();
  const client = new QueryClient({ mutationCache: cache });

  const mut1 = cache.build(client, { mutationKey: ['item', 1] });
  cache.build(client, { mutationKey: ['item', 2] });
  cache.build(client, { mutationKey: ['item', 3] });

  assert.strictEqual(cache.getAll().length, 3);

  cache.remove(mut1);
  assert.strictEqual(cache.getAll().length, 2);
  assert.strictEqual(findMutation(cache, { mutationKey: ['item', 1], exact: true }), undefined);

  cache.clear();
  assert.strictEqual(cache.getAll().length, 0);
}

// 6. assert MutationCache unsubscribe stops listener notifications on subsequent cache events
{
  const cache = new MutationCache();
  const client = new QueryClient({ mutationCache: cache });

  let notificationCount = 0;
  const unsubscribe = subscribeMutationCache(cache, () => {
    notificationCount++;
  });

  cache.build(client, { mutationKey: ['test1'] });
  assert.ok(notificationCount > 0);

  const countBefore = notificationCount;
  unsubscribe();

  cache.build(client, { mutationKey: ['test2'] });
  assert.strictEqual(notificationCount, countBefore);
}

console.log('All MutationCache contract assertions passed.');

オリジンシーダー

匿名