Skip to content

Instantly share code, notes, and snippets.

@DrFelder

DrFelder/wow.js Secret

Last active August 23, 2018 08:13
Show Gist options
  • Save DrFelder/122a72ffed3eb239a1a3ae33c99ea00d to your computer and use it in GitHub Desktop.
Save DrFelder/122a72ffed3eb239a1a3ae33c99ea00d to your computer and use it in GitHub Desktop.
Stateful flow that collects information about the presence of users
'use strict';
const identity = {
'usermgmt.user.presentSwitched': event => event.user.id,
'usermgmt.user.pauseSwitched': event => event.user.id
};
const initialState = {
is: 'pristine',
present: false,
pause: false,
presentSince: null,
presentUntil: null,
pauses: [],
};
const transitions = {
pristine: {
'usermgmt.user.presentSwitched' (flow, event) {
flow.setState({
present: true,
presentSince: event.data.timestamp
});
flow.transitionTo('present');
}
},
present: {
'usermgmt.user.presentSwitched' (flow, event) {
flow.setState({
present: false,
presentUntil: event.data.timestamp
});
flow.transitionTo('calculating');
},
'usermgmt.user.pauseSwitched' (flow, event) {
const newPause = {pauseSince: event.data.timestamp};
flow.setState({
pause: true,
pauses: [...flow.state.pauses, newPause]
});
flow.transitionTo('pause');
}
},
pause: {
'usermgmt.user.pauseSwitched' (flow, event) {
const pauses = Object.create(flow.state.pauses);
pauses[flow.state.pauses.length - 1].pauseUntil = event.data.timestamp;
flow.setState({
pause: false,
pauseSince: event.data.timestamp,
pauses
});
flow.transitionTo('present');
}
}
};
const reactions = {
present: {
'calculating' (flow, event, services) {
const {app, logger} = services;
const workDayId = 'kek';
const from = flow.state.presentSince;
const to = flow.state.presentUntil;
const pauses = flow.state.pauses;
logger.info(JSON.stringify(flow));
app.keksing.recording().createRecording({workDayId, from, to, type: 'working'});
flow.setState({
present: false,
pause: false,
presentSince: null,
presentUntil: null,
pauses: [],
});
flow.transitionTo('pristine');
}
}
};
module.exports = { identity, initialState, transitions, reactions };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment