UNPKG

9.19 kBTypeScriptView Raw
1/// <reference types="node" />
2
3declare namespace SelfReloadJSON {
4 export default class SelfReloadJSON<T> extends NodeJS.EventEmitter, T {
5 /**
6 * Construct the instance.
7 * @param fileName Defines the path to the JSON file.
8 */
9 constructor(fileName: string);
10
11 /**
12 * Construct the instance.
13 * @param options
14 */
15 constructor(options: SelfReloadJSONOptions);
16
17 [key: string | number]: any;
18
19 /**
20 * Stop watching the changes of the JSON file.
21 * You can use `resume` to continue watching the changes.
22 */
23 stop();
24
25 /**
26 * Start to watch the changes of the JSON file.
27 * In default, this is automatically called after instance construction.
28 */
29 resume();
30
31 /**
32 * Write the changes back to the JSON file.
33 * @param options
34 */
35 save(options?: SelfReloadJSONSaveOptions);
36
37 /**
38 * Force update the JSON file.
39 */
40 forceUpdate();
41
42 /**
43 * This event will be emitted after the JSON content refreshed.
44 * The `json` parameter is the raw parsed JSON object (not the SelfReloadJSON instance itself).
45 */
46 on(event: 'updated', callback: (json: any) => void): this;
47
48 /**
49 * This event will be emitted while error occured when loading the updated JSON file.
50 * The `err` parameter will be the error thrown by the updater.
51 */
52 on(event: 'save', callback: (err: any) => void): this;
53
54 /**
55 * This event will be emitted if any errors thrown during reload.
56 */
57 on(event: 'error', callback: (err: any) => void): this;
58
59 /**
60 * Deep patch an object in order to keep it's reference.
61 * @param source Object to be patched.
62 * @param patch Object for patching.
63 * @param options
64 */
65 static deepPatch(source: any, patch: any, options?: DeepPatchOptions);
66 }
67
68 export interface SelfReloadJSONOptions extends SelfReloadJSONSaveOptions {
69 /**
70 * Defines the path to the JSON file.
71 */
72 fileName?: string;
73
74 /**
75 * defines the behavior when the JSON file changed externally,
76 * set to `true` if you want to keep the removed properties in the instance.
77 * Default is `false`.
78 */
79 additive?: boolean;
80
81 /**
82 * Defines what method to determine the changes of JSON file.
83 * `'native'` mode will use system API to detect,
84 * and `'polling'` mode will poll the modified time of the JSON file.
85 * In the most case 'native' is more efficient,
86 * but some of the operating system does not support this,
87 * in this case `'polling'` should be used as fallback.
88 */
89 method?: 'native' | 'polling';
90
91 /**
92 * The checking interval if 'polling' mode is used.
93 * Default is `5000` milliseconds.
94 */
95 interval?: number;
96
97 /**
98 * The [reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter)
99 * function when parsing JSON. Default is `null`.
100 */
101 reviver?: (key: string, value: any) => any;
102
103 /**
104 * Set the delay time to trigger update the object in order to
105 * prevent event triggered several times or
106 * sometimes throws error because of trying to
107 * read the source JSON file but it is not yet completely updated.
108 * Default is 0 (immediately).
109 */
110 delay?: number;
111
112 /**
113 * Depth of deep patching which try to keeps child object
114 * with same references, `0` to disable,
115 * negative values to apply all. Default is `-1`.
116 */
117 depth?: number;
118 }
119
120 export interface SelfReloadJSONSaveOptions {
121 /**
122 * Defines the encoding of the JSON file. Default is `'utf8'`.
123 */
124 encoding?: string;
125
126 /**
127 * The [replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)
128 * function when converting JSON object back to string.
129 * You can override this option while calling `save()`. Default is `null`.
130 */
131 replacer?: ((this: any, key: string, value: any) => any) | string[];
132
133 /**
134 * [space](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument)
135 * parameter passed to `JSON.stringify()`. Default is `null`.
136 */
137 space?: number | string;
138 }
139
140 export interface DeepPatchOptions {
141 /**
142 * List of property keys to ignore.
143 */
144 ignoreProperties?: string[];
145
146 /**
147 * `true` to keep objects that not exists in patch object,
148 * otherwise `false`.
149 */
150 keepNonExists?: boolean;
151
152 /**
153 * How many levels to scan and patch,
154 * levels excesses this number will be patched directly by
155 * replacing with the patch object (which will not keep the reference).
156 * Passing negative values in here to disable this limitation.
157 */
158 depth?: number;
159 }
160
161 // Walk through tree exposed
162
163 /**
164 * This function will wrap the callback for calling it recursively
165 * with the `next()` binded in `this` scope.
166 * Optionally you can choose the direction to go through the
167 * virtual tree-like structure processed within the callback.
168 */
169 export function walkThroughTree<U>(fn: (this: WalkThroughTreeScope0) => U, options?: WalkThroughTreeOptions): U | undefined;
170 export function walkThroughTree<T, U>(fn: (this: WalkThroughTreeScope1<T>, arg: T) => U, options: WalkThroughTreeOptions, arg: T): U | undefined;
171 export function walkThroughTree<T1, T2, U>(fn: (this: WalkThroughTreeScope2<T1, T2>, arg1: T1, arg2: T2) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2): U | undefined;
172 export function walkThroughTree<T1, T2, T3, U>(fn: (this: WalkThroughTreeScope3<T1, T2, T3>, arg1: T1, arg2: T2, arg3: T3) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2, arg3: T3): U | undefined;
173 export function walkThroughTree<T1, T2, T3, T4, U>(fn: (this: WalkThroughTreeScope4<T1, T2, T3, T4>, arg1: T1, arg2: T2, arg3: T3, arg4: T4) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2, arg3: T3, arg4: T4): U | undefined;
174 export function walkThroughTree<T1, T2, T3, T4, T5, U>(fn: (this: WalkThroughTreeScope5<T1, T2, T3, T4, T5>, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): U | undefined;
175 export function walkThroughTree<U>(fn: (this: WalkThroughTreeScope, ...args: any[]) => U, options: WalkThroughTreeOptions, ...args: any[]): U | undefined;
176
177 /**
178 * Undely generator function for `walkThroughTree()`.
179 */
180 export function walkThroughTreeGen<U>(fn: (this: WalkThroughTreeScope0) => U, options?: WalkThroughTreeOptions): IterableIterator<U>;
181 export function walkThroughTreeGen<T, U>(fn: (this: WalkThroughTreeScope1<T>, arg: T) => U, options: WalkThroughTreeOptions, arg: T): IterableIterator<U>;
182 export function walkThroughTreeGen<T1, T2, U>(fn: (this: WalkThroughTreeScope2<T1, T2>, arg1: T1, arg2: T2) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2): IterableIterator<U>;
183 export function walkThroughTreeGen<T1, T2, T3, U>(fn: (this: WalkThroughTreeScope3<T1, T2, T3>, arg1: T1, arg2: T2, arg3: T3) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2, arg3: T3): IterableIterator<U>;
184 export function walkThroughTreeGen<T1, T2, T3, T4, U>(fn: (this: WalkThroughTreeScope4<T1, T2, T3, T4>, arg1: T1, arg2: T2, arg3: T3, arg4: T4) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2, arg3: T3, arg4: T4): IterableIterator<U>;
185 export function walkThroughTreeGen<T1, T2, T3, T4, T5, U>(fn: (this: WalkThroughTreeScope5<T1, T2, T3, T4, T5>, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => U, options: WalkThroughTreeOptions, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): IterableIterator<U>;
186 export function walkThroughTreeGen<U>(fn: (this: WalkThroughTreeScope, ...args: any[]) => U, options: WalkThroughTreeOptions, ...args: any[]): IterableIterator<U>;
187
188 export interface WalkThroughTreeOptions {
189 /**
190 * Should walk through the tree structure horizontally?
191 */
192 horizontal?: boolean;
193
194 /**
195 * First result or last result to return?
196 */
197 firstResult?: boolean;
198 }
199
200 export interface WalkThroughTreeScope {
201 next(...args: any[]): void;
202 }
203
204 export interface WalkThroughTreeScope0 {
205 next(): void;
206 }
207
208 export interface WalkThroughTreeScope1<T> {
209 next(arg: T): void;
210 }
211
212 export interface WalkThroughTreeScope2<T1, T2> {
213 next(arg1: T1, arg2: T2): void;
214 }
215
216 export interface WalkThroughTreeScope3<T1, T2, T3> {
217 next(arg1: T1, arg2: T2, arg3: T3): void;
218 }
219
220 export interface WalkThroughTreeScope4<T1, T2, T3, T4> {
221 next(arg1: T1, arg2: T2, arg3: T3, arg4: T4): void;
222 }
223
224 export interface WalkThroughTreeScope5<T1, T2, T3, T4, T5> {
225 next(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): void;
226 }
227}
228
229export as namespace SelfReloadJSON;
230
\No newline at end of file