-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAppLifecycleDriver.js
53 lines (46 loc) · 1.42 KB
/
AppLifecycleDriver.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
import xs from 'xstream';
import fromEvent from 'xstream/extra/fromEvent';
const eventMapping = {
willFinishLaunching$: 'will-finish-launching',
ready$: 'ready',
windowAllClosed$: 'window-all-closed',
beforeQuit$: 'before-quit',
willQuit$: 'will-quit'
};
export default function AppLifecycleDriver(app) {
return cfg$ => {
const source = Object.keys(eventMapping).reduce((obj, prop) => Object.defineProperty(obj, prop, {
get() {
return fromEvent(app, eventMapping[prop])
}
}), {});
Object.defineProperty(source, 'quit$', {
get() {
return fromEvent(app, 'quit')
.map(([ev, exitCode]) => Object.assign(ev, { exitCode }));
}
});
let subscriptions = [];
cfg$
.startWith({})
.addListener({
next: ({ state = 'started', exitCode, isQuittingEnabled = true, isAutoExitEnabled = true } = {}) => {
subscriptions.filter(Boolean).forEach(s => s.dispose());
subscriptions = [
!isQuittingEnabled && source.beforeQuit$.addListener({
next: e => e.preventDefault()
}),
!isAutoExitEnabled && source.willQuit$.addListener({
next: e => e.preventDefault()
})
];
if (state === 'quitting') {
app.quit();
} else if (state === 'exiting') {
app.exit(exitCode);
}
}
});
return source;
};
}