forked from sindresorhus/on-change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
119 lines (104 loc) · 2.46 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
113
114
115
116
117
118
119
declare namespace onChange {
interface Options {
/**
Deep changes will not trigger the callback. Only changes to the immediate properties of the original object.
@default false
@example
```
import onChange = require('on-change');
const object = {
a: {
b: false
}
};
let i = 0;
const watchedObject = onChange(object, () => {
console.log('Object changed:', ++i);
}, {isShallow: true});
watchedObject.a.b = true;
// Nothing happens
watchedObject.a = true;
//=> 'Object changed: 1'
```
*/
readonly isShallow?: boolean;
}
}
declare const onChange: {
/**
Watch an object or array for changes. It works recursively, so it will even detect if you modify a deep property like `obj.a.b[0].c = true`.
@param object - Object to watch for changes.
@param onChange - Function that gets called anytime the object changes.
@returns A version of `object` that is watched. It's the exact same object, just with some `Proxy` traps.
@example
```
import onChange = require('on-change');
const object = {
foo: false,
a: {
b: [
{
c: false
}
]
}
};
let i = 0;
const watchedObject = onChange(object, function (path, value, previousValue) {
console.log('Object changed:', ++i);
console.log('this:', this);
console.log('path:', path);
console.log('value:', value);
console.log('previousValue:', previousValue);
});
watchedObject.foo = true;
//=> 'Object changed: 1'
//=> 'this: {
// foo: true,
// a: {
// b: [
// {
// c: false
// }
// ]
// }
// }'
//=> 'path: "foo"'
//=> 'value: true'
//=> 'previousValue: false'
watchedObject.a.b[0].c = true;
//=> 'Object changed: 2'
//=> 'this: {
// foo: true,
// a: {
// b: [
// {
// c: true
// }
// ]
// }
// }'
//=> 'path: "a.b.0.c"'
//=> 'value: true'
//=> 'previousValue: false'
```
*/
<ObjectType extends {[key: string]: unknown}>(
object: ObjectType,
onChange: (
this: ObjectType,
path: string,
value: unknown,
previousValue: unknown
) => void,
options?: onChange.Options
): ObjectType;
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function onChange<ObjectType extends {[key: string]: unknown}>(
// object: ObjectType,
// onChange: (this: ObjectType, path: string, value: unknown, previousValue: unknown) => void
// ): ObjectType;
// export = onChange;
default: typeof onChange;
};
export = onChange;