API reference › @evolu/common › Types › CallbackWithTeardown
type CallbackWithTeardown<T> = (value: T) => void | (() => void);
Defined in: packages/common/src/Types.ts:73
A function that receives a value and optionally returns a teardown function.
Use for subscriptions or callbacks that need abort-time teardown.
Subscription teardown
import { assertEqual, type CallbackWithTeardown } from "@evolu/common";
interface EventSource {
readonly start: () => void;
readonly stop: () => void;
}
const events: Array<string> = [];
const source: EventSource = {
start: () => {
events.push("started");
},
stop: () => {
events.push("stopped");
},
};
const subscribe: CallbackWithTeardown<EventSource> = (source) => {
source.start();
return source.stop;
};
const teardown = subscribe(source);
if (teardown) teardown();
assertEqual(events, ["started", "stopped"]);