This commit is contained in:
Max Lynch 2015-11-30 13:27:25 -06:00
parent 2d70c6a3d4
commit 118da948cb
21 changed files with 293 additions and 36 deletions

View File

@ -69,6 +69,15 @@ export class Plugin {
});
} else {
console.log('Response: ', v);
if(v.subscribe) {
console.log('Observable response, subscribing...');
v.subscribe((val) => {
console.log('Observable val', val);
}, (err) => {
console.log('ERROR: Observable', err);
});
}
}
}

View File

@ -8,10 +8,10 @@
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<!--
Looking for a style sheet?
<!--
Looking for a style sheet?
Webpack loads styles as dependencies in your javascript
See www/app/app.js as an example
See www/app/app.js as an example
-->
</head>

4
dist/index.js vendored
View File

@ -10,8 +10,10 @@ __export(require('./plugins/device'));
__export(require('./plugins/geolocation'));
__export(require('./plugins/statusbar'));
__export(require('./plugins/toast'));
var before = +new Date;
var didFireReady = false;
window.addEventListener('deviceready', function () {
document.addEventListener('deviceready', function () {
console.log('DEVICE READY FIRED AFTER', (+new Date - before), 'ms');
didFireReady = true;
});
setTimeout(function () {

2
dist/index.js.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,IAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC,iBAAc,uBAAuB,CAAC,EAAA;AACtC,iBAAc,eAAe,CAAC,EAAA;AAC9B,iBAAc,kBAAkB,CAAC,EAAA;AACjC,iBAAc,oBAAoB,CAAC,EAAA;AACnC,iBAAc,kBAAkB,CAAC,EAAA;AACjC,iBAAc,uBAAuB,CAAC,EAAA;AACtC,iBAAc,qBAAqB,CAAC,EAAA;AACpC,iBAAc,iBAAiB,CAAC,EAAA;AAShC,IAAI,YAAY,GAAG,KAAK,CAAC;AACzB,MAAM,CAAC,gBAAgB,CAAC,aAAa,EAAE;IACrC,YAAY,GAAG,IAAI,CAAC;AACtB,CAAC,CAAC,CAAA;AAEF,UAAU,CAAC;IACT,EAAE,CAAA,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QACnC,OAAO,CAAC,IAAI,CAAC,0CAA0C,GAAG,oBAAoB,GAAG,0HAA0H,CAAC,CAAC;IAC/M,CAAC;AACH,CAAC,EAAE,oBAAoB,CAAC,CAAC"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,IAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC,iBAAc,uBAAuB,CAAC,EAAA;AACtC,iBAAc,eAAe,CAAC,EAAA;AAC9B,iBAAc,kBAAkB,CAAC,EAAA;AACjC,iBAAc,oBAAoB,CAAC,EAAA;AACnC,iBAAc,kBAAkB,CAAC,EAAA;AACjC,iBAAc,uBAAuB,CAAC,EAAA;AACtC,iBAAc,qBAAqB,CAAC,EAAA;AACpC,iBAAc,iBAAiB,CAAC,EAAA;AAShC,IAAI,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC;AAEvB,IAAI,YAAY,GAAG,KAAK,CAAC;AACzB,QAAQ,CAAC,gBAAgB,CAAC,aAAa,EAAE;IACvC,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,CAAC,CAAC,IAAI,IAAI,GAAG,MAAM,CAAC,EAAE,IAAI,CAAC,CAAA;IACnE,YAAY,GAAG,IAAI,CAAC;AACtB,CAAC,CAAC,CAAA;AAEF,UAAU,CAAC;IACT,EAAE,CAAA,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QACnC,OAAO,CAAC,IAAI,CAAC,0CAA0C,GAAG,oBAAoB,GAAG,0HAA0H,CAAC,CAAC;IAC/M,CAAC;AACH,CAAC,EAAE,oBAAoB,CAAC,CAAC"}

View File

@ -1,4 +1,4 @@
export declare class Device {
export declare class Geolocation {
static getCurrentPosition(options: any): void;
static watchPosition(options: any): void;
}

View File

@ -7,32 +7,33 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
}
};
var plugin_1 = require('./plugin');
var Device = (function () {
function Device() {
var Geolocation = (function () {
function Geolocation() {
}
Device.getCurrentPosition = function (options) { };
Geolocation.getCurrentPosition = function (options) { };
;
Device.watchPosition = function (options) { };
Geolocation.watchPosition = function (options) { };
;
Object.defineProperty(Device, "getCurrentPosition",
Object.defineProperty(Geolocation, "getCurrentPosition",
__decorate([
plugin_1.Cordova()
], Device, "getCurrentPosition", Object.getOwnPropertyDescriptor(Device, "getCurrentPosition")));
Object.defineProperty(Device, "watchPosition",
], Geolocation, "getCurrentPosition", Object.getOwnPropertyDescriptor(Geolocation, "getCurrentPosition")));
Object.defineProperty(Geolocation, "watchPosition",
__decorate([
plugin_1.Cordova({
callbackOrder: 'reverse',
observable: true,
clearFunction: 'clearWatch()'
})
], Device, "watchPosition", Object.getOwnPropertyDescriptor(Device, "watchPosition")));
Device = __decorate([
], Geolocation, "watchPosition", Object.getOwnPropertyDescriptor(Geolocation, "watchPosition")));
Geolocation = __decorate([
plugin_1.Plugin({
name: 'Geolocation',
plugin: 'cordova-plugin-geolocation',
pluginRef: 'navigator.geolocation'
})
], Device);
return Device;
], Geolocation);
return Geolocation;
})();
exports.Device = Device;
exports.Geolocation = Geolocation;
//# sourceMappingURL=geolocation.js.map

View File

@ -1 +1 @@
{"version":3,"file":"geolocation.js","sourceRoot":"","sources":["../../src/plugins/geolocation.ts"],"names":["Device","Device.constructor","Device.getCurrentPosition","Device.watchPosition"],"mappings":";;;;;;;;AAAA,uBAA8B,UAAU,CAAC,CAAA;AAMzC;IAAAA;IAeAC,CAACA;IARQD,yBAAkBA,GADzBA,UAC0BA,OAAWA,IAAEE,CAACA;;IAOjCF,oBAAaA,GAJpBA,UAIqBA,OAAWA,IAAEG,CAACA;;IARnCH,sBACOA,4BAAkBA;;YADxBA,gBAAOA,EAAEA;WACHA,4BAAkBA,kCAAlBA,4BAAkBA,IAAeA;IAGxCA,sBAIOA,uBAAaA;;YAJnBA,gBAAOA,CAACA;gBACPA,UAAUA,EAAEA,IAAIA;gBAChBA,aAAaA,EAAEA,cAAcA;aAC9BA,CAACA;WACKA,uBAAaA,kCAAbA,uBAAaA,IAAeA;IAdrCA;QAACA,eAAMA,CAACA;YACNA,IAAIA,EAAEA,aAAaA;YACnBA,MAAMA,EAAEA,4BAA4BA;YACpCA,SAASA,EAAEA,uBAAuBA;SACnCA,CAACA;eAWDA;IAADA,aAACA;AAADA,CAACA,AAfD,IAeC;AAVY,cAAM,SAUlB,CAAA"}
{"version":3,"file":"geolocation.js","sourceRoot":"","sources":["../../src/plugins/geolocation.ts"],"names":["Geolocation","Geolocation.constructor","Geolocation.getCurrentPosition","Geolocation.watchPosition"],"mappings":";;;;;;;;AAAA,uBAA8B,UAAU,CAAC,CAAA;AAMzC;IAAAA;IAgBAC,CAACA;IATQD,8BAAkBA,GADzBA,UAC0BA,OAAWA,IAAEE,CAACA;;IAQjCF,yBAAaA,GALpBA,UAKqBA,OAAWA,IAAEG,CAACA;;IATnCH,sBACOA,iCAAkBA;;YADxBA,gBAAOA,EAAEA;WACHA,iCAAkBA,kCAAlBA,iCAAkBA,IAAeA;IAGxCA,sBAKOA,4BAAaA;;YALnBA,gBAAOA,CAACA;gBACPA,aAAaA,EAAEA,SAASA;gBACxBA,UAAUA,EAAEA,IAAIA;gBAChBA,aAAaA,EAAEA,cAAcA;aAC9BA,CAACA;WACKA,4BAAaA,kCAAbA,4BAAaA,IAAeA;IAfrCA;QAACA,eAAMA,CAACA;YACNA,IAAIA,EAAEA,aAAaA;YACnBA,MAAMA,EAAEA,4BAA4BA;YACpCA,SAASA,EAAEA,uBAAuBA;SACnCA,CAACA;oBAYDA;IAADA,kBAACA;AAADA,CAACA,AAhBD,IAgBC;AAXY,mBAAW,cAWvB,CAAA"}

View File

@ -28,17 +28,14 @@ function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
if (opts === void 0) { opts = {}; }
if (!window.cordova) {
exports.cordovaWarn(pluginObj.name, methodName);
reject({
error: 'cordova_not_available'
});
return;
}
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
// If the plugin method expects myMethod(success, err, options)
if (opts.callbackOrder == 'reverse') {
args[0] = resolve;
args[1] = reject;
// Get those arguments in the order [reject, resolve, ...restOfArgs]
args.unshift(reject);
args.unshift(resolve);
}
else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
// If we've specified a success/error index
@ -60,7 +57,8 @@ function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
return;
}
console.log('Cordova calling', pluginObj.name, methodName, args);
return util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
// TODO: Illegal invocation needs window context
return util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
}
function wrapPromise(pluginObj, methodName, args, opts) {
if (opts === void 0) { opts = {}; }
@ -70,8 +68,12 @@ function wrapPromise(pluginObj, methodName, args, opts) {
}
function wrapObservable(pluginObj, methodName, args, opts) {
if (opts === void 0) { opts = {}; }
return Rx_1.Observable.create(function (observer) {
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.onNext, observer.onError);
return new Rx_1.Observable(function (observer) {
console.log('Calling inside observable', observer);
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function (d) {
console.log('WATCH RESP', d);
observer.next(d);
}, observer.error);
return function () {
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
};

File diff suppressed because one or more lines are too long

8
dist/src/index.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
export * from './plugins/actionsheet';
export * from './plugins/ble';
export * from './plugins/camera';
export * from './plugins/contacts';
export * from './plugins/device';
export * from './plugins/geolocation';
export * from './plugins/statusbar';
export * from './plugins/toast';

24
dist/src/index.js vendored Normal file
View File

@ -0,0 +1,24 @@
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
var DEVICE_READY_TIMEOUT = 2000;
__export(require('./plugins/actionsheet'));
__export(require('./plugins/ble'));
__export(require('./plugins/camera'));
__export(require('./plugins/contacts'));
__export(require('./plugins/device'));
__export(require('./plugins/geolocation'));
__export(require('./plugins/statusbar'));
__export(require('./plugins/toast'));
var before = +new Date;
var didFireReady = false;
document.addEventListener('deviceready', function () {
console.log('DEVICE READY FIRED AFTER', (+new Date - before), 'ms');
didFireReady = true;
});
setTimeout(function () {
if (!didFireReady && window.cordova) {
console.warn('Native: deviceready did not fire within ' + DEVICE_READY_TIMEOUT + 'ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.');
}
}, DEVICE_READY_TIMEOUT);
//# sourceMappingURL=index.js.map

1
dist/src/index.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,IAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC,iBAAc,uBAAuB,CAAC,EAAA;AACtC,iBAAc,eAAe,CAAC,EAAA;AAC9B,iBAAc,kBAAkB,CAAC,EAAA;AACjC,iBAAc,oBAAoB,CAAC,EAAA;AACnC,iBAAc,kBAAkB,CAAC,EAAA;AACjC,iBAAc,uBAAuB,CAAC,EAAA;AACtC,iBAAc,qBAAqB,CAAC,EAAA;AACpC,iBAAc,iBAAiB,CAAC,EAAA;AAShC,IAAI,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC;AAEvB,IAAI,YAAY,GAAG,KAAK,CAAC;AACzB,QAAQ,CAAC,gBAAgB,CAAC,aAAa,EAAE;IACvC,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,CAAC,CAAC,IAAI,IAAI,GAAG,MAAM,CAAC,EAAE,IAAI,CAAC,CAAA;IACnE,YAAY,GAAG,IAAI,CAAC;AACtB,CAAC,CAAC,CAAA;AAEF,UAAU,CAAC;IACT,EAAE,CAAA,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QACnC,OAAO,CAAC,IAAI,CAAC,0CAA0C,GAAG,oBAAoB,GAAG,0HAA0H,CAAC,CAAC;IAC/M,CAAC;AACH,CAAC,EAAE,oBAAoB,CAAC,CAAC"}

4
dist/src/plugins/geolocation.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
export declare class Geolocation {
static getCurrentPosition(options: any): void;
static watchPosition(options: any): void;
}

45
dist/src/plugins/geolocation.js vendored Normal file
View File

@ -0,0 +1,45 @@
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var plugin_1 = require('./plugin');
var Geolocation = (function () {
function Geolocation() {
}
Geolocation.getCurrentPosition = function (options) { };
;
Geolocation.watchPosition = function (options) { };
;
__decorate([
plugin_1.Cordova(),
__metadata('design:type', Function),
__metadata('design:paramtypes', [Object]),
__metadata('design:returntype', void 0)
], Geolocation, "getCurrentPosition", null);
__decorate([
plugin_1.Cordova({
callbackOrder: 'reverse',
observable: true,
clearFunction: 'clearWatch()'
}),
__metadata('design:type', Function),
__metadata('design:paramtypes', [Object]),
__metadata('design:returntype', void 0)
], Geolocation, "watchPosition", null);
Geolocation = __decorate([
plugin_1.Plugin({
name: 'Geolocation',
plugin: 'cordova-plugin-geolocation',
pluginRef: 'navigator.geolocation'
}),
__metadata('design:paramtypes', [])
], Geolocation);
return Geolocation;
})();
exports.Geolocation = Geolocation;
//# sourceMappingURL=geolocation.js.map

1
dist/src/plugins/geolocation.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"geolocation.js","sourceRoot":"","sources":["../../../src/plugins/geolocation.ts"],"names":["Geolocation","Geolocation.constructor","Geolocation.getCurrentPosition","Geolocation.watchPosition"],"mappings":";;;;;;;;;AAAA,uBAA8B,UAAU,CAAC,CAAA;AAMzC;IAAAA;IAgBAC,CAACA;IATQD,8BAAkBA,GADzBA,UAC0BA,OAAWA,IAAEE,CAACA;;IAQjCF,yBAAaA,GALpBA,UAKqBA,OAAWA,IAAEG,CAACA;;IATnCH;QAACA,gBAAOA,EAAEA;;;;OACHA,iCAAkBA,QAAeA;IAGxCA;QAACA,gBAAOA,CAACA;YACPA,aAAaA,EAAEA,SAASA;YACxBA,UAAUA,EAAEA,IAAIA;YAChBA,aAAaA,EAAEA,cAAcA;SAC9BA,CAACA;;;;OACKA,4BAAaA,QAAeA;IAfrCA;QAACA,eAAMA,CAACA;YACNA,IAAIA,EAAEA,aAAaA;YACnBA,MAAMA,EAAEA,4BAA4BA;YACpCA,SAASA,EAAEA,uBAAuBA;SACnCA,CAACA;;oBAYDA;IAADA,kBAACA;AAADA,CAACA,AAhBD,IAgBC;AAXY,mBAAW,cAWvB,CAAA"}

10
dist/src/plugins/plugin.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
export declare const getPlugin: (pluginRef: string) => any;
export declare const isInstalled: (pluginRef: string) => boolean;
export declare const pluginWarn: (pluginName: string, method: string, plugin: string) => void;
export declare const cordovaWarn: (pluginName: string, method: string) => void;
export declare const wrap: (pluginObj: any, methodName: string, opts?: any) => (...args: any[]) => any;
export declare function Plugin(config: any): (cls: any) => any;
export declare function Cordova(opts?: any): (target: Object, methodName: string, descriptor: TypedPropertyDescriptor<any>) => {
value: (...args: any[]) => any;
};
export declare function RequiresPlugin(target: Function, key: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any>;

137
dist/src/plugins/plugin.js vendored Normal file
View File

@ -0,0 +1,137 @@
var util_1 = require('../util');
var Rx_1 = require('@reactivex/rxjs/dist/cjs/Rx');
exports.getPlugin = function (pluginRef) {
return util_1.get(window, pluginRef);
};
exports.isInstalled = function (pluginRef) {
return !!exports.getPlugin(pluginRef);
};
exports.pluginWarn = function (pluginName, method, plugin) {
if (method) {
console.warn('Native: tried calling ' + pluginName + '.' + method +
', but the ' + pluginName + ' plugin is not installed. Install the ' +
plugin + ' plugin');
}
else {
console.warn('Native: tried accessing the ' + pluginName + ' plugin but it\'s not installed. Install the ' + plugin + ' plugin');
}
};
exports.cordovaWarn = function (pluginName, method) {
if (method) {
console.warn('Native: tried calling ' + pluginName + '.' + method + ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
}
else {
console.warn('Native: tried accessing the ' + pluginName + ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
}
};
function callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject) {
if (opts === void 0) { opts = {}; }
if (!window.cordova) {
exports.cordovaWarn(pluginObj.name, methodName);
}
if (opts.callbackOrder == 'reverse') {
args.unshift(reject);
args.unshift(resolve);
}
else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
args.splice(opts.successIndex, 0, resolve);
args.splice(opts.errorIndex, 0, reject);
}
else {
args.push(resolve);
args.push(reject);
}
var pluginInstance = exports.getPlugin(pluginObj.pluginRef);
if (!pluginInstance) {
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
reject({
error: 'plugin_not_installed'
});
return;
}
console.log('Cordova calling', pluginObj.name, methodName, args);
return util_1.get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
}
function wrapPromise(pluginObj, methodName, args, opts) {
if (opts === void 0) { opts = {}; }
return new Promise(function (resolve, reject) {
callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
});
}
function wrapObservable(pluginObj, methodName, args, opts) {
if (opts === void 0) { opts = {}; }
return new Rx_1.Observable(function (observer) {
console.log('Calling inside observable', observer);
var pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, function (d) {
console.log('WATCH RESP', d);
observer.next(d);
}, observer.error);
return function () {
return util_1.get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);
};
});
}
exports.wrap = function (pluginObj, methodName, opts) {
if (opts === void 0) { opts = {}; }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
if (opts.observable) {
console.log("Wrapping observable");
return wrapObservable(pluginObj, methodName, args, opts);
}
else {
return wrapPromise(pluginObj, methodName, args, opts);
}
};
};
function Plugin(config) {
return function (cls) {
for (var k in config) {
cls[k] = config[k];
}
return cls;
};
}
exports.Plugin = Plugin;
function Cordova(opts) {
if (opts === void 0) { opts = {}; }
return function (target, methodName, descriptor) {
var originalMethod = descriptor.value;
return {
value: function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return exports.wrap(this, methodName, opts).apply(this, args);
}
};
};
}
exports.Cordova = Cordova;
function RequiresPlugin(target, key, descriptor) {
var originalMethod = descriptor.value;
descriptor.value = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
console.log('Calling', this);
if (!window.cordova) {
exports.cordovaWarn(this.name, null);
return;
}
var pluginInstance = exports.getPlugin(this.pluginRef);
if (!pluginInstance) {
exports.pluginWarn(this.name, null, this.name);
return;
}
originalMethod.apply(this, args);
};
return descriptor;
}
exports.RequiresPlugin = RequiresPlugin;
//# sourceMappingURL=plugin.js.map

