Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ export type DOMElement = {
// Internal properties
isStaticDirty?: boolean;
staticNode?: DOMElement;
// Identity of `staticNode` at the end of the previous commit. Used by the
// reconciler to detect when the live <Static> instance has been replaced
// (key-driven remount), removed (no longer in the React tree) or added
// (first <Static> mount). On any identity change, ink resets its
// accumulated `fullStaticOutput` so future clearTerminal rewrites don't
// replay items that are no longer owned by the current React tree.
previousStaticNode?: DOMElement;
onComputeLayout?: () => void;
onRender?: () => void;
onImmediateRender?: () => void;
onStaticChange?: () => void;
internal_layoutListeners?: Set<LayoutListener>;
} & InkNode;

Expand Down
12 changes: 12 additions & 0 deletions src/ink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export default class Ink {
}

this.rootNode.onImmediateRender = this.onRender;
this.rootNode.onStaticChange = this.handleStaticChange;
this.log = logUpdate.create(options.stdout, {
incremental: options.incrementalRendering,
});
Expand Down Expand Up @@ -517,6 +518,17 @@ export default class Ink {
);
};

// Called from the reconciler when the live <Static> identity changes
// (first mount, last unmount, or key-driven remount). The previously
// accumulated `fullStaticOutput` belongs to a Static instance that is no
// longer in the React tree, so future `shouldClearTerminalForFrame`
// rewrites must not replay it. The new instance (if any) will re-emit
// its items into a fresh `fullStaticOutput` on the immediate render that
// follows this callback.
handleStaticChange = (): void => {
this.fullStaticOutput = '';
};

onRender: () => void = () => {
this.hasPendingThrottledRender = false;

Expand Down
16 changes: 16 additions & 0 deletions src/reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ export default createReconciler<

emitLayoutListeners(rootNode);

// Detect <Static> identity change for this commit: first mount,
// last unmount, or key-driven remount. Fire `onStaticChange` BEFORE
// `onImmediateRender` so ink can drop any accumulated static output
// from the previous Static instance before the new instance emits.
// Without this, items belonging to the unmounted/replaced Static
// stay in `fullStaticOutput` forever and get replayed on the next
// `shouldClearTerminalForFrame` rewrite (overflow / leaving
// fullscreen / unmount), surfacing as "cleared history reappears"
// for apps that drive Static via key bumps.
if (rootNode.staticNode !== rootNode.previousStaticNode) {
rootNode.previousStaticNode = rootNode.staticNode;
if (typeof rootNode.onStaticChange === 'function') {
rootNode.onStaticChange();
}
}

// Since renders are throttled at the instance level and <Static> component children
// are rendered only once and then get deleted, we need an escape hatch to
// trigger an immediate render to ensure <Static> children are written to output before they get erased
Expand Down
54 changes: 54 additions & 0 deletions test/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,60 @@ test('static output stops accumulating after Static unmounts (#904)', t => {
t.true(outputAfterChurn.includes('Dynamic'));
});

test('fullStaticOutput is reset when <Static> unmounts so stale items are not replayed', t => {
// In debug mode each stdout.write is `fullStaticOutput + dynamicOutput`.
// Before the fix, the items emitted by a Static that has since been
// unmounted stayed in `fullStaticOutput` forever and reappeared on every
// subsequent write (and on any `shouldClearTerminalForFrame` rewrite at
// runtime). After the fix, dropping the <Static> resets the
// accumulator so its items leave both the React tree and the output
// stream.
const stdout = createStdout();

function App({show, dynamicLabel}: {
Comment thread
chiga0 marked this conversation as resolved.
Resolved
readonly show: boolean;
readonly dynamicLabel: string;
}) {
return (
<Box>
{show ? (
<Static items={['HISTORY-A', 'HISTORY-B']}>
{item => <Text key={item}>{item}</Text>}
</Static>
) : null}
<Text>{dynamicLabel}</Text>
</Box>
);
}

const {rerender} = render(<App show dynamicLabel="d1" />, {
stdout,
debug: true,
});

const afterMount = (stdout.write as any).lastCall.args[0] as string;
t.true(
afterMount.includes('HISTORY-A') && afterMount.includes('HISTORY-B'),
'Static items must be emitted on first mount',
);

rerender(<App show={false} dynamicLabel="d2" />);

const afterUnmount = (stdout.write as any).lastCall.args[0] as string;
t.false(
afterUnmount.includes('HISTORY-A'),
'fullStaticOutput must NOT replay HISTORY-A after Static unmount',
);
t.false(
afterUnmount.includes('HISTORY-B'),
'fullStaticOutput must NOT replay HISTORY-B after Static unmount',
);
t.true(
afterUnmount.includes('d2'),
'new dynamic output must still render',
);
});

test('render only new items in static output on final render', t => {
const stdout = createStdout();

Expand Down