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.

\$\endgroup\$
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);
        }
    }
}]);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.