|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function Transaction() {} |
| 4 | + |
| 5 | +Transaction.start = (data) => { |
| 6 | + console.log('start transaction'); |
| 7 | + const events = { commit: [], rollback: [], timeout: [], change: [] }; |
| 8 | + let delta = {}; |
| 9 | + |
| 10 | + const methods = { |
| 11 | + commit: () => { |
| 12 | + console.log('commit transaction'); |
| 13 | + Object.assign(data, delta); |
| 14 | + delta = {}; |
| 15 | + }, |
| 16 | + rollback: () => { |
| 17 | + console.log('rollback transaction'); |
| 18 | + delta = {}; |
| 19 | + }, |
| 20 | + clone: () => { |
| 21 | + console.log('clone transaction'); |
| 22 | + const cloned = Transaction.start(data); |
| 23 | + Object.assign(cloned.delta, delta); |
| 24 | + return cloned; |
| 25 | + }, |
| 26 | + delta: () => delta, |
| 27 | + on: (name, callback) => { |
| 28 | + const event = events[name]; |
| 29 | + if (event) event.push(callback); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + const getKeys = () => { |
| 34 | + const changes = Object.keys(delta); |
| 35 | + const keys = Object.keys(data).concat(changes); |
| 36 | + return keys.filter((x, i, a) => a.indexOf(x) === i); |
| 37 | + }; |
| 38 | + |
| 39 | + const proxy = new Proxy(data, { |
| 40 | + get(target, key, proxy) { |
| 41 | + if (key === Symbol.iterator) return getKeys()[key](); |
| 42 | + if (methods.hasOwnProperty(key)) return methods[key]; |
| 43 | + if (delta.hasOwnProperty(key)) return delta[key]; |
| 44 | + return target[key]; |
| 45 | + }, |
| 46 | + ownKeys(target) { |
| 47 | + return getKeys(); |
| 48 | + }, |
| 49 | + getOwnPropertyDescriptor: (target, key) => ( |
| 50 | + Object.getOwnPropertyDescriptor( |
| 51 | + delta.hasOwnProperty(key) ? delta : target, key |
| 52 | + ) |
| 53 | + ), |
| 54 | + set(target, key, val) { |
| 55 | + console.log('set', key, val); |
| 56 | + if (target[key] === val) delete delta[key]; |
| 57 | + else delta[key] = val; |
| 58 | + return true; |
| 59 | + } |
| 60 | + }); |
| 61 | + return proxy; |
| 62 | +}; |
| 63 | + |
| 64 | +// Usage |
| 65 | + |
| 66 | +const data = { name: 'Marcus Aurelius', born: 121 }; |
| 67 | + |
| 68 | +const transaction = Transaction.start(data); |
| 69 | +console.log('data', JSON.stringify(data)); |
| 70 | +console.log('transaction', JSON.stringify(transaction)); |
| 71 | + |
| 72 | +transaction.name = 'Mao Zedong'; |
| 73 | +transaction.born = 1893; |
| 74 | +transaction.city = 'Shaoshan'; |
| 75 | + |
| 76 | +console.log('\noutput with JSON.stringify:'); |
| 77 | +console.log('data', JSON.stringify(data)); |
| 78 | +console.log('transaction', JSON.stringify(transaction)); |
| 79 | + |
| 80 | +console.log('\noutput with console.dir:'); |
| 81 | +console.dir({ transaction }); |
| 82 | + |
| 83 | +console.log('\noutput with for-in:'); |
| 84 | +for (const key in transaction) { |
| 85 | + console.log(key, transaction[key]); |
| 86 | +} |
| 87 | + |
| 88 | +transaction.commit(); |
| 89 | +console.log('data', JSON.stringify(data)); |
| 90 | +console.log('transaction', JSON.stringify(transaction)); |
| 91 | + |
| 92 | +transaction.born = 1976; |
| 93 | +console.log('data', JSON.stringify(data)); |
| 94 | +console.log('transaction', JSON.stringify(transaction)); |
| 95 | + |
| 96 | +transaction.rollback(); |
| 97 | +console.log('data', JSON.stringify(data)); |
| 98 | +console.log('transaction', JSON.stringify(transaction)); |
0 commit comments