This repository has been archived by the owner on May 6, 2024. It is now read-only.
forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Blightwidow
committed
Sep 27, 2018
1 parent
905c93e
commit 0f2f77f
Showing
128 changed files
with
7,689 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
export function loopAsync(turns, work, callback) { | ||
var currentTurn = 0, | ||
isDone = false; | ||
var sync = false, | ||
hasNext = false, | ||
doneArgs = void 0; | ||
|
||
function done() { | ||
isDone = true; | ||
if (sync) { | ||
// Iterate instead of recursing if possible. | ||
doneArgs = [].concat(Array.prototype.slice.call(arguments)); | ||
return; | ||
} | ||
|
||
callback.apply(this, arguments); | ||
} | ||
|
||
function next() { | ||
if (isDone) { | ||
return; | ||
} | ||
|
||
hasNext = true; | ||
if (sync) { | ||
// Iterate instead of recursing if possible. | ||
return; | ||
} | ||
|
||
sync = true; | ||
|
||
while (!isDone && currentTurn < turns && hasNext) { | ||
hasNext = false; | ||
work.call(this, currentTurn++, next, done); | ||
} | ||
|
||
sync = false; | ||
|
||
if (isDone) { | ||
// This means the loop finished synchronously. | ||
callback.apply(this, doneArgs); | ||
return; | ||
} | ||
|
||
if (currentTurn >= turns && hasNext) { | ||
isDone = true; | ||
callback(); | ||
} | ||
} | ||
|
||
next(); | ||
} | ||
|
||
export function mapAsync(array, work, callback) { | ||
var length = array.length; | ||
var values = []; | ||
|
||
if (length === 0) return callback(null, values); | ||
|
||
var isDone = false, | ||
doneCount = 0; | ||
|
||
function done(index, error, value) { | ||
if (isDone) return; | ||
|
||
if (error) { | ||
isDone = true; | ||
callback(error); | ||
} else { | ||
values[index] = value; | ||
|
||
isDone = ++doneCount === length; | ||
|
||
if (isDone) callback(null, values); | ||
} | ||
} | ||
|
||
array.forEach(function (item, index) { | ||
work(item, index, function (error, value) { | ||
done(index, error, value); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import PropTypes from 'prop-types'; | ||
|
||
// Works around issues with context updates failing to propagate. | ||
// Caveat: the context value is expected to never change its identity. | ||
// https://github.com/facebook/react/issues/2517 | ||
// https://github.com/reactjs/react-router/issues/470 | ||
|
||
var contextProviderShape = PropTypes.shape({ | ||
subscribe: PropTypes.func.isRequired, | ||
eventIndex: PropTypes.number.isRequired | ||
}); | ||
|
||
function makeContextName(name) { | ||
return '@@contextSubscriber/' + name; | ||
} | ||
|
||
export function ContextProvider(name) { | ||
var _childContextTypes, _ref2; | ||
|
||
var contextName = makeContextName(name); | ||
var listenersKey = contextName + '/listeners'; | ||
var eventIndexKey = contextName + '/eventIndex'; | ||
var subscribeKey = contextName + '/subscribe'; | ||
|
||
return _ref2 = { | ||
childContextTypes: (_childContextTypes = {}, _childContextTypes[contextName] = contextProviderShape.isRequired, _childContextTypes), | ||
|
||
getChildContext: function getChildContext() { | ||
var _ref; | ||
|
||
return _ref = {}, _ref[contextName] = { | ||
eventIndex: this[eventIndexKey], | ||
subscribe: this[subscribeKey] | ||
}, _ref; | ||
}, | ||
componentWillMount: function componentWillMount() { | ||
this[listenersKey] = []; | ||
this[eventIndexKey] = 0; | ||
}, | ||
componentWillReceiveProps: function componentWillReceiveProps() { | ||
this[eventIndexKey]++; | ||
}, | ||
componentDidUpdate: function componentDidUpdate() { | ||
var _this = this; | ||
|
||
this[listenersKey].forEach(function (listener) { | ||
return listener(_this[eventIndexKey]); | ||
}); | ||
} | ||
}, _ref2[subscribeKey] = function (listener) { | ||
var _this2 = this; | ||
|
||
// No need to immediately call listener here. | ||
this[listenersKey].push(listener); | ||
|
||
return function () { | ||
_this2[listenersKey] = _this2[listenersKey].filter(function (item) { | ||
return item !== listener; | ||
}); | ||
}; | ||
}, _ref2; | ||
} | ||
|
||
export function ContextSubscriber(name) { | ||
var _contextTypes, _ref4; | ||
|
||
var contextName = makeContextName(name); | ||
var lastRenderedEventIndexKey = contextName + '/lastRenderedEventIndex'; | ||
var handleContextUpdateKey = contextName + '/handleContextUpdate'; | ||
var unsubscribeKey = contextName + '/unsubscribe'; | ||
|
||
return _ref4 = { | ||
contextTypes: (_contextTypes = {}, _contextTypes[contextName] = contextProviderShape, _contextTypes), | ||
|
||
getInitialState: function getInitialState() { | ||
var _ref3; | ||
|
||
if (!this.context[contextName]) { | ||
return {}; | ||
} | ||
|
||
return _ref3 = {}, _ref3[lastRenderedEventIndexKey] = this.context[contextName].eventIndex, _ref3; | ||
}, | ||
componentDidMount: function componentDidMount() { | ||
if (!this.context[contextName]) { | ||
return; | ||
} | ||
|
||
this[unsubscribeKey] = this.context[contextName].subscribe(this[handleContextUpdateKey]); | ||
}, | ||
componentWillReceiveProps: function componentWillReceiveProps() { | ||
var _setState; | ||
|
||
if (!this.context[contextName]) { | ||
return; | ||
} | ||
|
||
this.setState((_setState = {}, _setState[lastRenderedEventIndexKey] = this.context[contextName].eventIndex, _setState)); | ||
}, | ||
componentWillUnmount: function componentWillUnmount() { | ||
if (!this[unsubscribeKey]) { | ||
return; | ||
} | ||
|
||
this[unsubscribeKey](); | ||
this[unsubscribeKey] = null; | ||
} | ||
}, _ref4[handleContextUpdateKey] = function (eventIndex) { | ||
if (eventIndex !== this.state[lastRenderedEventIndexKey]) { | ||
var _setState2; | ||
|
||
this.setState((_setState2 = {}, _setState2[lastRenderedEventIndexKey] = eventIndex, _setState2)); | ||
} | ||
}, _ref4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
import React from 'react'; | ||
import createReactClass from 'create-react-class'; | ||
import Link from './Link'; | ||
|
||
/** | ||
* An <IndexLink> is used to link to an <IndexRoute>. | ||
*/ | ||
var IndexLink = createReactClass({ | ||
displayName: 'IndexLink', | ||
|
||
render: function render() { | ||
return React.createElement(Link, _extends({}, this.props, { onlyActiveOnIndex: true })); | ||
} | ||
}); | ||
|
||
export default IndexLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import createReactClass from 'create-react-class'; | ||
import { string, object } from 'prop-types'; | ||
import warning from './routerWarning'; | ||
import invariant from 'invariant'; | ||
import Redirect from './Redirect'; | ||
import { falsy } from './InternalPropTypes'; | ||
|
||
/** | ||
* An <IndexRedirect> is used to redirect from an indexRoute. | ||
*/ | ||
/* eslint-disable react/require-render-return */ | ||
var IndexRedirect = createReactClass({ | ||
displayName: 'IndexRedirect', | ||
|
||
statics: { | ||
createRouteFromReactElement: function createRouteFromReactElement(element, parentRoute) { | ||
/* istanbul ignore else: sanity check */ | ||
if (parentRoute) { | ||
parentRoute.indexRoute = Redirect.createRouteFromReactElement(element); | ||
} else { | ||
process.env.NODE_ENV !== 'production' ? warning(false, 'An <IndexRedirect> does not make sense at the root of your route config') : void 0; | ||
} | ||
} | ||
}, | ||
|
||
propTypes: { | ||
to: string.isRequired, | ||
query: object, | ||
state: object, | ||
onEnter: falsy, | ||
children: falsy | ||
}, | ||
|
||
/* istanbul ignore next: sanity check */ | ||
render: function render() { | ||
!false ? process.env.NODE_ENV !== 'production' ? invariant(false, '<IndexRedirect> elements are for router configuration only and should not be rendered') : invariant(false) : void 0; | ||
} | ||
}); | ||
|
||
export default IndexRedirect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import createReactClass from 'create-react-class'; | ||
import { func } from 'prop-types'; | ||
import warning from './routerWarning'; | ||
import invariant from 'invariant'; | ||
import { createRouteFromReactElement as _createRouteFromReactElement } from './RouteUtils'; | ||
import { component, components, falsy } from './InternalPropTypes'; | ||
|
||
/** | ||
* An <IndexRoute> is used to specify its parent's <Route indexRoute> in | ||
* a JSX route config. | ||
*/ | ||
/* eslint-disable react/require-render-return */ | ||
var IndexRoute = createReactClass({ | ||
displayName: 'IndexRoute', | ||
|
||
statics: { | ||
createRouteFromReactElement: function createRouteFromReactElement(element, parentRoute) { | ||
/* istanbul ignore else: sanity check */ | ||
if (parentRoute) { | ||
parentRoute.indexRoute = _createRouteFromReactElement(element); | ||
} else { | ||
process.env.NODE_ENV !== 'production' ? warning(false, 'An <IndexRoute> does not make sense at the root of your route config') : void 0; | ||
} | ||
} | ||
}, | ||
|
||
propTypes: { | ||
path: falsy, | ||
component: component, | ||
components: components, | ||
getComponent: func, | ||
getComponents: func | ||
}, | ||
|
||
/* istanbul ignore next: sanity check */ | ||
render: function render() { | ||
!false ? process.env.NODE_ENV !== 'production' ? invariant(false, '<IndexRoute> elements are for router configuration only and should not be rendered') : invariant(false) : void 0; | ||
} | ||
}); | ||
|
||
export default IndexRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { func, object, arrayOf, oneOfType, element, shape, string } from 'prop-types'; | ||
|
||
export function falsy(props, propName, componentName) { | ||
if (props[propName]) return new Error('<' + componentName + '> should not have a "' + propName + '" prop'); | ||
} | ||
|
||
export var history = shape({ | ||
listen: func.isRequired, | ||
push: func.isRequired, | ||
replace: func.isRequired, | ||
go: func.isRequired, | ||
goBack: func.isRequired, | ||
goForward: func.isRequired | ||
}); | ||
|
||
export var component = oneOfType([func, string]); | ||
export var components = oneOfType([component, object]); | ||
export var route = oneOfType([object, element]); | ||
export var routes = oneOfType([route, arrayOf(route)]); |
Oops, something went wrong.