From 0d1bd1335f0009fc72cc4506cf3e3a2c01a4387f Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sun, 13 Mar 2016 15:30:21 -0400 Subject: [PATCH] feat(plugin): add event wrapper --- npm-debug.log | 40 ++++++++++++++++++++++++++++++++++++++++ src/plugins/plugin.ts | 25 +++++++++++++++++++++---- 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 npm-debug.log diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 00000000..0ce51aab --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,40 @@ +0 info it worked if it ends with ok +1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe', +1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', +1 verbose cli 'run', +1 verbose cli 'build_bundle' ] +2 info using npm@2.14.12 +3 info using node@v4.2.4 +4 verbose run-script [ 'prebuild_bundle', 'build_bundle', 'postbuild_bundle' ] +5 info prebuild_bundle ionic-native@1.0.12 +6 info build_bundle ionic-native@1.0.12 +7 verbose unsafe-perm in lifecycle true +8 info ionic-native@1.0.12 Failed to exec build_bundle script +9 verbose stack Error: ionic-native@1.0.12 build_bundle: `npm run-script build && browserify dist/index.js > dist/ionic.native.js` +9 verbose stack Exit status 1 +9 verbose stack at EventEmitter. (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:214:16) +9 verbose stack at emitTwo (events.js:87:13) +9 verbose stack at EventEmitter.emit (events.js:172:7) +9 verbose stack at ChildProcess. (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:24:14) +9 verbose stack at emitTwo (events.js:87:13) +9 verbose stack at ChildProcess.emit (events.js:172:7) +9 verbose stack at maybeClose (internal/child_process.js:818:16) +9 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) +10 verbose pkgid ionic-native@1.0.12 +11 verbose cwd C:\Users\Ibrahim\WebstormProjects\ionic-native +12 error Windows_NT 10.0.10586 +13 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build_bundle" +14 error node v4.2.4 +15 error npm v2.14.12 +16 error code ELIFECYCLE +17 error ionic-native@1.0.12 build_bundle: `npm run-script build && browserify dist/index.js > dist/ionic.native.js` +17 error Exit status 1 +18 error Failed at the ionic-native@1.0.12 build_bundle script 'npm run-script build && browserify dist/index.js > dist/ionic.native.js'. +18 error This is most likely a problem with the ionic-native package, +18 error not with npm itself. +18 error Tell the author that this fails on your system: +18 error npm run-script build && browserify dist/index.js > dist/ionic.native.js +18 error You can get their info via: +18 error npm owner ls ionic-native +18 error There is likely additional logging output above. +19 verbose exit [ 1, true ] diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index d2ec8cc1..a06bb16d 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -141,6 +141,19 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = }); } +/** + * Wrap the event with an observable + * @param event + * @returns {Observable} + */ +function wrapEventObservable (event : string) : Observable { + return new Observable(observer => { + let callback = (status : any) => observer.next(status); + window.addEventListener(event, callback, false); + return () => window.removeEventListener(event, callback, false); + }); +} + /** * @private * @param pluginObj @@ -151,13 +164,17 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = export const wrap = function(pluginObj:any, methodName:string, opts:any = {}) { return (...args) => { - if (opts.sync){ + if (opts.sync) return callCordovaPlugin(pluginObj, methodName, args, opts); - } else if (opts.observable) { + + else if (opts.observable) return wrapObservable(pluginObj, methodName, args, opts); - } else { + + else if (opts.eventObservable && opts.event) + return wrapEventObservable(opts.event); + + else return wrapPromise(pluginObj, methodName, args, opts); - } } };