Skip to content

Commit

Permalink
Implement strict event trigger update with eventually consistence (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhostVlad authored Nov 6, 2018
1 parent 83d8b31 commit 3e8d806
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ const buildProjection = (
}

await inputProjection[eventType](storeApi, event)
await metaApi.setLastTimestamp(event.timestamp)

if (!maybeUnordered) {
await metaApi.setLastTimestamp(event.timestamp)
}

await metaApi.setLastAggregateVersion(
event.aggregateId,
Expand Down
9 changes: 8 additions & 1 deletion packages/core/resolve-query/src/read-model/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const read = async (repository, { resolverName, resolverArgs, jwtToken }) => {
)
}

const store = await repository.getModelReadInterface(repository, true)
const getModelReadInterface = repository.getModelReadInterface.bind(
null,
repository
)

const store = await getModelReadInterface(true)

getModelReadInterface(false).catch(() => null)

return await resolver(store, resolverArgs, jwtToken)
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/resolve-query/src/view-model/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const eventHandler = async (
}

viewModel.state = projection[event.type](viewModel.state, event)
viewModel.lastTimestamp = event.timestamp - 1

if (!maybeUnordered) {
viewModel.lastTimestamp = event.timestamp - 1
}

viewModel.aggregatesVersionsMap.set(
event.aggregateId,
Expand Down
11 changes: 10 additions & 1 deletion packages/core/resolve-query/src/view-model/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ const read = async (repository, { aggregateIds } = {}) => {
)
}

const getViewModel = repository.getViewModel.bind(
null,
repository,
aggregateIds
)

try {
const viewModel = repository.getViewModel(repository, aggregateIds, true)
const viewModel = getViewModel(true)
await viewModel.initPromise

getViewModel(false).initPromise.catch(() => null)

return viewModel.state
} catch (error) {
return null
Expand Down
2 changes: 1 addition & 1 deletion packages/core/resolve-query/test/read-model/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('Read-model read should retrieve store and invoke resolver', async () => {
jwtToken
})

expect(repository.getModelReadInterface.callCount).toEqual(1)
expect(repository.getModelReadInterface.callCount).toEqual(2)

expect(repository.resolvers.RESOLVER_NAME.callCount).toEqual(1)
expect(repository.resolvers.RESOLVER_NAME.firstCall.args[0]).toEqual(store)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/resolve-query/test/view-model/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('View-model read should read view model with success returns state', async

const result = await read(repository, { aggregateIds })

expect(repository.getViewModel.callCount).toEqual(1)
expect(repository.getViewModel.callCount).toEqual(2)
expect(repository.getViewModel.firstCall.args[0]).toEqual(repository)
expect(repository.getViewModel.firstCall.args[1]).toEqual(aggregateIds)
expect(repository.getViewModel.firstCall.args[2]).toEqual(true)
Expand Down

0 comments on commit 3e8d806

Please sign in to comment.