/texture-bridge:managing-frame-forward-lifecyclefrom texture-bridge
Guides proper lifecycle management for TextureBridge.forwardFrames registrations, covering disposal, listener hygiene, and leak prevention.
Ships in texture-bridge. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A forwardFrames registration cleans itself up. It prunes its own entry when the target dies, unhooks its own listener when disposed, and refuses to register at all when registration could not self-clean. Bookkeeping layered on top is redundant — and the listener bookkeeping is itself the leak people write it to prevent.
| Situation | The library already does this | You do |
|---|---|---|
Target WebContents destroyed later (its window closed) | Entry is auto-pruned via an internal once("destroyed") — no forwarding to a dead target | nothing |
forward.dispose() | Deletes the entry and removes that internal "destroyed" listener from your target. Idempotent, never throws, safe after the target died | call it (see below) |
bridge.dispose() | Unhooks every entry's listener, then clears all entries | nothing |
forwardFrames() after bridge.dispose() | Does not register; returns an inert FrameForward whose dispose() is a no-op and whose active is false | see "own the truth" below |
forwardFrames() with an already-destroyed target | Same — inert handle, active: false, no registration, no leak | same |
| A live registration stops delivering (target dies mid-session, import or send fails) | Reports the change on "forwardStatus" / options.onStatus, and keeps trying every frame | log both edges (see 4) |
Repeated connect/disconnect on one long-lived multiviewer window accumulates nothing: each dispose() takes its listener with it.
Dispose before you drop the handle. Overwriting a Map entry that holds a live FrameForward leaks the registration — the library cannot see your map.
Re-register after the target window is recreated. A reopened window is a new WebContents; the old entry already pruned itself, so connect again against the new one.
Own the truth of your own connected state — and throw when it is false. A refused registration still hands you a handle; what it does not hand you is frames. FrameForward.active is false in that case, so the failure is detectable — but detectable is not the same as handled. Check bridge.isDisposed / target.isDestroyed() yourself, before registering, and throw a named error when the check fails; assert forward.active after the call as the backstop for whatever your pre-check missed. Do not return a boolean or an undefined handle: a status a caller can ignore is a UI that says "connected" over a dead deck.
Subscribe to forwardStatus (or pass onStatus) for the deaths that happen later. Registration succeeding says nothing about the next hour: the target can die, an import can start failing, and forwarding is best-effort — the stream survives, silently. The event is deduped to transitions, so a plain log of every status is already an outage report: { ok: false } with no { ok: true } after it means that target has been dark since that timestamp. Tag each registration with extraArgs so the log says which one.
export class ForwardTargetUnavailableError extends Error {
override name = "ForwardTargetUnavailableError";
}
const forwards = new Map<number, FrameForward>();
/** @throws {ForwardTargetUnavailableError} the bridge is disposed or the monitor is destroyed. */
export const connect = (slot: number, bridge: TextureBridge, monitor: BrowserWindow): void => {
forwards.get(slot)?.dispose(); // never overwrite a live handle
if (bridge.isDisposed || monitor.webContents.isDestroyed()) {
throw new ForwardTargetUnavailableError(`slot ${slot}: bridge disposed or monitor destroyed`);
}
forwards.set(slot, bridge.forwardFrames(monitor.webContents, { extraArgs: [slot] }));
};
export const disconnect = (slot: number): void => {
forwards.get(slot)?.dispose(); // idempotent — fine even if the window already died
forwards.delete(slot);
};
That is the whole manager. No "destroyed" listener, no removeListener, no try/catch, no bridge.on("disposed") teardown pass.
At the edge that drives the UI (an IPC handler, a menu action), fold that throw into a value once — Result.fromThrowable bound at module scope, matched at the edge, never re-exported:
const safeConnect = Result.fromThrowable(connect, (cause) => toConnectError(cause));
ipcMain.handle("deck:connect", (_e, slot: number) =>
safeConnect(slot, decks[slot].bridge, monitorWindow).match(
() => ({ connected: true }),
(error) => ({ connected: false, reason: error.message }),
),
);
Which bridge calls throw, reject, or model their failures instead: see the handling-texture-bridge-failures skill.
| Mistake | Reality |
|---|---|
target.once("destroyed", () => forward.dispose()) | The registration already does this internally. Yours is a second listener on the same target — the accumulation you were guarding against. |
Calling target.removeListener("destroyed", ...) (guarded by isDestroyed()) at teardown | That exact code lives inside forward.dispose(). |
try { forward.dispose() } catch {} "in case the target is dead" | dispose() is idempotent and internally guarded. It does not throw. |
bridge.on("disposed", ...) to tear down that bridge's forwards | bridge.dispose() clears and unhooks them before the event is emitted. |
Pre-checking isDisposed to stop the library from leaking a registration | It never registers — there is nothing to protect it from. You pre-check to report, not to guard. |
Returning false / undefined / a null handle when registration is impossible | A status the caller can drop on the floor, which is how a dead deck stays lit in the UI. Throw a named error; fold it with Result.fromThrowable at the edge. |
throw from inside a paint-rate path, or a "destroyed" handler | Throw at registration, where a human action is waiting for an answer. Per-frame failures stay best-effort. |
forwards.set(slot, bridge.forwardFrames(...)) over an existing entry | The overwritten handle is unreachable and still registered — the one real leak in this API. |
| Keeping decks "connected" across a window close and expecting frames after reopen | The old entry self-pruned. New window = new WebContents = new forwardFrames call. |
Expecting forward failures on bridge.on("error") / frameDropped | Wrong channel — forwarding reports on "forwardStatus" / options.onStatus. |
| Registering forwards and subscribing to nothing | The one failure mode with no other symptom: paint, sender and preview stay healthy while the monitor goes black. One forwardStatus listener is the whole fix. |
Already black? Diagnosing a forward that has already gone dead is a different job from wiring the handling: use diagnosing-dead-frame-forwards.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub naporin0624/electron-texture-bridge --plugin texture-bridge8 skills in this plugin
/choosing-texture-bridge-apiUse when deciding which @napolab/texture-bridge API tier to use for a feature — simple vs core, Syphon/Spout output vs in-app preview/monitor, forwardSharedTexture vs forwardFrames — when reviewing an integration plan that uses the library, or when unsure whether a texture-bridge function, method, or option actually exists./delivering-imported-texturesUse when delivering a GPU texture to a renderer from the Electron main process — about to call `sharedTexture.importSharedTexture` / `sendSharedTexture` / `release()` by hand, deciding where `release()` belongs, wrapping delivery in try/finally or neverthrow tees to guarantee exactly-once release, writing the same deliver-and-release dance in a second module, or delivering a texture you imported yourself from a receiver's native handle./diagnosing-dead-frame-forwardsUse when forwarded frames stop arriving — a preview or monitor window that goes black or shows a "no signal" placeholder, a multiviewer deck frozen on its last frame, previews that die during a long session or for only some targets while Syphon/Spout output keeps working, a forwardFrames registration you suspect never delivered anything, or a dead preview with nothing in the logs./handling-texture-bridge-failuresUse when adding error handling, telemetry, or crash-safety to code that uses @napolab/texture-bridge — deciding whether a call needs a try/catch or a Result.fromThrowable wrapper, reviewing the failure handling of a texture-bridge integration, defining error classes for bridge or receiver operations, or diagnosing silently black output or a main-process crash from a bridge call./migrating-to-forward-framesUse when replacing an existing Electron preview/monitor implementation with @napolab/texture-bridge zero-copy forwarding — capturePage polling, paint-event bitmap IPC, toBitmap/toPNG transfers, or a Syphon loopback feeding an in-app window — or when upgrading to the release that ships forwardFrames / forwardSharedTexture./receiving-shared-texturesUse when implementing the receiving side of @napolab/texture-bridge shared-texture frames in an Electron renderer — consuming forwardFrames/forwardSharedTexture output, displaying createSharedTextureReceiver frames, building a multiviewer/preview grid, or debugging leaked VideoFrames, black canvases, or frames from a disconnected source reappearing.Other plugins' skills for the same job