Skip to content

Instantly share code, notes, and snippets.

@willgriffiths
Created July 24, 2019 22:48
Show Gist options
  • Save willgriffiths/f2b11d7f41349b7ef5f4892a730f0f5f to your computer and use it in GitHub Desktop.
Save willgriffiths/f2b11d7f41349b7ef5f4892a730f0f5f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const playerMachine = Machine(
{
id: "player",
initial: "idle",
context: {
duration: 3,
currentTime: 0,
refreshInterval: 333
},
on: {
START_SEEKING: "seeking",
SEEK_TO: {
target: "idle.hist",
actions: "seekTo"
}
},
states: {
idle: {
initial: "paused",
states: {
paused: {
on: {
PLAY: "playing"
}
},
playing: {
entry: send("UPDATE_CURRENT_TIME", {
delay: (ctx, e) => ctx.refreshInterval
}),
on: {
UPDATE_CURRENT_TIME: [
{ target: "playing", actions: "updateCurrentTime", cond: "isNotFinished" },
{ target: "#player.ended", actions: "endPlayback" }
],
PAUSE: "paused"
}
},
hist: {
type: "history"
}
}
},
ended: "ended",
seeking: {
on: {
STOP_SEEKING: {
target: "idle.hist",
actions: "seekTo"
}
}
}
}
},
{
actions: {
seekTo: assign({
currentTime: (ctx, e) =>
(e.seekTime > 0 && e.seekTime < ctx.duration && e.seekTime) || undefined
}),
updateCurrentTime: assign({
currentTime: (ctx, e) => ctx.currentTime + ctx.refreshInterval / 1000
}),
endPlayback: assign({
currentTime: (ctx, e) => ctx.duration
})
},
guards: {
isNotFinished: (ctx, e) => ctx.currentTime + ctx.refreshInterval / 1000 < ctx.duration
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment