-
Notifications
You must be signed in to change notification settings - Fork 0
Single instance aggregate
Trym Skaar edited this page Feb 24, 2018
·
2 revisions
import {Aggregate} from 'ddbes'
class ApplicationSettings extends Aggregate {
static function reducer(state = {}, event, commit) {
switch (event.type) {
case 'DebugModeEnabled': {
return {
...state,
debugMode: true,
updatedAt: commit.committedAt
}
}
case 'DebugModeDisabled': {
return {
...state,
debugMode: false,
updatedAt: commit.committedAt
}
}
default: return state
}
}
enableDebug() {
return this.commit({type: 'DebugModeEnabled'})
},
disableDebug() {
return this.commit({type: 'DebugModeDisabled'})
}
}
async function testing() {
const settings = await ApplicationSettings.load()
await settings.enableDebug()
settings.state // {debugMode: true, updatedAt: '.....'}
const timeWhenDebugWasEnabled = new Date()
// ... time passes and disableDebug() is run elsewhere
// refresh aggregate instance from snapshots and store
await settings.hydrate()
settings.state.debugMode // false
const settingsAtVersion1 = ApplicationSettings.load({version: 1})
settings.state.debugMode // true
const settingsAtSpecificTime = ApplicationSettings.load({time: timeWhenDebugWasEnabled})
settings.state.debugMode // true
}