From 6f4737190b27058629fe43551fe330dc7e27433d Mon Sep 17 00:00:00 2001 From: Ibby Date: Thu, 27 Oct 2016 07:30:06 -0400 Subject: [PATCH 1/2] fix(sqlite): check if plugin exists before opening database --- src/plugins/sqlite.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/plugins/sqlite.ts b/src/plugins/sqlite.ts index 2b123aa27..ee9e25cae 100644 --- a/src/plugins/sqlite.ts +++ b/src/plugins/sqlite.ts @@ -1,4 +1,4 @@ -import { Cordova, CordovaInstance, Plugin } from './plugin'; +import {Cordova, CordovaInstance, Plugin, pluginWarn} from './plugin'; declare var sqlitePlugin; @@ -73,13 +73,20 @@ export class SQLite { */ openDatabase(config: any): Promise { return new Promise((resolve, reject) => { - sqlitePlugin.openDatabase(config, db => { - this._objectInstance = db; - resolve(db); - }, error => { - console.warn(error); - reject(error); - }); + if (typeof sqlitePlugin !== 'undefined') { + sqlitePlugin.openDatabase(config, db => { + this._objectInstance = db; + resolve(db); + }, error => { + console.warn(error); + reject(error); + }); + } else { + pluginWarn({ + name: 'SQLite', + plugin: 'cordova-sqlite-storage' + }); + } }); } From a72cd59b99de68dfccb007822d2e570237215c89 Mon Sep 17 00:00:00 2001 From: Ibby Date: Thu, 27 Oct 2016 07:59:33 -0400 Subject: [PATCH 2/2] fix(sqlite): fix callback issue with transaction method closes #732 --- src/plugins/plugin.ts | 33 +++++++++++++++++++++++---------- test/plugin.spec.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index ec11b3cd1..69ee6cc4f 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -65,20 +65,33 @@ function setIndex(args: any[], opts: any = {}, resolve?: Function, reject?: Func obj[opts.errorName] = reject; args.push(obj); } else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') { - // If we've specified a success/error index + const setSuccessIndex = () => { + // If we've specified a success/error index + if (opts.successIndex > args.length) { + args[opts.successIndex] = resolve; + } else { + args.splice(opts.successIndex, 0, resolve); + } + }; - if (opts.successIndex > args.length) { - args[opts.successIndex] = resolve; + const setErrorIndex = () => { + // We don't want that the reject cb gets spliced into the position of an optional argument that has not been defined and thus causing non expected behaviour. + if (opts.errorIndex > args.length) { + args[opts.errorIndex] = reject; // insert the reject fn at the correct specific index + } else { + args.splice(opts.errorIndex, 0, reject); // otherwise just splice it into the array + } + }; + + if(opts.successIndex > opts.errorIndex) { + setErrorIndex(); + setSuccessIndex(); } else { - args.splice(opts.successIndex, 0, resolve); + setSuccessIndex(); + setErrorIndex(); } - // We don't want that the reject cb gets spliced into the position of an optional argument that has not been defined and thus causing non expected behaviour. - if (opts.errorIndex > args.length) { - args[opts.errorIndex] = reject; // insert the reject fn at the correct specific index - } else { - args.splice(opts.errorIndex, 0, reject); // otherwise just splice it into the array - } + } else { // Otherwise, let's tack them on to the end of the argument list // which is 90% of cases diff --git a/test/plugin.spec.ts b/test/plugin.spec.ts index ecdb07a74..b808936c0 100644 --- a/test/plugin.spec.ts +++ b/test/plugin.spec.ts @@ -138,4 +138,36 @@ describe('plugin', () => { }); + it('reverse callback at the end of the function', done => { + + window.plugins.test.reverseEndCallback = (args, error, success) => { + success('Success'); + }; + + @Plugin(testPluginMeta) + class Test { + + @Cordova({ + successIndex: 2, + errorIndex: 1 + }) + static reverseEndCallback(args: any): Promise { return; } + + } + + const spy = spyOn(window.plugins.test, 'reverseEndCallback').and.callThrough(); + const cb = (result) => { + expect(result).toEqual('Success'); + done(); + }; + + Test.reverseEndCallback('foo').then(cb, cb); + + expect(spy.calls.mostRecent().args[0]).toEqual('foo'); + expect(spy.calls.mostRecent().args[1]).toBeDefined(); + expect(spy.calls.mostRecent().args[2]).toBeDefined(); + expect(spy.calls.mostRecent().args[3]).toBeUndefined(); + + }); + });