Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Removed $q dep, opts passing, optimized promises #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var osmAPIModule = angular.module('osm.api', [
this.options = {
url: 'http://api.openstreetmap.org/api'
};
this.$get = function osmAPIFactory($http, $q, osmx2js) {
return new osmAPI($http, $q, osmx2js, this.options);
this.$get = ($http, osmx2js) => {
return new osmAPI($http, osmx2js, this.options);
};
this.$get.$inject = ['$http', '$q', 'osmx2js'];
this.$get.$inject = ['$http', 'osmx2js'];
});

export default osmAPIModule;
42 changes: 10 additions & 32 deletions src/api/api.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
class OSMAPI {
/**
* @param {Object} $http angular http
* @param {Object} $q angular promise
* @param {Object} osmx2js service
* @param {Object} options to get set url of the API
*/
constructor($http, $q, osmx2js, options) {
constructor($http, osmx2js, options) {
this.url = options.url;
this.$http = $http;
this.$q = $q;
this.osmx2js = osmx2js;
this._oauth = null;
}
Expand Down Expand Up @@ -53,7 +51,6 @@ class OSMAPI {
* @return {Promise} the adapter response
*/
xhr(options) {
let deferred = this.$q.defer();
return this._oauth.xhr(options);
}

Expand All @@ -77,15 +74,8 @@ class OSMAPI {
* @returns {Promise} $http response with XML as string
*/
get(method, config) {
var deferred = this.$q.defer();
var self = this;
var url = this.url + method;
this.$http.get(url, config).then(function (data) {
deferred.resolve(self.osmx2js.xml2js(data.data));
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
return this.$http.get(this.url + method, config)
.then((data) => this.osmx2js.xml2js(data.data));
}
/**
* send a put request
Expand Down Expand Up @@ -131,8 +121,6 @@ class OSMAPI {
* @returns {Promise} $http response
*/
createChangeset(comment, author) {
var self = this;
var deferred = this.$q.defer();
var changeset = {osm: {
changeset: {
tag: [
Expand All @@ -141,43 +129,33 @@ class OSMAPI {
]
}
}};
this.put('/0.6/changeset/create', changeset).then(function (data) {
deferred.resolve(data);
});
return deferred.promise;
return this.put('/0.6/changeset/create', changeset);
}
/**
* @returns {Promise} $http response with the last changeset id
* or undefined if no changeset was opened
*/
getLastOpenedChangesetId() {
var self = this;
var deferred = this.$q.defer();
var config = {
params:{user: this._oauth.getUserID(), open: true}
};
this.get('/0.6/changesets', config).then(function (data) {
return this.get('/0.6/changesets', config).then((data) => {
var changesets = data.osm.changeset;
if (changesets.length > 0) {
deferred.resolve(changesets[0].id);
return changesets[0].id;
} else if (changesets._id) {
deferred.resolve(changesets._id);
return changesets._id;
} else {
deferred.resolve();
return;
}
});
return deferred.promise;
}
/**
* @returns {Promise} $http.put response of
* /0.6/changeset/CHANGESET_ID/close
*/
closeChangeset(id) {
var self = this;
return this.put(`/0.6/changeset/${id}/close`)
.then(function (data) {
return data;
});
return this.put(`/0.6/changeset/${id}/close`);
}


Expand Down Expand Up @@ -454,5 +432,5 @@ class OSMAPI {

}

OSMAPI.$inject = ['$http', '$q', 'osmx2js'];
OSMAPI.$inject = ['$http', 'osmx2js'];
export default OSMAPI;
4 changes: 2 additions & 2 deletions src/api/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

ngDescribe({
modules: ['osm.api'],
inject: ['osmAPI', 'osmx2js', '$q', '$http', '$httpBackend', '$rootScope'],
inject: ['osmAPI', 'osmx2js', '$http', '$httpBackend', '$rootScope'],
tests: function (deps) {
function setHTTPAdapter(deps) {
var adapter = {
Expand Down Expand Up @@ -48,7 +48,7 @@ ngDescribe({
it('should internal xhr works as expected', function() {
var backend = {
xhr: function(options) {
return deps.$q.when({data: 'data'});
return Promise.resolve({data: 'data'});
}
};
deps.osmAPI.setAuthAdapter(backend);
Expand Down