replace dispatch with separate methods in chromecast transport

This commit is contained in:
nklhrstv 2020-05-21 12:31:25 +03:00
parent ce17515be3
commit 91cbd2f36a
2 changed files with 38 additions and 56 deletions

View file

@ -40,14 +40,11 @@ const App = () => {
}; };
const onChromecastStateChange = () => { const onChromecastStateChange = () => {
if (services.chromecast.active) { if (services.chromecast.active) {
services.chromecast.transport.dispatch({ services.chromecast.transport.setOptions({
type: 'setOptions', receiverApplicationId: CONSTANTS.CHROMECAST_RECEIVER_APP_ID,
options: { autoJoinPolicy: chrome.cast.AutoJoinPolicy.PAGE_SCOPED,
receiverApplicationId: CONSTANTS.CHROMECAST_RECEIVER_APP_ID, resumeSavedSession: false,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.PAGE_SCOPED, language: null
resumeSavedSession: false,
language: null
}
}); });
} }
}; };

View file

@ -193,55 +193,40 @@ function ChromecastTransport() {
return null; return null;
}; };
this.dispatch = function(action) { this.setOptions = function(options) {
if (action) { try {
switch (action.type) { cast.framework.CastContext.getInstance().setOptions(options);
case 'setOptions': { } catch (error) {
try { events.emit('error', {
cast.framework.CastContext.getInstance().setOptions(action.options); ...CAST_ERROR.INVALID_OPTIONS,
} catch (error) { error
events.emit('error', { });
...CAST_ERROR.INVALID_OPTIONS, }
error };
}); this.requestSession = function() {
} try {
cast.framework.CastContext.getInstance().requestSession()
return; .catch((code) => {
} onCastError(code);
case 'requestSession': { });
try { } catch (error) {
cast.framework.CastContext.getInstance().requestSession() events.emit('error', {
.catch((code) => { ...CAST_ERROR.INVALID_OPTIONS,
onCastError(code); error
}); });
} catch (error) { }
events.emit('error', { };
...CAST_ERROR.INVALID_OPTIONS, this.endCurrentSession = function(stopCasting) {
error cast.framework.CastContext.getInstance().endCurrentSession(stopCasting);
}); };
} this.sendMessage = function(data) {
const castSession = cast.framework.CastContext.getInstance().getCurrentSession();
return; if (castSession !== null) {
} castSession.sendMessage(MESSAGE_NAMESPACE, JSON.stringify(data))
case 'endCurrentSession': { .catch((code) => {
cast.framework.CastContext.getInstance().endCurrentSession(action.stopCasting); onCastError(code);
return; });
}
case 'sendMessage': {
const castSession = cast.framework.CastContext.getInstance().getCurrentSession();
if (castSession !== null) {
castSession.sendMessage(MESSAGE_NAMESPACE, JSON.stringify(action.data))
.catch((code) => {
onCastError(code);
});
}
return;
}
}
} }
throw new Error('Invalid action dispatched: ' + JSON.stringify(action));
}; };
} }