Skip to content

Commit a7b729d

Browse files
committed
Split ObjectState out into utilities and SingleInstanceState
1 parent 7da2856 commit a7b729d

File tree

4 files changed

+642
-430
lines changed

4 files changed

+642
-430
lines changed

src/ObjectState.js

Lines changed: 41 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -23,116 +23,51 @@ export type AttributeMap = { [attr: string]: any };
2323
export type OpsMap = { [attr: string]: Op };
2424
export type ObjectCache = { [attr: string]: string };
2525

26-
type State = {
26+
export type State = {
2727
serverData: AttributeMap;
2828
pendingOps: Array<OpsMap>;
2929
objectCache: ObjectCache;
3030
tasks: TaskQueue;
3131
existed: boolean
3232
};
3333

34-
var objectState: {
35-
[className: string]: {
36-
[id: string]: State
37-
}
38-
} = {};
39-
40-
export function getState(className: string, id: string): ?State {
41-
var classData = objectState[className];
42-
if (classData) {
43-
return classData[id] || null;
44-
}
45-
return null;
46-
}
47-
48-
export function initializeState(className: string, id: string, initial?: State): State {
49-
var state = getState(className, id);
50-
if (state) {
51-
return state;
52-
}
53-
if (!objectState[className]) {
54-
objectState[className] = {};
55-
}
56-
if (!initial) {
57-
initial = {
58-
serverData: {},
59-
pendingOps: [{}],
60-
objectCache: {},
61-
tasks: new TaskQueue(),
62-
existed: false
63-
};
64-
}
65-
state = objectState[className][id] = initial;
66-
return state;
67-
}
68-
69-
export function removeState(className: string, id: string): ?State {
70-
var state = getState(className, id);
71-
if (state === null) {
72-
return null;
73-
}
74-
delete objectState[className][id];
75-
return state;
76-
}
77-
78-
export function getServerData(className: string, id: string): AttributeMap {
79-
var state = getState(className, id);
80-
if (state) {
81-
return state.serverData;
82-
}
83-
return {};
84-
}
85-
86-
export function setServerData(className: string, id: string, attributes: AttributeMap) {
87-
var data = initializeState(className, id).serverData;
88-
for (var attr in attributes) {
34+
export function setServerData(serverData: AttributeMap, attributes: AttributeMap) {
35+
for (let attr in attributes) {
8936
if (typeof attributes[attr] !== 'undefined') {
90-
data[attr] = attributes[attr];
37+
serverData[attr] = attributes[attr];
9138
} else {
92-
delete data[attr];
39+
delete serverData[attr];
9340
}
9441
}
9542
}
9643

97-
export function getPendingOps(className: string, id: string): Array<OpsMap> {
98-
var state = getState(className, id);
99-
if (state) {
100-
return state.pendingOps;
101-
}
102-
return [{}];
103-
}
104-
105-
export function setPendingOp(className: string, id: string, attr: string, op: ?Op) {
106-
var pending = initializeState(className, id).pendingOps;
107-
var last = pending.length - 1;
44+
export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op: ?Op) {
45+
let last = pendingOps.length - 1;
10846
if (op) {
109-
pending[last][attr] = op;
47+
pendingOps[last][attr] = op;
11048
} else {
111-
delete pending[last][attr];
49+
delete pendingOps[last][attr];
11250
}
11351
}
11452

115-
export function pushPendingState(className: string, id: string) {
116-
var pending = initializeState(className, id).pendingOps;
117-
pending.push({});
53+
export function pushPendingState(pendingOps: Array<OpsMap>) {
54+
pendingOps.push({});
11855
}
11956

120-
export function popPendingState(className: string, id: string): OpsMap {
121-
var pending = initializeState(className, id).pendingOps;
122-
var first = pending.shift();
123-
if (!pending.length) {
124-
pending[0] = {};
57+
export function popPendingState(pendingOps: Array<OpsMap>): OpsMap {
58+
let first = pendingOps.shift();
59+
if (!pendingOps.length) {
60+
pendingOps[0] = {};
12561
}
12662
return first;
12763
}
12864

129-
export function mergeFirstPendingState(className: string, id: string) {
130-
var first = popPendingState(className, id);
131-
var pending = getPendingOps(className, id);
132-
var next = pending[0];
133-
for (var attr in first) {
65+
export function mergeFirstPendingState(pendingOps: Array<OpsMap>) {
66+
let first = popPendingState(pendingOps);
67+
let next = pendingOps[0];
68+
for (let attr in first) {
13469
if (next[attr] && first[attr]) {
135-
var merged = next[attr].mergeWith(first[attr]);
70+
let merged = next[attr].mergeWith(first[attr]);
13671
if (merged) {
13772
next[attr] = merged;
13873
}
@@ -142,80 +77,58 @@ export function mergeFirstPendingState(className: string, id: string) {
14277
}
14378
}
14479

145-
export function getObjectCache(className: string, id: string): ObjectCache {
146-
var state = getState(className, id);
147-
if (state) {
148-
return state.objectCache;
149-
}
150-
return {};
151-
}
152-
153-
export function estimateAttribute(className: string, id: string, attr: string): mixed {
154-
var serverData = getServerData(className, id);
155-
var value = serverData[attr];
156-
var pending = getPendingOps(className, id);
157-
for (var i = 0; i < pending.length; i++) {
158-
if (pending[i][attr]) {
159-
if (pending[i][attr] instanceof RelationOp) {
160-
value = pending[i][attr].applyTo(
80+
export function estimateAttribute(serverData: AttributeMap, pendingOps: Array<OpsMap>, className: string, id: string, attr: string): mixed {
81+
let value = serverData[attr];
82+
for (let i = 0; i < pendingOps.length; i++) {
83+
if (pendingOps[i][attr]) {
84+
if (pendingOps[i][attr] instanceof RelationOp) {
85+
value = pendingOps[i][attr].applyTo(
16186
value,
16287
{ className: className, id: id },
16388
attr
16489
);
16590
} else {
166-
value = pending[i][attr].applyTo(value);
91+
value = pendingOps[i][attr].applyTo(value);
16792
}
16893
}
16994
}
17095
return value;
17196
}
17297

173-
export function estimateAttributes(className: string, id: string): AttributeMap {
174-
var data = {};
175-
var attr;
176-
var serverData = getServerData(className, id);
98+
export function estimateAttributes(serverData: AttributeMap, pendingOps: Array<OpsMap>, className: string, id: string): AttributeMap {
99+
let data = {};
100+
let attr;
177101
for (attr in serverData) {
178102
data[attr] = serverData[attr];
179103
}
180-
var pending = getPendingOps(className, id);
181-
for (var i = 0; i < pending.length; i++) {
182-
for (attr in pending[i]) {
183-
if (pending[i][attr] instanceof RelationOp) {
184-
data[attr] = pending[i][attr].applyTo(
104+
for (let i = 0; i < pendingOps.length; i++) {
105+
for (attr in pendingOps[i]) {
106+
if (pendingOps[i][attr] instanceof RelationOp) {
107+
data[attr] = pendingOps[i][attr].applyTo(
185108
data[attr],
186109
{ className: className, id: id },
187110
attr
188111
);
189112
} else {
190-
data[attr] = pending[i][attr].applyTo(data[attr]);
113+
data[attr] = pendingOps[i][attr].applyTo(data[attr]);
191114
}
192115
}
193116
}
194117
return data;
195118
}
196119

197-
export function commitServerChanges(className: string, id: string, changes: AttributeMap) {
198-
var state = initializeState(className, id);
199-
for (var attr in changes) {
200-
var val = changes[attr];
201-
state.serverData[attr] = val;
120+
export function commitServerChanges(serverData: AttributeMap, objectCache: ObjectCache, changes: AttributeMap) {
121+
for (let attr in changes) {
122+
let val = changes[attr];
123+
serverData[attr] = val;
202124
if (val &&
203125
typeof val === 'object' &&
204126
!(val instanceof ParseObject) &&
205127
!(val instanceof ParseFile) &&
206128
!(val instanceof ParseRelation)
207129
) {
208-
var json = encode(val, false, true);
209-
state.objectCache[attr] = JSON.stringify(json);
130+
let json = encode(val, false, true);
131+
objectCache[attr] = JSON.stringify(json);
210132
}
211133
}
212134
}
213-
214-
export function enqueueTask(className: string, id: string, task: () => ParsePromise) {
215-
var state = initializeState(className, id);
216-
return state.tasks.enqueue(task);
217-
}
218-
219-
export function _clearAllState() {
220-
objectState = {};
221-
}

src/SingleInstanceState.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*/
11+
12+
import * as ObjectState from './ObjectState';
13+
import TaskQueue from './TaskQueue';
14+
15+
let objectState: {
16+
[className: string]: {
17+
[id: string]: State
18+
}
19+
} = {};
20+
21+
export function getState(className: string, id: string): ?State {
22+
let classData = objectState[className];
23+
if (classData) {
24+
return classData[id] || null;
25+
}
26+
return null;
27+
}
28+
29+
export function initializeState(className: string, id: string, initial?: State): State {
30+
let state = getState(className, id);
31+
if (state) {
32+
return state;
33+
}
34+
if (!objectState[className]) {
35+
objectState[className] = {};
36+
}
37+
if (!initial) {
38+
initial = {
39+
serverData: {},
40+
pendingOps: [{}],
41+
objectCache: {},
42+
tasks: new TaskQueue(),
43+
existed: false
44+
};
45+
}
46+
state = objectState[className][id] = initial;
47+
return state;
48+
}
49+
50+
export function removeState(className: string, id: string): ?State {
51+
let state = getState(className, id);
52+
if (state === null) {
53+
return null;
54+
}
55+
delete objectState[className][id];
56+
return state;
57+
}
58+
59+
export function getServerData(className: string, id: string): AttributeMap {
60+
let state = getState(className, id);
61+
if (state) {
62+
return state.serverData;
63+
}
64+
return {};
65+
}
66+
67+
export function setServerData(className: string, id: string, attributes: AttributeMap) {
68+
let serverData = initializeState(className, id).serverData;
69+
ObjectState.setServerData(serverData, attributes);
70+
}
71+
72+
export function getPendingOps(className: string, id: string): Array<OpsMap> {
73+
let state = getState(className, id);
74+
if (state) {
75+
return state.pendingOps;
76+
}
77+
return [{}];
78+
}
79+
80+
export function setPendingOp(className: string, id: string, attr: string, op: ?Op) {
81+
let pendingOps = initializeState(className, id).pendingOps;
82+
ObjectState.setPendingOp(pendingOps, attr, op);
83+
}
84+
85+
export function pushPendingState(className: string, id: string) {
86+
let pendingOps = initializeState(className, id).pendingOps;
87+
ObjectState.pushPendingState(pendingOps);
88+
}
89+
90+
export function popPendingState(className: string, id: string): OpsMap {
91+
let pendingOps = initializeState(className, id).pendingOps;
92+
return ObjectState.popPendingState(pendingOps);
93+
}
94+
95+
export function mergeFirstPendingState(className: string, id: string) {
96+
let pendingOps = getPendingOps(className, id);
97+
ObjectState.mergeFirstPendingState(pendingOps);
98+
}
99+
100+
export function getObjectCache(className: string, id: string): ObjectCache {
101+
let state = getState(className, id);
102+
if (state) {
103+
return state.objectCache;
104+
}
105+
return {};
106+
}
107+
108+
export function estimateAttribute(className: string, id: string, attr: string): mixed {
109+
let serverData = getServerData(className, id);
110+
let pendingOps = getPendingOps(className, id);
111+
return ObjectState.estimateAttribute(serverData, pendingOps, className, id, attr);
112+
}
113+
114+
export function estimateAttributes(className: string, id: string): AttributeMap {
115+
let serverData = getServerData(className, id);
116+
let pendingOps = getPendingOps(className, id);
117+
return ObjectState.estimateAttributes(serverData, pendingOps, className, id);
118+
}
119+
120+
export function commitServerChanges(className: string, id: string, changes: AttributeMap) {
121+
let state = initializeState(className, id);
122+
ObjectState.commitServerChanges(state.serverData, state.objectCache, changes);
123+
}
124+
125+
export function enqueueTask(className: string, id: string, task: () => ParsePromise) {
126+
let state = initializeState(className, id);
127+
return state.tasks.enqueue(task);
128+
}
129+
130+
export function _clearAllState() {
131+
objectState = {};
132+
}

0 commit comments

Comments
 (0)