forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectStateMutations.js
158 lines (145 loc) · 4.26 KB
/
ObjectStateMutations.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
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
import encode from './encode';
import ParseFile from './ParseFile';
import ParseObject from './ParseObject';
import ParseRelation from './ParseRelation';
import TaskQueue from './TaskQueue';
import { RelationOp } from './ParseOp';
import type { Op } from './ParseOp';
export type AttributeMap = { [attr: string]: any };
export type OpsMap = { [attr: string]: Op };
export type ObjectCache = { [attr: string]: string };
export type State = {
serverData: AttributeMap;
pendingOps: Array<OpsMap>;
objectCache: ObjectCache;
tasks: TaskQueue;
existed: boolean
};
export function defaultState(): State {
return {
serverData: {},
pendingOps: [{}],
objectCache: {},
tasks: new TaskQueue(),
existed: false
};
}
export function setServerData(serverData: AttributeMap, attributes: AttributeMap) {
for (const attr in attributes) {
if (typeof attributes[attr] !== 'undefined') {
serverData[attr] = attributes[attr];
} else {
delete serverData[attr];
}
}
}
export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op: ?Op) {
const last = pendingOps.length - 1;
if (op) {
pendingOps[last][attr] = op;
} else {
delete pendingOps[last][attr];
}
}
export function pushPendingState(pendingOps: Array<OpsMap>) {
pendingOps.push({});
}
export function popPendingState(pendingOps: Array<OpsMap>): OpsMap {
const first = pendingOps.shift();
if (!pendingOps.length) {
pendingOps[0] = {};
}
return first;
}
export function mergeFirstPendingState(pendingOps: Array<OpsMap>) {
const first = popPendingState(pendingOps);
const next = pendingOps[0];
for (const attr in first) {
if (next[attr] && first[attr]) {
const merged = next[attr].mergeWith(first[attr]);
if (merged) {
next[attr] = merged;
}
} else {
next[attr] = first[attr];
}
}
}
export function estimateAttribute(serverData: AttributeMap, pendingOps: Array<OpsMap>, className: string, id: ?string, attr: string): mixed {
let value = serverData[attr];
for (let i = 0; i < pendingOps.length; i++) {
if (pendingOps[i][attr]) {
if (pendingOps[i][attr] instanceof RelationOp) {
if (id) {
value = pendingOps[i][attr].applyTo(
value,
{ className: className, id: id },
attr
);
}
} else {
value = pendingOps[i][attr].applyTo(value);
}
}
}
return value;
}
export function estimateAttributes(serverData: AttributeMap, pendingOps: Array<OpsMap>, className: string, id: ?string): AttributeMap {
const data = {};
let attr;
for (attr in serverData) {
data[attr] = serverData[attr];
}
for (let i = 0; i < pendingOps.length; i++) {
for (attr in pendingOps[i]) {
if (pendingOps[i][attr] instanceof RelationOp) {
if (id) {
data[attr] = pendingOps[i][attr].applyTo(
data[attr],
{ className: className, id: id },
attr
);
}
} else {
if (attr.includes('.')) {
// convert a.b.c into { a: { b: { c: value } } }
const fields = attr.split('.');
const last = fields[fields.length - 1];
let object = Object.assign({}, data);
for (let i = 0; i < fields.length - 1; i++) {
object = object[fields[i]];
}
object[last] = pendingOps[i][attr].applyTo(object[last]);
} else {
data[attr] = pendingOps[i][attr].applyTo(data[attr]);
}
}
}
}
return data;
}
export function commitServerChanges(serverData: AttributeMap, objectCache: ObjectCache, changes: AttributeMap) {
for (const attr in changes) {
const val = changes[attr];
serverData[attr] = val;
if (val &&
typeof val === 'object' &&
!(val instanceof ParseObject) &&
!(val instanceof ParseFile) &&
!(val instanceof ParseRelation)
) {
const json = encode(val, false, true);
objectCache[attr] = JSON.stringify(json);
}
}
}