-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.d.ts
112 lines (94 loc) · 2.51 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
export interface IHistoryOptions<T = any> {
/**
* Optional rules array for optimizing data transforming.
* By defining rules you can specify how to transform between states and internal "chunks".
*/
rules?: IRuleOptions<T>[];
/**
* Optional initial state.
*/
initialState?: T;
/**
* Debounce time for push in milliseconds, 50 by default.
*/
delay?: number;
/**
* Max length saving history states, 100 by default.
*/
maxLength?: number;
/**
* Whether serializing state data into chunks. true by default.
*/
useChunks?: boolean;
/**
* Fired when pushing / pulling states with changed state passed in.
*/
onChange?: (state: T) => void;
}
export interface IRuleOptions<T = any> {
/**
* Defines whether a rule can be matched.
* @param state the state that you pushed.
*/
match: (state: T) => boolean;
/**
* Split state into shareable chunks.
*/
toRecord: (
state: T,
) => {
chunks: any[];
children?: any[];
};
/**
* Parse the chunks back into the state node
* @param shareableChunks
*/
fromRecord: (shareableChunks: { chunks: any[]; children: any[] }) => T;
}
export class History<T = any> {
/**
* Valid record length of current instance
*/
readonly length: number;
/**
* Whether current state has undo records before.
*/
readonly hasUndo: boolean;
/**
* Whether current state has redo records after.
*/
readonly hasRedo: boolean;
/**
* Main class for state management
*/
constructor(options?: IHistoryOptions<T>);
/**
* Push state data into history, using pushSync under the hood.
* @param state state data to push.
* @param pickIndex if specified, only this index of state's child will be serialized.
*/
push(state: T, pickIndex?: number): Promise<History<T>>;
/**
* Push state data into history.
* @param state state data to push.
* @param pickIndex if specified, only this index of state's child will be serialized.
*/
pushSync(state: T, pickIndex?: number): History<T>;
/**
* Undo a record if possible, supports chaining.
*/
undo(): History<T>;
/**
* Redo a record if possible, supports chaining.
*/
redo(): History<T>;
/**
* Pull out a history state from records.
*/
get(): T;
/**
* Clear internal data structure.
*/
reset(): History<T>;
}