Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mårten Wikström committed Feb 24, 2018
1 parent 54927ae commit 38eceff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/Protector.ts
@@ -1,6 +1,6 @@
import { Tracker } from "./Tracker";

export type ProtectionMode = "ready" | "idle";
export type ProtectionMode = "ready" | "idle" | "any";
export type ProtectorStatus = "busy" | ProtectorIdleStatus;
export type ProtectorIdleStatus = "created" | "ready" | "disposed";

Expand All @@ -19,11 +19,12 @@ export class Protector {

public sync<T extends (...args: any[]) => any>(
func: T,
mode: ProtectionMode = "idle",
then?: ProtectorIdleStatus,
): T {
const self = this;
const wrapped = (...args: any[]) => {
self.enter();
self.enter(mode === "any");
try {
return func.apply(this, args);
} finally {
Expand All @@ -40,7 +41,7 @@ export class Protector {
): T {
const self = this;
const wrapped = async (...args: any[]) => {
self.enter();
self.enter(mode === "any");
try {
if (mode === "ready" && !self.tracker.ready) {
await self.tracker.start();
Expand All @@ -65,9 +66,13 @@ export class Protector {
});
}

private enter() {
private enter(ignoreDisposed: boolean) {
this.throwIfLocked();
this.throwIfDisposed();

if (!ignoreDisposed) {
this.throwIfDisposed();
}

this.isBusy = true;
}

Expand Down
27 changes: 27 additions & 0 deletions src/createAppHistory.spec.ts
Expand Up @@ -55,6 +55,33 @@ describe("createAppHistory", async () => {
expect(history.action).toBe(POP);
});

it("can resume previous history", async () => {
const first = await createAndInitAppHistory({mode: "memory"});

await first.push("apa");
expect(first.depth).toBe(1);
first.dispose();

const second = await createAndInitAppHistory({
mode: "memory",
provider: {
createMemoryHistory() { return first.source; },
},
});
expect(second.depth).toBe(1);
});

it("can be initialized twice", async () => {
const history = await createAndInitAppHistory({mode: "memory"});
await history.init();
});

it("can be disposed twice", async () => {
const history = await createAndInitAppHistory({mode: "memory"});
history.dispose();
history.dispose();
});

it("can push using location descriptor", async () => {
const history = await createAndInitAppHistory({mode: "memory"});
await history.push({ hash: "#foo" });
Expand Down
2 changes: 1 addition & 1 deletion src/createAppHistory.ts
Expand Up @@ -52,7 +52,7 @@ export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory
const cut = protector.async(cutter.cut);
const block = blocker.block;
const createHref = source.createHref.bind(source);
const dispose = protector.sync(tracker.stop, "disposed");
const dispose = protector.sync(tracker.stop, "any", "disposed");
const findLast = protector.async(scanner.findLast);
const go = protector.async(goer.go);
const goBack = protector.async(goer.goBack);
Expand Down

0 comments on commit 38eceff

Please sign in to comment.