1
dist/src/plugins/plugin.js.map vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -16,8 +16,11 @@ export * from './plugins/toast';
// an inconsistent plugin is often the culprit.
declare var window;
let before = +new Date;
let didFireReady = false;
window.addEventListener('deviceready', function() {
document.addEventListener('deviceready', function() {
console.log('DEVICE READY FIRED AFTER', (+new Date - before), 'ms')
didFireReady = true;
})

View File

@ -9,12 +9,13 @@ declare var window;
plugin: 'cordova-plugin-geolocation',
pluginRef: 'navigator.geolocation'
})
export class Device {
export class Geolocation {
@Cordova()
static getCurrentPosition(options:any){};
@Cordova({
callbackOrder: 'reverse',
observable: true,
clearFunction: 'clearWatch()'
})

View File

@ -32,10 +32,12 @@ export const cordovaWarn = function(pluginName: string, method: string) {
function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:any={}, resolve:any, reject:any) {
if(!window.cordova) {
cordovaWarn(pluginObj.name, methodName);
/*
reject({
error: 'cordova_not_available'
})
return;
*/
}
// Try to figure out where the success/error callbacks need to be bound
@ -43,8 +45,9 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
// If the plugin method expects myMethod(success, err, options)
if(opts.callbackOrder == 'reverse') {
args[0] = resolve;
args[1] = reject;
// Get those arguments in the order [reject, resolve, ...restOfArgs]
args.unshift(reject);
args.unshift(resolve);
} else if(typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
// If we've specified a success/error index
args.splice(opts.successIndex, 0, resolve);
@ -68,7 +71,8 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
console.log('Cordova calling', pluginObj.name, methodName, args);
return get(window, pluginObj.pluginRef)[methodName].apply(pluginObj, args);
// TODO: Illegal invocation needs window context
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
}
function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={}) {
@ -78,8 +82,12 @@ function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={})
}
function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = {}) {
return Observable.create((observer) => {
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.onNext, observer.onError);
return new Observable(observer => {
console.log('Calling inside observable', observer);
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, (d) => {
console.log('WATCH RESP', d);
observer.next(d)
}, observer.error);
return () => {
return get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, pluginResult);