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 = () => {
if (services.chromecast.active) {
services.chromecast.transport.dispatch({
type: 'setOptions',
options: {
receiverApplicationId: CONSTANTS.CHROMECAST_RECEIVER_APP_ID,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.PAGE_SCOPED,
resumeSavedSession: false,
language: null
}
services.chromecast.transport.setOptions({
receiverApplicationId: CONSTANTS.CHROMECAST_RECEIVER_APP_ID,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.PAGE_SCOPED,
resumeSavedSession: false,
language: null
});
}
};

View file

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