-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathfindTargetActivityIndices.ts
More file actions
116 lines (98 loc) · 3.07 KB
/
findTargetActivityIndices.ts
File metadata and controls
116 lines (98 loc) · 3.07 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
import type { DomainEvent } from "../event-types";
import type { Activity, ActivityTransitionState } from "../Stack";
import { findIndices, last } from "../utils";
function isActivityNotExited(activity: Activity) {
return !activity.exitedBy;
}
function compareActivitiesByEventDate(a1: Activity, a2: Activity) {
return a2.enteredBy.eventDate - a1.enteredBy.eventDate;
}
function findLatestActiveActivity(activities: Activity[]) {
return activities
.filter(isActivityNotExited)
.sort(compareActivitiesByEventDate)[0];
}
export function findTargetActivityIndices(
activities: Activity[],
event: DomainEvent,
context: {
now: number;
transitionDuration: number;
},
): number[] {
const targetActivities: number[] = [];
switch (event.name) {
case "Replaced": {
const alreadyExistingActivityIndex = last(
findIndices(activities, (activity) => activity.id === event.activityId),
);
if (alreadyExistingActivityIndex !== undefined) {
break;
}
const sorted = activities
.slice()
.sort(compareActivitiesByEventDate)
.filter(isActivityNotExited);
const isTransitionDone =
context.now - event.eventDate >= context.transitionDuration;
const transitionState: ActivityTransitionState =
event.skipEnterActiveState || isTransitionDone
? "enter-done"
: "enter-active";
if (transitionState === "enter-done") {
const range = sorted.findIndex(
(activity) =>
!(
event.skipEnterActiveState &&
activity.enteredBy.name === "Replaced" &&
activity.transitionState === "enter-active"
),
);
return sorted.slice(0, range + 1).map((a) => activities.indexOf(a));
}
break;
}
case "Popped": {
const sorted = activities
.filter(isActivityNotExited)
.sort(compareActivitiesByEventDate);
const latestActivity = sorted.slice(0, sorted.length - 1)[0];
if (latestActivity) {
targetActivities.push(activities.indexOf(latestActivity));
}
break;
}
case "StepPushed":
case "StepReplaced": {
let targetActivity: Activity | undefined;
if (event.targetActivityId) {
targetActivity = activities.find(
(activity) => activity.id === event.targetActivityId,
);
} else {
targetActivity = findLatestActiveActivity(activities);
}
if (targetActivity) {
targetActivities.push(activities.indexOf(targetActivity));
}
break;
}
case "StepPopped": {
let targetActivity: Activity | undefined;
if (event.targetActivityId) {
targetActivity = activities.find(
(activity) => activity.id === event.targetActivityId,
);
} else {
targetActivity = findLatestActiveActivity(activities);
}
if (targetActivity && targetActivity.steps.length > 1) {
targetActivities.push(activities.indexOf(targetActivity));
}
break;
}
default:
break;
}
return targetActivities;
}