[js][database] fixed an issue where thenable ref promise would sometimes cause other promises to hang - no idea why ;p

This commit is contained in:
Salakar 2017-08-14 18:42:39 +01:00
parent dedfa48aa0
commit c6a26606d1
1 changed files with 21 additions and 9 deletions

View File

@ -484,7 +484,10 @@ export default class Reference extends ReferenceBase {
*/
get then() {
if (this._promise && this._promise.then) {
return this._promise.then.bind(this._promise);
return this._promise.then.bind(this._promise)(() => {
this._promise = null;
return this;
});
}
return undefined;
@ -496,7 +499,10 @@ export default class Reference extends ReferenceBase {
*/
get catch() {
if (this._promise && this._promise.catch) {
return this._promise.catch.bind(this._promise);
return this._promise.catch.bind(this._promise)((exception) => {
this._promise = null;
return Promise.reject(exception);
});
}
return undefined;
@ -570,12 +576,13 @@ export default class Reference extends ReferenceBase {
*
* @param eventType
* @param callback
* @param cancelCallback
* @param cancelCallbackOrContext
* @param context
* @return {*}
*/
// todo:context shouldn't be needed - confirm
// todo refId should no longer be required - update native to work without it then remove from js internals
on(eventType: string, callback: () => any, cancelCallback: () => any): Function {
on(eventType: string, callback: () => any, cancelCallbackOrContext?: () => any, context?: Object): Function {
if (!eventType) {
throw new Error('Query.on failed: Function called with 0 arguments. Expects at least 2.');
}
@ -592,16 +599,21 @@ export default class Reference extends ReferenceBase {
throw new Error('Query.on failed: Second argument must be a valid function.');
}
if (cancelCallback && !isFunction(cancelCallback)) {
throw new Error('Query.on failed: Function called with 3 arguments, but third optional argument `cancelCallback` was not a function.');
if (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext) && !isObject(context) && !isObject(cancelCallbackOrContext)) {
throw new Error('Query.on failed: Function called with 3 arguments, but third optional argument `cancelCallbackOrContext` was not a function.');
}
if (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext) && context) {
throw new Error('Query.on failed: Function called with 4 arguments, but third optional argument `cancelCallbackOrContext` was not a function.');
}
const eventQueryKey = `${this.makeQueryKey()}$${eventType}`;
const _context = (cancelCallbackOrContext && !isFunction(cancelCallbackOrContext)) ? cancelCallbackOrContext : context;
INTERNALS.SharedEventEmitter.addListener(eventQueryKey, callback);
INTERNALS.SharedEventEmitter.addListener(eventQueryKey, _context ? callback.bind(_context) : callback);
if (isFunction(cancelCallback)) {
INTERNALS.SharedEventEmitter.once(`${this.makeQueryKey()}:cancelled`, cancelCallback);
if (isFunction(cancelCallbackOrContext)) {
INTERNALS.SharedEventEmitter.once(`${eventQueryKey}:cancelled`, _context ? cancelCallbackOrContext.bind(_context) : cancelCallbackOrContext);
}
// initialise the native listener if not already listening