Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Fixes issue using request in a web worker #351

Merged
merged 3 commits into from
Sep 7, 2017
Merged
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
23 changes: 23 additions & 0 deletions src/has.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,26 @@ add('blob', function () {
add('node-buffer', 'Buffer' in global && typeof global.Buffer === 'function', true);

add('fetch', 'fetch' in global && typeof global.fetch === 'function', true);

add('web-worker-xhr-upload', new Promise((resolve) => {
if (global.Worker !== undefined && global.URL && global.URL.createObjectURL) {
const blob = new Blob([ `(function () {
self.addEventListener('message', function () {
var xhr = new XMLHttpRequest();
try {
xhr.upload;
postMessage('true');
} catch (e) {
postMessage('false');
}
});
})()` ], { type: 'application/javascript' });
const worker = new Worker(URL.createObjectURL(blob));
worker.addEventListener('message', ({ data: result }) => {
resolve(result === 'true');
});
worker.postMessage({});
} else {
resolve(false);
}
}));
8 changes: 5 additions & 3 deletions src/request/providers/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ export default function xhr(url: string, options: XhrRequestOptions = {}): Uploa
const uploadObserverPool = new SubscriptionPool<number>();
task.upload = new Observable<number>(observer => uploadObserverPool.add(observer));

request.upload.addEventListener('progress', event => {
uploadObserverPool.next(event.loaded);
});
if (has('host-browser') || has('web-worker-xhr-upload')) {
request.upload.addEventListener('progress', event => {
uploadObserverPool.next(event.loaded);
});
}

request.send(options.body || null);

Expand Down
57 changes: 55 additions & 2 deletions tests/unit/request/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import * as assert from 'intern/chai!assert';

import * as dojo1xhr from 'dojo/request/xhr';

import xhrRequest from '../../../src/request/providers/xhr';
import xhrRequest, { XhrResponse } from '../../../src/request/providers/xhr';
import { Response } from '../../../src/request/interfaces';
import UrlSearchParams from '../../../src/UrlSearchParams';
import has from '@dojo/has/has';
import { XhrResponse } from '../../../src/request/providers/xhr';
import Promise from '@dojo/shim/Promise';

let echoServerAvailable = false;
Expand Down Expand Up @@ -553,5 +552,59 @@ registerSuite({
});
}
}
},

'Web Workers': {
'from blob'(this: any) {
const testUrl = location.origin + '/__echo/foo.json';
const baseUrl = location.origin;
const dfd = this.async();

const blob = new Blob([ `(function() {
self.addEventListener('message', function (event) {
testXhr(event.data.baseUrl, event.data.testUrl);
});

function testXhr(baseUrl, testUrl) {
importScripts(baseUrl + '/node_modules/@dojo/loader/loader.js');

require.config({
baseUrl: baseUrl,
packages: [
{ name: '@dojo', location: 'node_modules/@dojo' }
]
});

require(['_build/src/request/providers/xhr'], function (xhr) {
xhr.default(testUrl).then(function (response) {
return response.json();
}).then(function (json) {
self.postMessage({ status: 'success' });
}).catch(function (e) {
self.postMessage({ status: 'error', message: e.message });
});
});
}
})()` ], { type: 'application/javascript' });
const worker = new Worker(URL.createObjectURL(blob));
worker.addEventListener('error', (error) => {
dfd.reject(error.message);
});
worker.addEventListener('message', ({ data: result }) => {
const { status } = result;

if (status === 'success') {
dfd.resolve();
}
else if (status === 'error') {
dfd.reject(result.message);
}
});

worker.postMessage({
baseUrl,
testUrl
});
}
}
});