Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport: safeguard against possibly unset transport state and gracefully fallback to defaults #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/actions/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getShowTransportControl, getTransportControlState } from "../selectors/
import { clamp } from "../lib/util";
import { BPMRange } from "../lib/constants";

export type PartialTransportStatus = Partial<{ bpm: number; rolling: boolean; sync: boolean; }>;

export enum TransportActionType {
INIT = "INIT_TRANSPORT",
SET_SHOW_TRANSPORT_CONTROL = "SET_SHOW_TRANSPORT_CONTROL",
Expand All @@ -14,11 +16,7 @@ export enum TransportActionType {

export interface IInitTransport extends ActionBase {
type: TransportActionType.INIT;
payload: {
bpm: number;
rolling: boolean;
sync: boolean;
};
payload: PartialTransportStatus;
}

export interface ISetShowTransportControl extends ActionBase {
Expand All @@ -30,7 +28,7 @@ export interface ISetShowTransportControl extends ActionBase {

export interface IUpdateTransport extends ActionBase {
type: TransportActionType.UPDATE_TRANSPORT,
payload: Partial<IInitTransport["payload"]>;
payload: PartialTransportStatus;
}

export type TransportAction = IInitTransport | ISetShowTransportControl | IUpdateTransport;
Expand Down Expand Up @@ -61,13 +59,13 @@ export const toggleTransportControl = () : AppThunk =>
dispatch({ type: TransportActionType.SET_SHOW_TRANSPORT_CONTROL, payload: { show: !isShown } });
};

export const initTransport = (info: OSCQueryRNBOJackTransport) => {
export const initTransport = (info?: OSCQueryRNBOJackTransport) => {
return {
type: TransportActionType.INIT,
payload: {
bpm: info.CONTENTS?.bpm?.VALUE || 100,
rolling: info.CONTENTS?.rolling?.TYPE === OSCQueryValueType.True || false,
sync: info.CONTENTS?.sync?.TYPE === OSCQueryValueType.True || false
bpm: info?.CONTENTS?.bpm?.VALUE,
rolling: info?.CONTENTS?.rolling?.TYPE === OSCQueryValueType.True || false,
sync: info?.CONTENTS?.sync?.TYPE === OSCQueryValueType.True || false
}
};
};
Expand Down Expand Up @@ -134,7 +132,7 @@ export const decrementTransportBPMOnRemote = (scale: number = 1): AppThunk =>
dispatch(setTransportBPMOnRemote(bpm));
};

export const updateTransportStatus = (status: Partial<{ bpm: number; rolling: boolean; sync: boolean; }>): TransportAction => {
export const updateTransportStatus = (status: PartialTransportStatus): TransportAction => {
return {
type: TransportActionType.UPDATE_TRANSPORT,
payload: status
Expand Down
2 changes: 1 addition & 1 deletion src/controller/oscqueryBridgeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class OSCQueryBridgeControllerPrivate {
dispatch(initRunnerConfig(state));

// Init Transport
dispatch(initTransport(state.CONTENTS.jack.CONTENTS.transport));
dispatch(initTransport(state.CONTENTS.jack.CONTENTS?.transport));

// Init Patcher Info
dispatch(initPatchers(state.CONTENTS.patchers));
Expand Down
22 changes: 16 additions & 6 deletions src/reducers/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ export interface TransportState {
show: boolean;
}

export const transport = (state: TransportState = {
const transportDefaults = {
bpm: 100,
rolling: false,
sync: true,
sync: true
};

export const transport = (state: TransportState = {
bpm: transportDefaults.bpm,
rolling: transportDefaults.rolling,
sync: transportDefaults.sync,
show: false

}, action: TransportAction): TransportState => {
Expand All @@ -22,16 +28,20 @@ export const transport = (state: TransportState = {

return {
...state,
bpm,
rolling,
sync
bpm: bpm || transportDefaults.bpm,
rolling: rolling || transportDefaults.rolling,
sync: sync || transportDefaults.sync
};
}

case TransportActionType.UPDATE_TRANSPORT: {
const { bpm, rolling, sync } = action.payload;

return {
...state,
...action.payload
bpm: bpm === undefined ? state.bpm : bpm,
rolling: rolling === undefined ? state.rolling : rolling,
sync: sync === undefined ? state.sync : sync
};
}

Expand Down
Loading