Skip to content

Commit 3dd0cfc

Browse files
committed
Fix bug in repeated collapse of empty bodies
The initial rendering of the empty body wrote '' to the format cache, and observes it. The second rendering reads from the cache, but then because the value is falsey it writes to the cache again! This write side-effect in a computed on an observable is bad bad bad and breaks the rendering. We now properly distinguish cache presence, not just falsey-ness.
1 parent 7ecda0e commit 3dd0cfc

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/components/editor/content-viewer.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ export class ContentViewer extends React.Component<ContentViewerProps> {
142142

143143
const { cache } = this.props;
144144
const cacheKey = this.formatter.cacheKey;
145-
const cachedValue = cache.get(cacheKey) as ObservablePromise<string> | string | undefined;
145+
const hasCachedValue = cache.has(cacheKey);
146146

147-
const renderingContent = cachedValue ||
148-
this.formatter.render(this.contentBuffer, this.props.headers) as ObservablePromise<string> | string;
149-
if (!cachedValue) cache.set(cacheKey, renderingContent);
147+
let renderingContent: string | ObservablePromise<string>;
148+
if (hasCachedValue) {
149+
const cachedValue = cache.get(cacheKey) as ObservablePromise<string> | string;
150+
renderingContent = cachedValue;
151+
} else {
152+
renderingContent = this.formatter.render(this.contentBuffer, this.props.headers) as ObservablePromise<string> | string;
153+
cache.set(cacheKey, renderingContent);
154+
}
150155

151156
if (typeof renderingContent === 'string') {
152157
return renderingContent;

src/model/observable-cache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export class ObservableCache<T extends {
2424
return this.#lazyInstData;
2525
}
2626

27+
has<K extends keyof T>(key: K): boolean {
28+
return key in this.#data;
29+
}
30+
2731
get<K extends keyof T>(key: K): T[K] | undefined {
2832
return this.#data[key] as T[K] | undefined;
2933
}

0 commit comments

Comments
 (0)