Skip to content

Commit 386610b

Browse files
committed
Let ParseObject and ParseUser use UniqueInstanceState
1 parent 230f8f8 commit 386610b

File tree

6 files changed

+98
-61
lines changed

6 files changed

+98
-61
lines changed

src/ParseObject.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import ParseQuery from './ParseQuery';
3535
import ParseRelation from './ParseRelation';
3636
import * as SingleInstanceState from './SingleInstanceState';
3737
import unique from './unique';
38+
import * as UniqueInstanceState from './UniqueInstanceState';
3839
import unsavedChildren from './unsavedChildren';
3940

4041
import type { AttributeMap, OpsMap } from './ObjectState';
@@ -133,8 +134,7 @@ export default class ParseObject {
133134
get attributes(): AttributeMap {
134135
let attributes = singleInstance ?
135136
SingleInstanceState.estimateAttributes(this.className, this._getStateIdentifier()) :
136-
null;
137-
// TODO: implement uniqueInstanceState
137+
UniqueInstanceState.estimateAttributes(this);
138138
return Object.freeze(attributes);
139139
}
140140

@@ -192,7 +192,7 @@ export default class ParseObject {
192192
if (singleInstance) {
193193
return SingleInstanceState.getServerData(this.className, this._getStateIdentifier());
194194
} else {
195-
// TODO: implement uniqueInstanceState
195+
return UniqueInstanceState.getServerData(this);
196196
}
197197
}
198198

@@ -205,15 +205,15 @@ export default class ParseObject {
205205
if (singleInstance) {
206206
SingleInstanceState.setServerData(this.className, this._getStateIdentifier(), unset);
207207
} else {
208-
// TODO: implement uniqueInstanceState
208+
UniqueInstanceState.setServerData(this, unset);
209209
}
210210
}
211211

212212
_getPendingOps(): Array<OpsMap> {
213213
if (singleInstance) {
214214
return SingleInstanceState.getPendingOps(this.className, this._getStateIdentifier());
215215
} else {
216-
// TODO: implement uniqueInstanceState
216+
return UniqueInstanceState.getPendingOps(this);
217217
}
218218
}
219219

@@ -230,8 +230,7 @@ export default class ParseObject {
230230
var attributes = this.attributes;
231231
var objectCache = singleInstance ?
232232
SingleInstanceState.getObjectCache(this.className, this._getStateIdentifier()) :
233-
{};
234-
// TODO: implement uniqueInstanceState
233+
UniqueInstanceState.getObjectCache(this);
235234
var dirty = {};
236235
for (var attr in attributes) {
237236
var val = attributes[attr];
@@ -303,7 +302,7 @@ export default class ParseObject {
303302
if (singleInstance) {
304303
SingleInstanceState.initializeState(this.className, this._getStateIdentifier());
305304
} else {
306-
// TODO: implement uniqueInstanceState
305+
UniqueInstanceState.initializeState(this);
307306
}
308307
var decoded = {};
309308
for (var attr in serverData) {
@@ -328,27 +327,31 @@ export default class ParseObject {
328327
if (singleInstance) {
329328
SingleInstanceState.commitServerChanges(this.className, this._getStateIdentifier(), decoded);
330329
} else {
331-
// TODO: implement uniqueInstanceState
330+
UniqueInstanceState.commitServerChanges(this, decoded);
332331
}
333332
}
334333

335334
_setExisted(existed: boolean) {
336335
let state = singleInstance ?
337336
SingleInstanceState.getState(this.className, this._getStateIdentifier()) :
338-
null;
339-
// TODO: implement uniqueInstanceState
337+
UniqueInstanceState.getState(this);
340338
if (state) {
341339
state.existed = existed;
342340
}
343341
}
344342

345343
_migrateId(serverId: string) {
346344
if (this._localId && serverId) {
347-
var oldState = SingleInstanceState.removeState(this.className, this._getStateIdentifier());
348-
this.id = serverId;
349-
delete this._localId;
350-
if (oldState) {
351-
SingleInstanceState.initializeState(this.className, this._getStateIdentifier(), oldState);
345+
if (singleInstance) {
346+
var oldState = SingleInstanceState.removeState(this.className, this._getStateIdentifier());
347+
this.id = serverId;
348+
delete this._localId;
349+
if (oldState) {
350+
SingleInstanceState.initializeState(this.className, this._getStateIdentifier(), oldState);
351+
}
352+
} else {
353+
this.id = serverId;
354+
delete this._localId;
352355
}
353356
}
354357
}
@@ -358,8 +361,7 @@ export default class ParseObject {
358361
var attr;
359362
var pending = singleInstance ?
360363
SingleInstanceState.popPendingState(this.className, this._getStateIdentifier()) :
361-
null;
362-
// TODO: implement uniqueInstanceState
364+
UniqueInstanceState.popPendingState(this);
363365
for (attr in pending) {
364366
if (pending[attr] instanceof RelationOp) {
365367
changes[attr] = pending[attr].applyTo(undefined, this, attr);
@@ -391,7 +393,7 @@ export default class ParseObject {
391393
if (singleInstance) {
392394
SingleInstanceState.commitServerChanges(this.className, this._getStateIdentifier(), changes);
393395
} else {
394-
// TODO: implement uniqueInstanceState
396+
UniqueInstanceState.commitServerChanges(this, changes);
395397
}
396398
}
397399

@@ -400,7 +402,7 @@ export default class ParseObject {
400402
if (singleInstance) {
401403
SingleInstanceState.mergeFirstPendingState(this.className, this._getStateIdentifier());
402404
} else {
403-
// TODO: implement uniqueInstanceState
405+
UniqueInstanceState.mergeFirstPendingState(this);
404406
}
405407
}
406408

@@ -693,7 +695,7 @@ export default class ParseObject {
693695
if (singleInstance) {
694696
SingleInstanceState.setPendingOp(this.className, this._getStateIdentifier(), attr, nextOp);
695697
} else {
696-
// TODO: implement uniqueInstanceState
698+
UniqueInstanceState.setPendingOp(this, attr, nextOp);
697699
}
698700
}
699701

@@ -823,8 +825,7 @@ export default class ParseObject {
823825
}
824826
var state = singleInstance ?
825827
SingleInstanceState.getState(this.className, this._getStateIdentifier()) :
826-
null;
827-
// TODO: implement uniqueInstanceState
828+
UniqueInstanceState.getState(this);
828829
if (state) {
829830
return state.existed;
830831
}
@@ -1095,7 +1096,7 @@ export default class ParseObject {
10951096
if (singleInstance) {
10961097
SingleInstanceState._clearAllState();
10971098
} else {
1098-
// TODO: implement uniqueInstanceState
1099+
UniqueInstanceState._clearAllState();
10991100
}
11001101
}
11011102

@@ -1514,6 +1515,15 @@ export default class ParseObject {
15141515
static disableSingleInstance() {
15151516
singleInstance = false;
15161517
}
1518+
1519+
/**
1520+
* Returns a boolean marking whether single instance mode is enabled or not.
1521+
* @method isSingleInstance
1522+
* @return {Boolean} A boolean flag that is true if single instance is enabled.
1523+
*/
1524+
static isSingleInstance(): boolean {
1525+
return singleInstance;
1526+
}
15171527
}
15181528

15191529
var DefaultController = {
@@ -1753,7 +1763,8 @@ var DefaultController = {
17531763
SingleInstanceState.pushPendingState(obj.className, obj._getStateIdentifier());
17541764
batchTasks.push(SingleInstanceState.enqueueTask(obj.className, obj._getStateIdentifier(), task));
17551765
} else {
1756-
// TODO: implement uniqueInstanceState
1766+
UniqueInstanceState.pushPendingState(obj);
1767+
batchTasks.push(UniqueInstanceState.enqueueTask(obj, task));
17571768
}
17581769
});
17591770

@@ -1801,7 +1812,8 @@ var DefaultController = {
18011812
SingleInstanceState.pushPendingState(target.className, target._getStateIdentifier());
18021813
enqueueTask = SingleInstanceState.enqueueTask(target.className, target._getStateIdentifier(), task);
18031814
} else {
1804-
// TODO: implement uniqueInstanceState
1815+
UniqueInstanceState.pushPendingState(target);
1816+
enqueueTask = UniqueInstanceState.enqueueTask(target, task);
18051817
}
18061818

18071819
return enqueueTask.then(() => {

src/ParseUser.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
import CoreManager from './CoreManager';
1313
import isRevocableSession from './isRevocableSession';
14-
import * as ObjectState from './ObjectState';
1514
import ParseError from './ParseError';
1615
import ParseObject from './ParseObject';
1716
import ParsePromise from './ParsePromise';
1817
import ParseSession from './ParseSession';
18+
import * as SingleInstanceState from './SingleInstanceState';
1919
import Storage from './Storage';
20+
import * as UniqueInstanceState from './UniqueInstanceState';
2021

2122
import type { AttributeMap } from './ObjectState';
2223
import type { RequestOptions, FullOptions } from './RESTController';
@@ -857,12 +858,21 @@ var DefaultController = {
857858
).then((response, status) => {
858859
user._migrateId(response.objectId);
859860
user._setExisted(true);
860-
ObjectState.setPendingOp(
861-
user.className, user._getId(), 'username', undefined
862-
);
863-
ObjectState.setPendingOp(
864-
user.className, user._getId(), 'password', undefined
865-
);
861+
if (ParseObject.isSingleInstance()) {
862+
SingleInstanceState.setPendingOp(
863+
user.className, user._getId(), 'username', undefined
864+
);
865+
SingleInstanceState.setPendingOp(
866+
user.className, user._getId(), 'password', undefined
867+
);
868+
} else {
869+
UniqueInstanceState.setPendingOp(
870+
user, 'username', undefined
871+
);
872+
UniqueInstanceState.setPendingOp(
873+
user, 'password', undefined
874+
);
875+
}
866876
response.password = undefined;
867877
user._finishFetch(response);
868878
if (!canUseCurrentUser) {

src/__tests__/ParseObject-test.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jest.dontMock('../decode');
1414
jest.dontMock('../encode');
1515
jest.dontMock('../equals');
1616
jest.dontMock('../escape');
17-
jest.dontMock('../ObjectState')
17+
jest.dontMock('../ObjectState');
1818
jest.dontMock('../parseDate');
1919
jest.dontMock('../ParseError');
2020
jest.dontMock('../ParseFile');
@@ -23,8 +23,10 @@ jest.dontMock('../ParseObject');
2323
jest.dontMock('../ParseOp');
2424
jest.dontMock('../ParsePromise');
2525
jest.dontMock('../RESTController');
26+
jest.dontMock('../SingleInstanceState');
2627
jest.dontMock('../TaskQueue');
2728
jest.dontMock('../unique');
29+
jest.dontMock('../UniqueInstanceState');
2830
jest.dontMock('../unsavedChildren');
2931
jest.dontMock('../ParseACL');
3032

@@ -59,20 +61,21 @@ mockQuery.prototype.find = function() {
5961
};
6062
jest.setMock('../ParseQuery', mockQuery);
6163

62-
var CoreManager = require('../CoreManager');
63-
var ObjectState = require('../ObjectState');
64-
var ParseACL = require('../ParseACL');
65-
var ParseError = require('../ParseError');
66-
var ParseFile = require('../ParseFile');
67-
var ParseGeoPoint = require('../ParseGeoPoint');
68-
var ParseObject = require('../ParseObject');
69-
var ParseOp = require('../ParseOp');
70-
var ParsePromise = require('../ParsePromise');
71-
var RESTController = require('../RESTController');
72-
var unsavedChildren = require('../unsavedChildren');
73-
74-
var asyncHelper = require('./test_helpers/asyncHelper');
75-
var mockXHR = require('./test_helpers/mockXHR');
64+
const CoreManager = require('../CoreManager');
65+
const ObjectState = require('../ObjectState');
66+
const ParseACL = require('../ParseACL');
67+
const ParseError = require('../ParseError');
68+
const ParseFile = require('../ParseFile');
69+
const ParseGeoPoint = require('../ParseGeoPoint');
70+
const ParseObject = require('../ParseObject');
71+
const ParseOp = require('../ParseOp');
72+
const ParsePromise = require('../ParsePromise');
73+
const RESTController = require('../RESTController');
74+
const SingleInstanceState = require('../SingleInstanceState');
75+
const unsavedChildren = require('../unsavedChildren');
76+
77+
const asyncHelper = require('./test_helpers/asyncHelper');
78+
const mockXHR = require('./test_helpers/mockXHR');
7679

7780
CoreManager.setRESTController(RESTController);
7881
CoreManager.setInstallationController({
@@ -84,15 +87,18 @@ CoreManager.set('APPLICATION_ID', 'A');
8487
CoreManager.set('JAVASCRIPT_KEY', 'B');
8588
CoreManager.set('MASTER_KEY', 'C');
8689
CoreManager.set('VERSION', 'V');
87-
ParseObject.enableSingleInstance();
8890

89-
var {
91+
const {
9092
SetOp,
9193
UnsetOp,
9294
IncrementOp
9395
} = require('../ParseOp');
9496

9597
describe('ParseObject', () => {
98+
beforeEach(() => {
99+
ParseObject.enableSingleInstance();
100+
});
101+
96102
it('is initially created with no Id', () => {
97103
var o = new ParseObject('Item');
98104
expect(o.id).toBe(undefined);
@@ -746,13 +752,13 @@ describe('ParseObject', () => {
746752
p.set('age', 34);
747753
expect(p._localId).toBeTruthy();
748754
expect(p.id).toBe(undefined);
749-
var oldState = ObjectState.getState('Person', p._localId);
755+
var oldState = SingleInstanceState.getState('Person', p._localId);
750756
p._handleSaveResponse({
751757
objectId: 'P4'
752758
});
753759
expect(p._localId).toBe(undefined);
754760
expect(p.id).toBe('P4');
755-
var newState = ObjectState.getState('Person', 'P4');
761+
var newState = SingleInstanceState.getState('Person', 'P4');
756762
expect(oldState.serverData).toBe(newState.serverData);
757763
expect(oldState.pendingOps).toBe(newState.pendingOps);
758764
expect(oldState.tasks).toBe(newState.tasks);
@@ -1595,7 +1601,7 @@ describe('ObjectController', () => {
15951601
// Objects in the second batch will not be prepared for save yet
15961602
// This means they can also be modified before the first batch returns
15971603
expect(
1598-
ObjectState.getState('Person', objects[20]._getId()).pendingOps.length
1604+
SingleInstanceState.getState('Person', objects[20]._getId()).pendingOps.length
15991605
).toBe(1);
16001606
objects[20].set('index', 0);
16011607

src/__tests__/ParseRole-test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ jest.dontMock('../ParseError');
1313
jest.dontMock('../ParseObject');
1414
jest.dontMock('../ParseOp');
1515
jest.dontMock('../ParseRole');
16+
jest.dontMock('../UniqueInstanceState');
1617

17-
var ParseACL = require('../ParseACL');
18-
var ParseError = require('../ParseError');
19-
var ParseObject = require('../ParseObject');
20-
var ParseRole = require('../ParseRole');
18+
const ParseACL = require('../ParseACL');
19+
const ParseError = require('../ParseError');
20+
const ParseObject = require('../ParseObject');
21+
const ParseRole = require('../ParseRole');
2122

2223
describe('ParseRole', () => {
24+
beforeEach(() => {
25+
ParseObject.disableSingleInstance();
26+
});
27+
2328
it('can create Roles', () => {
2429
var role = new ParseRole();
2530
expect(role.getName()).toBe(undefined);

src/__tests__/ParseSession-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jest.dontMock('../Storage');
2323
jest.dontMock('../StorageController.default');
2424
jest.dontMock('../TaskQueue');
2525
jest.dontMock('../unique');
26+
jest.dontMock('../UniqueInstanceState');
2627

2728
jest.dontMock('./test_helpers/asyncHelper');
2829
jest.dontMock('./test_helpers/mockXHR');

0 commit comments

Comments
 (0)