forked from forbesmyester/SyncIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateResult.js
107 lines (98 loc) · 2.73 KB
/
updateResult.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
module.exports = (function (manip) {
// Author: Matthew Forrester <matt_at_keyboardwritescode.com>
// Copyright: Matthew Forrester
// License: MIT/BSD-style
"use strict";
/**
* ### updateResult()
*
* Runs a `Pathitem` on a `PathRoot` or a `PathRoot` with one or more `Pathitem` already ran on it.
*
***Parameters**
*
* * **@param {PathRoot|updateResult(PathRoot,Pathitem)} `obToApplyTo`**
* * **@param {Pathitem} `pathitem`**
*/
var updateResult = function(obToApplyTo,pathitem,cloningFunction) {
if (!updateResult.hasOwnProperty('_op_'+pathitem.o)) {
throw new Error(
'SyncLib.updateResult No Operation: updateResult has no ' +
'operation '+
pathitem.o
);
}
var f = updateResult['_op_'+pathitem.o];
return f.call(this,obToApplyTo,pathitem,cloningFunction);
};
/**
* ### updateResult.op_update()
*
* Performs a MongoDB like update operation.
*
* #### Parameters
*
* * **@param {Object} `ob`** The Object to update
* * **@param {Object} `pathitem`** The *Pathitem* to apply
* * **@param {Function} `cloningFunction`** The function to use to create a clone of `ob`
* * **@return {Object}** The result.
*/
updateResult._op_update = function(ob,pathitem,cloningFunction) {
var r = cloningFunction(ob);
r.i = manip(r.i,pathitem.u,cloningFunction);
r.v = r.v + 1;
if (pathitem.hasOwnProperty('m')) {
r.m = pathitem.m;
}
r.t = pathitem.t;
r.r = false;
return r;
};
/**
* ### updateResult.op_removeData()
*
* Remove operations set the removed flag, increment the version and set the
* *modifier*.
*
* #### Parameters
*
* * **@param {Object} `ob`** The Object to update
* * **@param {Object} `pathitem`** The Pathitem to be applied
* * **@param {Function} `cloningFunction`** The function to use to create a clone of `ob`
* * **@return {Object}** The result
*/
updateResult._op_remove = function(ob,pathitem,cloningFunction) {
var r = cloningFunction(ob);
r.v = r.v + 1;
if (pathitem.hasOwnProperty('m')) {
r.m = pathitem.m;
}
r.t = pathitem.t;
r.r = true;
return r;
};
/**
* ### updateResult.op_set()
*
* A set operation is a local *Operation* which will overwrite all data. The
* version will be incremented, *Modifier* set and the removed flag will be unset.
*
* #### Parameters
*
* * **@param {Object} `ob`** The Object to update
* * **@param {Object} `pathitem`** The Pathitem to be applied
* * **@param {Function} `cloningFunction`** The function to use to create a clone of `ob`
* * **@return {Object}** The result
*/
updateResult._op_set = function(ob,pathitem,cloningFunction) {
var r = cloningFunction(ob);
r.i = pathitem.u;
r.v = r.v + 1;
if (pathitem.hasOwnProperty('m')) {
r.m = pathitem.m;
}
r.t = pathitem.t;
r.r = false;
return r;
};
return updateResult;
}(require('manip')));