Skip to content

Tempo Plugin

@magmacomputing/tempo-plugin-sync ​

npm version npm peer dependency version License TypeScript Ready

This is a Community plugin for the Tempo library that provides lock-free, nanosecond-accurate cross-thread time synchronization using SharedArrayBuffer and Atomics.

It eliminates inter-process message passing (IPC) latency, allowing worker threads and Web Workers to read current timestamps synchronously in constant O(1) time.

Perfect For

High-frequency trading platforms, real-time multiplayer authoritative game servers, distributed microservice tracing, and extreme-precision scientific telemetry.


🚀 Installation & Quickstart ​

bash
npm install @magmacomputing/tempo-plugin-sync

Live Interactive Sandbox Note

This plugin leverages SharedArrayBuffer and Atomics for hardware-level cross-thread synchronization. Modern browsers require Cross-Origin Isolation (Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp) to enable SharedArrayBuffer. Because GitHub Pages static hosting cannot emit custom HTTP response headers, the live browser sandbox widget is disabled on this documentation page. To test the Sync plugin, run it in Node.js (supported natively) or in a local development environment with custom server headers configured.

Master Process: Initializing the Atomic Clock ​

typescript
import { Tempo } from '@magmacomputing/tempo';
import { SyncPlugin } from '@magmacomputing/tempo-plugin-sync';

Tempo.use(SyncPlugin);

// Start master clock loop (interval in ms, default is 10)
const clock = Tempo.sync.startClock({ interval: 5 });
const buffer = clock.getBuffer(); // Pass this SharedArrayBuffer to your workers

Zero-Boilerplate Auto-Installation (Side-Effect Import) ​

typescript
import { Tempo } from '@magmacomputing/tempo';
import '@magmacomputing/tempo-plugin-sync/install';

const clock = Tempo.sync.startClock({ interval: 10 });

Worker Process: Lock-Free Reading ​

typescript
// worker.ts
import { workerData } from 'node:worker_threads';
import { AtomicReader } from '@magmacomputing/tempo-plugin-sync';

// Hydrate reader directly from shared memory
const reader = new AtomicReader(workerData.buffer);

// 1. Get raw epoch milliseconds (O(1) Atomic Read)
const ms = reader.now(); 

// 2. Get high-precision BigInt nanoseconds
const ns = reader.nowNano();

// 3. Hydrate a brand new Tempo instance with exact precision
const t = reader.getTempo();
console.log(t.iso);

📚 API Surface Catalog ​

The Sync plugin exposes two primary abstractions: AtomicClock for the master thread, and AtomicReader for worker threads:

Class / MethodContextParametersReturnsDescription
Tempo.sync.startClock(opts?)MasterClockOptions?: { interval?: number }AtomicClockInitializes and starts the master clock loop, continuously writing time to shared memory.
Tempo.sync.stopClock()MasterNonevoidHalts the master clock synchronization loop.
Tempo.sync.getBuffer()MasterNoneSharedArrayBufferReturns the underlying 8-byte shared buffer for worker transfer.
new AtomicReader(buffer)Workerbuffer: SharedArrayBufferAtomicReaderInstantiates a high-speed reader bound to the shared clock buffer.
reader.now()WorkerNonenumberReturns synchronized epoch timestamp in milliseconds in O(1) time.
reader.nowNano()WorkerNonebigintReturns synchronized epoch timestamp in BigInt nanoseconds.
reader.getTempo()WorkerNoneTempoHydrates a new immutable Tempo instance representing current synchronized time.

📖 Architecture & Specialized Guides ​


📄 Licensing ​

This is a Community plugin. It is completely free and open-source for personal and commercial use under the MIT license. No license token is required.

Released under the MIT License.