UNPKG

2.99 kBTypeScriptView Raw
1import { IInterceptor, IKeyValueMap, IMapDidChange, IMapWillChange, Lambda } from "mobx";
2import { IAnyType, IType, ExtractCSTWithSTN, IHooksGetter } from "../../internal";
3/** @hidden */
4export interface IMapType<IT extends IAnyType> extends IType<IKeyValueMap<IT["CreationType"]> | undefined, IKeyValueMap<IT["SnapshotType"]>, IMSTMap<IT>> {
5 hooks(hooks: IHooksGetter<IMSTMap<IT>>): IMapType<IT>;
6}
7/** @hidden */
8export interface IMSTMap<IT extends IAnyType> {
9 clear(): void;
10 delete(key: string): boolean;
11 forEach(callbackfn: (value: IT["Type"], key: string, map: this) => void, thisArg?: any): void;
12 get(key: string): IT["Type"] | undefined;
13 has(key: string): boolean;
14 set(key: string, value: ExtractCSTWithSTN<IT>): this;
15 readonly size: number;
16 put(value: ExtractCSTWithSTN<IT>): IT["Type"];
17 keys(): IterableIterator<string>;
18 values(): IterableIterator<IT["Type"]>;
19 entries(): IterableIterator<[string, IT["Type"]]>;
20 [Symbol.iterator](): IterableIterator<[string, IT["Type"]]>;
21 /** Merge another object into this map, returns self. */
22 merge(other: IMSTMap<IType<any, any, IT["TypeWithoutSTN"]>> | IKeyValueMap<ExtractCSTWithSTN<IT>> | any): this;
23 replace(values: IMSTMap<IType<any, any, IT["TypeWithoutSTN"]>> | IKeyValueMap<ExtractCSTWithSTN<IT>> | any): this;
24 toJSON(): IKeyValueMap<IT["SnapshotType"]>;
25 toString(): string;
26 [Symbol.toStringTag]: "Map";
27 /**
28 * Observes this object. Triggers for the events 'add', 'update' and 'delete'.
29 * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
30 * for callback details
31 */
32 observe(listener: (changes: IMapDidChange<string, IT["Type"]>) => void, fireImmediately?: boolean): Lambda;
33 intercept(handler: IInterceptor<IMapWillChange<string, IT["Type"]>>): Lambda;
34}
35/**
36 * `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
37 * If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
38 *
39 * This type will always produce [observable maps](https://mobx.js.org/api.html#observablemap)
40 *
41 * Example:
42 * ```ts
43 * const Todo = types.model({
44 * id: types.identifier,
45 * task: types.string
46 * })
47 *
48 * const TodoStore = types.model({
49 * todos: types.map(Todo)
50 * })
51 *
52 * const s = TodoStore.create({ todos: {} })
53 * unprotect(s)
54 * s.todos.set(17, { task: "Grab coffee", id: 17 })
55 * s.todos.put({ task: "Grab cookie", id: 18 }) // put will infer key from the identifier
56 * console.log(s.todos.get(17).task) // prints: "Grab coffee"
57 * ```
58 *
59 * @param subtype
60 * @returns
61 */
62export declare function map<IT extends IAnyType>(subtype: IT): IMapType<IT>;
63/**
64 * Returns if a given value represents a map type.
65 *
66 * @param type
67 * @returns `true` if it is a map type.
68 */
69export declare function isMapType<Items extends IAnyType = IAnyType>(type: IAnyType): type is IMapType<Items>;