Skip to content

Instantly share code, notes, and snippets.

@zpao
Created July 23, 2015 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zpao/29cc0a4bd03791d08cd1 to your computer and use it in GitHub Desktop.
Save zpao/29cc0a4bd03791d08cd1 to your computer and use it in GitHub Desktop.
diff -U4 lib_/Deferred.js lib/Deferred.js
--- lib_/Deferred.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/Deferred.js 2015-07-23 15:54:02.000000000 -0700
@@ -7,9 +7,9 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule Deferred
* @typechecks
- *
+ * @flow
*/
'use strict';
@@ -36,10 +36,10 @@
_classCallCheck(this, Deferred);
this._settled = false;
this._promise = new Promise(function (resolve, reject) {
- _this._resolve = resolve;
- _this._reject = reject;
+ _this._resolve = /*:: resolve: any*/;
+ _this._reject = /*:: reject: any*/;
});
}
_createClass(Deferred, [{
@@ -48,31 +48,31 @@
return this._promise;
}
}, {
key: 'resolve',
- value: function resolve(value) {
+ value: function resolve(value /*: Tvalue*/) /*: void*/ {
this._settled = true;
this._resolve(value);
}
}, {
key: 'reject',
- value: function reject(reason) {
+ value: function reject(reason /*: Treason*/) /*: void*/ {
this._settled = true;
this._reject(reason);
}
}, {
key: 'then',
- value: function then() {
+ value: function then() /*: Promise*/ {
return Promise.prototype.then.apply(this._promise, arguments);
}
}, {
key: 'done',
- value: function done() {
+ value: function done() /*: void*/ {
Promise.prototype.done.apply(this._promise, arguments);
}
}, {
key: 'isSettled',
- value: function isSettled() {
+ value: function isSettled() /*: boolean*/ {
return this._settled;
}
}]);
diff -U4 lib_/PromiseMap.js lib/PromiseMap.js
--- lib_/PromiseMap.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/PromiseMap.js 2015-07-23 15:54:02.000000000 -0700
@@ -6,16 +6,11 @@
* 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 PromiseMap
- *
+ * @flow
*/
-/**
- * A map of asynchronous values that can be get or set in any order. Unlike a
- * normal map, setting the value for a particular key more than once throws.
- * Also unlike a normal map, a key can either be resolved or rejected.
- */
'use strict';
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
@@ -24,8 +19,14 @@
var Deferred = require('./Deferred');
var invariant = require('./invariant');
+/**
+ * A map of asynchronous values that can be get or set in any order. Unlike a
+ * normal map, setting the value for a particular key more than once throws.
+ * Also unlike a normal map, a key can either be resolved or rejected.
+ */
+/*:: import type * as Promise from 'Promise';*/
var PromiseMap = (function () {
function PromiseMap() {
_classCallCheck(this, PromiseMap);
@@ -33,21 +34,21 @@
}
_createClass(PromiseMap, [{
key: 'get',
- value: function get(key) {
+ value: function get(key /*: string*/) /*: Promise*/ {
return getDeferred(this._deferred, key).getPromise();
}
}, {
key: 'resolveKey',
- value: function resolveKey(key, value) {
+ value: function resolveKey(key /*: string*/, value /*: Tvalue*/) /*: void*/ {
var entry = getDeferred(this._deferred, key);
!!entry.isSettled() ? process.env.NODE_ENV !== 'production' ? invariant(false, 'PromiseMap: Already settled `%s`.', key) : invariant(false) : undefined;
entry.resolve(value);
}
}, {
key: 'rejectKey',
- value: function rejectKey(key, reason) {
+ value: function rejectKey(key /*: string*/, reason /*: Treason*/) /*: void*/ {
var entry = getDeferred(this._deferred, key);
!!entry.isSettled() ? process.env.NODE_ENV !== 'production' ? invariant(false, 'PromiseMap: Already settled `%s`.', key) : invariant(false) : undefined;
entry.reject(reason);
}
@@ -55,9 +56,9 @@
return PromiseMap;
})();
-function getDeferred(entries, key) {
+function getDeferred(entries /*: {[key: string]: Deferred}*/, key /*: string*/) /*: Deferred*/ {
if (!entries.hasOwnProperty(key)) {
entries[key] = new Deferred();
}
return entries[key];
diff -U4 lib_/URI.js lib/URI.js
--- lib_/URI.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/URI.js 2015-07-23 15:54:03.000000000 -0700
@@ -7,9 +7,9 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule URI
* @typechecks
- *
+ * @flow
*/
'use strict';
diff -U4 lib_/areEqual.js lib/areEqual.js
--- lib_/areEqual.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/areEqual.js 2015-07-23 15:54:02.000000000 -0700
@@ -6,9 +6,9 @@
* 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 areEqual
- *
+ * @flow
*/
'use strict';
@@ -22,9 +22,9 @@
* @see http://underscorejs.org
* @copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
* @license MIT
*/
-function areEqual(a, b) {
+function areEqual(a /*: any*/, b /*: any*/) /*: boolean*/ {
var aStack = aStackPool.length ? aStackPool.pop() : [];
var bStack = bStackPool.length ? bStackPool.pop() : [];
var result = eq(a, b, aStack, bStack);
aStack.length = 0;
@@ -33,9 +33,9 @@
bStackPool.push(bStack);
return result;
}
-function eq(a, b, aStack, bStack) {
+function eq(a /*: any*/, b /*: any*/, aStack /*: Array<any>*/, bStack /*: Array<any>*/) /*: boolean*/ {
if (a === b) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
return a !== 0 || 1 / a == 1 / b;
}
diff -U4 lib_/everyObject.js lib/everyObject.js
--- lib_/everyObject.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/everyObject.js 2015-07-23 15:54:03.000000000 -0700
@@ -6,9 +6,9 @@
* 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 everyObject
- *
+ * @flow
* @typechecks
*/
'use strict';
@@ -32,9 +32,9 @@
* value passed to `callback` will be the value at the time `everyObject`
* visits them. Properties that are deleted before being visited are not
* visited.
*/
-function everyObject(object, callback, context) {
+function everyObject(object /*: ?Object*/, callback /*: (value: any, name: string, object: Object) => any*/, context? /*:: ?: any*/) /*: boolean*/ {
for (var name in object) {
if (hasOwnProperty.call(object, name)) {
if (!callback.call(context, object[name], name, object)) {
return false;
diff -U4 lib_/fetchWithRetries.js lib/fetchWithRetries.js
--- lib_/fetchWithRetries.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/fetchWithRetries.js 2015-07-23 15:54:03.000000000 -0700
@@ -7,9 +7,9 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule fetchWithRetries
* @typechecks
- *
+ * @flow
*/
'use strict';
@@ -21,17 +21,28 @@
var sprintf = require('./sprintf');
var fetch = require('./fetch');
var warning = require('./warning');
+/*:: type InitWitRetries = {
+ body?: mixed;
+ cache?: ?string;
+ credentials?: ?string;
+ fetchTimeout?: number;
+ headers?: mixed;
+ method?: ?string;
+ mode?: ?string;
+ retryDelays?: Array<number>;
+};*/
+
var DEFAULT_FETCH_TIMEOUT = 15000;
var DEFAULT_RETRY_DELAYS = [1000, 3000];
/**
* Posts a request to the server with the given data as the payload.
* Automatic reties are done based on the value of `retryDelays` in
* `RelayNetworkConfig`.
*/
-function fetchWithRetries(uri, initWithRetries) {
+function fetchWithRetries(uri /*: string*/, initWithRetries /*: InitWitRetries*/) /*: Promise*/ {
var fetchTimeout = initWithRetries.fetchTimeout;
var retryDelays = initWithRetries.retryDelays;
var init = _objectWithoutProperties(initWithRetries, ['fetchTimeout', 'retryDelays']);
@@ -47,9 +58,9 @@
* Sends a request to the server that will timeout after
* `RelayNetworkConfig.fetchTimeout`.
* If the request fails or times out a new request might be scheduled
*/
- function sendTimedRequest() {
+ function sendTimedRequest() /*: void*/ {
requestsAttempted++;
requestStartTime = Date.now();
var isRequestAlive = true;
var request = fetch(uri, init);
@@ -92,9 +103,9 @@
/**
* Schedules another run of sendTimedRequest based on how much time has
* passed between the time the last request was sent and now
*/
- function retryRequest() {
+ function retryRequest() /*: void*/ {
var retryDelay = nonNullRetryDelays[requestsAttempted - 1];
var retryStartTime = requestStartTime + retryDelay;
// Schedule retry for a configured duration after last request started.
setTimeout(sendTimedRequest, retryStartTime - Date.now());
@@ -102,9 +113,9 @@
/**
* Checks if another attempt should be doen to send a request to the server
*/
- function shouldRetry(attempt) {
+ function shouldRetry(attempt /*: number*/) /*: boolean*/ {
return ExecutionEnvironment.canUseDOM && attempt <= nonNullRetryDelays.length;
}
sendTimedRequest();
diff -U4 lib_/nullthrows.js lib/nullthrows.js
--- lib_/nullthrows.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/nullthrows.js 2015-07-23 15:54:03.000000000 -0700
@@ -6,14 +6,14 @@
* 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 nullthrows
- *
+ * @flow
*/
"use strict";
-var nullthrows = function (x) {
+var nullthrows = function /*:: <T>*/(x /*: ?T*/) /*: T*/ {
if (x != null) {
return x;
}
throw new Error("Got unexpected null or undefined");
diff -U4 lib_/removeFromArray.js lib/removeFromArray.js
--- lib_/removeFromArray.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/removeFromArray.js 2015-07-23 15:54:03.000000000 -0700
@@ -7,17 +7,17 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule removeFromArray
* @typechecks
- *
+ * @flow
*/
/**
* Removes an element from an array.
*/
"use strict";
-function removeFromArray(array, element) {
+function removeFromArray /*:: <T>*/(array /*: Array<T>*/, element /*: T*/) /*: void*/ {
var index = array.indexOf(element);
if (index !== -1) {
array.splice(index, 1);
}
diff -U4 lib_/resolveImmediate.js lib/resolveImmediate.js
--- lib_/resolveImmediate.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/resolveImmediate.js 2015-07-23 15:54:03.000000000 -0700
@@ -6,9 +6,9 @@
* 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 resolveImmediate
- *
+ * @flow
*/
'use strict';
@@ -18,9 +18,9 @@
/**
* An alternative to setImmediate based on Promise.
*/
-function resolveImmediate(callback) {
+function resolveImmediate(callback /*: () => any*/) /*: void*/ {
resolvedPromise.then(callback)['catch'](throwNext);
}
function throwNext(error) {
diff -U4 lib_/someObject.js lib/someObject.js
--- lib_/someObject.js 2015-07-23 15:50:29.000000000 -0700
+++ lib/someObject.js 2015-07-23 15:54:03.000000000 -0700
@@ -6,9 +6,9 @@
* 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 someObject
- *
+ * @flow
* @typechecks
*/
'use strict';
@@ -32,9 +32,9 @@
* value passed to `callback` will be the value at the time `someObject`
* visits them. Properties that are deleted before being visited are not
* visited.
*/
-function someObject(object, callback, context) {
+function someObject(object /*: ?Object*/, callback /*: (value: any, name: string, object: Object) => any*/, context? /*:: ?: any*/) /*: boolean*/ {
for (var name in object) {
if (hasOwnProperty.call(object, name)) {
if (callback.call(context, object[name], name, object)) {
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment