Skip to content

Commit

Permalink
async
Browse files Browse the repository at this point in the history
  • Loading branch information
Mårten Wikström committed Feb 17, 2018
1 parent d14c00e commit d3ad5e0
Show file tree
Hide file tree
Showing 9 changed files with 582 additions and 494 deletions.
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": ["--runInBand", "--coverage", "false"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest __DEBUG__",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": ["--runInBand", "--coverage", "false", "-t", "__DEBUG__"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:nocoverage": "jest --coverage false",
"test:debug": "jest --coverage false -t __DEBUG__",
"build": "webpack",
"lint": "tslint --format stylish src/*.ts src/**/*.ts",
"lint:fix": "tslint --format stylish --fix src/*.ts src/**/*.ts",
Expand Down Expand Up @@ -49,7 +51,7 @@
"transform": {
"^.+\\.ts$": "ts-jest"
},
"testRegex": "src/.*\\.spec\\.ts$",
"testRegex": "\\.spec\\.ts$",
"moduleFileExtensions": [
"ts",
"js",
Expand Down
18 changes: 13 additions & 5 deletions src/Blocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ import {
import { Suppressor } from "./Suppressor";

export class Blocker {
constructor(private source: IHistory, private suppressor: Suppressor) { }
constructor(private source: IHistory, private suppressor: Suppressor, private onBlocked: () => void) { }

public block(prompt?: boolean | string | BlockPrompt) {
public block(prompt: boolean | string | BlockPrompt = false) {
const wrapper = ((location: ILocation, action: NavigationAction) => {
let result;

if (this.suppressor.isActive) {
return true;
result = true;
} else if (typeof prompt === "function") {
return prompt(location, action);
result = prompt(location, action);
} else {
return prompt;
result = prompt;
}

if (result === false) {
this.onBlocked();
}

return result;
}).bind(this);

return this.source.block(wrapper);
Expand Down
28 changes: 14 additions & 14 deletions src/api/IAppHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ export interface IAppHistory extends IHistory {
readonly cacheLimit: number;
readonly depth: number;
readonly isSuppressed: boolean;
readonly source: IHistory;

cut(): IAppHistory;
cut(): Promise<void>;

findLast(match: string | RegExp | PathPredicate): number;
findLast(match: string | RegExp | PathPredicate): Promise<number>;

go(delta: number): IAppHistory;
go(delta: number): Promise<void>;

goBack(to?: RegExp | PathPredicate): boolean;
goBack(to: string, state?: any): IAppHistory;
goBack(to?: Partial<ILocation>): IAppHistory;
goBack(to: string, state?: any): Promise<boolean>;
goBack(to?: Partial<ILocation> | RegExp | PathPredicate): Promise<boolean>;

goForward(): IAppHistory;
goForward(): Promise<void>;

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

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

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

suppress(): UnregisterCallback;
suppress(action: WithSuppressionAction): IAppHistory;
suppress(action: WithSuppressionAction): Promise<void>;
}
8 changes: 6 additions & 2 deletions src/api/IAppHistoryOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { IHistory } from "./IHistory";
export type SourceType = "browser" | "memory";

export type UserConfirmationCallback = (result: boolean) => void;
export type UserConfirmation = (message: string, callback: UserConfirmationCallback) => void;

export interface IAppHistoryOptions {
source?: IHistory;
sourceType?: SourceType;
getUserConfirmation?: UserConfirmation;
cacheLimit?: number;
}
2 changes: 1 addition & 1 deletion src/api/WithSuppressionAction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { IAppHistory } from "./IAppHistory";

export type WithSuppressionAction = (history: IAppHistory) => void;
export type WithSuppressionAction = (history: IAppHistory) => void | Promise<void>;
Loading

0 comments on commit d3ad5e0

Please sign in to comment.