Rewrite array methods
The following rewrites array methods to generate events and
for performance reasons.
Array’s methods that mutate are rewritten to generate patch events.
Other methods on array are rewritten to:
- Avoid calling
ObservationRecorder.add
on every property.
- Make the returned result observable.
Rewrite mutating methods
The following defines a relationship between an array
mutation method and the patch events that should be dispatched
for that mutation.
var mutateMethods = {
"push": function(arr, args) {
return [{
index: arr.length - args.length,
deleteCount: 0,
insert: args,
type: "splice"
}];
},
"pop": function(arr) {
return [{
index: arr.length,
deleteCount: 1,
insert: [],
type: "splice"
}];
},
"shift": function() {
return [{
index: 0,
deleteCount: 1,
insert: [],
type: "splice"
}];
},
"unshift": function(arr, args) {
return [{
index: 0,
deleteCount: 0,
insert: args,
type: "splice"
}];
},
"splice": function(arr, args) {
return [{
index: args[0],
deleteCount: args[1],
insert: args.slice(2),
type: "splice"
}];
},
"sort": function(arr) {