Skip to content

Commit

Permalink
FORMATTING
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Jae-Yoon Chung committed May 23, 2019
1 parent fd7bbb1 commit d1a580e
Show file tree
Hide file tree
Showing 20 changed files with 1,036 additions and 859 deletions.
203 changes: 110 additions & 93 deletions examples/actionbank/sleep/src/SleepAction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import xs from 'xstream';
import dropRepeats from 'xstream/extra/dropRepeats';
import {Stream} from 'xstream';
import {TimeSource} from '@cycle/time';
import xs from "xstream";
import dropRepeats from "xstream/extra/dropRepeats";
import { Stream } from "xstream";
import { TimeSource } from "@cycle/time";
import {
GoalID, Goal, Status, GoalStatus, Result,
ActionSources, ActionSinks,
initGoal, generateGoalStatus, isEqualGoalStatus, isEqualGoalID,
} from '@cycle-robot-drivers/action';
GoalID,
Goal,
Status,
GoalStatus,
Result,
ActionSources,
ActionSinks,
initGoal,
generateGoalStatus,
isEqualGoalStatus,
isEqualGoalID
} from "@cycle-robot-drivers/action";
// import {
// GoalID, Goal, Status, GoalStatus, Result,
// ActionSources, ActionSinks,
Expand All @@ -15,146 +23,158 @@ import {
// initGoal, generateGoalStatus, isEqualGoalStatus, isEqualGoalID,
// } from './utils';


enum State {
RUN = 'RUN',
WAIT = 'WAIT',
RUN = "RUN",
WAIT = "WAIT"
}

type Variables = {
goal_id: GoalID,
goal_id: GoalID;
};

type SleepInput = {
goal_id: GoalID,
duration: number,
}
goal_id: GoalID;
duration: number;
};

type SleepOutput = {
goal_id: GoalID,
targetTime: number,
}
goal_id: GoalID;
targetTime: number;
};

type Outputs = {
Time?: SleepInput,
result?: Result,
Time?: SleepInput;
result?: Result;
};

type ReducerState = {
state: State,
variables: Variables,
outputs: Outputs,
state: State;
variables: Variables;
outputs: Outputs;
};

type Reducer = (prev?: ReducerState) => ReducerState | undefined;

enum InputType {
GOAL = 'GOAL',
CANCEL = 'CANCEL',
SLEEP_DONE = 'SLEEP_DONE',
GOAL = "GOAL",
CANCEL = "CANCEL",
SLEEP_DONE = "SLEEP_DONE"
}

type Input = {
type: InputType,
value: Goal | GoalID | SleepOutput,
type: InputType;
value: Goal | GoalID | SleepOutput;
};

function input(
goal$: Stream<Goal | number>,
cancel$: Stream<GoalID>,
sleepDone$: Stream<SleepOutput>,
sleepDone$: Stream<SleepOutput>
) {
return xs.merge(
goal$.filter(g => typeof g !== 'undefined' && g !== null).map(g => {
const goal: Goal = initGoal(g);
return {
type: InputType.GOAL,
value: typeof goal.goal === 'number'
? {
goal_id: goal.goal_id,
goal: {goal_id: goal.goal_id, duration: goal.goal},
} : goal,
};
}),
cancel$.map(val => ({type: InputType.CANCEL, value: val})),
sleepDone$.map(val => ({type: InputType.SLEEP_DONE, value: val})),
goal$
.filter(g => typeof g !== "undefined" && g !== null)
.map(g => {
const goal: Goal = initGoal(g);
return {
type: InputType.GOAL,
value:
typeof goal.goal === "number"
? {
goal_id: goal.goal_id,
goal: { goal_id: goal.goal_id, duration: goal.goal }
}
: goal
};
}),
cancel$.map(val => ({ type: InputType.CANCEL, value: val })),
sleepDone$.map(val => ({ type: InputType.SLEEP_DONE, value: val }))
);
}

function transition(prev: ReducerState, input: Input): ReducerState {
if (prev.state === State.WAIT) {
if (input.type === InputType.GOAL) {
const goal = (input.value as Goal);
const goal = input.value as Goal;
return {
...prev,
state: State.RUN,
variables: {
goal_id: goal.goal_id,
goal_id: goal.goal_id
},
outputs: {
Time: goal.goal,
},
Time: goal.goal
}
};
}
} else if (prev.state === State.RUN) {
if (input.type === InputType.GOAL || input.type === InputType.CANCEL
&& (input.value === null ||
isEqualGoalID(input.value as GoalID, prev.variables.goal_id))) {
if (
input.type === InputType.GOAL ||
(input.type === InputType.CANCEL &&
(input.value === null ||
isEqualGoalID(input.value as GoalID, prev.variables.goal_id)))
) {
return {
...prev,
state: input.type === InputType.GOAL ? State.RUN : State.WAIT,
variables: {
goal_id: input.type === InputType.GOAL ?
(input.value as Goal).goal_id : prev.variables.goal_id,
goal_id:
input.type === InputType.GOAL
? (input.value as Goal).goal_id
: prev.variables.goal_id
},
outputs: {
Time: (input.value as Goal).goal,
},
Time: (input.value as Goal).goal
}
};
} else if (
input.type === InputType.SLEEP_DONE
&& isEqualGoalID(
(input.value as SleepOutput).goal_id, prev.variables.goal_id)
input.type === InputType.SLEEP_DONE &&
isEqualGoalID(
(input.value as SleepOutput).goal_id,
prev.variables.goal_id
)
) {
return {
...prev,
state: State.WAIT,
variables: {
goal_id: null,
goal_id: null
},
outputs: {
result: {
status: {
goal_id: prev.variables.goal_id,
status: Status.SUCCEEDED,
status: Status.SUCCEEDED
},
result: input.value,
},
},
result: input.value
}
}
};
}
}
return prev;
}

