mgschoen / prosecco-js

Super light and fizzy data binding
0 stars 0 forks source link

Execute array watchers for mutator methods only once #8

Closed mgschoen closed 5 years ago

mgschoen commented 5 years ago

Some prototype mutator methods of the Array prototype perform more than one operation on the Array, making all watchers fire several times for a single operation. In order to prevent that, the respective methods (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype) need to be overwritten and use something like this:

    this._executeAsAtomicOperation = function (operation, args) {
        var target = _this.proxyObject
        var oldObject = Array.isArray(target) ? [] : {}
        Object.assign(oldObject, target)
        _this.atomicOperationOngoing = true
        operation.apply(target, args)
        _this.atomicOperationOngoing = false
        for (var j = 0; j < _this.watchers.length; j++) {
            _this.watchers[j](oldObject, target)
        }
    }
mgschoen commented 5 years ago

☝️doesn't work because prototype functions are passed by-reference. Next try: https://stackoverflow.com/questions/1833588/javascript-clone-a-function