diff --git a/packages/suspense/src/cache/createCache.test.ts b/packages/suspense/src/cache/createCache.test.ts index ce2f218..029ab23 100644 --- a/packages/suspense/src/cache/createCache.test.ts +++ b/packages/suspense/src/cache/createCache.test.ts @@ -770,7 +770,7 @@ describe("createCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("Creating cache"), + expect.stringContaining("Cache created"), ]) ); @@ -780,7 +780,7 @@ describe("createCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("readAsync"), + expect.stringContaining("read"), expect.stringContaining("one"), ]) ); @@ -797,7 +797,7 @@ describe("createCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("readAsync"), + expect.stringContaining("read"), expect.stringContaining("three"), ]) ); diff --git a/packages/suspense/src/cache/createCache.ts b/packages/suspense/src/cache/createCache.ts index e53de70..48c640e 100644 --- a/packages/suspense/src/cache/createCache.ts +++ b/packages/suspense/src/cache/createCache.ts @@ -103,7 +103,7 @@ export function createCache, Value>( ]); }; - debugLog("Creating cache ..."); + debugLog("Cache created"); // This map enables selective mutations to be scheduled with React // (one record can be invalidated without affecting others) @@ -168,6 +168,8 @@ export function createCache, Value>( } function cache(value: Value, ...params: Params): void { + debugLog("cache()", params); + const cacheKey = getKey(params); const pendingMutationRecordMap = getCacheForType( createPendingMutationRecordMap @@ -176,8 +178,6 @@ export function createCache, Value>( let record: Record | undefined = getRecord(...params); if (record != null) { if (isPendingRecord(record)) { - debugLog("cache()", params, "Update pending record to:", value); - const { abortController, deferred } = record.data; abortController.abort(); @@ -191,8 +191,6 @@ export function createCache, Value>( } } - debugLog("cache()", params, "Create new resolved record with:", value); - record = createResolvedRecord(value); recordMap.set(cacheKey, record); @@ -200,12 +198,10 @@ export function createCache, Value>( } function createPendingMutationRecordMap(): CacheMap> { - return getCache((key) => { + return getCache(() => { // We don't really need to do anything here // This map will almost always be a subset of the recordMap // but we also don't want it to bypass the getCache() eviction logic (if any) - // Leave a debug log here in case we need to revisit this - debugLog(`getCache() ${key}`); }); } @@ -223,7 +219,7 @@ export function createCache, Value>( createPendingMutationRecordMap ); - debugLog(`evict()`, params); + debugLog("evict()", params); const didDelete = recordMap.delete(cacheKey); pendingMutationRecordMap.delete(cacheKey); @@ -238,7 +234,7 @@ export function createCache, Value>( createPendingMutationRecordMap ); - debugLog(`evictAll()`, undefined); + debugLog("evictAll()", undefined); recordMap.clear(); pendingMutationRecordMap.clear(); @@ -268,7 +264,8 @@ export function createCache, Value>( let record = getRecord(...params); if (record == null) { - debugLog("getOrCreateRecord(): Cache miss. Creating record...", params); + debugLog("read() Cache miss", params); + const abortController = new AbortController(); const deferred = createDeferred( debugLabel ? `${debugLabel} ${cacheKey}` : cacheKey @@ -281,18 +278,16 @@ export function createCache, Value>( notifySubscribers(params); processPendingRecord(abortController.signal, record, ...params); + } else { + debugLog("read() Cache hit", params); } - debugLog( - "getOrCreateRecord(): Cache hit. Returning record", - params, - record - ); - return record; } function getStatus(...params: Params): Status { + debugLog("getStatus()", params); + const cacheKey = getKey(params); // Check for pending mutations first @@ -313,6 +308,8 @@ export function createCache, Value>( } function getValue(...params: Params): Value { + debugLog("getValue()", params); + const cacheKey = getKey(params); const record = recordMap.get(cacheKey); @@ -328,6 +325,8 @@ export function createCache, Value>( } function getValueIfCached(...params: Params): Value | undefined { + debugLog("getValueIfCached()", params); + const cacheKey = getKey(params); const record = recordMap.get(cacheKey); if (record && isResolvedRecord(record)) { @@ -336,8 +335,6 @@ export function createCache, Value>( } function onExternalCacheEviction(key: string): void { - debugLog(`onExternalCacheEviction(${key})`); - const set = subscriberMap.get(key); if (set) { set.forEach((callback) => { @@ -347,7 +344,7 @@ export function createCache, Value>( } function prefetch(...params: Params): void { - debugLog(`prefetch()`, params); + debugLog("prefetch()", params); const promiseOrValue = readAsync(...params); if (isPromiseLike(promiseOrValue)) { @@ -361,8 +358,7 @@ export function createCache, Value>( } function readAsync(...params: Params): PromiseLike | Value { - debugLog(`readAsync()`, params); - + // getOrCreateRecord() will call debugLog (cache hit or miss) const record = getOrCreateRecord(...params); if (isPendingRecord(record)) { return record.data.deferred.promise; @@ -374,8 +370,7 @@ export function createCache, Value>( } function read(...params: Params): Value { - debugLog(`read()`, params); - + // getOrCreateRecord() will call debugLog (cache hit or miss) const record = getOrCreateRecord(...params); if (isPendingRecord(record)) { throw record.data.deferred.promise; @@ -413,19 +408,14 @@ export function createCache, Value>( : valueOrPromiseLike; if (!abortSignal.aborted) { - debugLog( - "processPendingRecord(): resolved", - params, - "resolved value: ", - value - ); + debugLog("read() Pending request resolved", params, value); updateRecordToResolved(record, value); deferred.resolve(value); } } catch (error) { if (!abortSignal.aborted) { - debugLog("processPendingRecord(): rejected", params, error); + debugLog("read() Pending request rejected", params, error); updateRecordToRejected(record, error); deferred.reject(error); @@ -441,6 +431,8 @@ export function createCache, Value>( callback: StatusCallback, ...params: Params ): UnsubscribeCallback { + debugLog("subscribeToStatus()", params); + const cacheKey = getKey(params); let set = subscriberMap.get(cacheKey); if (set) { diff --git a/packages/suspense/src/cache/createIntervalCache/createIntervalCache.test.ts b/packages/suspense/src/cache/createIntervalCache/createIntervalCache.test.ts index f036da2..5086e3d 100644 --- a/packages/suspense/src/cache/createIntervalCache/createIntervalCache.test.ts +++ b/packages/suspense/src/cache/createIntervalCache/createIntervalCache.test.ts @@ -1029,7 +1029,7 @@ describe("createIntervalCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("Creating cache"), + expect.stringContaining("Cache created"), ]) ); @@ -1039,7 +1039,7 @@ describe("createIntervalCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("readAsync"), + expect.stringContaining("read"), expect.stringContaining("one"), ]) ); @@ -1056,7 +1056,7 @@ describe("createIntervalCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("readAsync"), + expect.stringContaining("read"), expect.stringContaining("three"), ]) ); diff --git a/packages/suspense/src/cache/createIntervalCache/createIntervalCache.ts b/packages/suspense/src/cache/createIntervalCache/createIntervalCache.ts index 7d8d755..6ae019b 100644 --- a/packages/suspense/src/cache/createIntervalCache/createIntervalCache.ts +++ b/packages/suspense/src/cache/createIntervalCache/createIntervalCache.ts @@ -139,9 +139,11 @@ export function createIntervalCache< ]); }; - debugLog("Creating cache ..."); + debugLog("Cache created"); function abort(...params: Params): boolean { + debugLog("abort()", params); + const metadataMapKey = getKey(...params); let caught; @@ -150,8 +152,6 @@ export function createIntervalCache< if (metadata) { const { pendingMetadata } = metadata; if (pendingMetadata.length > 0) { - debugLog("abort()", params); - const cloned = [...pendingMetadata]; pendingMetadata.splice(0); @@ -231,7 +231,7 @@ export function createIntervalCache< } function evictAll(): boolean { - debugLog(`evictAll()`, undefined, `${metadataMap.size} records`); + debugLog("evictAll()"); const hadValues = metadataMap.size > 0; @@ -278,6 +278,8 @@ export function createIntervalCache< let record = metadata.recordMap.get(cacheKey); if (record == null) { + debugLog(`read(${start}, ${end}) Cache miss`, params); + const abortController = new AbortController(); const deferred = createDeferred( debugLabel ? `${debugLabel}: ${cacheKey}` : `${cacheKey}` @@ -302,12 +304,16 @@ export function createIntervalCache< end, ...params ); + } else { + debugLog(`read(${start}, ${end}) Cache hit`, params); } return record; } function getStatus(start: Point, end: Point, ...params: Params) { + debugLog(`getStatus(${start}, ${end})`, params); + const metadata = getOrCreateIntervalMetadata(...params); const cacheKey = createCacheKey(start, end); @@ -553,15 +559,6 @@ export function createIntervalCache< intervalUtils ); - debugLog( - `processPendingRecord(${start}, ${end})`, - params, - "\n-> metadata:", - metadata, - "\n-> found:", - foundIntervals - ); - // If any of the unloaded intervals contain a failed request, // we shouldn't try loading them again // This is admittedly somewhat arbitrary but matches Replay's functionality @@ -573,7 +570,7 @@ export function createIntervalCache< ); if (previouslyFailedInterval != null) { const error = Error( - `Cannot load interval that contains previously failed interval` + "Cannot load interval that contains previously failed interval" ); record.data = { error, @@ -633,12 +630,6 @@ export function createIntervalCache< ...pendingPromiseLikes, ]); - debugLog( - `processPendingRecord(${start}, ${end}): resolved`, - params, - values - ); - if (!signal.aborted) { let value = sliceValues( metadata.sortedValues, @@ -660,6 +651,12 @@ export function createIntervalCache< value, }; + debugLog( + `read() Pending request resolved (${start}, ${end})`, + params, + values + ); + deferred.resolve(value); notifySubscribers(start, end, ...params); @@ -673,7 +670,7 @@ export function createIntervalCache< } debugLog( - `processPendingRecord(${start}, ${end}): failed`, + `read() Pending request rejected (${start}, ${end})`, params, errorMessage ); @@ -692,8 +689,7 @@ export function createIntervalCache< } function read(start: Point, end: Point, ...params: Params): Value[] { - debugLog(`read(${start}, ${end})`, params); - + // getOrCreateRecord() will call debugLog (cache hit or miss) const record = getOrCreateRecord(start, end, ...params); if (record.data.status === STATUS_RESOLVED) { return record.data.value as Value[]; @@ -709,8 +705,7 @@ export function createIntervalCache< end: Point, ...params: Params ): PromiseLike | Value[] { - debugLog(`readAsync(${start}, ${end})`, params); - + // getOrCreateRecord() will call debugLog (cache hit or miss) const record = getOrCreateRecord(start, end, ...params); switch (record.data.status) { case STATUS_PENDING: @@ -728,6 +723,8 @@ export function createIntervalCache< end: Point, ...params: Params ) { + debugLog(`subscribeToStatus(${start}, ${end})`, params); + const cacheKey = getKey(...params); let tree = subscriberMap.get(cacheKey); diff --git a/packages/suspense/src/cache/createStreamingCache.test.ts b/packages/suspense/src/cache/createStreamingCache.test.ts index d5a2e65..ddae588 100644 --- a/packages/suspense/src/cache/createStreamingCache.test.ts +++ b/packages/suspense/src/cache/createStreamingCache.test.ts @@ -507,7 +507,7 @@ describe("createStreamingCache", () => { expect(consoleMock.mock.calls[0]).toEqual( expect.arrayContaining([ expect.stringContaining("test-cache"), - expect.stringContaining("Creating cache"), + expect.stringContaining("Cache created"), ]) ); diff --git a/packages/suspense/src/cache/createStreamingCache.ts b/packages/suspense/src/cache/createStreamingCache.ts index db51085..88c5ab1 100644 --- a/packages/suspense/src/cache/createStreamingCache.ts +++ b/packages/suspense/src/cache/createStreamingCache.ts @@ -63,7 +63,7 @@ export function createStreamingCache< ]); }; - debugLog("Creating cache ..."); + debugLog("Cache created"); const abortControllerMap = new Map(); const streamingValuesMap = new Map< @@ -101,7 +101,7 @@ export function createStreamingCache< } function evictAll(): boolean { - debugLog(`evictAll()`, undefined, `${streamingValuesMap.size} records`); + debugLog("evictAll()"); const hadValues = streamingValuesMap.size > 0; @@ -117,10 +117,7 @@ export function createStreamingCache< let cached = streamingValuesMap.get(cacheKey); if (cached == null) { - debugLog( - "getOrCreateStreamingValue(): Cache miss. Creating streaming value...", - params - ); + debugLog("stream() Cache miss", params); const deferred = createDeferred>( debugLabel ? `${debugLabel}: ${cacheKey}` : cacheKey @@ -165,6 +162,8 @@ export function createStreamingCache< return false; } + debugLog(`stream() Pending request aborted`, params); + streamingValues.status = STATUS_ABORTED; streamingValuesMap.delete(cacheKey); @@ -199,6 +198,8 @@ export function createStreamingCache< notifySubscribers(); }, resolve: () => { + debugLog(`stream() Pending request resolved`, params); + assertPending(); streamingValues.complete = true; @@ -210,6 +211,8 @@ export function createStreamingCache< deferred.resolve(streamingValues); }, reject: (error: Error) => { + debugLog(`stream() Pending request rejected`, params); + assertPending(); streamingValues.complete = true; @@ -229,18 +232,19 @@ export function createStreamingCache< return streamingValues; } + debugLog("stream() Cache hit", params); + return cached; } function prefetch(...params: Params): void { - debugLog(`prefetch()`, params); + debugLog("prefetch()", params); getOrCreateStreamingValue(...params); } function stream(...params: Params) { - debugLog(`stream()`, params); - + // getOrCreateStreamingValue() will call debugLog (cache hit or miss) return getOrCreateStreamingValue(...params); }