UNPKG

static-injector

Version:

Angular 依赖注入独立版本;Angular dependency injection standalone version

119 lines (118 loc) 5.32 kB
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import { Signal, ValueEqualityFn } from '../render3/reactivity/api'; import { WritableSignal } from '../render3/reactivity/signal'; import { Resource, ResourceOptions, ResourceSnapshot, ResourceStatus, ResourceStreamingLoader, ResourceStreamItem, type ResourceParamsContext, type ResourceRef, type WritableResource } from './api'; import { Injector } from '../di/injector'; import { StateKey } from '../transfer_state'; /** * Constructs a `Resource` that projects a reactive request to an asynchronous operation defined by * a loader function, which exposes the result of the loading operation via signals. * * Note that `resource` is intended for _read_ operations, not operations which perform mutations. * `resource` will cancel in-progress loads via the `AbortSignal` when destroyed or when a new * request object becomes available, which could prematurely abort mutations. * * @see [Async reactivity with resources](guide/signals/resource) * * @publicApi 22.0 */ export declare function resource<T, R>(options: ResourceOptions<T, R> & { defaultValue: NoInfer<T>; }): ResourceRef<T>; /** * Constructs a `Resource` that projects a reactive request to an asynchronous operation defined by * a loader function, which exposes the result of the loading operation via signals. * * Note that `resource` is intended for _read_ operations, not operations which perform mutations. * `resource` will cancel in-progress loads via the `AbortSignal` when destroyed or when a new * request object becomes available, which could prematurely abort mutations. * * @publicApi 22.0 * @see [Async reactivity with resources](guide/signals/resource) */ export declare function resource<T, R>(options: ResourceOptions<T, R>): ResourceRef<T | undefined>; type ResourceInternalStatus = 'idle' | 'loading' | 'resolved' | 'local'; type WrappedRequest = { request?: unknown; reload: number; status?: ResourceInternalStatus; error?: Error; }; /** * Base class which implements `.value` as a `WritableSignal` by delegating `.set` and `.update`. */ declare abstract class BaseWritableResource<T> implements WritableResource<T> { readonly value: WritableSignal<T>; abstract readonly status: Signal<ResourceStatus>; abstract readonly error: Signal<Error | undefined>; abstract reload(): boolean; readonly isLoading: Signal<boolean>; constructor(value: Signal<T>, debugName: string | undefined); abstract set(value: T): void; private readonly isError; update(updateFn: (value: T) => T): void; private readonly isValueDefined; private _snapshot; get snapshot(): Signal<ResourceSnapshot<T>>; hasValue(): this is ResourceRef<Exclude<T, undefined>>; asReadonly(): Resource<T>; } /** * Implementation for `resource()` which uses a `linkedSignal` to manage the resource's state. */ export declare class ResourceImpl<T, R> extends BaseWritableResource<T> implements ResourceRef<T> { private readonly loaderFn; private readonly equal; private readonly debugName; private transferCacheKey; private readonly pendingTasks; /** * The current state of the resource. Status, value, and error are derived from this. */ private readonly state; /** * Combines the current request with a reload counter which allows the resource to be reloaded on * imperative command. */ protected readonly extRequest: WritableSignal<WrappedRequest>; private readonly effectRef; private pendingController; private resolvePendingTask; private destroyed; private unregisterOnDestroy; readonly status: Signal<ResourceStatus>; readonly error: Signal<Error | undefined>; private readonly transferState; constructor(request: (ctx: ResourceParamsContext) => R, loaderFn: ResourceStreamingLoader<T, R>, defaultValue: T, equal: ValueEqualityFn<T> | undefined, debugName: string | undefined, injector: Injector, transferCacheKey: StateKey<T> | undefined, getInitialStream?: (request: R) => Signal<ResourceStreamItem<T>> | undefined); /** * Called either directly via `WritableResource.set` or via `.value.set()`. */ set(value: T): void; reload(): boolean; destroy(): void; private loadEffect; private abortInProgressLoad; } export declare function encapsulateResourceError(error: unknown): Error; export declare function isErrorLike(error: unknown): error is Error; export declare class ResourceValueError extends Error { constructor(error: Error); } /** * Chains the value of another resource into the params of the current resource, returning the value * of the other resource if it is available, or propagating the status to the current resource if it * is not. */ export declare function chain<T>(resource: Resource<T>): T; export declare const paramsContext: ResourceParamsContext; export declare function isInParamsFunction(): boolean; export declare function setInParamsFunction(value: boolean): void; export declare function invalidResourceCreationInParams(): Error; export declare function rethrowFatalErrors(error: unknown): void; export {};