@the-link/core
v0.3.1
Published
Composable bidirectional event routing primitives.
Downloads
432
Readme
The Link Core
Composable bidirectional event routing without a transport or application model. Core provides the primitives shared by every The Link adapter: tunnels, links, synchronized properties, method decorators, and optional byte utilities.
Install
npm install @the-link/coreTunnel
A Tunnel publishes named events and aggregates the values returned by their
handlers.
import { Tunnel } from "@the-link/core"
const tunnel = new Tunnel()
tunnel.subscribe("sum", (left: number, right: number) => left + right)
const total = await tunnel.publishFirst<number>("sum", 20, 22)
console.log(total) // 42Every publication passes through four ordered phases:
- Interceptors transform or reject the payload.
- Subscribers handle the exact event.
- Forwarders route matching event prefixes.
- Finalizers transform the aggregated results.
Registration methods return cleanup functions, so routing can be composed and removed without retaining separate listener bookkeeping.
const unsubscribe = tunnel.subscribe("status", console.log)
await tunnel.publish("status", "ready")
unsubscribe()subscribeOnce(), waitFor(), and waitFirst() cover one-time and awaitable
events. forwardTo() connects a tunnel or forwarding function and can rewrite
the matching event prefix.
TheLink
TheLink is the transport-neutral unit built from two tunnels:
$outboundcarries events toward an adapter or another routing layer.$inboundcarries events received from an adapter or another routing layer.
Links compose through subscribeTo(), publishTo(), and connectTo(). Prefix
arguments create namespaces while the returned cleanup function removes the
relationship.
import { TheLink } from "@the-link/core"
const application = new TheLink()
const account = new TheLink()
const disconnect = application.connectTo(account, "", "account:")
disconnect()Core does not decide what an event means or which transport carries it. The HTTP, IPC, Worker, Frame, and Tab packages adapt this same contract to concrete communication boundaries.
Properties
Property synchronizes one provider-owned value through a link. A public
provider accepts update requests; a private provider rejects them. Consumers
bind to the provider's generated key.
import { Property, TheLink } from "@the-link/core"
const providerLink = new TheLink()
const consumerLink = new TheLink()
providerLink.$outbound.forwardTo(consumerLink.$inbound)
consumerLink.$outbound.forwardTo(providerLink.$inbound)
const theme = Property.public(providerLink, "light")
const snapshot = theme.toJSON()
const remoteTheme = Property.consumer(consumerLink, snapshot.key, snapshot.value)
remoteTheme.tunnel.subscribe("change", value => console.log(value))
await theme.update("dark")The links must already be connected through the routing or transport topology.
Each property exposes its current value, public key, local change tunnel,
and an optional inbound interceptor.
Decorators
The ECMAScript decorators register methods against a link during construction:
import { TheLink } from "@the-link/core"
import { Subscribe } from "@the-link/core/decorators"
class ApplicationLink extends TheLink {
@Subscribe("greet")
async greet(name: string) {
return `Hello ${name}`
}
}The available decorators are Intercept, Subscribe, Forward, Finalize,
Publish, and Connect. Projects that still compile TypeScript's legacy
decorator form can import the compatible implementations from
@the-link/core/decorators/legacy.
JSON bytes
serializeJSON() and deserializeJSON() provide the equivalent of JSON
stringification and parsing over UTF-8 bytes:
import { deserializeJSON, serializeJSON } from "@the-link/core"
const bytes = serializeJSON({ ready: true })
const value = deserializeJSON(bytes)They are utilities, not part of Link routing. Adapters may use them, replace them, or operate without them.
Entry points
| Import | Contract |
| --- | --- |
| @the-link/core | links, tunnels, properties, JSON byte utilities, and their types |
| @the-link/core/decorators | ECMAScript decorators |
| @the-link/core/decorators/legacy | legacy TypeScript decorators |
Development
bun install --frozen-lockfile
bun run verifyLicense
MIT
