-
Notifications
You must be signed in to change notification settings - Fork 13
/
ad.js
103 lines (97 loc) · 2.42 KB
/
ad.js
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
import { combineEpics, ofType } from 'redux-observable'
import { combineLatest as $combineLatest, of as $of } from 'rxjs'
import {
delay,
distinctUntilChanged,
filter,
map,
mapTo,
mergeMap,
skip,
takeUntil
} from 'rxjs/operators'
import {
END_TEST,
SCHEDULE_AD_START,
scheduleAdStart,
SET_AD_ACTIVE,
SET_AD_MUTED,
SET_CONFIG,
setAdActive,
startAd,
VAST_EVENT,
vastEvent,
VERIFICATION_READY
} from '../actions'
import { PRELOAD_SIMULATION_TIME } from '../settings'
const scheduleAdStartEpic = action$ =>
action$.pipe(
ofType(SET_CONFIG),
map(({ payload: config }) => config),
filter(Boolean),
mergeMap(config =>
$combineLatest(
action$.pipe(
ofType(VAST_EVENT),
filter(({ payload: { type } }) => type === 'loaded')
),
action$.pipe(ofType(VERIFICATION_READY))
).pipe(mapTo(config), takeUntil(action$.pipe(ofType(END_TEST))))
),
map(config => scheduleAdStart(config.startDelayed))
)
const adStartEpic = action$ =>
action$.pipe(
ofType(SCHEDULE_AD_START),
mergeMap(({ payload: settings }) => {
let result = $of(settings)
if (settings.delayed) {
result = result.pipe(
delay(PRELOAD_SIMULATION_TIME),
takeUntil(action$.pipe(ofType(END_TEST)))
)
}
return result
}),
map(settings => startAd(settings.delayed))
)
const muteUnmuteEpic = action$ =>
action$.pipe(
ofType(SET_CONFIG),
map(({ payload: config }) => config),
filter(Boolean),
mergeMap(config =>
$combineLatest(
action$.pipe(ofType(SET_AD_MUTED)),
action$.pipe(
ofType(SET_AD_ACTIVE),
filter(({ payload: { active } }) => active)
)
).pipe(
map(
([
{
payload: { muted }
}
]) => muted
),
distinctUntilChanged(),
skip(1),
map(muted => vastEvent(muted ? 'mute' : 'unmute')),
takeUntil(action$.pipe(ofType(END_TEST)))
)
)
)
const mapVastEventsToAdActive = (vastEvents, active) => action$ =>
action$.pipe(
ofType(VAST_EVENT),
filter(({ payload: { type } }) => vastEvents.indexOf(type) >= 0),
mapTo(setAdActive(active))
)
export default combineEpics(
scheduleAdStartEpic,
adStartEpic,
muteUnmuteEpic,
mapVastEventsToAdActive(['start'], true),
mapVastEventsToAdActive(['complete', 'skip', 'error'], false)
)