-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathatom.js
190 lines (157 loc) · 4.83 KB
/
atom.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
var React = require("react");
var DeepFreeze = require("../utils/deepFreeze");
var AtomUtils = require("./atomUtils");
var AtomCursor = require("./atomCursor");
var NOOP = function noop() { } // Convenient but probably not performant: TODO ?
/**
* Creates an Atom
* It contains an immutable state that is never modified directly, but can be swapped to a new immutable state
* @param options
* @constructor
*/
var Atom = function Atom(options) {
this.state = options.initialState || {};
this.beforeTransactionCommit = options.beforeTransactionCommit || NOOP;
this.afterTransactionCommit = options.afterTransactionCommit || NOOP;
this.currentTransactionState = undefined;
if ( process.env.NODE_ENV !== "production" ) {
DeepFreeze(this.state);
}
};
/**
* Change the state reference hold in this Atom
* @param newState
*/
Atom.prototype.swap = function(newState) {
if ( !this.isInTransaction() ) {
throw new Error("It is forbidden to swap the atom outside of a transaction");
}
if ( this.locked ) {
throw new Error("Atom is locked because: "+this.lockReason);
}
if ( process.env.NODE_ENV !== "production" ) {
DeepFreeze(newState);
}
this.currentTransactionState = newState;
};
Atom.prototype.isInTransaction = function() {
return !!this.currentTransactionState;
};
Atom.prototype.openTransaction = function() {
this.currentTransactionState = this.state;
this.currentTransactionDate = Date.now();
};
Atom.prototype.commitTransaction = function() {
var transactionState = this.currentTransactionState;
this.currentTransactionState = undefined
this.state = transactionState;
var duration = Date.now() - this.currentTransactionDate;
this.currentTransactionDate = undefined;
var transactionData = {
duration: duration
};
return transactionData;
};
Atom.prototype.rollbackTransaction = function() {
this.currentTransactionState = undefined
};
Atom.prototype.lock = function(lockReason) {
this.locked = true;
this.lockReason = lockReason
};
Atom.prototype.unlock = function() {
this.locked = false;
this.lockReason = undefined
};
Atom.prototype.doWithLock = function(lockReason,task) {
try {
this.lock(lockReason);
task();
} finally {
this.unlock();
}
};
Atom.prototype.transact = function(tasks) {
// TODO do we need to implement more complex transaction propagation rules than joining the existing transaction?
if ( this.isInTransaction() ) {
tasks();
}
else {
this.openTransaction();
try {
tasks();
// "lock" these values before calling the callbacks
var previousState = this.state;
this.beforeTransactionCommit(this.currentTransactionState,previousState);
var transactionData = this.commitTransaction();
try {
this.afterTransactionCommit(this.state,previousState,transactionData);
} catch (error) {
console.error("Error in 'afterTransactionCommit' callback. The transaction will still be commited -> ",error);
}
} catch (error) {
this.rollbackTransaction();
throw error;
}
}
};
/**
* Get the current state of the Atom
* @return the Atom state
*/
Atom.prototype.get = function() {
// If we are inside a transaction, we can read the transaction state (read your writes)
return this.currentTransactionState || this.state;
};
/**
* Get a cursor,, that permits to focus on a given path of the Atom
* @param options
* @return {AtomCursor}
*/
Atom.prototype.cursor = function(options) {
return new AtomCursor(this,[],options);
};
/**
* Change the value at a given path of the atom
* @param path
* @param value
*/
Atom.prototype.setPathValue = function(path,value) {
var self = this;
this.transact(function() {
var newState = AtomUtils.setPathValue(self.get(),path,value);
self.swap(newState);
});
};
Atom.prototype.unsetPathValue = function(path) {
var self = this;
this.transact(function() {
var newState = AtomUtils.setPathValue(self.get(),path,undefined);
self.swap(newState);
})
};
/**
* Get the value at a given path of the atom
* @param path
* @return value
*/
Atom.prototype.getPathValue = function(path) {
return AtomUtils.getPathValue(this.get(),path);
};
/**
* Compare and swap a value at a given path of the atom
* @param path
* @param expectedValue
* @param newValue
* @return true if the CAS operation was successful
*/
Atom.prototype.compareAndSwapPathValue = function(path,expectedValue,newValue) {
var actualValue = this.getPathValue(path);
if ( actualValue === expectedValue ) {
this.setPathValue(path,newValue);
return true;
}
return false;
};
module.exports = Atom;