|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -function Transaction() {} |
4 | | - |
5 | | -Transaction.start = (data) => { |
| 3 | +const start = (data) => { |
6 | 4 | console.log('start transaction'); |
7 | 5 | let delta = {}; |
8 | | - |
9 | | - const methods = { |
10 | | - commit: () => { |
11 | | - console.log('commit transaction'); |
12 | | - Object.assign(data, delta); |
13 | | - delta = {}; |
14 | | - }, |
15 | | - rollback: () => { |
16 | | - console.log('rollback transaction'); |
17 | | - delta = {}; |
18 | | - } |
19 | | - }; |
20 | | - |
21 | | - const proxy = new Proxy(data, { |
22 | | - get(target, key, proxy) { |
23 | | - if (key === Symbol.iterator) { |
24 | | - const changes = Object.keys(delta); |
25 | | - const keys = Object.keys(target).concat(changes); |
26 | | - const props = keys.filter((x, i, a) => a.indexOf(x) === i); |
27 | | - return props[Symbol.iterator](); |
| 6 | + return new Proxy(data, { |
| 7 | + get(target, key) { |
| 8 | + if (key === 'commit') { |
| 9 | + return () => { |
| 10 | + console.log('commit transaction'); |
| 11 | + Object.assign(data, delta); |
| 12 | + delta = {}; |
| 13 | + }; |
28 | 14 | } |
29 | | - if (methods.hasOwnProperty(key)) return methods[key]; |
30 | 15 | if (delta.hasOwnProperty(key)) return delta[key]; |
31 | 16 | return target[key]; |
32 | 17 | }, |
33 | | - getOwnPropertyDescriptor: (target, key) => ( |
34 | | - Object.getOwnPropertyDescriptor( |
35 | | - delta.hasOwnProperty(key) ? delta : target, key |
36 | | - ) |
37 | | - ), |
38 | 18 | set(target, key, val) { |
39 | 19 | console.log('set', key, val); |
40 | 20 | if (target[key] === val) delete delta[key]; |
41 | 21 | else delta[key] = val; |
42 | 22 | return true; |
43 | 23 | } |
44 | 24 | }); |
45 | | - return proxy; |
46 | 25 | }; |
47 | 26 |
|
48 | 27 | // Usage |
49 | 28 |
|
50 | 29 | const data = { name: 'Marcus Aurelius', city: 'Rome', born: 121 }; |
51 | 30 |
|
52 | | -const transaction = Transaction.start(data); |
53 | | -console.dir(transaction); |
| 31 | +console.log('data.name', data.name); |
| 32 | +console.log('data.born', data.born); |
| 33 | + |
| 34 | +const transaction = start(data); |
54 | 35 |
|
55 | 36 | transaction.name = 'Mao Zedong'; |
56 | 37 | transaction.born = 1893; |
57 | | -console.dir({ data, transaction }); |
| 38 | + |
| 39 | +console.log('data.name', data.name); |
| 40 | +console.log('data.born', data.born); |
| 41 | + |
| 42 | +console.log('transaction.name', transaction.name); |
| 43 | +console.log('transaction.born', transaction.born); |
58 | 44 |
|
59 | 45 | transaction.commit(); |
60 | | -console.dir({ data, transaction }); |
61 | 46 |
|
62 | | -transaction.city = 'Shaoshan'; |
63 | | -console.dir({ data, transaction }); |
| 47 | +console.log('data.name', data.name); |
| 48 | +console.log('data.born', data.born); |
64 | 49 |
|
65 | | -transaction.rollback(); |
66 | | -console.dir({ data, transaction }); |
| 50 | +console.log('transaction.name', transaction.name); |
| 51 | +console.log('transaction.born', transaction.born); |
0 commit comments