-
Notifications
You must be signed in to change notification settings - Fork 1
/
createAppHistory.ts
101 lines (92 loc) · 3.35 KB
/
createAppHistory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
AppHistoryStatus,
IAppHistory,
IAppHistoryOptions,
IHistory,
ILocation,
NavigationAction,
} from "./api";
import { Blocker } from "./Blocker";
import { createSource } from "./createSource";
import { Cutter } from "./Cutter";
import { Goer } from "./Goer";
import { Mutator } from "./Mutator";
import { Notifier } from "./Notifier";
import { Protector } from "./Protector";
import { Scanner } from "./Scanner";
import { Suppressor } from "./Suppressor";
import { Tracker } from "./Tracker";
export function createAppHistory(options: IAppHistoryOptions = {}): IAppHistory {
const {
mode = "browser",
getUserConfirmation,
cacheLimit = 20,
provider,
} = options;
// Navigation block trampoline
const onChangeWasBlocked = () => mutator.changeWasBlocked();
// Source history object
const source = createSource(
onChangeWasBlocked,
mode,
getUserConfirmation,
provider,
);
// Helpers
const suppressor = new Suppressor();
const mutator = new Mutator(source, cacheLimit);
const blocker = new Blocker(source, suppressor, onChangeWasBlocked);
const scanner = new Scanner(source, suppressor, mutator);
const notifier = new Notifier(source, suppressor);
const tracker = new Tracker(source, suppressor, mutator);
const cutter = new Cutter(source, suppressor, mutator);
const goer = new Goer(source, suppressor, mutator, scanner);
const protector = new Protector(tracker);
// Public methods
const cut = protector.async(cutter.cut);
const block = blocker.block;
const createHref = source.createHref.bind(source);
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);
const goForward = protector.async(goer.goForward);
const goHome = protector.async(goer.goHome);
const init = protector.async(tracker.start, "idle", "ready");
const listen = notifier.listen;
const push = protector.async(mutator.push);
const replace = protector.async(mutator.replace);
const suppress = protector.sync(suppressor.suppress);
const suppressWhile = protector.async(suppressor.suppressWhile, "idle");
const whenIdle = protector.whenIdle;
// Make the App History object
const history: IAppHistory = {
get cacheLimit() { return cacheLimit; },
get depth(): number { return tracker.depth; },
get isSuppressed(): boolean { return suppressor.isActive; },
get length(): number { return source.length; },
get action(): NavigationAction { return tracker.action; },
get location(): ILocation { return tracker.location; },
get source(): IHistory { return source; },
get status(): AppHistoryStatus { return protector.status; },
get isBusy(): boolean { return protector.status === "busy"; },
block,
createHref,
cut,
dispose,
findLast,
go,
goBack,
goForward,
goHome,
init,
listen,
push,
replace,
suppress,
suppressWhile,
whenIdle,
};
Object.freeze(history);
return history;
}