-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (26 loc) · 941 Bytes
/
index.js
File metadata and controls
30 lines (26 loc) · 941 Bytes
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
'use strict';
/**
* Function to make Objects observable
* @param objToWatch - an Object or to make it observable
* @param callback - callback function with these arguments:
* property - a property which was changed or added,
* value - a new value of the changed property,
* previous - an old value of the property
*
* @returns observable Object
*/
const observe = (objToWatch, callback) => {
const handler = {
//value changing and rearranging
set(target, property, value, receiver) {
const previous = Reflect.get(target, property, receiver);
//a new property added or a property was changed
if (!previous || previous !== value) {
callback(property, value, previous);
}
return Reflect.set(target, property, value);
}
};
return new Proxy(objToWatch, handler);
};
module.exports = observe;