static-injector
Version:
Angular 依赖注入独立版本;Angular dependency injection standalone version
90 lines (89 loc) • 2.67 kB
TypeScript
/**
* @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
*/
/**
* A type-safe key to use with `TransferState`.
*
* Example:
*
* ```ts
* const COUNTER_KEY = makeStateKey<number>('counter');
* let value = 10;
*
* transferState.set(COUNTER_KEY, value);
* ```
*
* @publicApi
*/
export type StateKey<T> = string & {
__not_a_string: never;
__value_type?: T;
};
/**
* Create a `StateKey<T>` that can be used to store value of type T with `TransferState`.
*
* Example:
*
* ```ts
* const COUNTER_KEY = makeStateKey<number>('counter');
* let value = 10;
*
* transferState.set(COUNTER_KEY, value);
* ```
*
* @publicApi
*/
export declare function makeStateKey<T = void>(key: string): StateKey<T>;
/**
* A key value store that is transferred from the application on the server side to the application
* on the client side.
*
* The `TransferState` is available as an injectable token.
* On the client, just inject this token using DI and use it, it will be lazily initialized.
* On the server it's already included if `renderApplication` function is used. Otherwise, import
* the `ServerTransferStateModule` module to make the `TransferState` available.
*
* The values in the store are serialized/deserialized using JSON.stringify/JSON.parse. So only
* boolean, number, string, null and non-class objects will be serialized and deserialized in a
* non-lossy manner.
*
* @publicApi
*/
export declare class TransferState {
/** @nocollapse */
static ɵprov: import("./di/interface/defs").ɵɵInjectableDeclaration<TransferState>;
private onSerializeCallbacks;
/**
* Get the value corresponding to a key. Return `defaultValue` if key is not found.
*/
get<T>(key: StateKey<T>, defaultValue: T): T;
/**
* Set the value corresponding to a key.
*/
set<T>(key: StateKey<T>, value: T): void;
/**
* Remove a key from the store.
*/
remove<T>(key: StateKey<T>): void;
/**
* Test whether a key exists in the store.
*/
hasKey<T>(key: StateKey<T>): boolean;
/**
* Indicates whether the state is empty.
*/
get isEmpty(): boolean;
/**
* Register a callback to provide the value for a key when `toJson` is called.
*/
onSerialize<T>(key: StateKey<T>, callback: () => T): void;
/**
* Serialize the current state of the store to JSON.
*/
toJson(): string;
}
export declare function retrieveTransferredState(doc: Document, appId: string): Record<string, unknown | undefined>;