mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* fix(editor): preserve toggle fold state across remote and local document edits
Fold state was re-derived from localStorage on every remote collaborative
transaction (and partially on local doc-changing ones), so a silently
failed Storage.set (quota exceeded, blocked storage) left a stale
{fold:true} and the next edit re-folded blocks the user had expanded —
the plugin seed from previous state was ineffective because
toggleFoldPluginKey.getState(newState) is always undefined during the
plugin's own apply.
Track a knownIds set alongside foldedIds and reconcile from the previous
plugin state (the second apply argument, which is reliable): existing
fold decisions are preserved verbatim and storage is only consulted for
toggle blocks seen for the first time.
Fixes #13424
* fix(editor): bound and harden toggle fold state persistence
Fold state was stored under a localStorage key per toggle block id with
no bound on how many keys accumulate, and a write that failed silently
(quota exceeded, storage unavailable) left a stale value that the next
edit would re-fold from. Two complementary changes:
- Storage falls back to session storage when a write to the primary
interface fails at runtime, so values at least survive the browsing
session; reads prefer the fallback copy while it is the most recent
successful write, and successful writes drop it again.
- Toggle fold state now lives in an LRUCache mirrored to local storage,
capping the number of stored keys and evicting the least recently used
fold states first. The persisted key format is unchanged so fold state
continues to sync across tabs via storage events.
Follow-up to #13425, addressing the underlying feedback in #13424.
Co-Authored-By: Claude <noreply@anthropic.com>
* docs: add development workflow best practices to CLAUDE.md
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(editor): rename persistTo option to storage in LRUCache
Rename the `persistTo` cache option to `storage` with a union type of
`"memory" | "localStorage" | "sessionStorage"` per review feedback.
The option is now required instead of optional; callers that only need
an in-memory cache pass `storage: "memory"`.
The private Storage instance field is renamed from `storage` to `store`
to avoid colliding with the new option name, and the mapping from
user-facing values to Storage constructor arguments is handled in
`hydrate()`.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(editor): eliminate redundant findBlockNodes scan in reconcileFoldState
The hot path for every local document change previously called
findBlockNodes twice on the same document — once in reconcileFoldState
and again in createDecorations. Now reconcileFoldState collects the
blocks once and passes them directly to the decoration builder.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(editor): fix createDecorations signature to accept optional blocks parameter
The init and ACTION branches pass a NodeWithPos[] as the second argument,
which previously landed in the foldedIds parameter — a type error missed
due to missing tsc validation.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(editor): correct createDecorations argument order and NodeWithPos import
The createDecorations signature is (doc, foldedIds, blocks?) but both the
init and ACTION branches passed arguments in the old order (doc, blocks,
foldedIds). Because the Set is always truthy, the ?? fallback never fired
and buildDecorationsFromBlocks iterated over strings, crashing on
block.node.attrs. NodeWithPos is also not exported from findChildren.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(editor): address review feedback and fix CI
- Document the default-folded fallback for evicted blocks in the
fold-state cache comment.
- Cast the Storage test mock through unknown so the type check passes.
- Provide in-memory web storage for the shared jsdom test project so the
persistence tests run instead of crashing on a missing implementation.
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(editor): scope storage fallback writes and drop unrelated changes
- Track keys mirrored to the storage fallback and clear only those,
rather than clearing all of session storage
- Restore CLAUDE.md as a symlink to AGENTS.md
- Remove the unrelated scrollToNonce changes
- Make the fallback interface private and probe with a namespaced key
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
205 lines
5.5 KiB
TypeScript
205 lines
5.5 KiB
TypeScript
import { Storage } from "./Storage";
|
|
|
|
/** Options accepted when constructing an `LRUCache`. */
|
|
export interface LRUCacheOptions {
|
|
/** The maximum number of entries to retain before evicting. */
|
|
max: number;
|
|
/** Prefix under which entries are stored, used to namespace and version them. */
|
|
namespace?: string;
|
|
/** Mirror entries to the given web storage, requires a namespace. */
|
|
storage: "memory" | "localStorage" | "sessionStorage";
|
|
}
|
|
|
|
/**
|
|
* A cache holding a bounded number of entries, evicting the least recently used
|
|
* once full. Reading or writing an entry marks it as the most recently used.
|
|
*
|
|
* Entries are optionally mirrored to web storage so that they survive a
|
|
* reload, in which case values must be JSON-serializable. Persisted values are
|
|
* read back only when they are first accessed, so the cache holds no more in
|
|
* memory than the session has used. Storage failures are ignored, leaving the
|
|
* cache to operate in memory alone.
|
|
*/
|
|
export class LRUCache<T> {
|
|
public constructor(options: LRUCacheOptions) {
|
|
this.max = options.max;
|
|
this.namespace = options.namespace;
|
|
this.storageType = options.storage;
|
|
}
|
|
|
|
/**
|
|
* Returns the value stored under a key, marking it as most recently used.
|
|
*
|
|
* @param key the key to look up.
|
|
* @returns the cached value, or undefined if it is not cached.
|
|
*/
|
|
public get(key: string): T | undefined {
|
|
this.hydrate();
|
|
|
|
// `has` distinguishes a missing key from one that is persisted but has not
|
|
// been read into memory yet.
|
|
const value = this.data.has(key)
|
|
? (this.data.get(key) ?? this.load(key))
|
|
: undefined;
|
|
if (value === undefined) {
|
|
return undefined;
|
|
}
|
|
|
|
// Re-insert so that the entry becomes the most recently used.
|
|
this.data.delete(key);
|
|
this.data.set(key, value);
|
|
this.writeKeys();
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* Stores a value under a key, evicting the least recently used entries when
|
|
* the cache is over its maximum size.
|
|
*
|
|
* @param key the key to store under.
|
|
* @param value the value to store.
|
|
*/
|
|
public set(key: string, value: T) {
|
|
this.hydrate();
|
|
|
|
this.data.delete(key);
|
|
this.data.set(key, value);
|
|
this.writeValue(key, value);
|
|
|
|
// Map iteration follows insertion order, so the first key is the least
|
|
// recently used.
|
|
while (this.data.size > this.max) {
|
|
const oldest = this.data.keys().next().value;
|
|
if (oldest === undefined) {
|
|
break;
|
|
}
|
|
this.data.delete(oldest);
|
|
this.removeValue(oldest);
|
|
}
|
|
|
|
this.writeKeys();
|
|
}
|
|
|
|
/**
|
|
* Returns whether a key is cached, without affecting its recency.
|
|
*
|
|
* @param key the key to look up.
|
|
* @returns true if the key is cached.
|
|
*/
|
|
public has(key: string): boolean {
|
|
this.hydrate();
|
|
return this.data.has(key);
|
|
}
|
|
|
|
/**
|
|
* Removes the entry stored under a key.
|
|
*
|
|
* @param key the key to remove.
|
|
* @returns true if an entry was removed.
|
|
*/
|
|
public delete(key: string): boolean {
|
|
this.hydrate();
|
|
|
|
if (!this.data.delete(key)) {
|
|
return false;
|
|
}
|
|
|
|
this.removeValue(key);
|
|
this.writeKeys();
|
|
return true;
|
|
}
|
|
|
|
/** Removes every entry from the cache, including any persisted copies. */
|
|
public clear() {
|
|
this.hydrate();
|
|
|
|
for (const key of this.data.keys()) {
|
|
this.removeValue(key);
|
|
}
|
|
this.data.clear();
|
|
this.writeKeys();
|
|
}
|
|
|
|
/** The number of entries currently cached. */
|
|
public get size(): number {
|
|
this.hydrate();
|
|
return this.data.size;
|
|
}
|
|
|
|
private max: number;
|
|
private namespace?: string;
|
|
private storageType: "memory" | "localStorage" | "sessionStorage";
|
|
// An entry is undefined while it is persisted but not yet read into memory.
|
|
private data = new Map<string, T | undefined>();
|
|
private store?: Storage;
|
|
private hydrated = false;
|
|
|
|
/** The storage key holding the cached keys in least-to-most recent order. */
|
|
private get keysKey(): string {
|
|
return `${this.namespace}:keys`;
|
|
}
|
|
|
|
private valueKey(key: string): string {
|
|
return `${this.namespace}:${key}`;
|
|
}
|
|
|
|
/**
|
|
* Loads persisted entries into memory, deferred until first use so that
|
|
* constructing a cache at module scope does not read from storage on import.
|
|
*/
|
|
private hydrate() {
|
|
if (this.hydrated) {
|
|
return;
|
|
}
|
|
this.hydrated = true;
|
|
|
|
if (this.storageType === "memory" || !this.namespace) {
|
|
return;
|
|
}
|
|
|
|
this.store = new Storage(
|
|
this.storageType === "localStorage" ? "local" : "session"
|
|
);
|
|
|
|
// A corrupt or absent index reads as undefined, leaving the cache empty.
|
|
const keys: unknown = this.store.get(this.keysKey);
|
|
if (!Array.isArray(keys)) {
|
|
return;
|
|
}
|
|
|
|
for (const key of keys.slice(-this.max)) {
|
|
if (typeof key === "string") {
|
|
this.data.set(key, undefined);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reads a persisted value into memory, dropping the key if its value is no
|
|
* longer stored, which happens when an earlier write exceeded the quota.
|
|
*/
|
|
private load(key: string): T | undefined {
|
|
const value: T | undefined = this.store?.get(this.valueKey(key));
|
|
if (value === undefined) {
|
|
this.data.delete(key);
|
|
this.writeKeys();
|
|
return undefined;
|
|
}
|
|
|
|
this.data.set(key, value);
|
|
return value;
|
|
}
|
|
|
|
private writeValue(key: string, value: T) {
|
|
this.store?.set(this.valueKey(key), value);
|
|
}
|
|
|
|
private removeValue(key: string) {
|
|
this.store?.remove(this.valueKey(key));
|
|
}
|
|
|
|
private writeKeys() {
|
|
this.store?.set(this.keysKey, [...this.data.keys()]);
|
|
}
|
|
}
|