From 38eceff46d06991f6a7d25c6ee7c7a96906c446c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Wikstr=C3=B6m?= <> Date: Sat, 24 Feb 2018 11:22:54 +0100 Subject: [PATCH] test --- src/Protector.ts | 15 ++++++++++----- src/createAppHistory.spec.ts | 27 +++++++++++++++++++++++++++ src/createAppHistory.ts | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Protector.ts b/src/Protector.ts index 8473dba..278d404 100644 --- a/src/Protector.ts +++ b/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"; @@ -19,11 +19,12 @@ export class Protector { public sync 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 { @@ -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(); @@ -65,9 +66,13 @@ export class Protector { }); } - private enter() { + private enter(ignoreDisposed: boolean) { this.throwIfLocked(); - this.throwIfDisposed(); + + if (!ignoreDisposed) { + this.throwIfDisposed(); + } + this.isBusy = true; } diff --git a/src/createAppHistory.spec.ts b/src/createAppHistory.spec.ts index 2da31a5..f61526e 100644 --- a/src/createAppHistory.spec.ts +++ b/src/createAppHistory.spec.ts @@ -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" }); diff --git a/src/createAppHistory.ts b/src/createAppHistory.ts index 2ff85bf..3d86ca2 100644 --- a/src/createAppHistory.ts +++ b/src/createAppHistory.ts @@ -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);