Skip to content

Commit fbcc117

Browse files
committed
Refactor examples
1 parent 4b60bc6 commit fbcc117

12 files changed

Lines changed: 72 additions & 62 deletions

JavaScript/1-proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const start = (data) => {
3+
const start = data => {
44
console.log('\nstart transaction');
55
let delta = {};
66
const commit = () => {

JavaScript/2-transaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
let delta = {};
88

JavaScript/3-problems.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
let delta = {};
88

JavaScript/4-descriptor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
let delta = {};
88

JavaScript/5-own-keys.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
let delta = {};
88

JavaScript/6-delta.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
let delta = {};
88

@@ -68,10 +68,14 @@ transaction.age = (
6868
console.dir({ transaction });
6969
console.dir({ delta: transaction.delta });
7070

71+
const transaction2 = transaction.clone();
72+
7173
transaction.commit();
7274
console.dir({ data });
7375
console.dir({ transaction });
76+
console.dir({ transaction2 });
7477
console.dir({ delta: transaction.delta });
78+
console.dir({ delta2: transaction2.delta });
7579

7680
transaction.born = 1976;
7781
console.dir({ transaction });

JavaScript/7-delete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
let delta = {};
88
const deleteDelta = new Set();

JavaScript/7-events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
const events = {
88
commit: [], rollback: [], timeout: [], change: []
99
};
1010
let delta = {};
1111

12-
const emit = (name) => {
12+
const emit = name => {
1313
const event = events[name];
1414
for (const listener of event) listener(data);
1515
};

JavaScript/8-dataset.js

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
function Transaction() {}
44

5-
Transaction.start = (data) => {
5+
Transaction.start = data => {
66
console.log('\nstart transaction');
77
const events = {
88
commit: [], rollback: [], timeout: [], change: []
99
};
1010
let delta = {};
1111

12-
const emit = (name) => {
12+
const emit = name => {
1313
const event = events[name];
1414
for (const listener of event) listener(data);
1515
};
@@ -61,42 +61,45 @@ Transaction.start = (data) => {
6161
});
6262
};
6363

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+
}
77101
}
78102

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-
100103
// Usage
101104

102105
const data = [

JavaScript/9-separate.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
// Interface definition
44

5-
function Transaction() {}
6-
7-
Transaction.start = (data) => {
8-
// place implementation here
9-
return [obj, transaction];
10-
};
11-
12-
// Transaction.delta
13-
Transaction.prototype.commit = () => {};
14-
Transaction.prototype.rollback = () => {};
15-
Transaction.prototype.revoke = () => {};
16-
Transaction.prototype.timeout = (msec) => {};
17-
Transaction.prototype.before = (event, listener) => {};
18-
Transaction.prototype.after = (event, listener) => {};
19-
// Events: commit, rollback, revoke, set, get, timeout
5+
class Transaction {
6+
constructor() {
7+
// this.delta
8+
}
9+
10+
static start(data) {
11+
// place implementation here
12+
return [obj, transaction];
13+
}
14+
15+
commit() {}
16+
rollback() {}
17+
revoke() {}
18+
timeout(msec) {}
19+
before(event, listener) {}
20+
after(event, listener) {}
21+
// Events: commit, rollback, revoke, set, get, timeout
22+
}
2023

2124
// Usage
2225

0 commit comments

Comments
 (0)