|
2 | 2 |
|
3 | 3 | function Transaction() {} |
4 | 4 |
|
5 | | -Transaction.start = (data) => { |
| 5 | +Transaction.start = data => { |
6 | 6 | console.log('\nstart transaction'); |
7 | 7 | const events = { |
8 | 8 | commit: [], rollback: [], timeout: [], change: [] |
9 | 9 | }; |
10 | 10 | let delta = {}; |
11 | 11 |
|
12 | | - const emit = (name) => { |
| 12 | + const emit = name => { |
13 | 13 | const event = events[name]; |
14 | 14 | for (const listener of event) listener(data); |
15 | 15 | }; |
@@ -61,42 +61,45 @@ Transaction.start = (data) => { |
61 | 61 | }); |
62 | 62 | }; |
63 | 63 |
|
64 | | -function DatasetTransaction(dataset) { |
65 | | - this.dataset = dataset; |
66 | | - this.log = []; // array of LogRecord { time, operation, delta } |
67 | | - // [ |
68 | | - // { id, time: '2018-01-01T12:01:00', operation: 'start' } |
69 | | - // { id, time: '2018-01-01T12:02:15', operation: 'set', delta } |
70 | | - // { id, time: '2018-01-01T12:02:32', operation: 'commit', delta } |
71 | | - // { id, time: '2018-01-01T12:02:37', operation: 'set', delta } |
72 | | - // { id, time: '2018-01-01T12:03:11', operation: 'rollback', delta } |
73 | | - // { id, time: '2018-01-01T12:03:18', operation: 'set', delta } |
74 | | - // { id, time: '2018-01-01T12:04:42', operation: 'timeout' } |
75 | | - // { id, time: '2018-01-01T12:04:52', operation: 'rollback', delta } |
76 | | - // ] |
| 64 | +// Dataset `this.log` example: [ |
| 65 | +// { id, time: '2018-01-01T12:01:00', operation: 'start' } |
| 66 | +// { id, time: '2018-01-01T12:02:15', operation: 'set', delta } |
| 67 | +// { id, time: '2018-01-01T12:02:32', operation: 'commit', delta } |
| 68 | +// { id, time: '2018-01-01T12:02:37', operation: 'set', delta } |
| 69 | +// { id, time: '2018-01-01T12:03:11', operation: 'rollback', delta } |
| 70 | +// { id, time: '2018-01-01T12:03:18', operation: 'set', delta } |
| 71 | +// { id, time: '2018-01-01T12:04:42', operation: 'timeout' } |
| 72 | +// { id, time: '2018-01-01T12:04:52', operation: 'rollback', delta } |
| 73 | +// ] |
| 74 | +class DatasetTransaction { |
| 75 | + constructor(dataset) { |
| 76 | + this.dataset = dataset; |
| 77 | + this.log = []; // array of LogRecord { time, operation, delta } |
| 78 | + } |
| 79 | + |
| 80 | + static start(dataset) { |
| 81 | + // place implementation here |
| 82 | + return new DatasetTransaction(dataset); |
| 83 | + } |
| 84 | + |
| 85 | + commit() { |
| 86 | + // place implementation here |
| 87 | + } |
| 88 | + |
| 89 | + rollback(id) { |
| 90 | + // place implementation here |
| 91 | + } |
| 92 | + |
| 93 | + // msec <number> timeout, 0 to disable |
| 94 | + // commit <boolean> true to commit, false to rollback |
| 95 | + // listener <Function> (optional) |
| 96 | + // callback <Function> |
| 97 | + // result <boolean> |
| 98 | + timeout(msec, commit, listener) { |
| 99 | + // place implementation here |
| 100 | + } |
77 | 101 | } |
78 | 102 |
|
79 | | -DatasetTransaction.start = function(dataset) { |
80 | | - // place implementation here |
81 | | - return new DatasetTransaction(dataset); |
82 | | -}; |
83 | | - |
84 | | -DatasetTransaction.prototype.commit = function() { |
85 | | - // place implementation here |
86 | | -}; |
87 | | - |
88 | | -DatasetTransaction.prototype.rollback = function(id /* optional log id */) { |
89 | | - // place implementation here |
90 | | -}; |
91 | | - |
92 | | -DatasetTransaction.prototype.timeout = function( |
93 | | - msec, // timeout, 0 - disable |
94 | | - commit, // true - commit, false - rollback |
95 | | - listener // (optional) function(boolean) : boolean |
96 | | -) { |
97 | | - // place implementation here |
98 | | -}; |
99 | | - |
100 | 103 | // Usage |
101 | 104 |
|
102 | 105 | const data = [ |
|
0 commit comments