Skip to content

Commit 8a7b2f4

Browse files
committed
Migrate ParseObject to use SingleInstanceState
1 parent a7b729d commit 8a7b2f4

File tree

1 file changed

+84
-23
lines changed

1 file changed

+84
-23
lines changed

src/ParseObject.js

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import decode from './decode';
1515
import encode from './encode';
1616
import equals from './equals';
1717
import escape from './escape';
18-
import * as ObjectState from './ObjectState';
1918
import ParseACL from './ParseACL';
2019
import parseDate from './parseDate';
2120
import ParseError from './ParseError';
@@ -34,6 +33,7 @@ import {
3433
import ParsePromise from './ParsePromise';
3534
import ParseQuery from './ParseQuery';
3635
import ParseRelation from './ParseRelation';
36+
import * as SingleInstanceState from './SingleInstanceState';
3737
import unique from './unique';
3838
import unsavedChildren from './unsavedChildren';
3939

@@ -131,9 +131,11 @@ export default class ParseObject {
131131
/** Prototype getters / setters **/
132132

133133
get attributes(): AttributeMap {
134-
return Object.freeze(
135-
ObjectState.estimateAttributes(this.className, this._getStateIdentifier())
136-
);
134+
let attributes = singleInstance ?
135+
SingleInstanceState.estimateAttributes(this.className, this._getStateIdentifier()) :
136+
null;
137+
// TODO: implement uniqueInstanceState
138+
return Object.freeze(attributes);
137139
}
138140

139141
/**
@@ -187,7 +189,11 @@ export default class ParseObject {
187189
}
188190

189191
_getServerData(): AttributeMap {
190-
return ObjectState.getServerData(this.className, this._getStateIdentifier());
192+
if (singleInstance) {
193+
return SingleInstanceState.getServerData(this.className, this._getStateIdentifier());
194+
} else {
195+
// TODO: implement uniqueInstanceState
196+
}
191197
}
192198

193199
_clearServerData() {
@@ -196,11 +202,19 @@ export default class ParseObject {
196202
for (var attr in serverData) {
197203
unset[attr] = undefined;
198204
}
199-
ObjectState.setServerData(this.className, this._getStateIdentifier(), unset);
205+
if (singleInstance) {
206+
SingleInstanceState.setServerData(this.className, this._getStateIdentifier(), unset);
207+
} else {
208+
// TODO: implement uniqueInstanceState
209+
}
200210
}
201211

202212
_getPendingOps(): Array<OpsMap> {
203-
return ObjectState.getPendingOps(this.className, this._getStateIdentifier());
213+
if (singleInstance) {
214+
return SingleInstanceState.getPendingOps(this.className, this._getStateIdentifier());
215+
} else {
216+
// TODO: implement uniqueInstanceState
217+
}
204218
}
205219

206220
_clearPendingOps() {
@@ -214,7 +228,10 @@ export default class ParseObject {
214228

215229
_getDirtyObjectAttributes(): AttributeMap {
216230
var attributes = this.attributes;
217-
var objectCache = ObjectState.getObjectCache(this.className, this._getStateIdentifier());
231+
var objectCache = singleInstance ?
232+
SingleInstanceState.getObjectCache(this.className, this._getStateIdentifier()) :
233+
{};
234+
// TODO: implement uniqueInstanceState
218235
var dirty = {};
219236
for (var attr in attributes) {
220237
var val = attributes[attr];
@@ -283,7 +300,11 @@ export default class ParseObject {
283300
if (!this.id && serverData.objectId) {
284301
this.id = serverData.objectId;
285302
}
286-
ObjectState.initializeState(this.className, this._getStateIdentifier());
303+
if (singleInstance) {
304+
SingleInstanceState.initializeState(this.className, this._getStateIdentifier());
305+
} else {
306+
// TODO: implement uniqueInstanceState
307+
}
287308
var decoded = {};
288309
for (var attr in serverData) {
289310
if (attr === 'ACL') {
@@ -304,31 +325,41 @@ export default class ParseObject {
304325
if (!decoded.updatedAt && decoded.createdAt) {
305326
decoded.updatedAt = decoded.createdAt;
306327
}
307-
ObjectState.commitServerChanges(this.className, this._getStateIdentifier(), decoded);
328+
if (singleInstance) {
329+
SingleInstanceState.commitServerChanges(this.className, this._getStateIdentifier(), decoded);
330+
} else {
331+
// TODO: implement uniqueInstanceState
332+
}
308333
}
309334

310335
_setExisted(existed: boolean) {
311-
var state = ObjectState.getState(this.className, this._getStateIdentifier());
336+
let state = singleInstance ?
337+
SingleInstanceState.getState(this.className, this._getStateIdentifier()) :
338+
null;
339+
// TODO: implement uniqueInstanceState
312340
if (state) {
313341
state.existed = existed;
314342
}
315343
}
316344

317345
_migrateId(serverId: string) {
318346
if (this._localId && serverId) {
319-
var oldState = ObjectState.removeState(this.className, this._getStateIdentifier());
347+
var oldState = SingleInstanceState.removeState(this.className, this._getStateIdentifier());
320348
this.id = serverId;
321349
delete this._localId;
322350
if (oldState) {
323-
ObjectState.initializeState(this.className, this._getStateIdentifier(), oldState);
351+
SingleInstanceState.initializeState(this.className, this._getStateIdentifier(), oldState);
324352
}
325353
}
326354
}
327355

328356
_handleSaveResponse(response, status: number) {
329357
var changes = {};
330358
var attr;
331-
var pending = ObjectState.popPendingState(this.className, this._getStateIdentifier());
359+
var pending = singleInstance ?
360+
SingleInstanceState.popPendingState(this.className, this._getStateIdentifier()) :
361+
null;
362+
// TODO: implement uniqueInstanceState
332363
for (attr in pending) {
333364
if (pending[attr] instanceof RelationOp) {
334365
changes[attr] = pending[attr].applyTo(undefined, this, attr);
@@ -357,12 +388,20 @@ export default class ParseObject {
357388
this._setExisted(true);
358389
}
359390

360-
ObjectState.commitServerChanges(this.className, this._getStateIdentifier(), changes);
391+
if (singleInstance) {
392+
SingleInstanceState.commitServerChanges(this.className, this._getStateIdentifier(), changes);
393+
} else {
394+
// TODO: implement uniqueInstanceState
395+
}
361396
}
362397

363398
_handleSaveError() {
364399
var pending = this._getPendingOps();
365-
ObjectState.mergeFirstPendingState(this.className, this._getStateIdentifier());
400+
if (singleInstance) {
401+
SingleInstanceState.mergeFirstPendingState(this.className, this._getStateIdentifier());
402+
} else {
403+
// TODO: implement uniqueInstanceState
404+
}
366405
}
367406

368407
/** Public methods **/
@@ -651,7 +690,11 @@ export default class ParseObject {
651690
var last = pendingOps.length - 1;
652691
for (var attr in newOps) {
653692
var nextOp = newOps[attr].mergeWith(pendingOps[last][attr]);
654-
ObjectState.setPendingOp(this.className, this._getStateIdentifier(), attr, nextOp);
693+
if (singleInstance) {
694+
SingleInstanceState.setPendingOp(this.className, this._getStateIdentifier(), attr, nextOp);
695+
} else {
696+
// TODO: implement uniqueInstanceState
697+
}
655698
}
656699

657700
return this;
@@ -778,7 +821,10 @@ export default class ParseObject {
778821
if (!this.id) {
779822
return false;
780823
}
781-
var state = ObjectState.getState(this.className, this._getStateIdentifier());
824+
var state = singleInstance ?
825+
SingleInstanceState.getState(this.className, this._getStateIdentifier()) :
826+
null;
827+
// TODO: implement uniqueInstanceState
782828
if (state) {
783829
return state.existed;
784830
}
@@ -1046,7 +1092,11 @@ export default class ParseObject {
10461092
/** Static methods **/
10471093

10481094
static _clearAllState() {
1049-
ObjectState._clearAllState();
1095+
if (singleInstance) {
1096+
SingleInstanceState._clearAllState();
1097+
} else {
1098+
// TODO: implement uniqueInstanceState
1099+
}
10501100
}
10511101

10521102
/**
@@ -1699,8 +1749,12 @@ var DefaultController = {
16991749
}
17001750
});
17011751
};
1702-
ObjectState.pushPendingState(obj.className, obj._getStateIdentifier());
1703-
batchTasks.push(ObjectState.enqueueTask(obj.className, obj._getStateIdentifier(), task));
1752+
if (singleInstance) {
1753+
SingleInstanceState.pushPendingState(obj.className, obj._getStateIdentifier());
1754+
batchTasks.push(SingleInstanceState.enqueueTask(obj.className, obj._getStateIdentifier(), task));
1755+
} else {
1756+
// TODO: implement uniqueInstanceState
1757+
}
17041758
});
17051759

17061760
ParsePromise.when(batchReady).then(() => {
@@ -1742,8 +1796,15 @@ var DefaultController = {
17421796
return ParsePromise.error(error);
17431797
});
17441798
}
1745-
ObjectState.pushPendingState(target.className, target._getStateIdentifier());
1746-
return ObjectState.enqueueTask(target.className, target._getStateIdentifier(), task).then(() => {
1799+
let enqueueTask;
1800+
if (singleInstance) {
1801+
SingleInstanceState.pushPendingState(target.className, target._getStateIdentifier());
1802+
enqueueTask = SingleInstanceState.enqueueTask(target.className, target._getStateIdentifier(), task);
1803+
} else {
1804+
// TODO: implement uniqueInstanceState
1805+
}
1806+
1807+
return enqueueTask.then(() => {
17471808
return target;
17481809
}, (error) => {
17491810
return ParsePromise.error(error);

0 commit comments

Comments
 (0)