-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathservice.js
309 lines (248 loc) · 7.45 KB
/
service.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { get, set, computed } from "@ember/object";
import { scheduleOnce } from "@ember/runloop";
import { default as Service, inject as service } from "@ember/service";
import { vars as varsConfig } from "config";
import { logDebug, logError } from "./logger";
import { Aborted, ExitSignalError, HostingError } from "./errors";
import { clearCache } from "./cache";
import resolvePlayer from "./player/resolve";
import resolveProvider from "./provider/resolve";
import launch from "./launch";
import { setShowInTaskbar, setMinimized, setVisibility } from "nwjs/Window";
import {
ATTR_GUI_MINIMIZE_MINIMIZE,
ATTR_GUI_MINIMIZE_TRAY
} from "data/models/settings/gui/fragment";
const { "stream-reload-interval": streamReloadInterval } = varsConfig;
const modelName = "stream";
function setIfNotNull( objA, keyA, objB, keyB ) {
const val = get( objB, keyB );
if ( val !== null ) {
set( objA, keyA, val );
}
}
export default Service.extend({
chat: service(),
modal: service(),
settings: service(),
store: service(),
active: null,
model: computed(function() {
const store = get( this, "store" );
return store.peekAll( modelName );
}),
init() {
this._super( ...arguments );
// invalidate cache: listen for all settings changes
// changed properties of model relationships and nested attributes don't trigger isDirty
get( this, "settings" ).on( "didUpdate", clearCache );
},
closeStream( twitchStream ) {
const model = get( this, "model" );
const stream = model.findBy( "stream", twitchStream );
if ( !stream ) { return false; }
// remove from list if the stream has already ended (eg. with an error)
// TODO: refactor this
if ( get( stream, "hasEnded" ) && !get( stream, "isDeleted" ) ) {
stream.destroyStream();
} else {
stream.kill();
}
return true;
},
async startStream( twitchStream, quality ) {
get( this, "modal" ).openModal( "streaming", this, {
error: null,
active: null
});
const store = get( this, "store" );
const channel = get( twitchStream, "channel" );
const id = get( channel, "name" );
let stream;
// is the stream already running?
if ( store.hasRecordForId( modelName, id ) ) {
stream = store.recordForId( modelName, id );
if ( quality !== undefined && get( stream, "quality" ) !== quality ) {
set( stream, "quality", quality );
}
set( this, "active", stream );
this.onModalOpened( stream );
return;
}
// create a new Stream record
stream = store.createRecord( modelName, {
id,
channel,
stream: twitchStream,
quality: get( this, "settings.streaming.quality" ),
chat_open: get( this, "settings.streams.chat_open" ),
started: new Date()
});
this.onModalOpened( stream );
// override record with channel specific settings
await this.getChannelSettings( stream, quality );
await this.launchStream( stream, true );
},
async launchStream( stream, launchChat ) {
// begin the stream launch procedure
try {
set( this, "active", stream );
await logDebug(
"Preparing to launch stream",
() => stream.toJSON({ includeId: true })
);
const settingsStreaming = get( this, "settings.streaming" ).toJSON();
// resolve streaming provider
const providerObj = await resolveProvider(
stream,
settingsStreaming.provider,
settingsStreaming.providers
);
// resolve player
const playerObj = await resolvePlayer(
stream,
settingsStreaming.player,
settingsStreaming.players
);
// launch the stream
await launch(
stream,
providerObj,
playerObj,
() => this.onStreamSuccess( stream, launchChat )
);
} catch ( error ) {
await this.onStreamFailure( stream, error );
} finally {
await this.onStreamEnd( stream );
}
},
onStreamSuccess( stream, launchChat ) {
if ( get( stream, "isWatching" ) ) {
return;
}
set( stream, "isWatching", true );
// setup stream refresh interval
this.refreshStream( stream );
// automatically close modal on success
if ( get( this, "settings.streams.modal_close_launch" ) ) {
this.closeStreamModal( stream );
}
// automatically open chat
if (
// do not open chat on stream restarts
launchChat
// require open chat setting
&& get( stream, "chat_open" )
&& (
// context menu not used
!get( stream, "strictQuality" )
// or context menu setting disabled
|| !get( this, "settings.streams.chat_open_context" )
)
) {
const channel = get( stream, "channel" );
const chat = get( this, "chat" );
chat.openChat( channel )
.catch( () => {} );
}
// hide the GUI
this.minimize( false );
},
onStreamFailure( stream, error ) {
if ( error instanceof Aborted ) {
return;
}
logError( error, () => stream.toJSON({ includeId: true }) );
// clear cache on error
if ( !( error instanceof HostingError ) ) {
clearCache();
}
// show error in modal
set( stream, "error", error );
},
onStreamEnd( stream ) {
if ( get( this, "active" ) === stream ) {
const error = get( stream, "error" );
// close modal of the active stream if it has been enabled in the settings
if ( !error && get( this, "settings.streams.modal_close_end" ) ) {
this.closeStreamModal( stream );
}
} else if ( !get( stream, "isDeleted" ) ) {
const error = get( stream, "error" );
// remove stream from store if modal is not opened
if ( !error || error instanceof ExitSignalError ) {
stream.destroyStream();
}
}
// restore the GUI
this.minimize( true );
},
onModalOpened( stream ) {
get( this, "modal" ).one( "close", () => {
// unset the active property on the next tick (ModalDialogComponent.willDestroyElement)
scheduleOnce( "destroy", () => {
set( this, "active", null );
// remove the record from the store
if ( get( stream, "hasEnded" ) && !get( stream, "isDeleted" ) ) {
stream.destroyStream();
}
});
});
},
closeStreamModal( stream ) {
if ( get( this, "active" ) !== stream ) {
return;
}
get( this, "modal" ).closeModal( this );
},
async getChannelSettings( stream, quality ) {
const channel = get( stream, "channel" );
const channelSettings = await channel.getChannelSettings();
// override channel specific settings
if ( quality === undefined ) {
setIfNotNull( stream, "quality", channelSettings, "streaming_quality" );
set( stream, "strictQuality", false );
} else {
set( stream, "quality", quality );
set( stream, "strictQuality", true );
}
setIfNotNull( stream, "chat_open", channelSettings, "streams_chat_open" );
},
killAll() {
/** @type {Stream[]} */
const model = get( this, "model" );
model.slice().forEach( stream => stream.kill() );
},
minimize( restore ) {
switch ( get( this, "settings.gui.minimize" ) ) {
// minimize
case ATTR_GUI_MINIMIZE_MINIMIZE:
setMinimized( !restore );
break;
// move to tray: toggle window and taskbar visibility
case ATTR_GUI_MINIMIZE_TRAY:
setVisibility( restore );
if ( get( this, "settings.gui.isVisibleInTaskbar" ) ) {
setShowInTaskbar( restore );
}
break;
}
},
refreshStream( stream ) {
if ( stream.isDestroyed || stream.isDeleted ) {
return;
}
const twitchStream = get( stream, "stream" );
const reload = () => twitchStream.reload();
let promise = Promise.reject();
// try to reload the record at least 3 times
for ( let i = 0; i < 3; i++ ) {
promise = promise.catch( reload );
}
// queue another refresh
promise.then( () => {
setTimeout( () => this.refreshStream( stream ), streamReloadInterval );
});
}
});