function transitionReducer(input$: Stream<Input>): Stream<Reducer> {
const initReducer$: Stream<Reducer> = xs.of(
function initReducer(prev: ReducerState): ReducerState {
return {
state: State.WAIT,
variables: {
goal_id: null,
},
outputs: null,
const initReducer$: Stream<Reducer> = xs.of(function initReducer(
prev: ReducerState
): ReducerState {
return {
state: State.WAIT,
variables: {
goal_id: null
},
outputs: null
};
});

const inputReducer$: Stream<Reducer> = input$.map(
input =>
function inputReducer(prev: ReducerState): ReducerState {
return transition(prev, input);
}
}
);

const inputReducer$: Stream<Reducer> = input$
.map(input => function inputReducer(prev: ReducerState): ReducerState {
return transition(prev, input);
});

return xs.merge(initReducer$, inputReducer$);
}

Expand All @@ -164,9 +184,10 @@ function status(reducerState$): Stream<GoalStatus> {
.map(rs => rs.outputs.result.status);
const active$: Stream<GoalStatus> = reducerState$
.filter(rs => rs.state === State.RUN)
.map(rs => ({goal_id: rs.variables.goal_id, status: Status.ACTIVE}));
const initGoalStatus = generateGoalStatus({status: Status.SUCCEEDED});
return xs.merge(done$, active$)
.map(rs => ({ goal_id: rs.variables.goal_id, status: Status.ACTIVE }));
const initGoalStatus = generateGoalStatus({ status: Status.SUCCEEDED });
return xs
.merge(done$, active$)
.compose(dropRepeats(isEqualGoalStatus))
.startWith(initGoalStatus);
}
Expand All @@ -176,17 +197,13 @@ function output(reducerState$) {
.filter(rs => !!rs.outputs)
.map(rs => rs.outputs);
return {
result: outputs$
.filter(o => !!o.result)
.map(o => o.result),
Time: outputs$
.filter(o => typeof o.Time !== 'undefined')
.map(o => o.Time),
result: outputs$.filter(o => !!o.result).map(o => o.result),
Time: outputs$.filter(o => typeof o.Time !== "undefined").map(o => o.Time)
};
};
}

export interface Sources extends ActionSources {
Time: TimeSource,
Time: TimeSource;
}

function sleep(timeSource) {
Expand All @@ -195,13 +212,13 @@ function sleep(timeSource) {

return xs.create({
start(listener) {
const {schedule, currentTime} = timeSource.createOperator();
const { schedule, currentTime } = timeSource.createOperator();

sourceListener = stream.addListener({
next({goal_id, duration}) {
next({ goal_id, duration }) {
schedule.next(listener, currentTime() + duration, {
goal_id,
targetTime: currentTime() + duration,
targetTime: currentTime() + duration
});
},

Expand All @@ -211,7 +228,7 @@ function sleep(timeSource) {

complete() {
schedule.complete(listener, currentTime());
},
}
});
},

Expand All @@ -220,7 +237,7 @@ function sleep(timeSource) {
}
});
};
};
}

export function SleepAction(sources: Sources): ActionSinks {
const status$ = status(sources.state.stream);
Expand All @@ -229,7 +246,7 @@ export function SleepAction(sources: Sources): ActionSinks {
const input$ = input(
sources.goal || xs.never(),
sources.cancel || xs.never(),
outputs.Time.compose(sleep(sources.Time)),
outputs.Time.compose(sleep(sources.Time))
);
const reducer = transitionReducer(input$);

Expand All @@ -238,4 +255,4 @@ export function SleepAction(sources: Sources): ActionSinks {
status: status$,
...outputs
};
}
}
31 changes: 16 additions & 15 deletions examples/actionbank/sleep/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import xs from 'xstream';
import delay from 'xstream/extra/delay';
import {withState} from '@cycle/state';
import {run} from '@cycle/run';
import {timeDriver} from '@cycle/time';
import {SleepAction} from './SleepAction';

import xs from "xstream";
import delay from "xstream/extra/delay";
import { withState } from "@cycle/state";
import { run } from "@cycle/run";
import { timeDriver } from "@cycle/time";
import { SleepAction } from "./SleepAction";

function main(sources) {
sources.state.stream.addListener({next: s => console.debug('reducer state', s)});
sources.state.stream.addListener({
next: s => console.debug("reducer state", s)
});

const sleepAction = SleepAction({
state: sources.state,
goal: xs.of(1000).compose(delay(1000)) as any,
Time: sources.Time,
Time: sources.Time
});

return {
state: sleepAction.state,
}
};
return {
state: sleepAction.state
};
}

run(withState(main), {
Time: timeDriver,
});
Time: timeDriver
});
Loading

0 comments on commit d1a580e

Please sign in to comment.