Skip to content

Commit

Permalink
chainable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mårten Wikström committed Feb 15, 2018
1 parent 475f2a7 commit 0a6c762
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 61 deletions.
20 changes: 16 additions & 4 deletions src/api/IAppHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,25 @@ export interface IAppHistory extends IHistory {
readonly depth: number;
readonly isSuppressed: boolean;

cut(): void;
cut(): IAppHistory;

findLast(match: string | RegExp | PathPredicate): number;

goHome(to: string, state?: any): void;
goHome(to?: Partial<ILocation>): void;
go(delta: number): IAppHistory;

goBack(): IAppHistory;

goForward(): IAppHistory;

goHome(to: string, state?: any): IAppHistory;
goHome(to?: Partial<ILocation>): IAppHistory;

push(path: string, state?: any): IAppHistory;
push(location: Partial<ILocation>): IAppHistory;

replace(path: string, state?: any): IAppHistory;
replace(location: Partial<ILocation>): IAppHistory;

suppress(): UnregisterCallback;
suppress(action: WithSuppressionAction): void;
suppress(action: WithSuppressionAction): IAppHistory;
}
91 changes: 65 additions & 26 deletions src/createAppHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
import { Blocker } from "./Blocker";
import { initialMetaState } from "./initialMetaState";
import { isWrappedLocation } from "./isWrappedLocation";
import { makeNavigationFunc } from "./makeNavigationFunc";
import { nextLocation } from "./nextLocation";
import { nextState } from "./nextState";
import { Notifier } from "./Notifier";
import { Suppressor } from "./Suppressor";
import { unwrapLocation } from "./unwrapLocation";
Expand All @@ -40,9 +41,6 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
const notifier = new Notifier(source, suppressor);
const blocker = new Blocker(source, suppressor);

const push = makeNavigationFunc(source, PUSH, cacheLimit);
const replace = makeNavigationFunc(source, REPLACE, cacheLimit);

const internalFindLast = (
match: string | RegExp | PathPredicate,
): IInternalFindLastResult => {
Expand Down Expand Up @@ -198,6 +196,48 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
isAfterDirtyCut = true;
};

const push = (
pathOrDescriptor: string | Partial<ILocation>,
state?: any,
): IAppHistory => {
if (typeof pathOrDescriptor === "string") {
source.push(pathOrDescriptor, nextState(source, PUSH, state, cacheLimit));
} else {
source.push(nextLocation(source, PUSH, pathOrDescriptor, cacheLimit));
}

return history;
};

const replace = (
pathOrDescriptor: string | Partial<ILocation>,
state?: any,
): IAppHistory => {
if (typeof pathOrDescriptor === "string") {
source.replace(pathOrDescriptor, nextState(source, REPLACE, state, cacheLimit));
} else {
source.replace(nextLocation(source, REPLACE, pathOrDescriptor, cacheLimit));
}

return history;
};

function suppress(): UnregisterCallback;
function suppress(action: WithSuppressionAction): IAppHistory;
function suppress(action?: WithSuppressionAction): UnregisterCallback | IAppHistory {
if (typeof action === "undefined") {
return suppressor.suppress();
} else {
const resume = suppressor.suppress();
try {
action(history);
} finally {
resume();
}
return history;
}
}

const history: IAppHistory = {
get cacheLimit(): number {
return cacheLimit;
Expand All @@ -223,12 +263,9 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
return exposedLocation;
},

push,

replace,

cut(): void {
cut(): IAppHistory {
const resume = suppressor.suppress();

try {
if (canMakeCleanCut()) {
makeCleanCut();
Expand All @@ -238,6 +275,8 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
} finally {
resume();
}

return history;
},

block(prompt?: boolean | string | BlockPrompt): UnregisterCallback {
Expand All @@ -262,19 +301,25 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
return result.delta;
},

go(delta: number): void {
go(delta: number): IAppHistory {
source.go(delta);
return history;
},

goBack(): void {
goBack(): IAppHistory {
source.goBack();
return history;
},

goForward(): void {
goForward(): IAppHistory {
source.goForward();
return history;
},

goHome(to?: string | Partial<ILocation>, state?: any) {
goHome(
to?: string | Partial<ILocation>,
state?: any,
): IAppHistory {
if (isWrappedLocation(source.location)) {
const meta = source.location.state.meta;
if (meta.depth > 0) {
Expand All @@ -297,25 +342,19 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
if (typeof to !== "undefined") {
replace(to, state);
}

return history;
},

listen(listener: NavigationListener): UnregisterCallback {
return notifier.listen(listener);
},

suppress(action?: WithSuppressionAction): UnregisterCallback {
if (typeof action === "undefined") {
return suppressor.suppress();
} else {
const resume = suppressor.suppress();
try {
action(history);
} finally {
resume();
}
return resume;
}
},
push,

replace,

suppress,
};

Object.freeze(history);
Expand Down
29 changes: 0 additions & 29 deletions src/makeNavigationFunc.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/nextLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { nextState } from "./nextState";
export function nextLocation(
source: IHistory,
action: NavigationAction,
location: ILocation,
location: Partial<ILocation>,
cacheLimit: number,
): ILocation<IWrappedState> {
): Partial<ILocation<IWrappedState>> {
return {
hash: location.hash,
key: location.key,
Expand Down

0 comments on commit 0a6c762

Please sign in to comment.