StunkStunk

Utilities

Helper functions for working with chunks and async state.

Stunk exports a set of utility functions for inspecting chunks and guarding against invalid values.

import { isChunk, isValidChunk, isValidChunkValue, shallowEqual } from "stunk";
import { combineAsyncChunks } from "stunk/query";

isChunk(value)

Type guard that checks whether an unknown value is a valid Chunk. Verifies the value is an object with all required chunk methods: get, set, peek, subscribe, derive, reset, and destroy.

import { isChunk, chunk } from "stunk";

const count = chunk(0);

isChunk(count); // true
isChunk(42); // false
isChunk(null); // false
isChunk({}); // false — missing required methods

Useful when building generic utilities that may receive unknown values:

function printValue(value: unknown) {
  if (isChunk(value)) {
    console.log("Chunk value:", value.get());
  }
}

isValidChunk(value, validateBehavior?)

A stricter version of isChunk. Optionally validates that the chunk behaves correctly by calling get() and set() and verifying no errors are thrown.

import { isValidChunk, chunk } from "stunk";

const count = chunk(0);

isValidChunk(count); // true — shape check only
isValidChunk(count, true); // true — also tests get/set behavior
isValidChunk({ get: () => 0 }); // false — missing required methods
ParameterTypeDefaultDescription
valueunknownValue to check
validateBehaviorbooleanfalseIf true, also tests get() and set() behavior

Use isChunk for most cases. isValidChunk with validateBehavior: true is useful in tests or when accepting chunks from external sources.


isValidChunkValue(value)

Checks whether a value is valid as a chunk value. Returns false only for undefinednull is valid in v3.

import { isValidChunkValue } from "stunk";

isValidChunkValue(0); // true
isValidChunkValue("hello"); // true
isValidChunkValue(null); // true — null is valid in v3
isValidChunkValue(undefined); // false — undefined is never allowed

shallowEqual(a, b)

Compares two values with shallow equality. Returns true if both values are the same reference, or if they are objects/arrays with the same keys and values at the top level.

Used internally by computed() and select() to avoid unnecessary subscriber notifications when object values haven't meaningfully changed.

import { shallowEqual } from "stunk";

shallowEqual(1, 1); // true
shallowEqual({ a: 1 }, { a: 1 }); // true — same flat values
shallowEqual({ a: 1 }, { a: 2 }); // false
shallowEqual({ a: { b: 1 } }, { a: { b: 1 } }); // false — nested refs differ
shallowEqual([1, 2, 3], [1, 2, 3]); // true

Useful when writing custom middleware or utilities that need to compare chunk values:

import { shallowEqual } from "stunk";
import type { Middleware } from "stunk";

const deduplicateObjects: Middleware<object> = (value) => {
  // custom dedup logic using shallow equality
};

combineAsyncChunks(chunks)

Merges multiple async chunks into a single reactive chunk with unified loading, error, errors, and data state. Imported from stunk/query.

import { asyncChunk, combineAsyncChunks } from "stunk/query";

const combined = combineAsyncChunks({
  user: userChunk,
  posts: postsChunk,
});

combined.get();
// { loading: true, error: null, errors: {}, data: { user: null, posts: null } }

See Combine Async Chunks for full documentation.


What's next?

On this page