@reventlessdev/reventless-core 3.0.0-alpha.325 → 3.0.0-alpha.327
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/package.json +7 -7
- package/src/Message.res +46 -0
- package/src/Message.res.mjs +45 -0
- package/src/Projection.res +269 -131
- package/src/Projection.res.mjs +338 -224
- package/src/ViewChange.res +194 -0
- package/src/ViewChange.res.mjs +235 -0
- package/src/adapter/Runtime/AggregateRuntime_Builder_Common.res.mjs +5 -5
- package/src/adapter/Runtime/EventCollectorRuntime_Builder_Single.res.mjs +2 -2
- package/src/admin/Platform_Admin_Callback.res.mjs +1 -1
- package/src/components/Aggregate/Aggregate_Callback.res +93 -18
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +54 -32
- package/src/components/AutomationSlice/AutomationSlice_Builder.res.mjs +1 -1
- package/src/components/AutomationSlice/AutomationSlice_Callback.res +29 -9
- package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +35 -20
- package/src/components/CommandGenerator/CommandGenerator_Callback.res +47 -6
- package/src/components/CommandGenerator/CommandGenerator_Callback.res.mjs +41 -4
- package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
- package/src/components/CommandTopic/CommandTopic_Callback.res.mjs +1 -1
- package/src/components/CommandTopic/CommandTopic_Helpers.res +64 -26
- package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +37 -17
- package/src/components/CommandTopic/CommandTopic_Operations.res +2 -0
- package/src/components/CommandTopic/CommandTopic_Operations.res.mjs +2 -2
- package/src/components/Counter/Counter_Callback.res.mjs +3 -3
- package/src/components/Counter/Counter_Operations.res.mjs +4 -4
- package/src/components/DcbEventLog/DcbEventLog_Operations.res.mjs +3 -3
- package/src/components/EventLog/EventLog_Operations.res +1 -0
- package/src/components/EventLog/EventLog_Operations.res.mjs +3 -3
- package/src/components/EventMapper/EventMapper_Callback.res +4 -0
- package/src/components/EventMapper/EventMapper_Callback.res.mjs +13 -12
- package/src/components/EventTopic/EventTopic_Operations.res +2 -0
- package/src/components/EventTopic/EventTopic_Operations.res.mjs +2 -2
- package/src/components/Extension/Extension_Operations.res +4 -0
- package/src/components/Extension/Extension_Operations.res.mjs +8 -8
- package/src/components/ExtensionPoint/ExtensionPoint_Callback.res.mjs +2 -2
- package/src/components/ExtensionPoint/ExtensionPoint_Operations.res +2 -0
- package/src/components/ExtensionPoint/ExtensionPoint_Operations.res.mjs +9 -8
- package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res.mjs +1 -1
- package/src/components/InboundTranslationSlice/InboundTranslationSlice_Webhook.res.mjs +1 -1
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +7 -3
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +33 -12
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +30 -17
- package/src/components/ReadModel/ReadModel_Callback.res +24 -3
- package/src/components/ReadModel/ReadModel_Callback.res.mjs +15 -3
- package/src/components/SideEffectHandler/SideEffectHandler_Callback.res +2 -0
- package/src/components/SideEffectHandler/SideEffectHandler_Callback.res.mjs +6 -6
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +43 -7
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +38 -20
- package/src/components/StateViewSlice/StateViewSlice_Builder.res +16 -2
- package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +15 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res +35 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +12 -4
- package/src/plugin/component/Plugin_Callback.res.mjs +2 -2
- package/src/plugin/connect/PluginExtensionPoint_Plugin.res.mjs +6 -6
- package/src/util/CommandPublisher.res.mjs +9 -9
- package/src/util/EffectLogger.res +111 -42
- package/src/util/EffectLogger.res.mjs +72 -30
- package/src/util/FTPHandler.res.mjs +6 -6
- package/src/util/Logger.res +231 -35
- package/src/util/Logger.res.mjs +263 -42
- package/src/util/ScheduleOps.res.mjs +4 -4
- package/src/util/Util_AdapterRuntime.res.mjs +2 -2
- package/src/util/Util_Promise.res.mjs +1 -1
- package/src/util/Util_QueryDbRuntime.res.mjs +1 -1
- package/src/util/Validation.res.mjs +1 -1
- package/tests/commandgenerator/OwnerActingTest.res +3 -1
- package/tests/commandgenerator/OwnerStampingTest.res +65 -0
- package/tests/commandgenerator/OwnerStampingTest.res.mjs +62 -0
- package/tests/commandtopic/CommandOutcomeHookTest.res +99 -2
- package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +120 -0
- package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
- package/tests/logger/ComponentLineIdsTest.res +97 -0
- package/tests/logger/ComponentLineIdsTest.res.mjs +118 -0
- package/tests/logger/LogFormatTest.res.mjs +5 -5
- package/tests/logger/LoggerSinkTest.res +205 -0
- package/tests/logger/LoggerSinkTest.res.mjs +209 -0
- package/tests/projection/ViewChangeTest.res +173 -0
- package/tests/projection/ViewChangeTest.res.mjs +292 -0
package/src/Projection.res
CHANGED
|
@@ -181,6 +181,44 @@ let rewriteJsonAction = (
|
|
|
181
181
|
)
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
+
// ── What an applied action changed ─────────────────────────────────────────
|
|
185
|
+
// A caller that wants to know which rows an action changed passes `~onChange`;
|
|
186
|
+
// it is called once per row written or removed, after the store said yes.
|
|
187
|
+
|
|
188
|
+
type changeKind =
|
|
189
|
+
| Created
|
|
190
|
+
| Changed
|
|
191
|
+
| Removed
|
|
192
|
+
|
|
193
|
+
/** An event that led to a change: its message id, its flow, and the entity it
|
|
194
|
+
is about (an aggregate's id and name, or a DCB event's tag value and key). */
|
|
195
|
+
type changeSource = {
|
|
196
|
+
msgId: string,
|
|
197
|
+
correlationId?: string,
|
|
198
|
+
entityId?: string,
|
|
199
|
+
entityType?: string,
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** One row a projection created, changed or removed. `before` is there where the
|
|
203
|
+
row was loaded first; `Set` and `Delete` write without loading, so theirs is
|
|
204
|
+
absent. `sources` are the events whose actions were applied to the row, all of
|
|
205
|
+
them when several actions on it were merged. */
|
|
206
|
+
type rowChange<'state> = {
|
|
207
|
+
key: string,
|
|
208
|
+
subId?: string,
|
|
209
|
+
kind: changeKind,
|
|
210
|
+
before?: 'state,
|
|
211
|
+
after?: 'state,
|
|
212
|
+
sources: array<changeSource>,
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** An action with the events it came from. */
|
|
216
|
+
type sourced<'id, 'state> = {action: action<'id, 'state>, sources: array<changeSource>}
|
|
217
|
+
|
|
218
|
+
/** Both lists, each source once. */
|
|
219
|
+
let mergeSources = (a: array<changeSource>, b: array<changeSource>): array<changeSource> =>
|
|
220
|
+
a->Array.concat(b->Array.filter(s => !(a->Array.some(other => other.msgId == s.msgId))))
|
|
221
|
+
|
|
184
222
|
let applyChanges = async (
|
|
185
223
|
action,
|
|
186
224
|
id,
|
|
@@ -188,6 +226,12 @@ let applyChanges = async (
|
|
|
188
226
|
afterStates,
|
|
189
227
|
{QueryDb.saveBatch: saveBatch, deleteBatch},
|
|
190
228
|
{subIdField, getSubId},
|
|
229
|
+
~onRow: (~subId: string, ~before: option<'state>, ~after: option<'state>, changeKind) => unit=(
|
|
230
|
+
~subId as _,
|
|
231
|
+
~before as _,
|
|
232
|
+
~after as _,
|
|
233
|
+
_,
|
|
234
|
+
) => (),
|
|
191
235
|
) => {
|
|
192
236
|
let beforeCount = beforeStates->Array.length
|
|
193
237
|
let beforeSubIds = beforeStates->Array.map(state => state->getSubId)->Set.fromArray
|
|
@@ -230,7 +274,22 @@ let applyChanges = async (
|
|
|
230
274
|
}
|
|
231
275
|
) {
|
|
232
276
|
| Some(err) => err
|
|
233
|
-
| None =>
|
|
277
|
+
| None =>
|
|
278
|
+
let beforeBySubId = Dict.make()
|
|
279
|
+
beforeStates->Array.forEach(before => beforeBySubId->Dict.set(before->getSubId, before))
|
|
280
|
+
afterStates->Array.forEach(after => {
|
|
281
|
+
let subId = after->getSubId
|
|
282
|
+
switch beforeBySubId->Dict.get(subId) {
|
|
283
|
+
| None => onRow(~subId, ~before=None, ~after=Some(after), Created)
|
|
284
|
+
| Some(before) if before != after =>
|
|
285
|
+
onRow(~subId, ~before=Some(before), ~after=Some(after), Changed)
|
|
286
|
+
| Some(_) => ()
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
deletedSubIds->Array.forEach(subId =>
|
|
290
|
+
onRow(~subId, ~before=beforeBySubId->Dict.get(subId), ~after=None, Removed)
|
|
291
|
+
)
|
|
292
|
+
Ok()
|
|
234
293
|
}
|
|
235
294
|
}
|
|
236
295
|
|
|
@@ -246,10 +305,35 @@ let statesToString: array<'a> => string = states =>
|
|
|
246
305
|
|
|
247
306
|
let handleAction = async (
|
|
248
307
|
~comp="Projection",
|
|
308
|
+
~sources: array<changeSource>=[],
|
|
309
|
+
~onChange: option<rowChange<'state> => unit>=?,
|
|
249
310
|
action,
|
|
250
311
|
{QueryDb.loadStream: loadStream, save, saveBatch, delete, deleteBatch} as operations,
|
|
251
312
|
subIdConfig,
|
|
252
313
|
) => {
|
|
314
|
+
// A listener that throws is noted and skipped: the row is already written.
|
|
315
|
+
let report = (~key, ~subId=?, ~before=?, ~after=?, kind) =>
|
|
316
|
+
onChange->Option.forEach(f =>
|
|
317
|
+
try f({key, ?subId, kind, ?before, ?after, sources}) catch {
|
|
318
|
+
| exn =>
|
|
319
|
+
log.warn(
|
|
320
|
+
~comp,
|
|
321
|
+
`a view-change listener failed: ${exn
|
|
322
|
+
->JsExn.fromException
|
|
323
|
+
->Option.flatMap(JsExn.message)
|
|
324
|
+
->Option.getOr("unknown")}`,
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
)
|
|
328
|
+
let subIdOf = state => subIdConfig->Option.map(({getSubId}) => getSubId(state))
|
|
329
|
+
// Reports only what the store accepted.
|
|
330
|
+
let after = (result, reportAll) => {
|
|
331
|
+
switch (result, onChange) {
|
|
332
|
+
| (Ok(), Some(_)) => reportAll()
|
|
333
|
+
| _ => ()
|
|
334
|
+
}
|
|
335
|
+
result
|
|
336
|
+
}
|
|
253
337
|
// Log each applied action at Debug with the full state it creates/changes,
|
|
254
338
|
// attributed to the owning component. Debug-lazy so the state is only
|
|
255
339
|
// serialized when debug logging is enabled; shadows the module-level
|
|
@@ -275,19 +359,21 @@ let handleAction = async (
|
|
|
275
359
|
|
|
276
360
|
| Create(id, state) =>
|
|
277
361
|
logAction(() => `Create(${id}, ${state->stateToString})`)
|
|
278
|
-
await save(id, state, Init, None)
|
|
362
|
+
(await save(id, state, Init, None))->after(() => report(~key=id, ~after=state, Created))
|
|
279
363
|
| CreateMultiState(id, states) =>
|
|
280
364
|
logAction(() =>
|
|
281
365
|
`CreateMultiState(${id}, ${states
|
|
282
366
|
->Array.map(state => state->stateToString)
|
|
283
367
|
->Array.joinUnsafe(", ")})`
|
|
284
368
|
)
|
|
369
|
+
let reportAll = () =>
|
|
370
|
+
states->Array.forEach(state => report(~key=id, ~subId=?subIdOf(state), ~after=state, Created))
|
|
285
371
|
switch states {
|
|
286
372
|
| [] => Ok()
|
|
287
|
-
| [state] => await save(id, state, Init, None)
|
|
373
|
+
| [state] => (await save(id, state, Init, None))->after(reportAll)
|
|
288
374
|
| states =>
|
|
289
375
|
let batch = states->Array.map(state => (id, state, None))
|
|
290
|
-
await saveBatch(batch)
|
|
376
|
+
(await saveBatch(batch))->after(reportAll)
|
|
291
377
|
}
|
|
292
378
|
| CreateMany(states) =>
|
|
293
379
|
let batch = states->Array.map(((id, state)) => (id, state, None))
|
|
@@ -296,11 +382,14 @@ let handleAction = async (
|
|
|
296
382
|
->Array.map(((id, state, _)) => `(${id},${state->stateToString})`)
|
|
297
383
|
->Array.joinUnsafe(", ")})`
|
|
298
384
|
)
|
|
299
|
-
|
|
385
|
+
// TODO: think about using single saves with saveMode Init
|
|
386
|
+
(await saveBatch(batch))->after(() =>
|
|
387
|
+
batch->Array.forEach(((id, state, _)) => report(~key=id, ~after=state, Created))
|
|
388
|
+
)
|
|
300
389
|
|
|
301
390
|
| Set(id, state) =>
|
|
302
391
|
logAction(() => `Set(${id}, ${state->stateToString})`)
|
|
303
|
-
await save(id, state, Any, None)
|
|
392
|
+
(await save(id, state, Any, None))->after(() => report(~key=id, ~after=state, Changed))
|
|
304
393
|
| SetMany(ids, set) =>
|
|
305
394
|
let batch = ids->Array.map(id => (id, set(id), None))
|
|
306
395
|
logAction(() =>
|
|
@@ -308,7 +397,9 @@ let handleAction = async (
|
|
|
308
397
|
->Array.map(((id, state, _)) => `(${id},${state->stateToString})`)
|
|
309
398
|
->Array.joinUnsafe(", ")})`
|
|
310
399
|
)
|
|
311
|
-
await saveBatch(batch)
|
|
400
|
+
(await saveBatch(batch))->after(() =>
|
|
401
|
+
batch->Array.forEach(((id, state, _)) => report(~key=id, ~after=state, Changed))
|
|
402
|
+
)
|
|
312
403
|
|
|
313
404
|
| Update(id, update) =>
|
|
314
405
|
switch await loadAtMost(2, id) {
|
|
@@ -320,7 +411,11 @@ let handleAction = async (
|
|
|
320
411
|
| [oldState] =>
|
|
321
412
|
let newState = oldState->update
|
|
322
413
|
logAction(() => `Update(${id}, ${oldState->stateToString} => ${newState->stateToString})`)
|
|
323
|
-
await save(id, newState, Overwrite, None)
|
|
414
|
+
(await save(id, newState, Overwrite, None))->after(() =>
|
|
415
|
+
if newState != oldState {
|
|
416
|
+
report(~key=id, ~before=oldState, ~after=newState, Changed)
|
|
417
|
+
}
|
|
418
|
+
)
|
|
324
419
|
| _ =>
|
|
325
420
|
logAction(() => `Update Error: Multiple oldStates for ${id})`)
|
|
326
421
|
Error(ReventlessInfra.QueryDb.StaleState)
|
|
@@ -333,13 +428,17 @@ let handleAction = async (
|
|
|
333
428
|
switch states {
|
|
334
429
|
| [] =>
|
|
335
430
|
logAction(() => `UpdateWithDefault(${id}, default: ${default->stateToString})`)
|
|
336
|
-
await save(id, default, Init, None)
|
|
431
|
+
(await save(id, default, Init, None))->after(() => report(~key=id, ~after=default, Created))
|
|
337
432
|
| [oldState] =>
|
|
338
433
|
let newState = oldState->update
|
|
339
434
|
logAction(() =>
|
|
340
435
|
`UpdateWithDefault(${id}, ${oldState->stateToString} => ${newState->stateToString})`
|
|
341
436
|
)
|
|
342
|
-
await save(id, newState, Overwrite, None)
|
|
437
|
+
(await save(id, newState, Overwrite, None))->after(() =>
|
|
438
|
+
if newState != oldState {
|
|
439
|
+
report(~key=id, ~before=oldState, ~after=newState, Changed)
|
|
440
|
+
}
|
|
441
|
+
)
|
|
343
442
|
| _ =>
|
|
344
443
|
logAction(() => `UpdateWithDefault Error: Multiple oldStates for ${id})`)
|
|
345
444
|
Error(ReventlessInfra.QueryDb.StaleState)
|
|
@@ -355,7 +454,15 @@ let handleAction = async (
|
|
|
355
454
|
| (Ok(states), Some(subIdConfig)) =>
|
|
356
455
|
let beforeStates = states
|
|
357
456
|
let afterStates = beforeStates->update
|
|
358
|
-
await applyChanges(
|
|
457
|
+
await applyChanges(
|
|
458
|
+
"UpdateMultiState",
|
|
459
|
+
id,
|
|
460
|
+
beforeStates,
|
|
461
|
+
afterStates,
|
|
462
|
+
operations,
|
|
463
|
+
subIdConfig,
|
|
464
|
+
~onRow=(~subId, ~before, ~after, kind) => report(~key=id, ~subId, ~before?, ~after?, kind),
|
|
465
|
+
)
|
|
359
466
|
|
|
360
467
|
| (Error(err), Some(_)) =>
|
|
361
468
|
logAction(() =>
|
|
@@ -368,10 +475,12 @@ let handleAction = async (
|
|
|
368
475
|
}
|
|
369
476
|
| Delete(id) =>
|
|
370
477
|
logAction(() => `Delete(${id})`)
|
|
371
|
-
await delete(id, None)
|
|
478
|
+
(await delete(id, None))->after(() => report(~key=id, Removed))
|
|
372
479
|
| DeleteMany(ids) =>
|
|
373
480
|
logAction(() => `DeleteMany(${ids->Array.joinUnsafe(", ")})`)
|
|
374
|
-
await deleteBatch(ids->Array.map(id => (id, None)))
|
|
481
|
+
(await deleteBatch(ids->Array.map(id => (id, None))))->after(() =>
|
|
482
|
+
ids->Array.forEach(id => report(~key=id, Removed))
|
|
483
|
+
)
|
|
375
484
|
|
|
376
485
|
// An action reaching here is one this projection engine doesn't implement.
|
|
377
486
|
// Returning Ok() reported the read-model write as done while silently losing
|
|
@@ -404,8 +513,13 @@ let actionsWithId = action =>
|
|
|
404
513
|
[]
|
|
405
514
|
}
|
|
406
515
|
|
|
407
|
-
let
|
|
408
|
-
let allActionsWithId =
|
|
516
|
+
let groupSourcedById = (actions: array<sourced<'id, 'state>>) => {
|
|
517
|
+
let allActionsWithId =
|
|
518
|
+
actions
|
|
519
|
+
->Array.map(({action, sources}) =>
|
|
520
|
+
action->actionsWithId->Array.map(((id, action)) => (id, {action, sources}))
|
|
521
|
+
)
|
|
522
|
+
->Array.flat
|
|
409
523
|
// One pass into a dict keyed by id (was O(ids × actions): a filterMap over
|
|
410
524
|
// every action per unique id). The id ordering is unchanged — still the sorted
|
|
411
525
|
// Belt.Set order — and each group preserves the actions' original order.
|
|
@@ -423,112 +537,98 @@ let groupActionsById = actions => {
|
|
|
423
537
|
->Array.map(id => (id, groups->Dict.getUnsafe(id)))
|
|
424
538
|
}
|
|
425
539
|
|
|
426
|
-
let
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
| (Update(id1, _f), Set(id2, state2)) if id1 == id2 =>
|
|
510
|
-
log.warn(
|
|
511
|
-
~comp="Projection",
|
|
512
|
-
`optimizing Set after Update for id=${id1}, ignoring the Update`,
|
|
513
|
-
)
|
|
514
|
-
previousActions->Array.concat([Set(id1, state2)])
|
|
515
|
-
| (UpdateWithDefault(id1, _defaultState1, _f), Set(id2, state2)) if id1 == id2 =>
|
|
516
|
-
log.warn(
|
|
517
|
-
~comp="Projection",
|
|
518
|
-
`optimizing Set after UpdateWithDefault for id=${id1}, ignoring the UpdateWithDefault`,
|
|
519
|
-
)
|
|
520
|
-
previousActions->Array.concat([Set(id1, state2)])
|
|
521
|
-
// MULTI STATES
|
|
522
|
-
/*
|
|
540
|
+
let groupActionsById = actions =>
|
|
541
|
+
actions
|
|
542
|
+
->Array.map(action => {action, sources: []})
|
|
543
|
+
->groupSourcedById
|
|
544
|
+
->Array.map(((id, group)) => (id, group->Array.map(s => s.action)))
|
|
545
|
+
|
|
546
|
+
// The one action two consecutive actions on one id amount to, or `None` when they
|
|
547
|
+
// stay two.
|
|
548
|
+
let mergeActions = (lastAction, action) =>
|
|
549
|
+
switch (lastAction, action) {
|
|
550
|
+
// SINGLE STATES
|
|
551
|
+
| (Create(id1, state1), Update(id2, f)) if id1 == id2 => Some(Create(id1, f(state1)))
|
|
552
|
+
| (Create(id1, state1), UpdateWithDefault(id2, _defaultState2, f)) if id1 == id2 =>
|
|
553
|
+
Some(Create(id1, f(state1)))
|
|
554
|
+
| (Update(id1, f), Update(id2, g)) if id1 == id2 => Some(Update(id1, state => g(f(state))))
|
|
555
|
+
| (UpdateWithDefault(id1, defaultState1, f), UpdateWithDefault(id2, _defaultState2, g))
|
|
556
|
+
if id1 == id2 =>
|
|
557
|
+
Some(UpdateWithDefault(id1, g(defaultState1), state => g(f(state))))
|
|
558
|
+
| (Update(id1, f), UpdateWithDefault(id2, defaultState2, g)) if id1 == id2 =>
|
|
559
|
+
Some(
|
|
560
|
+
UpdateWithDefault(
|
|
561
|
+
id1,
|
|
562
|
+
/* if no state exists before the first upate, it is ignored */
|
|
563
|
+
defaultState2,
|
|
564
|
+
state => g(f(state)),
|
|
565
|
+
),
|
|
566
|
+
)
|
|
567
|
+
| (UpdateWithDefault(id1, defaultState1, f), Update(id2, g)) if id1 == id2 =>
|
|
568
|
+
Some(UpdateWithDefault(id1, g(defaultState1), state => g(f(state))))
|
|
569
|
+
| (UpdateWithDefault(id1, defaultState1, f), Create(id2, _state2)) if id1 == id2 =>
|
|
570
|
+
log.warn(
|
|
571
|
+
~comp="Projection",
|
|
572
|
+
`optimizing Create after UpdateWithDefault for id=${id1}, ignoring the Create`,
|
|
573
|
+
)
|
|
574
|
+
Some(UpdateWithDefault(id1, defaultState1, f))
|
|
575
|
+
| (Create(id1, state1), Create(id2, state2)) if id1 == id2 =>
|
|
576
|
+
log.warn(
|
|
577
|
+
~comp="Projection",
|
|
578
|
+
`optimizing 2 sequential Create for id=${id1}, ignoring second: ${state2
|
|
579
|
+
->JSON.stringifyAny
|
|
580
|
+
->Option.getOr("?")}`,
|
|
581
|
+
)
|
|
582
|
+
Some(Create(id1, state1))
|
|
583
|
+
| (Create(id1, state1), Delete(id2)) if id1 == id2 =>
|
|
584
|
+
log.warn(
|
|
585
|
+
~comp="Projection",
|
|
586
|
+
`optimizing Delete after Create for id=${id1}, ignoring Create: ${state1
|
|
587
|
+
->JSON.stringifyAny
|
|
588
|
+
->Option.getOr("?")}`,
|
|
589
|
+
)
|
|
590
|
+
Some(Delete(id1))
|
|
591
|
+
| (Update(id1, _f), Delete(id2)) if id1 == id2 =>
|
|
592
|
+
log.warn(
|
|
593
|
+
~comp="Projection",
|
|
594
|
+
`optimizing Delete after Update for id=${id1}, ignoring the Update`,
|
|
595
|
+
)
|
|
596
|
+
Some(Delete(id1))
|
|
597
|
+
| (UpdateWithDefault(id1, _defaultState1, _f), Delete(id2)) if id1 == id2 =>
|
|
598
|
+
log.warn(
|
|
599
|
+
~comp="Projection",
|
|
600
|
+
`optimizing Delete after UpdateWithDefault for id=${id1}, ignoring the UpdateWithDefault`,
|
|
601
|
+
)
|
|
602
|
+
Some(Delete(id1))
|
|
603
|
+
| (Delete(id1), Create(id2, state2)) if id1 == id2 => Some(Set(id1, state2))
|
|
604
|
+
| (Create(id1, state1), Set(id2, state2)) if id1 == id2 =>
|
|
605
|
+
log.warn(
|
|
606
|
+
~comp="Projection",
|
|
607
|
+
`optimizing Set after Create for id=${id1}, ignoring Create: ${state1
|
|
608
|
+
->JSON.stringifyAny
|
|
609
|
+
->Option.getOr("?")}`,
|
|
610
|
+
)
|
|
611
|
+
Some(Set(id1, state2))
|
|
612
|
+
| (Update(id1, _f), Set(id2, state2)) if id1 == id2 =>
|
|
613
|
+
log.warn(~comp="Projection", `optimizing Set after Update for id=${id1}, ignoring the Update`)
|
|
614
|
+
Some(Set(id1, state2))
|
|
615
|
+
| (UpdateWithDefault(id1, _defaultState1, _f), Set(id2, state2)) if id1 == id2 =>
|
|
616
|
+
log.warn(
|
|
617
|
+
~comp="Projection",
|
|
618
|
+
`optimizing Set after UpdateWithDefault for id=${id1}, ignoring the UpdateWithDefault`,
|
|
619
|
+
)
|
|
620
|
+
Some(Set(id1, state2))
|
|
621
|
+
// MULTI STATES
|
|
622
|
+
/*
|
|
523
623
|
| (CreateMultiState(id1, states1), UpdateMultiState(id2, f)) if id1 == id2 =>
|
|
524
624
|
THIS IS FALSE: previousActions->Array.concat([CreateMultiState(id1, f(states1))])
|
|
525
625
|
suggestion: UpdateMultiState with following updateFunction:
|
|
526
626
|
- apply f to states1 and states of UpdateMultiState separately
|
|
527
627
|
- concat unique states of both results
|
|
528
628
|
*/
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
629
|
+
| (UpdateMultiState(id1, f), UpdateMultiState(id2, g)) if id1 == id2 =>
|
|
630
|
+
Some(UpdateMultiState(id1, state => g(f(state))))
|
|
631
|
+
/*
|
|
532
632
|
| (UpdateMultiState(id1, _f), CreateMultiState(id2, states2)) if id1 == id2 =>
|
|
533
633
|
Console..warn(
|
|
534
634
|
"optimizing CreateMultiState after UpdateMultiState, therefore ignoring the UpdateMultiState",
|
|
@@ -539,7 +639,7 @@ let optimizeActions = actions => {
|
|
|
539
639
|
- concat unique states of update results and states2
|
|
540
640
|
- if duplicates exist, prefer states2
|
|
541
641
|
*/
|
|
542
|
-
|
|
642
|
+
/*
|
|
543
643
|
| (CreateMultiState(id1, states1), CreateMultiState(id2, states2)) if id1 == id2 =>
|
|
544
644
|
Console..warn2(
|
|
545
645
|
"optimizing 2 sequential CreateMultiState actions, therefore ignoring the first one:",
|
|
@@ -550,21 +650,50 @@ let optimizeActions = actions => {
|
|
|
550
650
|
- concatenate states1 & states2
|
|
551
651
|
- if duplicates exist, prefer states2
|
|
552
652
|
*/
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
653
|
+
| (lastAction, action) =>
|
|
654
|
+
// any other action will be just appended
|
|
655
|
+
log.warn(
|
|
656
|
+
~comp="Projection",
|
|
657
|
+
`actions not optimized: ${lastAction->JSON.stringifyAny->Option.getOr("?")} + ${action
|
|
658
|
+
->JSON.stringifyAny
|
|
659
|
+
->Option.getOr("?")}`,
|
|
660
|
+
)
|
|
661
|
+
None
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// [ UpdateMultiSate(f), UpdateMultiState(g), Create(..)] => [ UpdateMultiState(f(g)), Create(..) ]
|
|
665
|
+
// A merged action keeps the sources of both.
|
|
666
|
+
let optimizeSourced = (actions: array<sourced<'id, 'state>>): array<sourced<'id, 'state>> =>
|
|
667
|
+
actions->Array.reduce([], (optimized, next) => {
|
|
668
|
+
let count = optimized->Array.length
|
|
669
|
+
if count == 0 {
|
|
670
|
+
[next]
|
|
671
|
+
} else {
|
|
672
|
+
let last = optimized->Array.getUnsafe(count - 1)
|
|
673
|
+
// Everything BEFORE the last action. `~end` is exclusive, so this must be
|
|
674
|
+
// `count - 1`; using `count` kept the last action in the slice while the
|
|
675
|
+
// merged action was also appended, applying it twice.
|
|
676
|
+
let previous = optimized->Array.slice(~start=0, ~end=count - 1)
|
|
677
|
+
switch mergeActions(last.action, next.action) {
|
|
678
|
+
| Some(action) =>
|
|
679
|
+
previous->Array.concat([{action, sources: mergeSources(last.sources, next.sources)}])
|
|
680
|
+
| None => optimized->Array.concat([next])
|
|
562
681
|
}
|
|
563
682
|
}
|
|
564
683
|
})
|
|
565
|
-
}
|
|
566
684
|
|
|
567
|
-
let
|
|
685
|
+
let optimizeActions = actions =>
|
|
686
|
+
actions->Array.map(action => {action, sources: []})->optimizeSourced->Array.map(s => s.action)
|
|
687
|
+
|
|
688
|
+
/** `handleActions` for actions that carry the events they came from, so each
|
|
689
|
+
row change can name them. */
|
|
690
|
+
let handleSourcedActions = async (
|
|
691
|
+
~comp="Projection",
|
|
692
|
+
~onChange=?,
|
|
693
|
+
actions: array<sourced<string, 'state>>,
|
|
694
|
+
operations,
|
|
695
|
+
subIdConfig,
|
|
696
|
+
) => {
|
|
568
697
|
let handleActionsForId = async (actions, id) => {
|
|
569
698
|
let actionCount = actions->Array.length
|
|
570
699
|
if actionCount > 1 {
|
|
@@ -574,12 +703,12 @@ let handleActions = async (~comp="Projection", actions, operations, subIdConfig)
|
|
|
574
703
|
)
|
|
575
704
|
}
|
|
576
705
|
|
|
577
|
-
let optimizedActions =
|
|
706
|
+
let optimizedActions = optimizeSourced(actions)
|
|
578
707
|
let optimizedActionCount = optimizedActions->Array.length
|
|
579
708
|
log.debug(~comp, `handleActions: id=${id} actions=${optimizedActionCount->Int.toString}`)
|
|
580
709
|
|
|
581
710
|
// FIXME: handle errors!
|
|
582
|
-
await optimizedActions->Array.reduce(Ok()->Promise.resolve, async (p, action) => {
|
|
711
|
+
await optimizedActions->Array.reduce(Ok()->Promise.resolve, async (p, {action, sources}) => {
|
|
583
712
|
switch await p {
|
|
584
713
|
| Ok() => ()
|
|
585
714
|
| Error(err) =>
|
|
@@ -590,12 +719,12 @@ let handleActions = async (~comp="Projection", actions, operations, subIdConfig)
|
|
|
590
719
|
->JSON.stringify}`,
|
|
591
720
|
)
|
|
592
721
|
}
|
|
593
|
-
await action->handleAction(~comp, operations, subIdConfig)
|
|
722
|
+
await action->handleAction(~comp, ~sources, ~onChange?, operations, subIdConfig)
|
|
594
723
|
})
|
|
595
724
|
}
|
|
596
725
|
|
|
597
726
|
let results = await actions
|
|
598
|
-
->
|
|
727
|
+
->groupSourcedById
|
|
599
728
|
->Array.map(((id, actions)) => actions->handleActionsForId(id))
|
|
600
729
|
->Promise.all
|
|
601
730
|
let errors = results->Array.filterMap(x =>
|
|
@@ -615,3 +744,12 @@ let handleActions = async (~comp="Projection", actions, operations, subIdConfig)
|
|
|
615
744
|
)
|
|
616
745
|
}
|
|
617
746
|
}
|
|
747
|
+
|
|
748
|
+
let handleActions = (~comp="Projection", ~onChange=?, actions, operations, subIdConfig) =>
|
|
749
|
+
handleSourcedActions(
|
|
750
|
+
~comp,
|
|
751
|
+
~onChange?,
|
|
752
|
+
actions->Array.map(action => {action, sources: []}),
|
|
753
|
+
operations,
|
|
754
|
+
subIdConfig,
|
|
755
|
+
)
|