2
\$\begingroup\$

I wrote a Cordova plugin for an app that I'm working on, and now I'm trying to write an Angular definition to wrap my plugin. So far, I've followed the model in ng-cordova.js:

angular.module('ngCordova.plugins.audioPlugin', [])
.factory('$audioPlugin', ['$q', function($q) {
 return {
 init: function(sessionKey) {
 var q = $q.defer();
 AudioPlugin.init(sessionKey, function(response) {
 q.resolve(response);
 }, function (error) {
 q.reject(error);
 });
 return q.promise;
 },
 playAudio: function(contentId) {
 var q = $q.defer();
 AudioPlugin.playAudio(contentId, function(response) {
 q.resolve(response);
 }, function (error) {
 q.reject(error);
 });
 return q.promise;
 },
 seekChapter: function(contentId, partIndex, chapterIndex) {
 var q = $q.defer();
 AudioPlugin.seekChapter(contentId, partIndex, chapterIndex function(response) {
 q.resolve(response);
 }, function (error) {
 q.reject(error);
 });
 return q.promise;
 },
 getCurrentPosition: function() {
 var q = $q.defer();
 AudioPlugin.getCurrentPosition(function(response) {
 q.resolve(response);
 }, function (error) {
 q.reject(error);
 });
 return q.promise;
 },
 getCurrentChapter: function() {
 var q = $q.defer();
 AudioPlugin.getCurrentChapter(function(response) {
 q.resolve(response);
 }, function (error) {
 q.reject(error);
 });
 return q.promise;
 }
 }
}]);

I'm looking for feedback on how to structure this wrapper. I'm a little unhappy with the amount of repetition involved to convert the callback-based architecture in the Cordova plugin to the angular promise-based style.

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Dec 11, 2015 at 22:53
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Bug:

You have a bug in the following line:

AudioPlugin.seekChapter(contentId, partIndex, chapterIndex function(response) {

You're missing a comma after chapterIndex.


Structure:

So, as init and playAudio follow the same style, as well as getCurrentChapter and getCurrentPosition following the same style, so you can substitute those out. Additionally, the resolve and reject functions can be taken out, leaving:

angular.module('ngCordova.plugins.audioPlugin', [])
.factory('$audioPlugin', ['$q', function($q) {
 function resolve(response){ return $q.defer.resolve(response).promise; }
 function error(error){ return $q.defer().reject(error).promise; }
 function generic(audioPluginMethod, parameter){
 if (parameter){
 return AudioPlugin[audioPluginMethod](parameter, resolve, error);
 }
 return AudioPlugin[audioPluginMethod](resolve, error);
 }
 return {
 init: generic('init', sessionKey),
 playAudio: generic('playAudio', contentId),
 getCurrentPosition: generic('getCurrentPosition'),
 getCurrentChapter: generic('getCurrentChapter'),
 seekChapter: function(contentId, partIndex, chapterIndex) {
 AudioPlugin.seekChapter(contentId, partIndex, chapterIndex, resolve, error);
 }
 }
}]);
answered Dec 19, 2015 at 4:40
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.