Skip to content

Commit 2cc3c63

Browse files
committed
update history schema (#70)
1 parent 0b6588f commit 2cc3c63

File tree

6 files changed

+108
-35
lines changed

6 files changed

+108
-35
lines changed

src/component.js

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ function _Array(conf) {
130130
var i = 0;
131131
var length = 0;
132132
var del = [];
133-
var oldValue = JSON.stringify(arrDb);
133+
var oldValue = null;
134+
135+
if ($history.isEnabled()) {
136+
oldValue = JSON.stringify(arrDb);
137+
}
134138

135139
if (!isReadOnly) {
136140
if (isClassName) {
@@ -158,7 +162,7 @@ function _Array(conf) {
158162
break;
159163
}
160164

161-
if (classId.indexOf('_') !== 0) {
165+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
162166
$history.pushState({
163167
action: 'update',
164168
collection: classId,
@@ -205,7 +209,7 @@ function _Array(conf) {
205209
break;
206210
}
207211

208-
if (classId.indexOf('_') !== 0) {
212+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
209213
$history.pushState({
210214
action: 'update',
211215
collection: classId,
@@ -252,7 +256,11 @@ function _Array(conf) {
252256
function _remove(action) {
253257
var result;
254258
var val = null;
255-
var oldValue = JSON.stringify(arrDb);
259+
var oldValue = null;
260+
261+
if ($history.isEnabled()) {
262+
oldValue = JSON.stringify(arrDb);
263+
}
256264

257265
if (!isReadOnly) {
258266
if (arrDb.length !== 0) {
@@ -267,7 +275,7 @@ function _Array(conf) {
267275
break;
268276
}
269277

270-
if (classId.indexOf('_') !== 0) {
278+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
271279
$history.pushState({
272280
action: 'update',
273281
collection: classId,
@@ -394,10 +402,15 @@ function _Array(conf) {
394402
* @description Override sort method
395403
*/
396404
arr.sort = function sort(funct) {
397-
var oldValue = JSON.stringify(oldValue);
405+
var oldValue = null;
406+
407+
if ($history.isEnabled()) {
408+
oldValue = JSON.stringify(oldValue);
409+
}
410+
398411
arrDb.sort(funct);
399412

400-
if (classId.indexOf('_') !== 0) {
413+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
401414
$history.pushState({
402415
action: 'update',
403416
collection: classId,
@@ -431,10 +444,14 @@ function _Array(conf) {
431444
* @description Override reverse method
432445
*/
433446
arr.reverse = function reverse() {
434-
var oldValue = JSON.stringify(oldValue);
447+
var oldValue = null;
448+
449+
if ($history.isEnabled()) {
450+
oldValue = JSON.stringify(oldValue);
451+
}
435452
arrDb.reverse();
436453

437-
if (classId.indexOf('_') !== 0) {
454+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
438455
$history.pushState({
439456
action: 'update',
440457
collection: classId,
@@ -468,18 +485,22 @@ function _Array(conf) {
468485
* @description Override splice method
469486
*/
470487
arr.splice = function splice(start, deleteCount, val) {
471-
var oldValue = JSON.stringify(oldValue);
488+
var oldValue = null;
472489
var result = [];
473490
var i = 0;
474491
var length = 0;
475492
var data = null;
476493

494+
if ($history.isEnabled()) {
495+
oldValue = JSON.stringify(oldValue);
496+
}
497+
477498
if (typeof val !== 'undefined') {
478499
_add(val, 'splice', start, deleteCount);
479500
} else {
480501
result = arrDb.splice(start, deleteCount);
481502

482-
if (classId.indexOf('_') !== 0) {
503+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
483504
$history.pushState({
484505
action: 'update',
485506
collection: classId,
@@ -804,7 +825,7 @@ function createClass(classId) {
804825
// create link to db
805826
$db.store[classId][config[$mson.ID]] = config;
806827

807-
if (classId.indexOf('_') !== 0) {
828+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
808829
$history.pushState({
809830
action: 'insert',
810831
collection: classId,
@@ -970,11 +991,13 @@ function addProperties(model, Class, classId) {
970991
data: [position, 'reset']
971992
});
972993

973-
oldValue = JSON.stringify(component[propertyName]);
994+
if ($history.isEnabled()) {
995+
oldValue = JSON.stringify(component[propertyName]);
996+
}
974997

975998
component[propertyName] = realVal;
976999

977-
if (classId.indexOf('_') !== 0) {
1000+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
9781001
$history.pushState({
9791002
action: 'update',
9801003
collection: classId,
@@ -1104,9 +1127,11 @@ function addProperties(model, Class, classId) {
11041127
component = search[0];
11051128
component[propertyName][position] = realVal;
11061129

1107-
oldValue = JSON.stringify(component[propertyName]);
1130+
if ($history.isEnabled()) {
1131+
oldValue = JSON.stringify(component[propertyName]);
1132+
}
11081133

1109-
if (classId.indexOf('_') !== 0) {
1134+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
11101135
$history.pushState({
11111136
action: 'update',
11121137
collection: classId,
@@ -1214,7 +1239,10 @@ function addProperties(model, Class, classId) {
12141239
});
12151240
if (search.length) {
12161241
component = search[0];
1217-
oldValue = JSON.stringify(component[propertyName]);
1242+
1243+
if ($history.isEnabled()) {
1244+
oldValue = JSON.stringify(component[propertyName]);
1245+
}
12181246

12191247
switch (true) {
12201248
case $metamodel.isClassName(propertyType):
@@ -1236,7 +1264,7 @@ function addProperties(model, Class, classId) {
12361264
break;
12371265
}
12381266

1239-
if (classId.indexOf('_') !== 0) {
1267+
if ($history.isEnabled() && classId.indexOf('_') !== 0) {
12401268
$history.pushState({
12411269
action: 'update',
12421270
collection: classId,
@@ -1378,7 +1406,10 @@ function addStructure(path, name, model, id) {
13781406
_id: id
13791407
});
13801408
if (search.length) {
1381-
oldValue = getStructureValue(model, id, fullPath);
1409+
if ($history.isEnabled()) {
1410+
oldValue = getStructureValue(model, id, fullPath);
1411+
}
1412+
13821413
setStructureValue(model, id, fullPath, position);
13831414

13841415
// all element
@@ -1397,7 +1428,7 @@ function addStructure(path, name, model, id) {
13971428
data: [position, 'reset']
13981429
});
13991430

1400-
if (model.indexOf('_') !== 0) {
1431+
if ($history.isEnabled() && model.indexOf('_') !== 0) {
14011432
$history.pushState({
14021433
action: 'update',
14031434
collection: model,
@@ -1543,10 +1574,13 @@ function addStructure(path, name, model, id) {
15431574
break;
15441575
}
15451576

1546-
oldValue = getStructureValue(model, id, fullPath);
1577+
if ($history.isEnabled()) {
1578+
oldValue = getStructureValue(model, id, fullPath);
1579+
}
1580+
15471581
setStructureValue(model, id, fullPath, arr);
15481582

1549-
if (model.indexOf('_') !== 0) {
1583+
if ($history.isEnabled() && model.indexOf('_') !== 0) {
15501584
$history.pushState({
15511585
action: 'update',
15521586
collection: model,
@@ -1668,7 +1702,9 @@ function addStructure(path, name, model, id) {
16681702
if (search.length) {
16691703
component = search[0];
16701704

1671-
oldValue = getStructureValue(model, id, fullPath);
1705+
if ($history.isEnabled()) {
1706+
oldValue = getStructureValue(model, id, fullPath);
1707+
}
16721708

16731709
switch (true) {
16741710
case $metamodel.isClassName(propertyType):
@@ -1687,7 +1723,7 @@ function addStructure(path, name, model, id) {
16871723
break;
16881724
}
16891725

1690-
if (model.indexOf('_') !== 0) {
1726+
if ($history.isEnabled() && model.indexOf('_') !== 0) {
16911727
$history.pushState({
16921728
action: 'update',
16931729
collection: model,

src/db.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ var internalDB = [
6868
'_System',
6969
'_Message',
7070
'_Channel',
71-
'_Logger'
71+
'_Logger',
72+
'_History'
7273
];
7374
var coreDb = [
7475
'_Schema',
7576
'_GeneratedSchema',
7677
'_Logger',
7778
'_Model',
7879
'_GeneratedModel',
79-
'_Type'
80+
'_Type',
81+
'_History'
8082
];
8183

8284
/* Private methods */
@@ -733,7 +735,7 @@ DatabaseCollection.prototype.insert = function insert(document) {
733735
component = new Component(obj);
734736
result.push(component.id());
735737
} else {
736-
if (this.name.indexOf('_') !== 0) {
738+
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
737739
$history.pushState({
738740
action: 'insert',
739741
collection: this.name,
@@ -853,7 +855,7 @@ DatabaseCollection.prototype.update = function update(query, update, options) {
853855
}
854856
if (type) {
855857
if ($metamodel.isValidType(update[attributeName], type)) {
856-
if (this.name.indexOf('_') !== 0) {
858+
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
857859
$history.pushState({
858860
action: 'update',
859861
collection: this.name,
@@ -909,7 +911,7 @@ DatabaseCollection.prototype.update = function update(query, update, options) {
909911
}
910912
} else {
911913
// TODO more check in case of schema update
912-
if (this.name.indexOf('_') !== 0) {
914+
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
913915
$history.pushState({
914916
action: 'update',
915917
collection: this.name,
@@ -970,7 +972,7 @@ DatabaseCollection.prototype.remove = function remove(query) {
970972
object = exports.store[this.name][id];
971973

972974
if (isValid(criteria, object)) {
973-
if (this.name.indexOf('_') !== 0) {
975+
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
974976
$history.pushState({
975977
action: 'remove',
976978
collection: this.name,
@@ -1004,7 +1006,7 @@ DatabaseCollection.prototype.remove = function remove(query) {
10041006
object = exports.store[this.name][id];
10051007

10061008
if (isValid(query, object)) {
1007-
if (this.name.indexOf('_') !== 0) {
1009+
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
10081010
$history.pushState({
10091011
action: 'remove',
10101012
collection: this.name,
@@ -1034,7 +1036,7 @@ DatabaseCollection.prototype.remove = function remove(query) {
10341036
}
10351037
} else {
10361038
for (id in exports.store[this.name]) {
1037-
if (this.name.indexOf('_') !== 0) {
1039+
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
10381040
$history.pushState({
10391041
action: 'remove',
10401042
collection: this.name,

src/history.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,35 @@ var $db = require('./db.js');
3232

3333
var stack = [];
3434
var cursorIndex = -1;
35-
36-
/* Private methods */
35+
var historizationEnabled = false;
3736

3837
/* Public methods */
3938

39+
/**
40+
* @method isEnabled
41+
* @returns {Boolean} true if the history is enabled
42+
* @description Is historization enabled
43+
*/
44+
exports.isEnabled = function isEnabled() {
45+
return historizationEnabled;
46+
};
47+
48+
/**
49+
* @method start
50+
* @description start historization
51+
*/
52+
exports.start = function start() {
53+
historizationEnabled = true;
54+
};
55+
56+
/**
57+
* @method stop
58+
* @description stop historization
59+
*/
60+
exports.stop = function stop() {
61+
historizationEnabled = false;
62+
};
63+
4064
/**
4165
* @method pushState
4266
* @param {Object} state new state

src/metamodel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ function initDbStructure() {
811811
$db.collection('_Type');
812812
$db.collection('_Message');
813813
$db.collection('_Channel');
814+
$db.collection('_History');
814815
}
815816

816817
/**

src/system/system-runtime.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/runtime/history-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ describe('System Runtime history component', () => {
66
expect = require('chai').expect;
77
}
88

9+
beforeEach(() => {
10+
const history = runtime.require('history');
11+
history.start();
12+
});
13+
14+
afterEach(() => {
15+
const history = runtime.require('history');
16+
history.stop();
17+
});
18+
919
it('exists', () => {
1020
const history = runtime.require('history');
1121

0 commit comments

Comments
 (0)