2323
2424'use strict' ;
2525
26- import type * as Delta from './Delta' ;
27- import type { Mutation } from './Mutation' ;
28-
2926var flatten = require ( './flatten' ) ;
3027var Id = require ( './Id' ) ;
3128var queryHash = require ( './QueryTools' ) . queryHash ;
3229
30+ import type * as Delta from './Delta' ;
31+ import type { Mutation } from './Mutation' ;
32+
33+ export type OrderingInfo = { [ key : string ] : any } ;
34+ export type IdWithOrderingInfo = {
35+ id : Id ;
36+ ordering: OrderingInfo ;
37+ } ;
38+ export type FlattenedObjectData = { [ attr : string ] : any } ;
39+ export type ObjectChangeDescriptor = {
40+ id : Id ;
41+ latest: any ; // TODO: this should really be FlattenedObjectData
42+ fields ?: Array < string > ;
43+ } ;
44+
3345/**
3446 * ObjectStore is a local cache for Parse Objects. It stores the last known
3547 * server version of each object it has seen, as well as a stack of pending
@@ -42,7 +54,7 @@ var queryHash = require('./QueryTools').queryHash;
4254// queries subscribed to the object.
4355var store : {
4456 [ id : Id ] : {
45- data : { [ attr : string ] : any } ;
57+ data : FlattenedObjectData ;
4658 queries: { [ hash : string ] : boolean }
4759 }
4860} = { } ;
@@ -66,7 +78,7 @@ var mutationCount = 0;
6678 * storeObject: takes a flattened object as the single argument, and places it
6779 * in the Store, indexed by its Id.
6880 */
69- function storeObject ( data : { [ attr : string ] : any } ) : Id {
81+ function storeObject ( data : FlattenedObjectData ) : Id {
7082 if ( ! ( data . id instanceof Id ) ) {
7183 throw new Error ( 'Cannot store an object without an Id' ) ;
7284 }
@@ -168,7 +180,11 @@ function destroyMutationStack(target: Id) {
168180 * Returns the latest object state and an array of keys that changed, or null
169181 * in the case of a Destroy
170182 */
171- function resolveMutation ( target : Id , payloadId : number , delta : Delta ) : { id: Id ; latest: any ; fields ?: Array < string > } {
183+ function resolveMutation (
184+ target : Id ,
185+ payloadId : number ,
186+ delta : Delta
187+ ) : ObjectChangeDescriptor {
172188 var stack = pendingMutations [ target ] ;
173189 var i ;
174190 for ( i = 0 ; i < stack . length ; i ++ ) {
@@ -219,8 +235,7 @@ function resolveMutation(target: Id, payloadId: number, delta: Delta): { id: Id;
219235 * Returns the latest object state and an array of keys that changed, or null
220236 * in the case of a Destroy
221237 */
222- function commitDelta ( delta : Delta ) :
223- { id: Id ; latest: any ; fields ?: Array < string > } {
238+ function commitDelta ( delta : Delta ) : ObjectChangeDescriptor {
224239 var id = delta . id ;
225240 if ( delta . destroy ) {
226241 if ( store [ id ] ) {
@@ -274,8 +289,10 @@ function commitDelta(delta: Delta):
274289 * Returns an array of object Ids, or an array of maps containing Ids and query-
275290 * specific ordering information.
276291 */
277- function storeQueryResults ( results : Array < ParseObject > | ParseObject , query : ParseQuery ) :
278- Array < Id | { id: Id ; ordering: any } > {
292+ function storeQueryResults (
293+ results : Array < ParseObject > | ParseObject ,
294+ query : ParseQuery
295+ ) : Array < Id | IdWithOrderingInfo > {
279296 var hash = queryHash ( query ) ;
280297 if ( ! Array . isArray ( results ) ) {
281298 results = [ results ] ;
@@ -336,7 +353,7 @@ function storeQueryResults(results: Array<ParseObject> | ParseObject, query: Par
336353 * Given a list of object Ids, fetches the latest data for each object
337354 * and returns the results as an array of shallow copies.
338355 */
339- function getDataForIds ( ids : Id | Array < Id > ) : Array < any > {
356+ function getDataForIds ( ids : Id | Array < Id > ) : Array < FlattenedObjectData > {
340357 if ( ! Array . isArray ( ids ) ) {
341358 ids = [ ids ] ;
342359 }
@@ -356,7 +373,7 @@ function getDataForIds(ids: Id | Array<Id>): Array<any> {
356373/**
357374 * Fetch objects from the store, converting pointers to objects where possible
358375 */
359- function deepFetch ( id : Id , seen ?: Array < string > ) {
376+ function deepFetch ( id : Id , seen ?: Array < string > ) : ? FlattenedObjectData {
360377 if ( ! store [ id ] ) {
361378 return null ;
362379 }
@@ -368,7 +385,7 @@ function deepFetch(id: Id, seen?: Array<string>) {
368385 var seenChildren = [ ] ;
369386 for ( var attr in source ) {
370387 var sourceVal = source [ attr ] ;
371- if ( sourceVal && typeof sourceVal === 'object' && sourceVal . __type === 'Pointer' ) {
388+ if ( sourceVal && typeof sourceVal === 'object' && sourceVal . __type === 'Pointer' ) {
372389 var childId = new Id ( sourceVal . className , sourceVal . objectId ) ;
373390 if ( seen . indexOf ( childId . toString ( ) ) < 0 && store [ childId ] ) {
374391 seenChildren = seenChildren . concat ( [ childId . toString ( ) ] ) ;
@@ -383,7 +400,7 @@ function deepFetch(id: Id, seen?: Array<string>) {
383400/**
384401 * Calculate the result of applying all Mutations to an object.
385402 */
386- function getLatest ( id : Id ) {
403+ function getLatest ( id : Id ) : ? FlattenedObjectData {
387404 if ( pendingMutations [ id ] && pendingMutations [ id ] . length > 0 ) {
388405 var base = { } ;
389406 var mutation ;
@@ -422,26 +439,22 @@ function getLatest(id: Id) {
422439 return store [ id ] ? deepFetch ( id ) : null ;
423440}
424441
425- var ObjectStore : { [ key : string ] : any } = {
426- storeObject : storeObject ,
427- removeObject : removeObject ,
428- addSubscriber : addSubscriber ,
429- removeSubscriber : removeSubscriber ,
430- fetchSubscribers : fetchSubscribers ,
431- stackMutation : stackMutation ,
432- destroyMutationStack : destroyMutationStack ,
433- resolveMutation : resolveMutation ,
434- commitDelta : commitDelta ,
435- storeQueryResults : storeQueryResults ,
436- getDataForIds : getDataForIds ,
437- deepFetch : deepFetch ,
438- getLatest : getLatest
439- } ;
442+ module . exports . storeObject = storeObject ;
443+ module . exports . removeObject = removeObject ;
444+ module . exports . addSubscriber = addSubscriber ;
445+ module . exports . removeSubscriber = removeSubscriber ;
446+ module . exports . fetchSubscribers = fetchSubscribers ;
447+ module . exports . stackMutation = stackMutation ;
448+ module . exports . destroyMutationStack = destroyMutationStack ;
449+ module . exports . resolveMutation = resolveMutation ;
450+ module . exports . commitDelta = commitDelta ;
451+ module . exports . storeQueryResults = storeQueryResults ;
452+ module . exports . getDataForIds = getDataForIds ;
453+ module . exports . deepFetch = deepFetch ;
454+ module . exports . getLatest = getLatest ;
440455
441456if ( typeof process !== 'undefined' && process . env . NODE_ENV === 'test' ) {
442457 // Expose the raw storage
443- ObjectStore . _rawStore = store ;
444- ObjectStore . _rawMutations = pendingMutations ;
458+ module . exports . _rawStore = store ;
459+ module . exports . _rawMutations = pendingMutations ;
445460}
446-
447- module . exports = ObjectStore ;
0 commit comments