Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit 96bf10e

Browse files
committed
More flow typing, fixing some undefined issues
1 parent 942e8db commit 96bf10e

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

src/ObjectStore.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ var queryHash = require('./QueryTools').queryHash;
4040

4141
// Stores the last known true state of each object, as well as the hashes of
4242
// queries subscribed to the object.
43-
var store: { [id: Id]:
44-
{ data: any; queries: { [hash: string]: boolean } } } = {};
43+
var store: {
44+
[id: Id]: {
45+
data: { [attr: string]: any };
46+
queries: { [hash: string]: boolean }
47+
}
48+
} = {};
4549

4650
// Stores the queries subscribed to local-only objects
47-
var localSubscribers = {};
51+
var localSubscribers: { [id: Id]: { [hash: string]: boolean } } = {};
4852

4953
// Stores a stack of pending mutations for each object
5054
var pendingMutations: { [id: Id]: Array<{
@@ -62,7 +66,7 @@ var mutationCount = 0;
6266
* storeObject: takes a flattened object as the single argument, and places it
6367
* in the Store, indexed by its Id.
6468
*/
65-
function storeObject(data): Id {
69+
function storeObject(data: { [attr: string]: any }): Id {
6670
if (!(data.id instanceof Id)) {
6771
throw new Error('Cannot store an object without an Id');
6872
}
@@ -270,7 +274,8 @@ function commitDelta(delta: Delta):
270274
* Returns an array of object Ids, or an array of maps containing Ids and query-
271275
* specific ordering information.
272276
*/
273-
function storeQueryResults(results, query): Array<Id | { id: Id; ordering: any }> {
277+
function storeQueryResults(results: Array<ParseObject> | ParseObject, query: ParseQuery):
278+
Array<Id | { id: Id; ordering: any }> {
274279
var hash = queryHash(query);
275280
if (!Array.isArray(results)) {
276281
results = [results];
@@ -362,7 +367,7 @@ function deepFetch(id: Id, seen?: Array<string>) {
362367
var obj = {};
363368
for (var attr in source) {
364369
var sourceVal = source[attr];
365-
if (sourceVal.__type === 'Pointer') {
370+
if (typeof sourceVal === 'object' && sourceVal.__type === 'Pointer') {
366371
var childId = new Id(sourceVal.className, sourceVal.objectId);
367372
if (seen.indexOf(childId.toString()) < 0 && store[childId]) {
368373
seen = seen.concat([childId.toString()]);

src/QueryTools.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
* IN THE SOFTWARE.
2020
*
21+
* @flow
2122
*/
2223

2324
'use strict';
@@ -50,12 +51,12 @@ function flattenOrQueries(where) {
5051
/**
5152
* Deterministically turns an object into a string. Disregards ordering
5253
*/
53-
function stringify(object) {
54+
function stringify(object): string {
5455
if (typeof object !== 'object') {
5556
if (typeof object === 'string') {
5657
return '"' + object.replace(/\|/g, '%|') + '"';
5758
}
58-
return object;
59+
return object + '';
5960
}
6061
if (Array.isArray(object)) {
6162
var copy = object.map(stringify);
@@ -75,12 +76,12 @@ function stringify(object) {
7576
* Generate a hash from a query, with unique fields for columns, values, order,
7677
* skip, and limit.
7778
*/
78-
function queryHash(query) {
79+
function queryHash(query: ParseQuery): string {
7980
if (!(query instanceof Parse.Query)) {
8081
throw new TypeError('Only a Parse Query can be hashed');
8182
}
8283
var where = flattenOrQueries(query._where || {});
83-
var columns;
84+
var columns = [];
8485
var values = [];
8586
var i;
8687
if (Array.isArray(where)) {
@@ -120,7 +121,8 @@ function queryHash(query) {
120121
/**
121122
* Extracts the className and keys from a query hash
122123
*/
123-
function keysFromHash(hash) {
124+
function keysFromHash(hash: string):
125+
{ className: string; keys: Array<string> } {
124126
var classNameSplit = hash.indexOf(':');
125127
var className = hash.substring(0, classNameSplit);
126128

@@ -142,7 +144,9 @@ function keysFromHash(hash) {
142144
* Since we find queries that match objects, rather than objects that match
143145
* queries, we can avoid building a full-blown query tool.
144146
*/
145-
function matchesQuery(object, query) {
147+
function matchesQuery(
148+
object: { [key: string]: any },
149+
query: ParseQuery | { [key: string]: any }): boolean {
146150
if (query instanceof Parse.Query) {
147151
var className =
148152
(object.id instanceof Id) ? object.id.className : object.className;

src/SubscriptionManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function getSubscription(hash: string): ?Subscription {
9898
* Indexes a query by the fields it depends upon. This lets us quickly find a
9999
* query that might match an object that has just modified a specific field.
100100
*/
101-
function indexQuery(query, hash) {
101+
function indexQuery(query: ParseQuery, hash: string) {
102102
var fields = keysFromHash(hash).keys;
103103
if (fields.length < 1) {
104104
fields = ['']; // Empty string is the key for no WHERE conditions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare class ParseObject {
2+
className: string;
3+
objectId: string;
4+
5+
get: (attr: string) => any;
6+
}

0 commit comments

Comments
 (0)