Skip to content

Instantly share code, notes, and snippets.

@withinboredom
Last active October 25, 2017 01:39
Show Gist options
  • Save withinboredom/9f00394b7066700f504b7a18432221ae to your computer and use it in GitHub Desktop.
Save withinboredom/9f00394b7066700f504b7a18432221ae to your computer and use it in GitHub Desktop.
Event Sourcing
class User extends LiveActor {
constructor( id, injectionContainer ) {
super( id, injectionContainer );
// set initial state for all users
this._state[ 'logged_in' ] = false;
}
/**
* Login a user
*/
DoLoginAttempt( password, browserFingerprint ) {
if ( this._state[ 'too_many_tries' ] ) {
return this.Fire( 'too_many_tries' );
}
if ( this._state[ 'password' ] === password ) {
const token = this.generateToken();
this.Fire( 'logged_in', {
browserFingerprint,
token
} );
return token;
} else {
return this.Fire( 'invalid_login', {
browserFingerprint
} );
}
}
/* Event handlers follow */
/*
* Handle an invalid login attempt
*/
invalid_login( data ) {
// do logic
}
}
await Given( 'A user logs in for the first time', [
{
name: 'signup',
data: {
username: 'test',
password: 'test'
}
}
] ).When( User, 'DoLogin', 'test', {} ).Then( [
{
name: 'logged_in',
data: {
browserFingerprint: {},
token: '{string}'
}
}
] ).AndState( {
logged_in: true,
invalid_password_tries_in_last_period: 0
// etc
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment