Skip to content

Commit b922b2b

Browse files
committed
Makes schemaCache clearning promise-based
1 parent ba0e529 commit b922b2b

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/Controllers/SchemaController.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,20 @@ export default class SchemaController {
323323
}
324324

325325
reloadData(options = {clearCache: false}) {
326+
let promise = Promise.resolve();
326327
if (options.clearCache) {
327-
this._cache.clear();
328+
promise = promise.then(() => {
329+
return this._cache.clear();
330+
});
328331
}
329332
if (this.reloadDataPromise && !options.clearCache) {
330333
return this.reloadDataPromise;
331334
}
332335
this.data = {};
333336
this.perms = {};
334-
this.reloadDataPromise = this.getAllClasses(options)
337+
this.reloadDataPromise = promise.then(() => {
338+
return this.getAllClasses(options);
339+
})
335340
.then(allSchemas => {
336341
allSchemas.forEach(schema => {
337342
this.data[schema.className] = injectDefaultSchema(schema).fields;
@@ -355,10 +360,13 @@ export default class SchemaController {
355360
}
356361

357362
getAllClasses(options = {clearCache: false}) {
363+
let promise = Promise.resolve();
358364
if (options.clearCache) {
359-
this._cache.clear();
365+
promise = this._cache.clear();
360366
}
361-
return this._cache.getAllClasses().then((allClasses) => {
367+
return promise.then(() => {
368+
return this._cache.getAllClasses()
369+
}).then((allClasses) => {
362370
if (allClasses && allClasses.length && !options.clearCache) {
363371
return Promise.resolve(allClasses);
364372
}
@@ -373,22 +381,25 @@ export default class SchemaController {
373381
}
374382

375383
getOneSchema(className, allowVolatileClasses = false, options = {clearCache: false}) {
384+
let promise = Promise.resolve();
376385
if (options.clearCache) {
377-
this._cache.clear();
378-
}
379-
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
380-
return Promise.resolve(this.data[className]);
386+
promise = this._cache.clear();
381387
}
382-
return this._cache.getOneSchema(className).then((cached) => {
383-
if (cached && !options.clearCache) {
384-
return Promise.resolve(cached);
388+
return promise.then(() => {
389+
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
390+
return Promise.resolve(this.data[className]);
385391
}
386-
return this._dbAdapter.getClass(className)
387-
.then(injectDefaultSchema)
388-
.then((result) => {
389-
return this._cache.setOneSchema(className, result).then(() => {
390-
return result;
391-
})
392+
return this._cache.getOneSchema(className).then((cached) => {
393+
if (cached && !options.clearCache) {
394+
return Promise.resolve(cached);
395+
}
396+
return this._dbAdapter.getClass(className)
397+
.then(injectDefaultSchema)
398+
.then((result) => {
399+
return this._cache.setOneSchema(className, result).then(() => {
400+
return result;
401+
})
402+
});
392403
});
393404
});
394405
}
@@ -409,8 +420,9 @@ export default class SchemaController {
409420
return this._dbAdapter.createClass(className, convertSchemaToAdapterSchema({ fields, classLevelPermissions, className }))
410421
.then(convertAdapterSchemaToParseSchema)
411422
.then((res) => {
412-
this._cache.clear();
413-
return res;
423+
return this._cache.clear().then(() => {
424+
return Promise.resolve(res);
425+
});
414426
})
415427
.catch(error => {
416428
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
@@ -508,6 +520,7 @@ export default class SchemaController {
508520
}
509521
})
510522
.catch(error => {
523+
console.error(error);
511524
// The schema still doesn't validate. Give up
512525
throw new Parse.Error(Parse.Error.INVALID_JSON, 'schema class name does not revalidate');
513526
});

0 commit comments

Comments
 (0)