UNPKG

2.51 kBTypeScriptView Raw
1import { IType, IAnyType, IAnyStateTreeNode, IAnyComplexType, IMaybe, ReferenceIdentifier, IStateTreeNode } from "../../internal";
2export declare type OnReferenceInvalidatedEvent<STN extends IAnyStateTreeNode> = {
3 parent: IAnyStateTreeNode;
4 invalidTarget: STN | undefined;
5 invalidId: ReferenceIdentifier;
6 replaceRef: (newRef: STN | null | undefined) => void;
7 removeRef: () => void;
8 cause: "detach" | "destroy" | "invalidSnapshotReference";
9};
10export declare type OnReferenceInvalidated<STN extends IAnyStateTreeNode> = (event: OnReferenceInvalidatedEvent<STN>) => void;
11/** @hidden */
12export declare type ReferenceT<IT extends IAnyType> = IT["TypeWithoutSTN"] & IStateTreeNode<IReferenceType<IT>>;
13export interface ReferenceOptionsGetSet<IT extends IAnyComplexType> {
14 get(identifier: ReferenceIdentifier, parent: IAnyStateTreeNode | null): ReferenceT<IT>;
15 set(value: ReferenceT<IT>, parent: IAnyStateTreeNode | null): ReferenceIdentifier;
16}
17export interface ReferenceOptionsOnInvalidated<IT extends IAnyComplexType> {
18 onInvalidated: OnReferenceInvalidated<ReferenceT<IT>>;
19}
20export declare type ReferenceOptions<IT extends IAnyComplexType> = ReferenceOptionsGetSet<IT> | ReferenceOptionsOnInvalidated<IT> | (ReferenceOptionsGetSet<IT> & ReferenceOptionsOnInvalidated<IT>);
21/** @hidden */
22export interface IReferenceType<IT extends IAnyComplexType> extends IType<ReferenceIdentifier, ReferenceIdentifier, IT["TypeWithoutSTN"]> {
23}
24/**
25 * `types.reference` - Creates a reference to another type, which should have defined an identifier.
26 * See also the [reference and identifiers](https://github.com/mobxjs/mobx-state-tree#references-and-identifiers) section.
27 */
28export declare function reference<IT extends IAnyComplexType>(subType: IT, options?: ReferenceOptions<IT>): IReferenceType<IT>;
29/**
30 * Returns if a given value represents a reference type.
31 *
32 * @param type
33 * @returns
34 */
35export declare function isReferenceType<IT extends IReferenceType<any>>(type: IT): type is IT;
36export declare function safeReference<IT extends IAnyComplexType>(subType: IT, options: (ReferenceOptionsGetSet<IT> | {}) & {
37 acceptsUndefined: false;
38 onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>;
39}): IReferenceType<IT>;
40export declare function safeReference<IT extends IAnyComplexType>(subType: IT, options?: (ReferenceOptionsGetSet<IT> | {}) & {
41 acceptsUndefined?: boolean;
42 onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>;
43}): IMaybe<IReferenceType<IT>>;