Skip to content

Commit 756cca2

Browse files
committed
We update computed values after each recalculation to observe new derivations
1 parent 6775f3b commit 756cca2

File tree

5 files changed

+84
-74
lines changed

5 files changed

+84
-74
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.20.0",
3+
"version": "0.20.1",
44
"main": "./dist/nodejs.js",
55
"browser": "./dist/browser.js",
66
"unpkg": "./dist/browser.min.js",

src/core/objects/createComputed.js

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,30 @@
11

2-
dop.core.createComputed = function (object, property, f, shallWeSet, oldValue) {
3-
4-
var computed_id = dop.data.computed_inc++,
5-
data_path = dop.data.path,
6-
derived_paths,
7-
derived_pathsids = [],
8-
derived_path,
9-
derived_pathid,
10-
computed_path,
11-
computed_pathid,
12-
value,
13-
index = 0,
14-
total,
15-
index2,
16-
total2;
17-
18-
19-
// Running function and saving paths from getters
20-
dop.data.gets_collecting = true;
21-
value = f.call(object, oldValue);
22-
dop.data.gets_collecting = false;
23-
derived_paths = dop.data.gets_paths;
24-
dop.data.gets_paths = [];
25-
26-
27-
// Generating and storing paths ids
28-
for (total=derived_paths.length; index<total; ++index) {
29-
derived_path = derived_paths[index];
30-
derived_pathid = '';
31-
for (index2=0,total2=derived_path.length; index2<total2; ++index2) {
32-
derived_pathid += dop.core.pathSeparator(derived_path[index2]);
33-
if (index2>0) {
34-
if (data_path[derived_pathid] === undefined)
35-
data_path[derived_pathid] = {};
36-
37-
if (data_path[derived_pathid].derivations === undefined)
38-
data_path[derived_pathid].derivations = [];
39-
40-
if (data_path[derived_pathid].derivations.indexOf(computed_id) < 0) {
41-
data_path[derived_pathid].derivations.push(computed_id);
42-
derived_pathsids.push(derived_pathid);
43-
}
44-
}
45-
}
46-
}
2+
dop.core.createComputed = function (object, prop, f, shallWeSet, oldValue) {
3+
var data_path = dop.data.path,
4+
computed_id = dop.data.computed_inc++,
5+
computed = {
6+
object_root: dop.getObjectRoot(object),
7+
prop: prop,
8+
function: f,
9+
derivations: []
10+
},
11+
path = dop.getObjectPath(object, false);
4712

48-
computed_path = dop.getObjectPath(object, false);
49-
computed_pathid = dop.core.getPathId(computed_path.concat(property));
13+
computed.path = path.slice(1);
14+
computed.pathid = dop.core.getPathId(path.concat(prop));
5015

51-
// Storing computed in dop.data
52-
if (data_path[computed_pathid] === undefined)
53-
data_path[computed_pathid] = {};
16+
if (data_path[computed.pathid] === undefined)
17+
data_path[computed.pathid] = {};
5418

55-
if (data_path[computed_pathid].computeds === undefined)
56-
data_path[computed_pathid].computeds = [];
57-
58-
data_path[computed_pathid].computeds.push(computed_id);
19+
if (data_path[computed.pathid].computeds === undefined)
20+
data_path[computed.pathid].computeds = [];
5921

60-
dop.data.computed[computed_id] = {
61-
object_root: dop.getObjectRoot(object),
62-
path: computed_path.slice(1),
63-
prop: property,
64-
function: f,
65-
derivations: derived_pathsids
66-
};
22+
dop.data.computed[computed_id] = computed;
23+
value = dop.core.updateComputed(computed_id, computed, object, oldValue);
6724

6825
// Setting value
6926
if (shallWeSet)
70-
dop.core.set(object, property, value);
27+
dop.core.set(object, prop, value);
7128

7229
return value;
7330
};

src/core/objects/runDerivations.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ dop.core.runDerivations = function(path_id) {
33
if (dop.data.path[path_id] !== undefined && dop.data.path[path_id].derivations !== undefined) {
44
var derivations = dop.data.path[path_id].derivations,
55
computed,
6+
computed_id,
67
object,
78
value,
89
total = derivations.length,
910
index = 0;
1011

1112
for (;index<total; ++index) {
12-
computed = dop.data.computed[derivations[index]];
13+
computed_id = derivations[index];
14+
computed = dop.data.computed[computed_id];
1315
object = dop.util.get(computed.object_root, computed.path);
1416
if (object !== undefined)
1517
dop.core.set(
1618
object,
1719
computed.prop,
18-
computed.function.call(object, object[computed.prop])
20+
// computed.function.call(object, object[computed.prop])
21+
dop.core.updateComputed(computed_id, computed, object, object[computed.prop])
1922
);
2023
}
2124
}

src/core/objects/storeMutation.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ dop.core.storeMutation = function(mutation) {
33

44
var collectors = dop.data.collectors,
55
index=0,
6-
total=collectors.length;
6+
total=collectors.length,
7+
path_id;
78

89
// Saving path_id
9-
mutation.path_id = dop.core.getPathId(
10-
(mutation.splice!==undefined || mutation.swaps!==undefined) ?
11-
mutation.path
12-
:
13-
mutation.path.concat(mutation.prop)
14-
);
10+
path_id = mutation.path_id = dop.core.getPathId(mutation.path);
1511

12+
if (mutation.splice===undefined && mutation.swaps===undefined)
13+
path_id += dop.core.pathSeparator(mutation.prop);
1614

1715
// Running collectors
1816
for (;index<total; index++)
1917
if (collectors[index].add(mutation))
20-
return dop.core.runDerivations(mutation.path_id);
18+
return dop.core.runDerivations(path_id);
2119

2220

2321
var snapshot = new dop.core.snapshot([mutation]);
2422
snapshot.emit();
2523

26-
dop.core.runDerivations(mutation.path_id);
24+
dop.core.runDerivations(path_id);
2725
};

src/core/objects/updateComputed.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
dop.core.updateComputed = function (computed_id, computed, context, oldValue) {
3+
4+
var data_path = dop.data.path,
5+
derived_paths,
6+
derived_pathsids = computed.derivations,
7+
derived_path,
8+
derived_pathid,
9+
value,
10+
index = 0,
11+
total,
12+
index2,
13+
total2;
14+
15+
16+
// Running function and saving paths from getters
17+
dop.data.gets_collecting = true;
18+
value = computed.function.call(context, oldValue);
19+
dop.data.gets_collecting = false;
20+
derived_paths = dop.data.gets_paths;
21+
dop.data.gets_paths = [];
22+
23+
24+
// Generating and storing paths ids
25+
for (total=derived_paths.length; index<total; ++index) {
26+
derived_path = derived_paths[index];
27+
derived_pathid = '';
28+
for (index2=0,total2=derived_path.length; index2<total2; ++index2) {
29+
derived_pathid += dop.core.pathSeparator(derived_path[index2]);
30+
if (index2>0) {
31+
if (data_path[derived_pathid] === undefined)
32+
data_path[derived_pathid] = {};
33+
34+
if (data_path[derived_pathid].derivations === undefined)
35+
data_path[derived_pathid].derivations = [];
36+
37+
if (data_path[derived_pathid].derivations.indexOf(computed_id) < 0) {
38+
data_path[derived_pathid].derivations.push(computed_id);
39+
derived_pathsids.push(derived_pathid);
40+
}
41+
}
42+
}
43+
}
44+
45+
46+
// Storing computed in dop.data
47+
if (data_path[computed.pathid].computeds.indexOf(computed_id) === -1)
48+
data_path[computed.pathid].computeds.push(computed_id);
49+
50+
51+
return value;
52+
};

0 commit comments

Comments
 (0)