@tempo-kit/plugin-signals
Tempo community plugin for reactive TC39 Signals (Signify), adaptive relative-time formatting, boundary-scheduled interval state observation, and global configuration reactivity.
Core tempo-kit is a static, immutable, zero-timer snapshot engine. @tempo-kit/plugin-signals layers fine-grained, event-driven reactivity on top of Tempo without adding background timers or reactivity overhead to core bundles.
Key Features
- Tree-Shakeable Functions & Explicit Installation: Standalone functions (
toRelativeSignal,intervalSignal,localeSignal,timeZoneSignal) andSignalsPluginare exported from the root entry with zero side effects; registration occurs only viaTempo.use(SignalsPlugin)or the/installentry (which mountsTempo.signals,tempo.toRelativeSignal(), andinterval.toSignal()). - Two Integration Patterns: Use standalone tree-shakeable functions directly, or mount
Tempo.signals,tempo.toRelativeSignal(), andinterval.toSignal()viaTempo.use(SignalsPlugin)or/install. - Adaptive Relative-Time Signals:
toRelativeSignalschedules one-shotsetTimeoutupdates at exact unit-bucket boundaries (second→minute→hour→day→month→year) instead of polling every second, and automatically tracks globalTempo.init({ locale })updates. - Boundary-Scheduled Interval Observation:
intervalSignaltracksisCurrent,isPast,isFuture, and normalizedprogress() with zero polling while future or past. - Reactive Global Configuration:
localeSignalandtimeZoneSignalsynchronize withTempo.init()viatempo-kit/plugin/sdk'sonConfigChangehook. - Deterministic Cleanup: Returned signals implement both
dispose()andSymbol.dispose(for TypeScriptusingdeclarations), backed by aFinalizersafety net.
Installation
npm install @tempo-kit/plugin-signalsQuickstart
// ⚡ Reactive TC39 Signals (@tempo-kit/plugin-signals)
const {
SignalsPlugin,
localeSignal,
toRelativeSignal,
intervalSignal,
} = await import('@tempo-kit/plugin-signals');
Tempo.use(SignalsPlugin);
// 1. Reactive Global Locale Signal (tracks Tempo.init updates)
const locale = localeSignal();
console.log('Initial Locale:', locale.get());
// 2. Adaptive Boundary-Scheduled Relative Time Signal
const meeting = new Tempo().add({ minutes: 5 });
const relative = meeting.toRelativeSignal({ style: 'long' });
console.log('Relative Time:', relative.get());
// 3. Interval State Signal (isCurrent, isPast, isFuture, progress)
const sprint = new Tempo.Interval(
new Tempo().subtract({ hours: 1 }),
new Tempo().add({ hours: 3 }),
);
const state = sprint.toSignal({ progressInterval: false });
console.log('Interval State:', {
isCurrent: state.isCurrent.get(),
isPast: state.isPast.get(),
isFuture: state.isFuture.get(),
progress: Number(state.progress.get().toFixed(2)),
});
// 4. Deterministic Cleanup
relative.dispose();
state.dispose();
Tempo.signals.dispose();
return relative.get();Plugin Registration (Tempo.use or /install)
Installing SignalsPlugin mounts the frozen Tempo.signals namespace, Tempo.prototype.toRelativeSignal(), and Tempo.Interval.prototype.toSignal():
import { Tempo } from 'tempo-kit';
import { SignalsPlugin } from '@tempo-kit/plugin-signals';
Tempo.use(SignalsPlugin);
const meeting = new Tempo().add({ minutes: 5 });
using relative = meeting.toRelativeSignal();
console.log(relative.get()); // e.g. "in 4m" (default 'narrow' style, truncating toward zero once the clock advances by a few ms)Or use the side-effect /install entrypoint to auto-register SignalsPlugin on import:
import '@tempo-kit/plugin-signals/install';
import { Tempo } from 'tempo-kit';
const deadline = new Tempo().add({ hours: 2 });
using relative = deadline.toRelativeSignal({ style: 'long' });
console.log(relative.value); // e.g. "in 1 hour"Tree-Shakeable Standalone Functions
For bundle-sensitive applications, import only the functions you need from the root entrypoint without mutating Tempo:
import { Tempo } from 'tempo-kit';
import { toRelativeSignal } from '@tempo-kit/plugin-signals';
const target = new Tempo().add({ minutes: 10 });
using relative = toRelativeSignal(target, { style: 'short' });
console.log(relative.get()); // e.g. "in 9 min."| Approach | Import Syntax | Side Effects | Ideal For |
|---|---|---|---|
| Tree-Shakeable Functions | import { toRelativeSignal, intervalSignal } from '@tempo-kit/plugin-signals' | None | Minimal client bundles and isolated components |
| Explicit Plugin | import { SignalsPlugin } from '@tempo-kit/plugin-signals' + Tempo.use(SignalsPlugin) | Explicit | Controlled initialization with SignalsPluginOptions |
| Auto-Install Import | import '@tempo-kit/plugin-signals/install' | Registers SignalsPlugin | Apps, REPLs, and convenience setup |
Global Configuration Signals
localeSignal, timeZoneSignal, and createConfigSignals expose ReadonlySignal<string> instances that stay synchronized with global Tempo.init() calls.
import { Tempo } from 'tempo-kit';
import { Signify } from 'tempo-kit/library';
import {
createConfigSignals,
disposeConfigSignals,
} from '@tempo-kit/plugin-signals';
Tempo.init({ locale: 'en-US', timeZone: 'UTC' });
const { locale, timeZone } = createConfigSignals({
locale: 'en-US',
timeZone: 'UTC',
});
const summary = Signify.computed(() => `${locale.get()} @ ${timeZone.get()}`);
console.log(summary.get()); // "en-US @ UTC"
Tempo.init({ locale: 'fr-FR', timeZone: 'Europe/Paris' });
console.log(summary.get()); // "fr-FR @ Europe/Paris"
// Detach the global onConfigChange listener when tearing down
disposeConfigSignals();When SignalsPlugin is installed, the same signals are exposed on Tempo.signals.locale and Tempo.signals.timeZone (and detached via Tempo.signals.dispose() or using _ = Tempo.signals).
Options (SignalsPluginOptions)
| Property | Type | Default | Description |
|---|---|---|---|
locale | string | Runtime Intl locale (or 'en') | Fallback BCP 47 locale before Tempo.init() specifies one |
timeZone | string | Runtime Intl time zone (or 'UTC') | Fallback IANA time zone before Tempo.init() specifies one |
System Locale & Time-Zone Watchers (watchSystemConfig)
To synchronize Tempo.init() automatically when the host OS or browser changes its locale or time zone (e.g., laptop travel across time zones or languagechange events) with zero background polling:
import { watchSystemConfig } from '@tempo-kit/plugin-signals';
// Sync both locale and timeZone on browser focus/visibility/languagechange events
using watcher = watchSystemConfig();
// Or keep a pinned locale and sync only timeZone
using tzOnly = watchSystemConfig({ locale: false });When SignalsPlugin is installed, the same helper is available at Tempo.signals.watchSystem(options?). Non-browser runtimes can pass a custom subscribe(trigger) hook (e.g., Electron powerMonitor), call watcher.check(), or opt into pollIntervalMs.
Adaptive Relative-Time Signals (toRelativeSignal)
toRelativeSignal(target, options?) (and tempo.toRelativeSignal(options?) / Tempo.signals.relative(target, options?)) returns a DisposableSignal<string> formatted via Intl.RelativeTimeFormat.
Instead of running a fixed 1-second timer, it computes the exact delay until the next unit-bucket transition and schedules a single one-shot setTimeout for that boundary—keeping background wakeups options.locale is omitted, the signal also reacts automatically to global Tempo.init({ locale }) changes.
import { Tempo } from 'tempo-kit';
import { Signify } from 'tempo-kit/library';
import { toRelativeSignal } from '@tempo-kit/plugin-signals';
const target = new Tempo().add({ minutes: 5 });
using relative = toRelativeSignal(target, {
style: 'long',
numeric: 'auto',
});
const label = Signify.computed(() => `Starts ${relative.get()}`);
console.log(label.get()); // "Starts in 4 minutes"
// Automatically updates when Tempo.init({ locale }) changes
Tempo.init({ locale: 'es-ES' });
console.log(label.get()); // "Starts dentro de 4 minutos"Accepted Targets (RelativeSignalTarget)
toRelativeSignal accepts any of the following target forms:
TempoinstanceTemporal.ZonedDateTimeorTemporal.Instant- Object with
{ epoch?: { ms?: number; ns?: bigint } }or{ epochMilliseconds?: number; epochNanoseconds?: bigint } string(parsed vianew Tempo(target))number(epoch milliseconds) orbigint(epoch nanoseconds)
Invalid targets throw a TypeError.
Options (RelativeSignalOptions)
| Property | Type | Default | Description |
|---|---|---|---|
locale | string | Reactive localeSignal() | Explicit BCP 47 locale override (disables reactive global locale tracking) |
style | 'long' | 'short' | 'narrow' | 'narrow' | Intl.RelativeTimeFormat formatting style |
numeric | 'always' | 'auto' | 'auto' | Intl.RelativeTimeFormat numeric mode (e.g. 'yesterday' vs '1 day ago') |
now | () => number | Date.now | Custom clock returning epoch milliseconds (useful for deterministic tests) |
DisposableSignal<T> Surface
DisposableSignal<T> extends ReadonlySignal<T> and is callable (sig()), readable via .get() or .value, and explicitly disposable:
disposed:boolean—trueafterdispose()or[Symbol.dispose]()has been called.dispose(): stops pending boundary timers and releases resources.[Symbol.dispose](): alias fordispose()enablingusingblocks.
Interval State Signals (intervalSignal)
intervalSignal(interval, options?) (and interval.toSignal(options?) / Tempo.signals.interval(interval, options?)) projects a Tempo Interval into four reactive ReadonlySignal properties:
isFuture:ReadonlySignal<boolean>—truewhilenow < start.isCurrent:ReadonlySignal<boolean>—truewhilenowis within[start, end).isPast:ReadonlySignal<boolean>—trueoncenow >= end.progress:ReadonlySignal<number>— normalized elapsed ratio in( 0before start,1at or after end).
Boundary transitions (start and end) use exact one-shot setTimeout scheduling—zero polling occurs while the interval is in the future or past. The progress ticker runs only while isCurrent is true (unless disabled).
import { Tempo, Interval } from 'tempo-kit';
import { intervalSignal } from '@tempo-kit/plugin-signals';
const start = new Tempo().add({ seconds: 10 });
const end = start.add({ minutes: 5 });
const window = new Interval(start, end);
using state = intervalSignal(window, { progressInterval: 500 });
console.log(state.isFuture.get()); // true
console.log(state.isCurrent.get()); // false
console.log(state.isPast.get()); // false
console.log(state.progress.get()); // 0With SignalsPlugin installed, you can call .toSignal() directly on any Interval:
import '@tempo-kit/plugin-signals/install';
import { Tempo, Interval } from 'tempo-kit';
const now = new Tempo();
const meeting = new Interval(now, now.add({ minutes: 30 }));
using state = meeting.toSignal({ progressInterval: false });
console.log(state.isCurrent.get()); // trueOptions (IntervalSignalOptions)
| Property | Type | Default | Description |
|---|---|---|---|
progressInterval | number | false | 1000 | Update interval in milliseconds for progress while isCurrent is true. Set to false or 0 to disable periodic progress ticks. |
now | () => number | Date.now | Custom clock returning epoch milliseconds. |
API Summary
| Export | Kind | Description |
|---|---|---|
SignalsPlugin, signalsPlugin, default | TempoPlugin<SignalsPluginOptions> | Plugin mounting Tempo.signals, Tempo.prototype.toRelativeSignal, and Tempo.Interval.prototype.toSignal |
localeSignal(options?) | Function | Shared ReadonlySignal<string> synchronized with global locale |
timeZoneSignal(options?) | Function | Shared ReadonlySignal<string> synchronized with global timeZone |
createConfigSignals(options?) | Function | Frozen { locale, timeZone } config signals object |
disposeConfigSignals() | Function | Detaches the global onConfigChange listener |
toRelativeSignal(target, options?) | Function | Adaptive, boundary-scheduled DisposableSignal<string> |
intervalSignal(interval, options?) | Function | Boundary-scheduled IntervalSignalState (isCurrent, isPast, isFuture, progress) |
Relationship with @tempo-kit/plugin-ticker
- Use
@tempo-kit/plugin-tickerwhen you need continuous fixed-cadence clock streams (Tempo.ticker.signal({ seconds: 1 })). - Use
@tempo-kit/plugin-signalswhen you need event-driven relative-time labels, interval lifecycle state, or reactiveTempo.init()configuration signals.
Topic Guides
- Production Use Cases & Architectural Patterns - Battery-friendly activity feeds, live interval progress bars, and app-wide locale/time zone reactivity.
License
This is a Community plugin. It is completely free and open-source for personal and commercial use under the MIT license.