mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-02-22 01:19:36 +08:00
Merge branch 'master' of github.com:driftyco/ionic-native
This commit is contained in:
commit
634843e1b2
@ -65,20 +65,33 @@ function setIndex(args: any[], opts: any = {}, resolve?: Function, reject?: Func
|
|||||||
obj[opts.errorName] = reject;
|
obj[opts.errorName] = reject;
|
||||||
args.push(obj);
|
args.push(obj);
|
||||||
} else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
} 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) {
|
const setErrorIndex = () => {
|
||||||
args[opts.successIndex] = resolve;
|
// 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 {
|
} 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 {
|
} else {
|
||||||
// Otherwise, let's tack them on to the end of the argument list
|
// Otherwise, let's tack them on to the end of the argument list
|
||||||
// which is 90% of cases
|
// which is 90% of cases
|
||||||
|
@ -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<any> { 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();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user