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 dde9d5d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 62 deletions.
2 changes: 1 addition & 1 deletion 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 Down
48 changes: 19 additions & 29 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,6 @@ export function createCache<Params extends Array<any>, Value>(

let record = getRecord(...params);
if (record == null) {
debugLog("getOrCreateRecord(): Cache miss. Creating record...", params);
const abortController = new AbortController();
const deferred = createDeferred<Value>(
debugLabel ? `${debugLabel} ${cacheKey}` : cacheKey
Expand All @@ -283,16 +278,12 @@ export function createCache<Params extends Array<any>, Value>(
processPendingRecord(abortController.signal, record, ...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 +304,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 +321,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 +331,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 +340,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,7 +354,7 @@ export function createCache<Params extends Array<any>, Value>(
}

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

const record = getOrCreateRecord(...params);
if (isPendingRecord(record)) {
Expand All @@ -374,7 +367,7 @@ export function createCache<Params extends Array<any>, Value>(
}

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

const record = getOrCreateRecord(...params);
if (isPendingRecord(record)) {
Expand Down Expand Up @@ -413,19 +406,14 @@ export function createCache<Params extends Array<any>, Value>(
: valueOrPromiseLike;

if (!abortSignal.aborted) {
debugLog(
"processPendingRecord(): resolved",
params,
"resolved value: ",
value
);
debugLog("Pending request resolved", params, value);
updateRecordToResolved(record, value);

deferred.resolve(value);
}
} catch (error) {
if (!abortSignal.aborted) {
debugLog("processPendingRecord(): rejected", params, error);
debugLog("Pending request rejected", params, error);
updateRecordToRejected(record, error);

deferred.reject(error);
Expand All @@ -441,6 +429,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 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 @@ -308,6 +308,8 @@ export function createIntervalCache<
}

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 +555,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 +566,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 +626,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 +647,8 @@ export function createIntervalCache<
value,
};

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

deferred.resolve(value);

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

debugLog(
`processPendingRecord(${start}, ${end}): failed`,
`Pending request rejected (${start}, ${end})`,
params,
errorMessage
);
Expand Down Expand Up @@ -728,6 +717,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
19 changes: 10 additions & 9 deletions packages/suspense/src/cache/createStreamingCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function createStreamingCache<
]);
};

debugLog("Creating cache ...");
debugLog("Cache created");

const abortControllerMap = new Map<string, AbortController>();
const streamingValuesMap = new Map<
Expand Down Expand Up @@ -101,7 +101,7 @@ export function createStreamingCache<
}

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

const hadValues = streamingValuesMap.size > 0;

Expand All @@ -117,11 +117,6 @@ export function createStreamingCache<

let cached = streamingValuesMap.get(cacheKey);
if (cached == null) {
debugLog(
"getOrCreateStreamingValue(): Cache miss. Creating streaming value...",
params
);

const deferred = createDeferred<StreamingValue<Value, AdditionalData>>(
debugLabel ? `${debugLabel}: ${cacheKey}` : cacheKey
);
Expand Down Expand Up @@ -165,6 +160,8 @@ export function createStreamingCache<
return false;
}

debugLog(`Pending request aborted`, params);

streamingValues.status = STATUS_ABORTED;

streamingValuesMap.delete(cacheKey);
Expand Down Expand Up @@ -199,6 +196,8 @@ export function createStreamingCache<
notifySubscribers();
},
resolve: () => {
debugLog(`Pending request resolved`, params);

assertPending();

streamingValues.complete = true;
Expand All @@ -210,6 +209,8 @@ export function createStreamingCache<
deferred.resolve(streamingValues);
},
reject: (error: Error) => {
debugLog(`Pending request rejected`, params);

assertPending();

streamingValues.complete = true;
Expand All @@ -233,13 +234,13 @@ export function createStreamingCache<
}

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

getOrCreateStreamingValue(...params);
}

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

return getOrCreateStreamingValue(...params);
}
Expand Down

0 comments on commit dde9d5d

Please sign in to comment.