-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path8-dataset.js
More file actions
122 lines (106 loc) · 3.01 KB
/
8-dataset.js
File metadata and controls
122 lines (106 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
function Transaction() {}
Transaction.start = (data) => {
console.log('\nstart transaction');
const events = {
commit: [],
rollback: [],
timeout: [],
change: [],
};
let delta = {};
const emit = (name) => {
const event = events[name];
for (const listener of event) listener(data);
};
const methods = {
commit: () => {
Object.assign(data, delta);
delta = {};
emit('commit');
},
rollback: () => {
delta = {};
emit('rollback');
},
clone: () => {
const cloned = Transaction.start(data);
Object.assign(cloned.delta, delta);
return cloned;
},
on: (name, callback) => {
const event = events[name];
if (event) event.push(callback);
},
};
return new Proxy(data, {
get(target, key) {
if (key === 'delta') return delta;
if (Object.hasOwn(methods, key)) return methods[key];
if (Object.hasOwn(delta, key)) return delta[key];
return target[key];
},
getOwnPropertyDescriptor: (target, key) =>
Object.getOwnPropertyDescriptor(
Object.hasOwn(delta, key) ? delta : target,
key,
),
ownKeys() {
const changes = Object.keys(delta);
const keys = Object.keys(data).concat(changes);
return keys.filter((x, i, a) => a.indexOf(x) === i);
},
set(target, key, val) {
console.log('set', key, val);
if (target[key] === val) delete delta[key];
else delta[key] = val;
return true;
},
});
};
// Dataset `this.log` example: [
// { id, time: '2018-01-01T12:01:00', operation: 'start' }
// { id, time: '2018-01-01T12:02:15', operation: 'set', delta }
// { id, time: '2018-01-01T12:02:32', operation: 'commit', delta }
// { id, time: '2018-01-01T12:02:37', operation: 'set', delta }
// { id, time: '2018-01-01T12:03:11', operation: 'rollback', delta }
// { id, time: '2018-01-01T12:03:18', operation: 'set', delta }
// { id, time: '2018-01-01T12:04:42', operation: 'timeout' }
// { id, time: '2018-01-01T12:04:52', operation: 'rollback', delta }
// ]
class DatasetTransaction {
constructor(dataset) {
this.dataset = dataset;
this.log = []; // array of LogRecord { time, operation, delta }
}
static start(dataset) {
// place implementation here
return new DatasetTransaction(dataset);
}
commit() {
// place implementation here
}
rollback(id) {
// place implementation here
}
// msec <number> timeout, 0 to disable
// commit <boolean> true to commit, false to rollback
// listener <Function> (optional)
// callback <Function>
// result <boolean>
timeout(msec, commit, listener) {
// place implementation here
}
}
// Usage
const data = [
{ name: 'Marcus Aurelius', born: 121 },
{ name: 'Marcus Aurelius', born: 121 },
{ name: 'Marcus Aurelius', born: 121 },
];
const transaction = DatasetTransaction.start(data);
for (const person of transaction.dataset) {
person.city = 'Shaoshan';
}
transaction.commit();
console.dir({ data });