mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-03-03 16:03:02 +08:00
fix(instance-wrapper):
This commit is contained in:
parent
0b15a2f037
commit
94a8a296c1
@ -4,6 +4,37 @@ declare var Media:any;
|
||||
* @name MediaPlugin
|
||||
* @description
|
||||
* @usage
|
||||
* ```ts
|
||||
* import {MediaPlugin} from 'ionic-native';
|
||||
*
|
||||
*
|
||||
* ...
|
||||
*
|
||||
*
|
||||
* // Playing a file
|
||||
* var file = new MediaPlugin("path/to/file.mp3");
|
||||
*
|
||||
* // play the file
|
||||
* file.play();
|
||||
*
|
||||
* // skip to 10 seconds
|
||||
* file.seekTo(10000);
|
||||
*
|
||||
* // stop plying the file
|
||||
* file.stop();
|
||||
*
|
||||
*
|
||||
* ...
|
||||
*
|
||||
* // Recording to a file
|
||||
* var newFile = new MediaPlugin("path/to/file.mp3");
|
||||
* newFile.startRecord();
|
||||
*
|
||||
* newFile.stopRecord();
|
||||
*
|
||||
*
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
@Plugin({
|
||||
repo: 'https://github.com/apache/cordova-plugin-media',
|
||||
@ -13,7 +44,6 @@ declare var Media:any;
|
||||
export class MediaPlugin {
|
||||
|
||||
// Constants
|
||||
|
||||
static MEDIA_NONE : number = 0;
|
||||
static MEDIA_STARTING : number = 1;
|
||||
static MEDIA_RUNNING : number = 2;
|
||||
@ -21,11 +51,9 @@ export class MediaPlugin {
|
||||
static MEDIA_STOPPED : number = 4;
|
||||
|
||||
// Properties
|
||||
|
||||
private _objectInstance : any;
|
||||
|
||||
// Methods
|
||||
|
||||
/**
|
||||
* Open a media file
|
||||
* @param src {string} A URI containing the audio content.
|
||||
|
@ -73,25 +73,7 @@ function setIndex (args:any[], opts:any={}, resolve? : Function, reject?: Functi
|
||||
|
||||
return args;
|
||||
}
|
||||
function systemCheck (pluginInstance : any, pluginObj : any, methodName, resolve?: Function, reject?: Function) : boolean {
|
||||
if(!pluginInstance) {
|
||||
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
|
||||
if(!window.cordova) {
|
||||
cordovaWarn(pluginObj.name, methodName);
|
||||
reject && reject({
|
||||
error: 'cordova_not_available'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
pluginWarn(pluginObj, methodName);
|
||||
reject && reject({
|
||||
error: 'plugin_not_installed'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:any={}, resolve?: Function, reject?: Function) {
|
||||
// Try to figure out where the success/error callbacks need to be bound
|
||||
// to our promise resolve/reject handlers.
|
||||
@ -100,9 +82,20 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
|
||||
|
||||
let pluginInstance = getPlugin(pluginObj.pluginRef);
|
||||
|
||||
if(!systemCheck(pluginInstance, pluginObj, methodName, resolve, reject)) return;
|
||||
if(!pluginInstance) {
|
||||
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
|
||||
if(!window.cordova) {
|
||||
cordovaWarn(pluginObj.name, methodName);
|
||||
return {
|
||||
error: 'cordova_not_available'
|
||||
}
|
||||
}
|
||||
|
||||
// console.log('Cordova calling', pluginObj.name, methodName, args);
|
||||
pluginWarn(pluginObj, methodName);
|
||||
return {
|
||||
error: 'plugin_not_installed'
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Illegal invocation needs window context
|
||||
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
|
||||
@ -163,34 +156,37 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any =
|
||||
}
|
||||
|
||||
function callInstance(pluginObj:any, methodName : string, args:any[], opts:any = {}, resolve? : Function, reject? : Function){
|
||||
if(!systemCheck(getPlugin(pluginObj), pluginObj, methodName, resolve, reject)) return;
|
||||
args = setIndex(args);
|
||||
//if(!systemCheck(null, pluginObj, methodName, resolve, reject)) return;
|
||||
args = setIndex(args, opts, resolve, reject);
|
||||
console.log("!====!", pluginObj, methodName, args);
|
||||
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
||||
}
|
||||
|
||||
function wrapInstance (pluginObj:any, methodName:string, args:any[], opts:any = {}){
|
||||
return (...args) => {
|
||||
if (opts.sync) {
|
||||
return callInstance(pluginObj, methodName, args, opts);
|
||||
return callInstance(pluginObj, methodName, args, opts);
|
||||
} else if (opts.observable) {
|
||||
return new Observable(observer => {
|
||||
let pluginResult = callInstance(pluginObj, methodName,args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
||||
return () => {
|
||||
try {
|
||||
if (opts.clearWithArgs){
|
||||
return pluginObj._objectInstance[opts.clearFunction].apply(pluginObj._objectInstance, args);
|
||||
}
|
||||
return pluginObj._objectInstance[opts.clearFunction].call(pluginObj, pluginResult);
|
||||
} catch(e) {
|
||||
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
|
||||
console.error(e);
|
||||
return new Observable(observer => {
|
||||
let pluginResult = callInstance(pluginObj, methodName,args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
||||
return () => {
|
||||
try {
|
||||
if (opts.clearWithArgs){
|
||||
return pluginObj._objectInstance[opts.clearFunction].apply(pluginObj._objectInstance, args);
|
||||
}
|
||||
return pluginObj._objectInstance[opts.clearFunction].call(pluginObj, pluginResult);
|
||||
} catch(e) {
|
||||
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return getPromise((resolve, reject) => {
|
||||
callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
||||
});
|
||||
return getPromise((resolve, reject) => {
|
||||
callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -290,8 +286,10 @@ export function Cordova(opts:any = {}) {
|
||||
*/
|
||||
export function CordovaInstance(opts:any = {}) {
|
||||
return (target: Object, methodName: string) => {
|
||||
console.log("P-A", target, methodName, opts);
|
||||
return {
|
||||
value: function(...args: any[]) {
|
||||
console.log("P-B", ...args);
|
||||
return wrapInstance(this, methodName, opts).apply(this, args);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
export function get(obj, path) {
|
||||
console.log(obj, path);
|
||||
for(var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
||||
if(!obj) { return null; }
|
||||
obj = obj[path[i]];
|
||||
|
Loading…
Reference in New Issue
Block a user