CodeSampleX

サンプル

scheduler 0.25.0: unstable_ImmediatePriority

検証済みサンプル — npm scheduler 0.25.0: unstable_ImmediatePriority. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: unstable_ImmediatePriority is…

sha256:9a609dc2d925debc93aecf53d37234124e9285ae22968bbe7f35d57f1748b796

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

実行証拠

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

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

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-21

ケース

HOW
ゴール
verify scheduler.unstable_ImmediatePriority in pkg:npm/scheduler@0.25.0
パッケージ
シンボル
  • scheduler.unstable_ImmediatePriority
環境
node 22.23.2
作成日
2026-08-21T02:45:29Z

コントラクト

  1. unstable_ImmediatePriority is exported as a priority constant equal to 1
  2. unstable_runWithPriority executes a callback synchronously at unstable_ImmediatePriority level and restores previous priority
  3. unstable_wrapCallback binds a function to unstable_ImmediatePriority when wrapped within an immediate priority scope
  4. unstable_scheduleCallback schedules tasks with unstable_ImmediatePriority to execute ahead of lower priority tasks
  5. unstable_cancelCallback cancels a task scheduled with unstable_ImmediatePriority

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 scheduler.unstable_ImmediatePriority in pkg:npm/scheduler@0.25.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/scheduler@0.25.0
Demonstrate these symbols/APIs:
  - scheduler.unstable_ImmediatePriority

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:5b48f332d3edb7a3f7dfbaf37d05b62187945a898928766e3eed48c227160d2d","contract":["unstable_ImmediatePriority is exported as a priority constant equal to 1","unstable_runWithPriority executes a callback synchronously at unstable_ImmediatePriority level and restores previous priority","unstable_wrapCallback binds a function to unstable_ImmediatePriority when wrapped within an immediate priority scope","unstable_scheduleCallback schedules tasks with unstable_ImmediatePriority to execute ahead of lower priority tasks","unstable_cancelCallback cancels a task scheduled with unstable_ImmediatePriority"],"goal":"verify scheduler.unstable_ImmediatePriority in pkg:npm/scheduler@0.25.0","kind":"HOW","packages":["pkg:npm/scheduler@0.25.0"],"schemaVersion":1,"symbols":["scheduler.unstable_ImmediatePriority"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/scheduler@0.25.0"],"schemaVersion":1,"subject":"pkg:npm/scheduler@0.25.0","symbols":["scheduler.unstable_ImmediatePriority"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-scheduler-immediate-priority",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-scheduler-immediate-priority",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "scheduler": "0.25.0"
      }
    },
    "node_modules/scheduler": {
      "version": "0.25.0",
      "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.25.0.tgz",
      "integrity": "sha512-xFVuu11jh+xcO7JOAGJNOXld8/TcEHK/4CituBUeUb5hqxJLj9YuemAEuvm9gQ/+pgXYfbQuqAkiYu+u7YEsNA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-scheduler-immediate-priority",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for scheduler.unstable_ImmediatePriority in scheduler@0.25.0",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "scheduler": "0.25.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify scheduler.unstable_ImmediatePriority in pkg:npm/scheduler@0.25.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/scheduler@0.25.0"
  ],
  "symbols": [
    "scheduler.unstable_ImmediatePriority"
  ]
}
src/index.js
const scheduler = require('scheduler');

const {
  unstable_ImmediatePriority,
  unstable_UserBlockingPriority,
  unstable_NormalPriority,
  unstable_LowPriority,
  unstable_IdlePriority,
  unstable_getCurrentPriorityLevel,
  unstable_runWithPriority,
  unstable_wrapCallback,
  unstable_scheduleCallback,
  unstable_cancelCallback
} = scheduler;

/**
 * Execute a callback immediately with highest priority level (unstable_ImmediatePriority).
 *
 * @param {Function} callback - Function to execute synchronously with immediate priority.
 * @returns {*} Return value of the callback.
 */
function runImmediate(callback) {
  return unstable_runWithPriority(unstable_ImmediatePriority, callback);
}

/**
 * Wrap a function so it always executes with unstable_ImmediatePriority.
 *
 * @param {Function} callback - Function to wrap.
 * @returns {Function} Wrapped callback executing with immediate priority.
 */
function wrapImmediate(callback) {
  return unstable_runWithPriority(unstable_ImmediatePriority, () => {
    return unstable_wrapCallback(callback);
  });
}

/**
 * Schedule a task with immediate priority level.
 *
 * @param {Function} callback - Task callback to schedule.
 * @param {object} [options] - Scheduling options.
 * @returns {object} Scheduled task node.
 */
function scheduleImmediate(callback, options) {
  return unstable_scheduleCallback(unstable_ImmediatePriority, callback, options);
}

