|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { InvariantError } from '../../shared/lib/invariant-error' |
|
|
|
|
|
export class CacheSignal { |
|
|
private count = 0 |
|
|
private earlyListeners: Array<() => void> = [] |
|
|
private listeners: Array<() => void> = [] |
|
|
private tickPending = false |
|
|
private taskPending = false |
|
|
|
|
|
private subscribedSignals: Set<CacheSignal> | null = null |
|
|
|
|
|
constructor() { |
|
|
if (process.env.NEXT_RUNTIME === 'edge') { |
|
|
|
|
|
throw new InvariantError( |
|
|
'CacheSignal cannot be used in the edge runtime, because `cacheComponents` does not support it.' |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
private noMorePendingCaches() { |
|
|
if (!this.tickPending) { |
|
|
this.tickPending = true |
|
|
process.nextTick(() => { |
|
|
this.tickPending = false |
|
|
if (this.count === 0) { |
|
|
for (let i = 0; i < this.earlyListeners.length; i++) { |
|
|
this.earlyListeners[i]() |
|
|
} |
|
|
this.earlyListeners.length = 0 |
|
|
} |
|
|
}) |
|
|
} |
|
|
if (!this.taskPending) { |
|
|
this.taskPending = true |
|
|
setTimeout(() => { |
|
|
this.taskPending = false |
|
|
if (this.count === 0) { |
|
|
for (let i = 0; i < this.listeners.length; i++) { |
|
|
this.listeners[i]() |
|
|
} |
|
|
this.listeners.length = 0 |
|
|
} |
|
|
}, 0) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inputReady() { |
|
|
return new Promise<void>((resolve) => { |
|
|
this.earlyListeners.push(resolve) |
|
|
if (this.count === 0) { |
|
|
this.noMorePendingCaches() |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cacheReady() { |
|
|
return new Promise<void>((resolve) => { |
|
|
this.listeners.push(resolve) |
|
|
if (this.count === 0) { |
|
|
this.noMorePendingCaches() |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
beginRead() { |
|
|
this.count++ |
|
|
|
|
|
if (this.subscribedSignals !== null) { |
|
|
for (const subscriber of this.subscribedSignals) { |
|
|
subscriber.beginRead() |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
endRead() { |
|
|
if (this.count === 0) { |
|
|
throw new InvariantError( |
|
|
'CacheSignal got more endRead() calls than beginRead() calls' |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.count-- |
|
|
if (this.count === 0) { |
|
|
this.noMorePendingCaches() |
|
|
} |
|
|
|
|
|
if (this.subscribedSignals !== null) { |
|
|
for (const subscriber of this.subscribedSignals) { |
|
|
subscriber.endRead() |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
trackRead<T>(promise: Promise<T>) { |
|
|
this.beginRead() |
|
|
|
|
|
const onFinally = this.endRead.bind(this) |
|
|
promise.then(onFinally, onFinally) |
|
|
return promise |
|
|
} |
|
|
|
|
|
subscribeToReads(subscriber: CacheSignal): () => void { |
|
|
if (subscriber === this) { |
|
|
throw new InvariantError('A CacheSignal cannot subscribe to itself') |
|
|
} |
|
|
if (this.subscribedSignals === null) { |
|
|
this.subscribedSignals = new Set() |
|
|
} |
|
|
this.subscribedSignals.add(subscriber) |
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.count; i++) { |
|
|
subscriber.beginRead() |
|
|
} |
|
|
|
|
|
return this.unsubscribeFromReads.bind(this, subscriber) |
|
|
} |
|
|
|
|
|
unsubscribeFromReads(subscriber: CacheSignal) { |
|
|
if (!this.subscribedSignals) { |
|
|
return |
|
|
} |
|
|
this.subscribedSignals.delete(subscriber) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|