| 182 | * Implementation for `resource()` which uses a `linkedSignal` to manage the resource's state. |
| 183 | */ |
| 184 | export class ResourceImpl<T, R> extends BaseWritableResource<T> implements ResourceRef<T> { |
| 185 | private readonly pendingTasks: PendingTasks; |
| 186 | |
| 187 | /** |
| 188 | * The current state of the resource. Status, value, and error are derived from this. |
| 189 | */ |
| 190 | private readonly state: WritableSignal<ResourceState<T>>; |
| 191 | |
| 192 | /** |
| 193 | * Combines the current request with a reload counter which allows the resource to be reloaded on |
| 194 | * imperative command. |
| 195 | */ |
| 196 | protected readonly extRequest: WritableSignal<WrappedRequest>; |
| 197 | private readonly effectRef: EffectRef; |
| 198 | |
| 199 | private pendingController: AbortController | undefined; |
| 200 | private resolvePendingTask: (() => void) | undefined = undefined; |
| 201 | private destroyed = false; |
| 202 | private unregisterOnDestroy: () => void; |
| 203 | |
| 204 | override readonly status: Signal<ResourceStatus>; |
| 205 | override readonly error: Signal<Error | undefined>; |
| 206 | private readonly transferState: TransferState | undefined; |
| 207 | |
| 208 | constructor( |
| 209 | request: (ctx: ResourceParamsContext) => R, |
| 210 | private readonly loaderFn: ResourceStreamingLoader<T, R>, |
| 211 | defaultValue: T, |
| 212 | private readonly equal: ValueEqualityFn<T> | undefined, |
| 213 | private readonly debugName: string | undefined, |
| 214 | injector: Injector, |
| 215 | private transferCacheKey: StateKey<T> | undefined, |
| 216 | getInitialStream?: (request: R) => Signal<ResourceStreamItem<T>> | undefined, |
| 217 | private readonly isStreaming = false, |
| 218 | ) { |
| 219 | if (isInParamsFunction()) { |
| 220 | throw invalidResourceCreationInParams(); |
| 221 | } |
| 222 | |
| 223 | super( |
| 224 | // Feed a computed signal for the value to `BaseWritableResource`, which will upgrade it to a |
| 225 | // `WritableSignal` that delegates to `ResourceImpl.set`. |
| 226 | computed( |
| 227 | () => { |
| 228 | const streamValue = this.state().stream?.(); |
| 229 | |
| 230 | if (!streamValue) { |
| 231 | return defaultValue; |
| 232 | } |
| 233 | |
| 234 | // Prevents `hasValue()` from throwing an error when a reload happened in the error state |
| 235 | if (this.state().status === 'loading' && this.error()) { |
| 236 | return defaultValue; |
| 237 | } |
| 238 | |
| 239 | if (!isResolved(streamValue)) { |
| 240 | throw new ResourceValueError(this.error()!); |
| 241 | } |
nothing calls this directly
no outgoing calls
no test coverage detected