Skip to content

Commit 053eed3

Browse files
committed
Restructure state implementations as swappable controllers
1 parent fb1c397 commit 053eed3

13 files changed

+362
-262
lines changed

src/CoreManager.js

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ type ObjectController = {
4343
save: (object: ParseObject, options: RequestOptions) => ParsePromise;
4444
destroy: (object: ParseObject, options: RequestOptions) => ParsePromise;
4545
};
46+
type ObjectStateController = {
47+
getState: (obj: ParseObject) => ?State;
48+
initializeState: (obj: ParseObject, initial?: State) => State;
49+
removeState: (obj: ParseObject) => ?State;
50+
getServerData: (obj: ParseObject) => AttributeMap;
51+
setServerData: (obj: ParseObject, attributes: AttributeMap) => void;
52+
getPendingOps: (obj: ParseObject) => Array<OpsMap>;
53+
setPendingOp: (obj: ParseObject, attr: string, op: ?Op) => void;
54+
pushPendingState: (obj: ParseObject) => void;
55+
popPendingState: (obj: ParseObject) => OpsMap;
56+
mergeFirstPendingState: (obj: ParseObject) => void;
57+
getObjectCache: (obj: ParseObject) => ObjectCache;
58+
estimateAttribute: (obj: ParseObject, attr: string) => mixed;
59+
estimateAttributes: (obj: ParseObject) => AttributeMap;
60+
commitServerChanges: (obj: ParseObject, changes: AttributeMap) => void;
61+
enqueueTask: (obj: ParseObject, task: () => ParsePromise) => void;
62+
clearAllState: () => void;
63+
};
4664
type PushController = {
4765
send: (data: PushData, options: RequestOptions) => ParsePromise;
4866
};
@@ -179,17 +197,6 @@ module.exports = {
179197
return config['InstallationController'];
180198
},
181199

182-
setPushController(controller: PushController) {
183-
if (typeof controller.send !== 'function') {
184-
throw new Error('PushController must implement send()');
185-
}
186-
config['PushController'] = controller;
187-
},
188-
189-
getPushController(): PushController {
190-
return config['PushController'];
191-
},
192-
193200
setObjectController(controller: ObjectController) {
194201
if (typeof controller.save !== 'function') {
195202
throw new Error('ObjectController must implement save()');
@@ -207,6 +214,107 @@ module.exports = {
207214
return config['ObjectController'];
208215
},
209216

217+
setObjectStateController(controller: ObjectStateController) {
218+
if (typeof controller.getState !== 'function') {
219+
console.log(controller);
220+
throw new Error(
221+
'ObjectStateController must implement getState()'
222+
);
223+
}
224+
if (typeof controller.initializeState !== 'function') {
225+
throw new Error(
226+
'ObjectStateController must implement initializeState()'
227+
);
228+
}
229+
if (typeof controller.removeState !== 'function') {
230+
throw new Error(
231+
'ObjectStateController must implement removeState()'
232+
);
233+
}
234+
if (typeof controller.getServerData !== 'function') {
235+
throw new Error(
236+
'ObjectStateController must implement getServerData()'
237+
);
238+
}
239+
if (typeof controller.setServerData !== 'function') {
240+
throw new Error(
241+
'ObjectStateController must implement setServerData()'
242+
);
243+
}
244+
if (typeof controller.getPendingOps !== 'function') {
245+
throw new Error(
246+
'ObjectStateController must implement getPendingOps()'
247+
);
248+
}
249+
if (typeof controller.setPendingOp !== 'function') {
250+
throw new Error(
251+
'ObjectStateController must implement setPendingOp()'
252+
);
253+
}
254+
if (typeof controller.pushPendingState !== 'function') {
255+
throw new Error(
256+
'ObjectStateController must implement pushPendingState()'
257+
);
258+
}
259+
if (typeof controller.popPendingState !== 'function') {
260+
throw new Error(
261+
'ObjectStateController must implement popPendingState()'
262+
);
263+
}
264+
if (typeof controller.mergeFirstPendingState !== 'function') {
265+
throw new Error(
266+
'ObjectStateController must implement mergeFirstPendingState()'
267+
);
268+
}
269+
if (typeof controller.getObjectCache !== 'function') {
270+
throw new Error(
271+
'ObjectStateController must implement getObjectCache()'
272+
);
273+
}
274+
if (typeof controller.estimateAttribute !== 'function') {
275+
throw new Error(
276+
'ObjectStateController must implement estimateAttribute()'
277+
);
278+
}
279+
if (typeof controller.estimateAttributes !== 'function') {
280+
throw new Error(
281+
'ObjectStateController must implement estimateAttributes()'
282+
);
283+
}
284+
if (typeof controller.commitServerChanges !== 'function') {
285+
throw new Error(
286+
'ObjectStateController must implement commitServerChanges()'
287+
);
288+
}
289+
if (typeof controller.enqueueTask !== 'function') {
290+
throw new Error(
291+
'ObjectStateController must implement enqueueTask()'
292+
);
293+
}
294+
if (typeof controller.clearAllState !== 'function') {
295+
throw new Error(
296+
'ObjectStateController must implement clearAllState()'
297+
);
298+
}
299+
300+
config['ObjectStateController'] = controller;
301+
},
302+
303+
getObjectStateController(): ObjectStateController {
304+
return config['ObjectStateController'];
305+
},
306+
307+
setPushController(controller: PushController) {
308+
if (typeof controller.send !== 'function') {
309+
throw new Error('PushController must implement send()');
310+
}
311+
config['PushController'] = controller;
312+
},
313+
314+
getPushController(): PushController {
315+
return config['PushController'];
316+
},
317+
210318
setQueryController(controller: QueryController) {
211319
if (typeof controller.find !== 'function') {
212320
throw new Error('QueryController must implement find()');

src/ObjectState.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ export type State = {
3131
existed: boolean
3232
};
3333

34+
export function defaultState(): State {
35+
return {
36+
serverData: {},
37+
pendingOps: [{}],
38+
objectCache: {},
39+
tasks: new TaskQueue(),
40+
existed: false
41+
};
42+
}
43+
3444
export function setServerData(serverData: AttributeMap, attributes: AttributeMap) {
3545
for (let attr in attributes) {
3646
if (typeof attributes[attr] !== 'undefined') {

0 commit comments

Comments
 (0)