Commit c1883dce4e666e45b62e08d2c4e605130ccb968a
set: partial update on 'set', only emit if new value is different (like struct)
Matt McKegg committed on 11/3/2016, 12:44:18 PMParent: 07bf7e400c9cbb4babe9aa90386152ccb2731feb
Files changed
set.js | changed |
set.js | ||
---|---|---|
@@ -76,15 +76,33 @@ | ||
76 | 76 … | } |
77 | 77 … | |
78 | 78 … | ProtoSet.prototype.set = function (values) { |
79 | 79 … | var self = this |
80 | - self.sources.length = 0 | |
80 … | + var changed = false | |
81 … | + | |
81 | 82 … | if (Array.isArray(values)) { |
83 … | + for (var i = this.sources.length - 1; i >= 0; i -= 1) { | |
84 … | + if (!~values.indexOf(this.sources[i])) { | |
85 … | + changed = true | |
86 … | + self.sources.splice(i, 1) | |
87 … | + } | |
88 … | + } | |
82 | 89 … | values.forEach(function (value) { |
83 | - self.sources.push(value) | |
90 … | + if (!~self.sources.indexOf(value)) { | |
91 … | + changed = true | |
92 … | + self.sources.push(value) | |
93 … | + } | |
84 | 94 … | }) |
95 … | + } else { | |
96 … | + if (self.sources.length > 0) { | |
97 … | + self.sources.length = 0 | |
98 … | + changed = true | |
99 … | + } | |
85 | 100 … | } |
86 | - self.binder.onUpdate() | |
101 … | + | |
102 … | + if (changed) { | |
103 … | + self.binder.onUpdate() | |
104 … | + } | |
87 | 105 … | } |
88 | 106 … | |
89 | 107 … | ProtoSet.prototype.get = function (index) { |
90 | 108 … | return this.sources[index] |
Built with git-ssb-web