Skip to content

Commit

Permalink
Tightened up debug logging messages for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Jun 24, 2023
1 parent 630509b commit f989738
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 72 deletions.
6 changes: 3 additions & 3 deletions packages/suspense/src/cache/createCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
])
);

Expand All @@ -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"),
])
);
Expand All @@ -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"),
])
);
Expand Down
54 changes: 23 additions & 31 deletions packages/suspense/src/cache/createCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function createCache<Params extends Array<any>, 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)
Expand Down Expand Up @@ -168,6 +168,8 @@ export function createCache<Params extends Array<any>, Value>(
}

function cache(value: Value, ...params: Params): void {
debugLog("cache()", params);

const cacheKey = getKey(params);
const pendingMutationRecordMap = getCacheForType(
createPendingMutationRecordMap
Expand All @@ -176,8 +178,6 @@ export function createCache<Params extends Array<any>, Value>(
let record: Record<Value> | undefined = getRecord(...params);
if (record != null) {
if (isPendingRecord(record)) {
debugLog("cache()", params, "Update pending record to:", value);

const { abortController, deferred } = record.data;

abortController.abort();
Expand All @@ -191,21 +191,17 @@ export function createCache<Params extends Array<any>, Value>(
}
}

debugLog("cache()", params, "Create new resolved record with:", value);

record = createResolvedRecord<Value>(value);

recordMap.set(cacheKey, record);
pendingMutationRecordMap.set(cacheKey, record);
}

function createPendingMutationRecordMap(): CacheMap<string, Record<Value>> {
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}`);
});
}

Expand All @@ -223,7 +219,7 @@ export function createCache<Params extends Array<any>, Value>(
createPendingMutationRecordMap
);

debugLog(`evict()`, params);
debugLog("evict()", params);

const didDelete = recordMap.delete(cacheKey);
pendingMutationRecordMap.delete(cacheKey);
Expand All @@ -238,7 +234,7 @@ export function createCache<Params extends Array<any>, Value>(
createPendingMutationRecordMap
);

debugLog(`evictAll()`, undefined);
debugLog("evictAll()", undefined);

recordMap.clear();
pendingMutationRecordMap.clear();
Expand Down Expand Up @@ -268,7 +264,8 @@ export function createCache<Params extends Array<any>, 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<Value>(
debugLabel ? `${debugLabel} ${cacheKey}` : cacheKey
Expand All @@ -281,18 +278,16 @@ export function createCache<Params extends Array<any>, 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
Expand All @@ -313,6 +308,8 @@ export function createCache<Params extends Array<any>, Value>(
}

function getValue(...params: Params): Value {
debugLog("getValue()", params);

const cacheKey = getKey(params);
const record = recordMap.get(cacheKey);

Expand All @@ -328,6 +325,8 @@ export function createCache<Params extends Array<any>, Value>(
}

function getValueIfCached(...params: Params): Value | undefined {
debugLog("getValueIfCached()", params);

const cacheKey = getKey(params);
const record = recordMap.get(cacheKey);
if (record && isResolvedRecord(record)) {
Expand All @@ -336,8 +335,6 @@ export function createCache<Params extends Array<any>, Value>(
}

function onExternalCacheEviction(key: string): void {
debugLog(`onExternalCacheEviction(${key})`);

const set = subscriberMap.get(key);
if (set) {
set.forEach((callback) => {
Expand All @@ -347,7 +344,7 @@ export function createCache<Params extends Array<any>, Value>(
}

function prefetch(...params: Params): void {
debugLog(`prefetch()`, params);
debugLog("prefetch()", params);

const promiseOrValue = readAsync(...params);
if (isPromiseLike(promiseOrValue)) {
Expand All @@ -361,8 +358,7 @@ export function createCache<Params extends Array<any>, Value>(
}

function readAsync(...params: Params): PromiseLike<Value> | Value {
debugLog(`readAsync()`, params);

// getOrCreateRecord() will call debugLog (cache hit or miss)
const record = getOrCreateRecord(...params);
if (isPendingRecord(record)) {
return record.data.deferred.promise;
Expand All @@ -374,8 +370,7 @@ export function createCache<Params extends Array<any>, 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;
Expand Down Expand Up @@ -413,19 +408,14 @@ export function createCache<Params extends Array<any>, 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);
Expand All @@ -441,6 +431,8 @@ export function createCache<Params extends Array<any>, Value>(
callback: StatusCallback,
...params: Params
): UnsubscribeCallback {
debugLog("subscribeToStatus()", params);

const cacheKey = getKey(params);
let set = subscriberMap.get(cacheKey);
if (set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
])
);

Expand All @@ -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"),
])
);
Expand All @@ -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"),
])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -231,7 +231,7 @@ export function createIntervalCache<
}

function evictAll(): boolean {
debugLog(`evictAll()`, undefined, `${metadataMap.size} records`);
debugLog("evictAll()");

const hadValues = metadataMap.size > 0;

Expand Down Expand Up @@ -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<Value[]>(
debugLabel ? `${debugLabel}: ${cacheKey}` : `${cacheKey}`
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -633,12 +630,6 @@ export function createIntervalCache<
...pendingPromiseLikes,
]);

debugLog(
`processPendingRecord(${start}, ${end}): resolved`,
params,
values
);

if (!signal.aborted) {
let value = sliceValues<Point, Value>(
metadata.sortedValues,
Expand All @@ -660,6 +651,12 @@ export function createIntervalCache<
value,
};

debugLog(
`read() Pending request resolved (${start}, ${end})`,
params,
values
);

deferred.resolve(value);

notifySubscribers(start, end, ...params);
Expand All @@ -673,7 +670,7 @@ export function createIntervalCache<
}

debugLog(
`processPendingRecord(${start}, ${end}): failed`,
`read() Pending request rejected (${start}, ${end})`,
params,
errorMessage
);
Expand All @@ -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[];
Expand All @@ -709,8 +705,7 @@ export function createIntervalCache<
end: Point,
...params: Params
): PromiseLike<Value[]> | 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:
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/suspense/src/cache/createStreamingCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
])
);

Expand Down
Loading

0 comments on commit f989738

Please sign in to comment.