mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-03-04 00:13:06 +08:00
fix(instance-wrapper):
This commit is contained in:
parent
0b15a2f037
commit
94a8a296c1
@ -4,6 +4,37 @@ declare var Media:any;
|
|||||||
* @name MediaPlugin
|
* @name MediaPlugin
|
||||||
* @description
|
* @description
|
||||||
* @usage
|
* @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({
|
@Plugin({
|
||||||
repo: 'https://github.com/apache/cordova-plugin-media',
|
repo: 'https://github.com/apache/cordova-plugin-media',
|
||||||
@ -13,7 +44,6 @@ declare var Media:any;
|
|||||||
export class MediaPlugin {
|
export class MediaPlugin {
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
|
|
||||||
static MEDIA_NONE : number = 0;
|
static MEDIA_NONE : number = 0;
|
||||||
static MEDIA_STARTING : number = 1;
|
static MEDIA_STARTING : number = 1;
|
||||||
static MEDIA_RUNNING : number = 2;
|
static MEDIA_RUNNING : number = 2;
|
||||||
@ -21,11 +51,9 @@ export class MediaPlugin {
|
|||||||
static MEDIA_STOPPED : number = 4;
|
static MEDIA_STOPPED : number = 4;
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
|
|
||||||
private _objectInstance : any;
|
private _objectInstance : any;
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a media file
|
* Open a media file
|
||||||
* @param src {string} A URI containing the audio content.
|
* @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;
|
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) {
|
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
|
// Try to figure out where the success/error callbacks need to be bound
|
||||||
// to our promise resolve/reject handlers.
|
// 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);
|
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
|
// TODO: Illegal invocation needs window context
|
||||||
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
|
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){
|
function callInstance(pluginObj:any, methodName : string, args:any[], opts:any = {}, resolve? : Function, reject? : Function){
|
||||||
if(!systemCheck(getPlugin(pluginObj), pluginObj, methodName, resolve, reject)) return;
|
//if(!systemCheck(null, pluginObj, methodName, resolve, reject)) return;
|
||||||
args = setIndex(args);
|
args = setIndex(args, opts, resolve, reject);
|
||||||
|
console.log("!====!", pluginObj, methodName, args);
|
||||||
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapInstance (pluginObj:any, methodName:string, args:any[], opts:any = {}){
|
function wrapInstance (pluginObj:any, methodName:string, args:any[], opts:any = {}){
|
||||||
|
return (...args) => {
|
||||||
if (opts.sync) {
|
if (opts.sync) {
|
||||||
return callInstance(pluginObj, methodName, args, opts);
|
return callInstance(pluginObj, methodName, args, opts);
|
||||||
} else if (opts.observable) {
|
} else if (opts.observable) {
|
||||||
return new Observable(observer => {
|
return new Observable(observer => {
|
||||||
let pluginResult = callInstance(pluginObj, methodName,args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
let pluginResult = callInstance(pluginObj, methodName,args, opts, observer.next.bind(observer), observer.error.bind(observer));
|
||||||
return () => {
|
return () => {
|
||||||
try {
|
try {
|
||||||
if (opts.clearWithArgs){
|
if (opts.clearWithArgs){
|
||||||
return pluginObj._objectInstance[opts.clearFunction].apply(pluginObj._objectInstance, args);
|
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 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 {
|
} else {
|
||||||
return getPromise((resolve, reject) => {
|
return getPromise((resolve, reject) => {
|
||||||
callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -290,8 +286,10 @@ export function Cordova(opts:any = {}) {
|
|||||||
*/
|
*/
|
||||||
export function CordovaInstance(opts:any = {}) {
|
export function CordovaInstance(opts:any = {}) {
|
||||||
return (target: Object, methodName: string) => {
|
return (target: Object, methodName: string) => {
|
||||||
|
console.log("P-A", target, methodName, opts);
|
||||||
return {
|
return {
|
||||||
value: function(...args: any[]) {
|
value: function(...args: any[]) {
|
||||||
|
console.log("P-B", ...args);
|
||||||
return wrapInstance(this, methodName, opts).apply(this, args);
|
return wrapInstance(this, methodName, opts).apply(this, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export function get(obj, path) {
|
export function get(obj, path) {
|
||||||
|
console.log(obj, path);
|
||||||
for(var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
for(var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
|
||||||
if(!obj) { return null; }
|
if(!obj) { return null; }
|
||||||
obj = obj[path[i]];
|
obj = obj[path[i]];
|
||||||
|
Loading…
Reference in New Issue
Block a user