-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.observe.polyfill.js
95 lines (80 loc) · 2.03 KB
/
array.observe.polyfill.js
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
Object.observe && !Array.observe && (function(O, A) {
"use strict";
var notifier = O.getNotifier,
perform = "performChange",
original = "_original",
type = "splice";
var wrappers = {
push: function push(item) {
var args = arguments,
ret = push[original].apply(this, args);
notifier(this)[perform](type, function() {
return {
index: ret - args.length,
addedCount: args.length,
removed: []
};
});
return ret;
},
unshift: function unshift(item) {
var args = arguments,
ret = unshift[original].apply(this, args);
notifier(this)[perform](type, function() {
return {
index: 0,
addedCount: args.length,
removed: []
};
});
return ret;
},
pop: function pop() {
var len = this.length,
item = pop[original].call(this);
if (this.length !== len)
notifier(this)[perform](type, function() {
return {
index: this.length,
addedCount: 0,
removed: [ item ]
};
}, this);
return item;
},
shift: function shift() {
var len = this.length,
item = shift[original].call(this);
if (this.length !== len)
notifier(this)[perform](type, function() {
return {
index: 0,
addedCount: 0,
removed: [ item ]
};
}, this);
return item;
},
splice: function splice(start, deleteCount) {
var args = arguments,
removed = splice[original].apply(this, args);
if (removed.length || args.length > 2)
notifier(this)[perform](type, function() {
return {
index: start,
addedCount: args.length - 2,
removed: removed
};
}, this);
return removed;
}
};
for (var wrapper in wrappers) {
wrappers[wrapper][original] = A.prototype[wrapper];
A.prototype[wrapper] = wrappers[wrapper];
}
A.observe = function(object, handler) {
return O.observe(object, handler, [ "add", "update", "delete", type ]);
};
A.unobserve = O.unobserve;
})(Object, Array);