Skip to content

Commit

Permalink
chore: Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance committed Dec 17, 2021
2 parents aef6c2b + 8bef6f4 commit 224cda0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/history/__tests__/TestSequences/ReplaceNewLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export default (history, done) => {
state: null,
key: expect.any(String)
});

history.replace('/');
},
({ action, location }) => {
expect(action).toBe('REPLACE');
expect(location).toMatchObject({
pathname: '/',
search: '',
state: null,
key: expect.any(String)
});
}
];

Expand Down
67 changes: 37 additions & 30 deletions packages/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface Path {
*/
export interface Location<S extends State = State> extends Path {
/**
* An object of arbitrary data associated with this location.
* A value of arbitrary data associated with this location.
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#location.state
*/
Expand Down Expand Up @@ -128,7 +128,7 @@ export type PartialLocation<S extends State = State> = Partial<Location<S>>;
/**
* A change to the current location.
*/
export interface Update<S extends State = State> {
export interface Update {
/**
* The action that triggered the change.
*/
Expand All @@ -137,21 +137,21 @@ export interface Update<S extends State = State> {
/**
* The new location.
*/
location: Location<S>;
location: Location;
}

/**
* A function that receives notifications about location changes.
*/
export interface Listener<S extends State = State> {
(update: Update<S>): void;
export interface Listener {
(update: Update): void;
}

/**
* A change to the current location that was blocked. May be retried
* after obtaining user confirmation.
*/
export interface Transition<S extends State = State> extends Update<S> {
export interface Transition extends Update {
/**
* Retries the update to the current location.
*/
Expand All @@ -161,8 +161,8 @@ export interface Transition<S extends State = State> extends Update<S> {
/**
* A function that receives transitions when navigation is blocked.
*/
export interface Blocker<S extends State = State> {
(tx: Transition<S>): void;
export interface Blocker {
(tx: Transition): void;
}

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ export interface History<S extends State = State> {
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#history.location
*/
readonly location: Location<S>;
readonly location: Location;

/**
* Returns a valid href for the given `to` value that may be used as
Expand Down Expand Up @@ -265,7 +265,7 @@ export interface History<S extends State = State> {
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#history.listen
*/
listen(listener: Listener<S>): () => void;
listen(listener: Listener): () => void;

/**
* Prevents the current location from changing and sets up a listener that
Expand All @@ -276,7 +276,7 @@ export interface History<S extends State = State> {
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#history.block
*/
block(blocker: Blocker<S>): () => void;
block(blocker: Blocker): () => void;
}

/**
Expand Down Expand Up @@ -338,7 +338,7 @@ function warning(cond: any, message: string) {
////////////////////////////////////////////////////////////////////////////////

type HistoryState = {
usr: State;
usr: any;
key?: string;
idx: number;
};
Expand Down Expand Up @@ -438,9 +438,12 @@ export function createBrowserHistory(
return typeof to === 'string' ? to : createPath(to);
}

function getNextLocation(to: To, state: State = null): Location {
// state defaults to `null` because `window.history.state` does
function getNextLocation(to: To, state: any = null): Location {
return readOnly<Location>({
...location,
pathname: location.pathname,
hash: '',
search: '',
...(typeof to === 'string' ? parsePath(to) : to),
state,
key: createKey()
Expand Down Expand Up @@ -473,7 +476,7 @@ export function createBrowserHistory(
listeners.call({ action, location });
}

function push(to: To, state?: State) {
function push(to: To, state?: any) {
let nextAction = Action.Push;
let nextLocation = getNextLocation(to, state);
function retry() {
Expand All @@ -497,7 +500,7 @@ export function createBrowserHistory(
}
}

function replace(to: To, state?: State) {
function replace(to: To, state?: any) {
let nextAction = Action.Replace;
let nextLocation = getNextLocation(to, state);
function retry() {
Expand Down Expand Up @@ -685,9 +688,11 @@ export function createHashHistory(
return getBaseHref() + '#' + (typeof to === 'string' ? to : createPath(to));
}

function getNextLocation(to: To, state: State = null): Location {
function getNextLocation(to: To, state: any = null): Location {
return readOnly<Location>({
...location,
pathname: location.pathname,
hash: '',
search: '',
...(typeof to === 'string' ? parsePath(to) : to),
state,
key: createKey()
Expand Down Expand Up @@ -720,7 +725,7 @@ export function createHashHistory(
listeners.call({ action, location });
}

function push(to: To, state?: State) {
function push(to: To, state?: any) {
let nextAction = Action.Push;
let nextLocation = getNextLocation(to, state);
function retry() {
Expand Down Expand Up @@ -751,7 +756,7 @@ export function createHashHistory(
}
}

function replace(to: To, state?: State) {
function replace(to: To, state?: any) {
let nextAction = Action.Replace;
let nextLocation = getNextLocation(to, state);
function retry() {
Expand Down Expand Up @@ -881,9 +886,11 @@ export function createMemoryHistory(
return typeof to === 'string' ? to : createPath(to);
}

function getNextLocation(to: To, state: State = null): Location {
function getNextLocation(to: To, state: any = null): Location {
return readOnly<Location>({
...location,
pathname: location.pathname,
search: '',
hash: '',
...(typeof to === 'string' ? parsePath(to) : to),
state,
key: createKey()
Expand All @@ -902,7 +909,7 @@ export function createMemoryHistory(
listeners.call({ action, location });
}

function push(to: To, state?: State) {
function push(to: To, state?: any) {
let nextAction = Action.Push;
let nextLocation = getNextLocation(to, state);
function retry() {
Expand All @@ -923,7 +930,7 @@ export function createMemoryHistory(
}
}

function replace(to: To, state?: State) {
function replace(to: To, state?: any) {
let nextAction = Action.Replace;
let nextLocation = getNextLocation(to, state);
function retry() {
Expand Down Expand Up @@ -1054,26 +1061,26 @@ export function createPath({
*
* @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#parsepath
*/
export function parsePath(path: string) {
let partialPath: PartialPath = {};
export function parsePath(path: string): PartialPath {
let parsedPath: PartialPath = {};

if (path) {
let hashIndex = path.indexOf('#');
if (hashIndex >= 0) {
partialPath.hash = path.substr(hashIndex);
parsedPath.hash = path.substr(hashIndex);
path = path.substr(0, hashIndex);
}

let searchIndex = path.indexOf('?');
if (searchIndex >= 0) {
partialPath.search = path.substr(searchIndex);
parsedPath.search = path.substr(searchIndex);
path = path.substr(0, searchIndex);
}

if (path) {
partialPath.pathname = path;
parsedPath.pathname = path;
}
}

return partialPath;
return parsedPath;
}
2 changes: 1 addition & 1 deletion packages/history/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "history",
"version": "5.0.0",
"version": "5.1.0",
"description": "Manage session history with JavaScript",
"author": "React Training <hello@reacttraining.com>",
"repository": "ReactTraining/history",
Expand Down

0 comments on commit 224cda0

Please sign in to comment.