Skip to content

Commit

Permalink
add labels to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-evans committed Feb 13, 2017
1 parent 9af35ce commit d682768
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
12 changes: 6 additions & 6 deletions addon/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default Ember.Object.extend({
}
});

let request = new HTTPRequest();
let request = new HTTPRequest({ label: `${options.method} ${get(this, 'name') } to ${options.url}` });
request.open(options.method, options.url);

Object.keys(options.headers).forEach(function (key) {
Expand Down Expand Up @@ -214,29 +214,29 @@ export default Ember.Object.extend({
}, (error) => {
set(this, 'state', 'failed');
return RSVP.reject(error);
}).finally(function () {
}, `ember-file-upload: Update "${get(this, 'name')}"'s upload state`).finally(function () {
// Decrement for Ember.Test
inflightRequests--;
});
},

readAsArrayBuffer() {
let reader = new FileReader();
let reader = new FileReader({ label: `Read ${get(this, 'name')} as an ArrayBuffer` });
return reader.readAsArrayBuffer(this.blob);
},

readAsDataURL() {
let reader = new FileReader();
let reader = new FileReader({ label: `Read ${get(this, 'name')} as a Data URI` });
return reader.readAsDataURL(this.blob);
},

readAsBinaryString() {
let reader = new FileReader();
let reader = new FileReader({ label: `Read ${get(this, 'name')} as a binary string` });
return reader.readAsBinaryString(this.blob);
},

readAsText() {
let reader = new FileReader();
let reader = new FileReader({ label: `Read ${get(this, 'name')} as text` });
return reader.readAsText(this.blob);
}

Expand Down
13 changes: 8 additions & 5 deletions addon/system/file-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import Ember from 'ember';

const { RSVP } = Ember;

export default function () {
let { resolve, reject, promise } = RSVP.defer();
export default function (options = {}) {
let { resolve, reject, promise } = RSVP.defer(`ember-file-upload: ${options.label}`);
let reader = new FileReader();

reader.onload = resolve;
reader.onerror = reject;

let aborted = RSVP.defer();
let aborted;
let cancel = function () {
reader.abort();
if (aborted == null) {
aborted = RSVP.defer(`ember-file-upload: Abort ${options.label}`);
reader.abort();
}
return aborted.promise;
};
reader.onabort = function () {
Expand All @@ -25,7 +28,7 @@ export default function () {
return reader.result;
}, function () {
return RSVP.reject(reader.error);
});
}, `ember-file-upload: Unpack ${options.label}`);
p.cancel = cancel;
return p;
};
Expand Down
11 changes: 7 additions & 4 deletions addon/system/http-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ function parseResponse(request) {
};
}

export default function () {
let { resolve, reject, promise } = RSVP.defer();
export default function (options = {}) {
let { resolve, reject, promise } = RSVP.defer(`ember-file-upload: ${options.label}`);
let request = new XMLHttpRequest();

let aborted = RSVP.defer();
let aborted;
promise.cancel = () => {
request.abort();
if (aborted == null) {
aborted = RSVP.defer(`ember-file-upload: Abort ${options.label}`);
request.abort();
}
return aborted.promise;
};
request.onabort = bind(this, function () {
Expand Down

0 comments on commit d682768

Please sign in to comment.