@sigx/lynx-runtime-main
v0.28.0
Published
Main Thread (Lepus) entry and ops applier for SignalX Lynx renderer
Readme
@sigx/lynx-runtime-main
Main-thread (Lepus) runtime for SignalX on Lynx. Receives the op stream from @sigx/lynx-runtime, mutates the native render tree via Lynx PAPI, and runs 'main thread'-marked worklets at the host's display refresh rate.
Application code rarely imports from this package directly. The build pipeline (
@sigx/lynx-plugin) wires it into the main-thread bundle automatically.
📚 Documentation
Full guides, API reference and live examples → https://sigx.dev/lynx/modules/runtime-main/overview/
Responsibilities
entry-main.ts— installsglobalThis.processData,renderPage,updatePage,sigxPatchUpdate,sigxRunOnMT, andrunOnBackground. This is the file the build plugin lists as the first import in the MT bundle so the Lynx runtime finds the global hooks it expects.ops-apply.ts— theapplyOpsloop that consumes the BG → MT op stream (CREATE,INSERT,SET_STYLE,SET_WORKLET_EVENT,INIT_MT_REF,REGISTER_AV_BRIDGE, ...) and translates them into PAPI calls (__CreatePage,__SetInlineStyles,__AddEvent, etc.).MTElementWrapper— high-level wrapper your worklets drive viamainThreadRef.current.method(...)(setStyleProperties,getComputedStyleProperty,animate,invoke, query selectors).- Hybrid worklet dispatch — the slot machine in
event-slots.tsplus the hybrid context inhybrid-worklet.tslets a single MT slot carry both a worklet handler and a BG-side handler for the same event, dispatching to both. - AnimatedValue bridge —
animated-bridge-mt.tsdiffs registered AVs against last-published snapshots and dispatches batchedLynx.Sigx.AvPublishevents to BG once per__FlushElementTreeboundary. Registration also arms an auto-flush setter on the SharedValue's MT envelope (armAvAutoFlush), so a bare worklet writesv.current.value = xschedules a microtask-coalesced flush by itself —useAnimatedStylebindings apply and the publish lands the same frame without a manual__FlushElementTree()in gesture code. The matching BG sink lives in@sigx/lynx-runtime. useAnimatedStylemapper registry —animated-style-mappers.tsships built-in mappers (translateX,scale,opacity, ...) and exposesregisterMapper(name, fn)so MT-side code can add custom ones.- Snapshot runtime —
snapshot-mt.ts(#620) instantiates compiled snapshot templates on the MT: lazyensureElements()(staged records until first materialization), hole patching through each template'supdate[i], synthetic negative ids feeding the same event-slot/ref machinery as op-built elements, and the hole-updater hooks installed into@sigx/lynx-runtime-internal/snapshotat bootstrap. No production callers yet — the wire protocol and the transform arrive in later #620 phases. - Frame-callback driver —
frame-callbacks-mt.ts(#933) keeps the registry behinduseFrameCallbackand drives onerequestAnimationFramechain for every active callback:requestAnimationFrameon the MT is a host bridge call, so a chain per callback would multiply bridge traffic for nothing, and one chain gives every callback the sameDate.now()sample and closes the frame with a single flush (throughscheduleAvFlush's shared latch). Each entry's worklet ctx is prototype-rebuilt once at registration and then handed torunWorkletunchanged, so upstream's identity-keyed hydration cache hits — building a fresh ctx per frame would re-walk_c, re-bind, and re-addRefeveryrunOnBackgroundhandle sixty times a second. It also installsglobalThis.__sigxFrameCallbacks, the channel thestartFrameCallback/stopFrameCallbackworklets use to start and stop a loop from main-thread code. - Parked gesture registrations —
gesture-park.ts(#958) holds aSET_GESTURE_DETECTORwhose element isn't bound yet and replays it, in arrival order, when that wvid binds. The drain lives inbindMtRef, not in theSET_MT_REFop handler, because elements inside a compiled snapshot template bind through the snapshot runtime and never emit that op — draining from the handler misses most refs in a real app. Registration normally arrives after the ref op, but not when the detector's owner mounts earlier than the element it binds to — a screen that measures itself before rendering its content does exactly that. Dropping the op there was silent and permanent: nothing threw, the batch stayed well-formed, and the gesture was simply dead. A REMOVE arriving while the SET is still parked forgets it, so a component that unmounts inside the window doesn't install a gesture for itself afterwards. mt-ref-bind.ts—MainThreadRef→ element binding (upstream ref map + web style fallback + the wvid → elementId record), shared by the SET_MT_REF op and the snapshot runtime.
Bootstrap order
Three modules must evaluate in this order on the MT thread:
entry-main— setsglobalThis.SystemInfoand the renderer hooks.@lynx-js/react/worklet-runtime— installslynxWorkletImpl,registerWorkletInternal,runWorklet.install-hybrid-worklet— registers the hybrid dispatcher into the now-populated worklet map.
@sigx/lynx-plugin prepends side-effect imports for these three at the top of every file in the MT bundle, so the order is enforced regardless of which user file the Lynx runtime evaluates first.
Gotchas
Diagnostics use console.*, not createLogger — deliberately
CONVENTIONS.md C10 routes package diagnostics through createLogger('<pkg>'). This package is the one that opts out, and the reasons are structural rather than stylistic. __tests__/worklet-logger-capture.test.ts holds the evidence so the exemption can't rot.
A logger cannot reach a
'main thread'worklet body.createLoggerreturns an object of closures over@sigx/lynx-core's module state. The SWC worklet transform does not leave a module-scope reference in place — it rewriteslog.warn(x)into a_ccapture (_c: { log: { warn: log.warn } }) and the registered body readsthis._c._ccrosses BG → MT as JSON (@sigx/lynx-runtime'sop-queue.ts:133), andJSON.stringifydrops function-valued properties, so the capture arrives as{}. The first log line in the worklet throwsTypeError: log.warn is not a functionand the rest of the handler never runs — a diagnostic that silently kills the scroll or gesture it was meant to explain.On the MT there is nothing behind the logger. The transports that make
createLoggerworth more thanconsole.*—@sigx/lynx-dev-client/install(thesigx devconsole streamer) and@sigx/lynx-observability/install— are prepended to the background layer only (@sigx/lynx-plugin'sentry.ts:747and:759). The main-thread layer's entry list is the user's imports plus the CSS HMR runtime; nothing patches the Lepus console.log.warn(...)andconsole.warn(...)land in exactly the same place.The import isn't free.
@sigx/lynx-corepublishes no logger subpath — only the barrel — so importing it would pull@sigx/reactivityand the whole native bridge into a bundle whose stated job is PAPI bootstrap only.
Every diagnostic here is therefore a bare console.* tagged [sigx-mt]. To read them, use the platform log (adb logcat, Xcode console) — they are not in the sigx dev terminal.
License
MIT
