|
| 1 | +export default class InterceptedArray extends Array { |
| 2 | + callback; |
| 3 | + // intercepts only methods that change the object |
| 4 | + constructor(callback) { |
| 5 | + super(); |
| 6 | + this.callback = callback; |
| 7 | + } |
| 8 | + static new(arrayLike, callback) { |
| 9 | + const interceptedArray = new InterceptedArray(callback); |
| 10 | + for (let i = 0; i < arrayLike.length; i++) { |
| 11 | + interceptedArray[i] = arrayLike[i]; |
| 12 | + } |
| 13 | + return interceptedArray; |
| 14 | + } |
| 15 | + copyWithin(target, start, end) { |
| 16 | + this.callback(this, 'copyWithin', target, start, end); |
| 17 | + return super.copyWithin(target, start, end); |
| 18 | + } |
| 19 | + fill(value, start, end) { |
| 20 | + this.callback(this, 'fill', value, start, end); |
| 21 | + return super.fill(value, start, end); |
| 22 | + } |
| 23 | + pop() { |
| 24 | + this.callback(this, 'pop'); |
| 25 | + return super.pop(); |
| 26 | + } |
| 27 | + push(...items) { |
| 28 | + this.callback(this, 'push', ...items); |
| 29 | + return super.push(...items); |
| 30 | + } |
| 31 | + reverse() { |
| 32 | + this.callback(this, 'reverse'); |
| 33 | + return super.reverse(); |
| 34 | + } |
| 35 | + shift() { |
| 36 | + this.callback(this, 'shift'); |
| 37 | + return super.shift(); |
| 38 | + } |
| 39 | + sort(compareFn) { |
| 40 | + this.callback(this, 'sort', compareFn); |
| 41 | + return super.sort(compareFn); |
| 42 | + } |
| 43 | + splice(start, deleteCount) { |
| 44 | + this.callback(this, 'splice', start, deleteCount); |
| 45 | + return super.splice(start, deleteCount); |
| 46 | + } |
| 47 | + unshift(...items) { |
| 48 | + this.callback(this, 'unshift', ...items); |
| 49 | + return super.unshift(...items); |
| 50 | + } |
| 51 | +} |
0 commit comments