MCPcopy Create free account
hub / github.com/angular/angular / constructor

Method constructor

packages/core/src/resource/resource.ts:208–364  ·  view source on GitHub ↗
(
    request: (ctx: ResourceParamsContext) => R,
    private readonly loaderFn: ResourceStreamingLoader<T, R>,
    defaultValue: T,
    private readonly equal: ValueEqualityFn<T> | undefined,
    private readonly debugName: string | undefined,
    injector: Injector,
    private transferCacheKey: StateKey<T> | undefined,
    getInitialStream?: (request: R) => Signal<ResourceStreamItem<T>> | undefined,
    private readonly isStreaming = false,
  )

Source from the content-addressed store, hash-verified

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 }
242
243 return streamValue.value;
244 },
245 {equal, ...(ngDevMode ? createDebugNameObject(debugName, 'value') : undefined)},
246 ),
247 debugName,
248 );
249
250 const cacheState = injector.get(CACHE_ACTIVE, undefined, {optional: true}) ?? {isActive: false};
251
252 this.transferState = injector.get(TransferState, undefined, {optional: true}) ?? undefined;
253
254 const paramsSignal = isSignal(request)
255 ? request
256 : computed(
257 () => {
258 try {
259 setInParamsFunction(true);
260 return request(paramsContext);
261 } finally {
262 setInParamsFunction(false);
263 }
264 },
265 ngDevMode ? createDebugNameObject(debugName, 'params') : undefined,

Callers

nothing calls this directly

Calls 15

destroyMethod · 0.95
computedFunction · 0.90
isSignalFunction · 0.90
linkedSignalFunction · 0.90
signalFunction · 0.90
effectFunction · 0.90
isInParamsFunctionFunction · 0.85
isResolvedFunction · 0.85
setInParamsFunctionFunction · 0.85
requestFunction · 0.85
rethrowFatalErrorsFunction · 0.85

Tested by

no test coverage detected