git ssb

1+

Matt McKegg / mutant



Commit 8cd7c88b209a2811af3674d0483a03c651d5d51b

add transaction to dict and set

Matt McKegg committed on 6/20/2017, 4:03:27 PM
Parent: 6e2b0b0942ba7f0b2a60ecbf703bb39a910e60ed

Files changed

dict.jschanged
set.jschanged
test/transaction.jsadded
dict.jsView
@@ -60,8 +60,12 @@
6060 delete object[key]
6161 binder.broadcast()
6262 }
6363
64 + observable.transaction = function (cb) {
65 + binder.transaction(observable, cb)
66 + }
67 +
6468 observable.set = function (values) {
6569 if (fixedIndexing) {
6670 var keys = []
6771
set.jsView
@@ -10,8 +10,9 @@
1010 observable.delete = instance.delete.bind(instance)
1111 observable.has = instance.has.bind(instance)
1212 observable.set = instance.set.bind(instance)
1313 observable.get = instance.get.bind(instance)
14 + observable.transaction = instance.transaction.bind(instance)
1415 observable.getLength = instance.getLength.bind(instance)
1516 return observable
1617 }
1718
@@ -114,8 +115,12 @@
114115 ProtoSet.prototype._bind = function (valueOrObs) {
115116 return typeof valueOrObs === 'function' ? valueOrObs(this.binder.onUpdate) : null
116117 }
117118
119 +ProtoSet.prototype.transaction = function (fn) {
120 + this.binder.transaction(this, fn)
121 +}
122 +
118123 ProtoSet.prototype._listen = function () {
119124 var self = this
120125 self.sources.forEach(function (obs, i) {
121126 self.releases[i] = self._bind(obs)
test/transaction.jsView
@@ -1,0 +1,49 @@
1 +var test = require('tape')
2 +var MutantArray = require('../array')
3 +var MutantDict = require('../dict')
4 +var MutantSet = require('../set')
5 +
6 +test(`array - transaction`, t => {
7 + var array = MutantArray()
8 + var changes = []
9 + array(x => changes.push(x))
10 +
11 + array.transaction(() => {
12 + array.push(1)
13 + array.push(2)
14 + array.push(3)
15 + })
16 +
17 + t.deepEqual(changes, [[1, 2, 3]])
18 + t.end()
19 +})
20 +
21 +test(`dict - transaction`, t => {
22 + var dict = MutantDict()
23 + var changes = []
24 + dict(x => changes.push(x))
25 +
26 + dict.transaction(() => {
27 + dict.put('a', 1)
28 + dict.put('b', 2)
29 + dict.put('c', 3)
30 + })
31 +
32 + t.deepEqual(changes, [{a: 1, b: 2, c: 3}])
33 + t.end()
34 +})
35 +
36 +test(`set - transaction`, t => {
37 + var set = MutantSet()
38 + var changes = []
39 + set(x => changes.push(x))
40 +
41 + set.transaction(() => {
42 + set.add(1)
43 + set.add(2)
44 + set.add(3)
45 + })
46 +
47 + t.deepEqual(changes, [[1, 2, 3]])
48 + t.end()
49 +})

Built with git-ssb-web