mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 09:35:48 +00:00
b5f14ea8f1
Summary: So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that. Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec. Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers. **T Closes https://github.com/facebook/react-native/pull/7017 Reviewed By: fkgozali Differential Revision: D3202021 Pulled By: martinbigio fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355 fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule XMLHttpRequest
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
var FormData = require('FormData');
|
|
var RCTNetworking = require('RCTNetworking');
|
|
var XMLHttpRequestBase = require('XMLHttpRequestBase');
|
|
|
|
type Header = [string, string];
|
|
|
|
function convertHeadersMapToArray(headers: Object): Array<Header> {
|
|
var headerArray = [];
|
|
for (var name in headers) {
|
|
headerArray.push([name, headers[name]]);
|
|
}
|
|
return headerArray;
|
|
}
|
|
|
|
class XMLHttpRequest extends XMLHttpRequestBase {
|
|
sendImpl(
|
|
method: ?string,
|
|
url: ?string,
|
|
headers: Object,
|
|
data: any,
|
|
useIncrementalUpdates: boolean,
|
|
timeout: number,
|
|
): void {
|
|
var body;
|
|
if (typeof data === 'string') {
|
|
body = {string: data};
|
|
} else if (data instanceof FormData) {
|
|
body = {
|
|
formData: data.getParts().map((part) => {
|
|
part.headers = convertHeadersMapToArray(part.headers);
|
|
return part;
|
|
}),
|
|
};
|
|
} else {
|
|
body = data;
|
|
}
|
|
var requestId = RCTNetworking.sendRequest(
|
|
method,
|
|
url,
|
|
convertHeadersMapToArray(headers),
|
|
body,
|
|
useIncrementalUpdates,
|
|
timeout
|
|
);
|
|
this.didCreateRequest(requestId);
|
|
}
|
|
}
|
|
|
|
module.exports = XMLHttpRequest;
|