module.exports = {
  scheduler,
  unstable_ImmediatePriority,
  unstable_UserBlockingPriority,
  unstable_NormalPriority,
  unstable_LowPriority,
  unstable_IdlePriority,
  unstable_getCurrentPriorityLevel,
  unstable_runWithPriority,
  unstable_wrapCallback,
  unstable_scheduleCallback,
  unstable_cancelCallback,
  runImmediate,
  wrapImmediate,
  scheduleImmediate
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  scheduler,
  unstable_ImmediatePriority,
  unstable_UserBlockingPriority,
  unstable_NormalPriority,
  unstable_LowPriority,
  unstable_IdlePriority,
  unstable_getCurrentPriorityLevel,
  unstable_runWithPriority,
  unstable_wrapCallback,
  unstable_scheduleCallback,
  unstable_cancelCallback,
  runImmediate,
  wrapImmediate,
  scheduleImmediate
} = require('../src/index.js');

async function testAll() {
  // 1. unstable_ImmediatePriority is exported as a priority constant equal to 1
  assert.equal(typeof scheduler, 'object', 'scheduler should be exported as an object');
  assert.equal(typeof unstable_ImmediatePriority, 'number', 'unstable_ImmediatePriority should be a number');
  assert.equal(unstable_ImmediatePriority, 1, 'unstable_ImmediatePriority value should equal 1');
  assert.equal(scheduler.unstable_ImmediatePriority, 1, 'scheduler.unstable_ImmediatePriority should equal 1');
  assert.equal(typeof runImmediate, 'function', 'runImmediate should be a function');
  assert.equal(typeof wrapImmediate, 'function', 'wrapImmediate should be a function');
  assert.equal(typeof scheduleImmediate, 'function', 'scheduleImmediate should be a function');

  // 2. unstable_runWithPriority executes a callback synchronously at unstable_ImmediatePriority level and restores previous priority
  const defaultPriority = unstable_getCurrentPriorityLevel();
  assert.equal(defaultPriority, unstable_NormalPriority, 'Default priority should be NormalPriority (3)');

  let recordedInsidePriority = null;
  const result = runImmediate(() => {
    recordedInsidePriority = unstable_getCurrentPriorityLevel();
    return 'priority-result';
  });

  assert.equal(recordedInsidePriority, unstable_ImmediatePriority, 'Priority inside runImmediate should be ImmediatePriority (1)');
  assert.equal(result, 'priority-result', 'runImmediate should return the value produced by callback');
  assert.equal(unstable_getCurrentPriorityLevel(), defaultPriority, 'Priority should be restored to default after runImmediate');

  // 3. unstable_wrapCallback binds a function to unstable_ImmediatePriority when wrapped within an immediate priority scope
  let callbackObservedPriority = null;
  const boundCallback = wrapImmediate(() => {
    callbackObservedPriority = unstable_getCurrentPriorityLevel();
  });

  // Call the wrapped callback from within a lower priority level
  unstable_runWithPriority(unstable_LowPriority, () => {
    boundCallback();
  });
  assert.equal(callbackObservedPriority, unstable_ImmediatePriority, 'Wrapped callback should execute with ImmediatePriority even when invoked from LowPriority');

  // 4. unstable_scheduleCallback schedules tasks with unstable_ImmediatePriority to execute ahead of lower priority tasks
  const executionOrder = [];
  await new Promise((resolve) => {
    unstable_scheduleCallback(unstable_LowPriority, () => {
      executionOrder.push('low');
      if (executionOrder.length === 3) resolve();
    });

    unstable_scheduleCallback(unstable_NormalPriority, () => {
      executionOrder.push('normal');
      if (executionOrder.length === 3) resolve();
    });

    scheduleImmediate(() => {
      executionOrder.push('immediate');
      if (executionOrder.length === 3) resolve();
    });
  });

  assert.deepEqual(
    executionOrder,
    ['immediate', 'normal', 'low'],
    'ImmediatePriority task should execute before NormalPriority and LowPriority tasks'
  );

  // 5. unstable_cancelCallback cancels a task scheduled with unstable_ImmediatePriority
  let cancelledRan = false;
  let companionRan = false;

  await new Promise((resolve) => {
    const taskToCancel = scheduleImmediate(() => {
      cancelledRan = true;
    });

    unstable_cancelCallback(taskToCancel);

    scheduleImmediate(() => {
      companionRan = true;
      resolve();
    });
  });

  assert.equal(cancelledRan, false, 'Cancelled immediate priority task should not have executed');
  assert.equal(companionRan, true, 'Companion task should have executed');

  console.log('All scheduler.unstable_ImmediatePriority contract tests passed successfully.');
}

testAll().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

オリジンシーダー

匿名