Skip to content

Commit 524398d

Browse files
committed
fixed bug with .splice in onChange
1 parent 184c37b commit 524398d

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

build/onChange.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*MIT License
2+
3+
OnChange: Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> , with major improvement by Rodrigo Solís (https://github.com/sorodrigo)
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6+
*/
7+
export default (object, onChange) => {
8+
const BLACKLIST = [
9+
'sort',
10+
'reverse',
11+
'splice',
12+
'pop',
13+
'unshift',
14+
'shift',
15+
'push'
16+
];
17+
let blocked = false;
18+
const handler = {
19+
get(target, property, receiver) {
20+
try {
21+
if (property === "prototype")
22+
return Reflect.get(target, property, receiver);
23+
else
24+
return new Proxy(target[property], handler);
25+
}
26+
catch (err) {
27+
return Reflect.get(target, property, receiver);
28+
}
29+
},
30+
defineProperty(target, property, descriptor) {
31+
const res = Reflect.defineProperty(target, property, descriptor);
32+
if (!blocked) {
33+
onChange();
34+
}
35+
return res;
36+
},
37+
deleteProperty(target, property) {
38+
const res = Reflect.deleteProperty(target, property);
39+
if (!blocked) {
40+
onChange();
41+
}
42+
return res;
43+
},
44+
apply(target, thisArg, argumentsList) {
45+
if (BLACKLIST.includes(target.name)) {
46+
blocked = true;
47+
const res = Reflect.apply(target, thisArg, argumentsList);
48+
onChange();
49+
blocked = false;
50+
return res;
51+
}
52+
return Reflect.apply(target, thisArg, argumentsList);
53+
}
54+
};
55+
return new Proxy(object, handler);
56+
};
57+
// --------------------------------------- FINISHED onChange ------------------------------------------------------------------ //

src/onChange.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*MIT License
2+
3+
OnChange: Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> , with major improvement by Rodrigo Solís (https://github.com/sorodrigo)
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6+
*/
7+
8+
export default (object:object, onChange:Function) => {
9+
10+
const BLACKLIST = [
11+
'sort',
12+
'reverse',
13+
'splice',
14+
'pop',
15+
'unshift',
16+
'shift',
17+
'push'
18+
];
19+
20+
let blocked = false;
21+
const handler = {
22+
get(target:any, property:string, receiver:object):any {
23+
try {
24+
if(property === "prototype") return Reflect.get(target, property, receiver);
25+
else return new Proxy(target[property], handler);
26+
} catch (err) {
27+
return Reflect.get(target, property, receiver);
28+
}
29+
},
30+
defineProperty(target:object, property:string, descriptor:object) {
31+
const res = Reflect.defineProperty(target, property, descriptor);
32+
if (!blocked) {
33+
onChange();
34+
}
35+
return res;
36+
},
37+
deleteProperty(target:object, property:string) {
38+
const res = Reflect.deleteProperty(target, property);
39+
if (!blocked) {
40+
onChange();
41+
}
42+
return res;
43+
},
44+
apply(target:any, thisArg:object, argumentsList:Array<any>) {
45+
if (BLACKLIST.includes(target.name)) {
46+
blocked = true;
47+
const res = Reflect.apply(target, thisArg, argumentsList);
48+
onChange();
49+
blocked = false;
50+
return res;
51+
}
52+
return Reflect.apply(target, thisArg, argumentsList);
53+
}
54+
};
55+
56+
return new Proxy(object, handler);
57+
};
58+
59+
60+
// --------------------------------------- FINISHED onChange ------------------------------------------------------------------ //

0 commit comments

Comments
 (0)