-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathmakeActivityReducer.ts
More file actions
149 lines (134 loc) · 3.63 KB
/
Copy pathmakeActivityReducer.ts
File metadata and controls
149 lines (134 loc) · 3.63 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type { Activity, ActivityTransitionState } from "../Stack";
import type {
DomainEvent,
PausedEvent,
PoppedEvent,
ReplacedEvent,
ResumedEvent,
StepPoppedEvent,
StepPushedEvent,
StepReplacedEvent,
} from "../event-types";
import { last } from "../utils";
import { makeReducer } from "./makeReducer";
function noop(activity: Activity) {
return activity;
}
/**
* Create activity reducers for each event type (Activity + Event => Activity)
*/
export function makeActivityReducer(context: {
transitionDuration: number;
now: number;
resumedAt?: number;
}) {
return makeReducer({
/**
* Change transition state to exit-done
*/
Replaced: (activity: Activity, event: ReplacedEvent): Activity => ({
...activity,
exitedBy: event,
transitionState: "exit-done",
}),
/**
* Change transition state to exit-done or exit-active depending on skipExitActiveState
*/
Popped: (activity: Activity, event: PoppedEvent): Activity => {
const isTransitionDone =
context.now - (context.resumedAt ?? event.eventDate) >=
context.transitionDuration;
const transitionState: ActivityTransitionState =
event.skipExitActiveState || isTransitionDone
? "exit-done"
: "exit-active";
return {
...activity,
exitedBy: event,
transitionState,
params:
transitionState === "exit-done"
? activity.steps[0].params
: activity.params,
steps:
transitionState === "exit-done"
? [activity.steps[0]]
: activity.steps,
};
},
/**
* Replace step params
* Push new step
*/
StepPushed: (activity: Activity, event: StepPushedEvent): Activity => {
const newRoute = {
id: event.stepId,
params: event.stepParams,
enteredBy: event,
zIndex: activity.zIndex,
hasZIndex: event.hasZIndex ?? false,
};
return {
...activity,
params: event.stepParams,
steps: [...activity.steps, newRoute],
};
},
/**
* Replace step params
* Replace the last step
*/
StepReplaced: (activity: Activity, event: StepReplacedEvent): Activity => {
const newRoute = {
id: event.stepId,
params: event.stepParams,
enteredBy: event,
zIndex: activity.zIndex,
hasZIndex: event.hasZIndex ?? false,
};
return {
...activity,
params: event.stepParams,
steps: [
...activity.steps.slice(0, activity.steps.length - 1),
newRoute,
],
};
},
/**
* Pop the last step
* If there are params in the previous step, set them as the new params
*/
StepPopped: (activity: Activity, event: StepPoppedEvent): Activity => {
activity.steps.pop();
const beforeActivityParams = last(activity.steps)?.params;
return {
...activity,
params: beforeActivityParams ?? activity.params,
};
},
/**
* noop
*/
Initialized: noop,
ActivityRegistered: noop,
Pushed: noop,
Paused: (activity: Activity, event: PausedEvent): Activity => {
return {
...activity,
transitionState: "enter-active",
};
},
Resumed: (activity: Activity, event: ResumedEvent): Activity => {
const isTransitionDone =
context.now - event.eventDate >= context.transitionDuration;
const transitionState: ActivityTransitionState = isTransitionDone
? "enter-done"
: "enter-active";
return {
...activity,
transitionState,
};
},
} as const);
}