@reventlessdev/reventless-core 3.0.0-alpha.324 → 3.0.0-alpha.326
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 +15 -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/components/Aggregate/Aggregate_Callback.res +11 -3
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +8 -6
- package/src/components/AutomationSlice/AutomationSlice_Callback.res +21 -9
- package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +19 -8
- package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
- package/src/components/CommandTopic/CommandTopic_Helpers.res +56 -26
- package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +35 -16
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +25 -12
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +20 -7
- package/src/components/ReadModel/ReadModel_Callback.res +23 -3
- package/src/components/ReadModel/ReadModel_Callback.res.mjs +14 -2
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +7 -0
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +7 -7
- package/src/components/StateViewSlice/StateViewSlice_Builder.res +14 -2
- package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +13 -2
- package/src/components/StateViewSlice/StateViewSlice_Callback.res +29 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +9 -2
- package/src/util/EffectLogger.res +30 -29
- package/src/util/EffectLogger.res.mjs +13 -22
- package/src/util/Logger.res +231 -35
- package/src/util/Logger.res.mjs +263 -42
- package/tests/commandgenerator/OwnerActingTest.res +205 -1
- package/tests/commandgenerator/OwnerActingTest.res.mjs +361 -0
- package/tests/commandtopic/CommandOutcomeHookTest.res +74 -2
- package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +92 -0
- package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
- 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.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
|
|
4
4
|
import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
|
|
5
5
|
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
|
|
6
|
+
import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
|
|
6
7
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
7
8
|
import * as Effect from "effect/Effect";
|
|
8
9
|
import * as Stream$1 from "effect/Stream";
|
|
@@ -11,6 +12,7 @@ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
|
|
|
11
12
|
import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
|
|
12
13
|
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
|
|
13
14
|
import * as Lifecycle$Reventless from "@reventlessdev/reventless-spec/src/types/Lifecycle.res.mjs";
|
|
15
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
14
16
|
import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
|
|
15
17
|
import * as Logger$ReventlessCore from "./util/Logger.res.mjs";
|
|
16
18
|
import * as DisplayName$Reventless from "@reventlessdev/reventless-spec/src/components/DisplayName.res.mjs";
|
|
@@ -204,9 +206,14 @@ function rewriteJsonAction(action, at, stateSchema) {
|
|
|
204
206
|
}
|
|
205
207
|
}
|
|
206
208
|
|
|
207
|
-
|
|
209
|
+
function mergeSources(a, b) {
|
|
210
|
+
return a.concat(b.filter(s => !a.some(other => other.msgId === s.msgId)));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function applyChanges(action, id, beforeStates, afterStates, param, param$1, $staropt$star) {
|
|
208
214
|
let getSubId = param$1.getSubId;
|
|
209
215
|
let subIdField = param$1.subIdField;
|
|
216
|
+
let onRow = $staropt$star !== undefined ? $staropt$star : (param, param$1, param$2, param$3) => {};
|
|
210
217
|
let beforeCount = beforeStates.length;
|
|
211
218
|
let beforeSubIds = Belt_SetString.fromArray(beforeStates.map(state => getSubId(state)));
|
|
212
219
|
let afterCount = afterStates.length;
|
|
@@ -251,12 +258,27 @@ async function applyChanges(action, id, beforeStates, afterStates, param, param$
|
|
|
251
258
|
let err = result.find(r => r.TAG !== "Ok");
|
|
252
259
|
if (err !== undefined) {
|
|
253
260
|
return err;
|
|
254
|
-
} else {
|
|
255
|
-
return {
|
|
256
|
-
TAG: "Ok",
|
|
257
|
-
_0: undefined
|
|
258
|
-
};
|
|
259
261
|
}
|
|
262
|
+
let beforeBySubId = {};
|
|
263
|
+
beforeStates.forEach(before => {
|
|
264
|
+
beforeBySubId[getSubId(before)] = before;
|
|
265
|
+
});
|
|
266
|
+
afterStates.forEach(after => {
|
|
267
|
+
let subId = getSubId(after);
|
|
268
|
+
let before = beforeBySubId[subId];
|
|
269
|
+
if (before === undefined) {
|
|
270
|
+
return onRow(subId, undefined, Primitive_option.some(after), "Created");
|
|
271
|
+
}
|
|
272
|
+
let before$1 = Primitive_option.valFromOption(before);
|
|
273
|
+
if (Primitive_object.notequal(before$1, after)) {
|
|
274
|
+
return onRow(subId, Primitive_option.some(before$1), Primitive_option.some(after), "Changed");
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
deletedSubIds.forEach(subId => onRow(subId, beforeBySubId[subId], undefined, "Removed"));
|
|
278
|
+
return {
|
|
279
|
+
TAG: "Ok",
|
|
280
|
+
_0: undefined
|
|
281
|
+
};
|
|
260
282
|
}
|
|
261
283
|
|
|
262
284
|
function stateToString(state) {
|
|
@@ -267,11 +289,36 @@ function statesToString(states) {
|
|
|
267
289
|
return states.map(stateToString).join(", ");
|
|
268
290
|
}
|
|
269
291
|
|
|
270
|
-
async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
292
|
+
async function handleAction($staropt$star, $staropt$star$1, onChange, action, operations, subIdConfig) {
|
|
271
293
|
let saveBatch = operations.saveBatch;
|
|
272
294
|
let save = operations.save;
|
|
273
295
|
let loadStream = operations.loadStream;
|
|
274
296
|
let comp = $staropt$star !== undefined ? $staropt$star : "Projection";
|
|
297
|
+
let sources = $staropt$star$1 !== undefined ? $staropt$star$1 : [];
|
|
298
|
+
let report = (key, subId, before, after, kind) => Stdlib_Option.forEach(onChange, f => {
|
|
299
|
+
try {
|
|
300
|
+
return f({
|
|
301
|
+
key: key,
|
|
302
|
+
subId: subId,
|
|
303
|
+
kind: kind,
|
|
304
|
+
before: before,
|
|
305
|
+
after: after,
|
|
306
|
+
sources: sources
|
|
307
|
+
});
|
|
308
|
+
} catch (raw_exn) {
|
|
309
|
+
let exn = Primitive_exceptions.internalToException(raw_exn);
|
|
310
|
+
return log.warn(comp, undefined, `a view-change listener failed: ` + Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown"));
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
let subIdOf = state => Stdlib_Option.map(subIdConfig, param => param.getSubId(state));
|
|
314
|
+
let after = (result, reportAll) => {
|
|
315
|
+
if (result.TAG === "Ok") {
|
|
316
|
+
if (onChange !== undefined) {
|
|
317
|
+
reportAll();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
};
|
|
275
322
|
let logAction = makeStr => log.debugLazy(comp, () => `handling action: ` + makeStr());
|
|
276
323
|
let loadAtMost = (n, id) => Effect.runPromise(Effect.catchAll(Effect.map(Stream.runCollect(Stream$1.take(loadStream(id), n)), states => ({
|
|
277
324
|
TAG: "Ok",
|
|
@@ -299,7 +346,7 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
299
346
|
let state = action._1;
|
|
300
347
|
let id = action._0;
|
|
301
348
|
logAction(() => `Create(` + id + `, ` + stateToString(state) + `)`);
|
|
302
|
-
return await save(id, state, "Init", undefined);
|
|
349
|
+
return after(await save(id, state, "Init", undefined), () => report(id, undefined, undefined, Primitive_option.some(state), "Created"));
|
|
303
350
|
case "CreateMany" :
|
|
304
351
|
let batch = action._0.map(param => [
|
|
305
352
|
param[0],
|
|
@@ -307,7 +354,9 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
307
354
|
undefined
|
|
308
355
|
]);
|
|
309
356
|
logAction(() => `CreateMany(` + batch.map(param => `(` + param[0] + `,` + stateToString(param[1]) + `)`).join(", ") + `)`);
|
|
310
|
-
return await saveBatch(batch)
|
|
357
|
+
return after(await saveBatch(batch), () => {
|
|
358
|
+
batch.forEach(param => report(param[0], undefined, undefined, Primitive_option.some(param[1]), "Created"));
|
|
359
|
+
});
|
|
311
360
|
case "Update" :
|
|
312
361
|
let id$1 = action._0;
|
|
313
362
|
let states = await loadAtMost(2, id$1);
|
|
@@ -337,7 +386,11 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
337
386
|
let oldState = states$1[0];
|
|
338
387
|
let newState = action._1(oldState);
|
|
339
388
|
logAction(() => `Update(` + id$1 + `, ` + stateToString(oldState) + ` => ` + stateToString(newState) + `)`);
|
|
340
|
-
return await save(id$1, newState, "Overwrite", undefined)
|
|
389
|
+
return after(await save(id$1, newState, "Overwrite", undefined), () => {
|
|
390
|
+
if (Primitive_object.notequal(newState, oldState)) {
|
|
391
|
+
return report(id$1, undefined, Primitive_option.some(oldState), Primitive_option.some(newState), "Changed");
|
|
392
|
+
}
|
|
393
|
+
});
|
|
341
394
|
case "UpdateWithDefault" :
|
|
342
395
|
let $$default = action._1;
|
|
343
396
|
let id$2 = action._0;
|
|
@@ -354,13 +407,17 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
354
407
|
};
|
|
355
408
|
} else {
|
|
356
409
|
logAction(() => `UpdateWithDefault(` + id$2 + `, default: ` + stateToString($$default) + `)`);
|
|
357
|
-
return await save(id$2, $$default, "Init", undefined);
|
|
410
|
+
return after(await save(id$2, $$default, "Init", undefined), () => report(id$2, undefined, undefined, Primitive_option.some($$default), "Created"));
|
|
358
411
|
}
|
|
359
412
|
}
|
|
360
413
|
let oldState$1 = states$3[0];
|
|
361
414
|
let newState$1 = action._2(oldState$1);
|
|
362
415
|
logAction(() => `UpdateWithDefault(` + id$2 + `, ` + stateToString(oldState$1) + ` => ` + stateToString(newState$1) + `)`);
|
|
363
|
-
return await save(id$2, newState$1, "Overwrite", undefined)
|
|
416
|
+
return after(await save(id$2, newState$1, "Overwrite", undefined), () => {
|
|
417
|
+
if (Primitive_object.notequal(newState$1, oldState$1)) {
|
|
418
|
+
return report(id$2, undefined, Primitive_option.some(oldState$1), Primitive_option.some(newState$1), "Changed");
|
|
419
|
+
}
|
|
420
|
+
});
|
|
364
421
|
}
|
|
365
422
|
let err = states$2._0;
|
|
366
423
|
logAction(() => `UpdateWithDefault Error: Couldn't load oldState(s) for ` + id$2 + `: ` + QueryDb$ReventlessCore.storageErrorToString(err) + `)`);
|
|
@@ -372,7 +429,7 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
372
429
|
let state$1 = action._1;
|
|
373
430
|
let id$3 = action._0;
|
|
374
431
|
logAction(() => `Set(` + id$3 + `, ` + stateToString(state$1) + `)`);
|
|
375
|
-
return await save(id$3, state$1, "Any", undefined);
|
|
432
|
+
return after(await save(id$3, state$1, "Any", undefined), () => report(id$3, undefined, undefined, Primitive_option.some(state$1), "Changed"));
|
|
376
433
|
case "SetMany" :
|
|
377
434
|
let set = action._1;
|
|
378
435
|
let batch$1 = action._0.map(id => [
|
|
@@ -381,22 +438,29 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
381
438
|
undefined
|
|
382
439
|
]);
|
|
383
440
|
logAction(() => `SetMany(` + batch$1.map(param => `(` + param[0] + `,` + stateToString(param[1]) + `)`).join(", ") + `)`);
|
|
384
|
-
return await saveBatch(batch$1)
|
|
441
|
+
return after(await saveBatch(batch$1), () => {
|
|
442
|
+
batch$1.forEach(param => report(param[0], undefined, undefined, Primitive_option.some(param[1]), "Changed"));
|
|
443
|
+
});
|
|
385
444
|
case "Delete" :
|
|
386
445
|
let id$4 = action._0;
|
|
387
446
|
logAction(() => `Delete(` + id$4 + `)`);
|
|
388
|
-
return await operations.delete(id$4, undefined);
|
|
447
|
+
return after(await operations.delete(id$4, undefined), () => report(id$4, undefined, undefined, undefined, "Removed"));
|
|
389
448
|
case "DeleteMany" :
|
|
390
449
|
let ids = action._0;
|
|
391
450
|
logAction(() => `DeleteMany(` + ids.join(", ") + `)`);
|
|
392
|
-
return await operations.deleteBatch(ids.map(id => [
|
|
451
|
+
return after(await operations.deleteBatch(ids.map(id => [
|
|
393
452
|
id,
|
|
394
453
|
undefined
|
|
395
|
-
]))
|
|
454
|
+
])), () => {
|
|
455
|
+
ids.forEach(id => report(id, undefined, undefined, undefined, "Removed"));
|
|
456
|
+
});
|
|
396
457
|
case "CreateMultiState" :
|
|
397
458
|
let states$4 = action._1;
|
|
398
459
|
let id$5 = action._0;
|
|
399
460
|
logAction(() => `CreateMultiState(` + id$5 + `, ` + states$4.map(stateToString).join(", ") + `)`);
|
|
461
|
+
let reportAll = () => {
|
|
462
|
+
states$4.forEach(state => report(id$5, subIdOf(state), undefined, Primitive_option.some(state), "Created"));
|
|
463
|
+
};
|
|
400
464
|
let len$2 = states$4.length;
|
|
401
465
|
if (len$2 !== 1) {
|
|
402
466
|
if (len$2 === 0) {
|
|
@@ -410,10 +474,10 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
410
474
|
state,
|
|
411
475
|
undefined
|
|
412
476
|
]);
|
|
413
|
-
return await saveBatch(batch$2);
|
|
477
|
+
return after(await saveBatch(batch$2), reportAll);
|
|
414
478
|
}
|
|
415
479
|
let state$2 = states$4[0];
|
|
416
|
-
return await save(id$5, state$2, "Init", undefined);
|
|
480
|
+
return after(await save(id$5, state$2, "Init", undefined), reportAll);
|
|
417
481
|
case "UpdateMultiState" :
|
|
418
482
|
let id$6 = action._0;
|
|
419
483
|
let match = await loadAll(id$6);
|
|
@@ -421,7 +485,7 @@ async function handleAction($staropt$star, action, operations, subIdConfig) {
|
|
|
421
485
|
if (subIdConfig !== undefined) {
|
|
422
486
|
let states$5 = match._0;
|
|
423
487
|
let afterStates = action._1(states$5);
|
|
424
|
-
return await applyChanges("UpdateMultiState", id$6, states$5, afterStates, operations, subIdConfig);
|
|
488
|
+
return await applyChanges("UpdateMultiState", id$6, states$5, afterStates, operations, subIdConfig, (subId, before, after, kind) => report(id$6, subId, before, after, kind));
|
|
425
489
|
}
|
|
426
490
|
} else if (subIdConfig !== undefined) {
|
|
427
491
|
let err$1 = match._0;
|
|
@@ -508,8 +572,17 @@ function actionsWithId(action) {
|
|
|
508
572
|
return [];
|
|
509
573
|
}
|
|
510
574
|
|
|
511
|
-
function
|
|
512
|
-
let allActionsWithId = actions.map(
|
|
575
|
+
function groupSourcedById(actions) {
|
|
576
|
+
let allActionsWithId = actions.map(param => {
|
|
577
|
+
let sources = param.sources;
|
|
578
|
+
return actionsWithId(param.action).map(param => [
|
|
579
|
+
param[0],
|
|
580
|
+
{
|
|
581
|
+
action: param[1],
|
|
582
|
+
sources: sources
|
|
583
|
+
}
|
|
584
|
+
]);
|
|
585
|
+
}).flat();
|
|
513
586
|
let groups = {};
|
|
514
587
|
allActionsWithId.forEach(param => {
|
|
515
588
|
let action = param[1];
|
|
@@ -527,235 +600,263 @@ function groupActionsById(actions) {
|
|
|
527
600
|
]);
|
|
528
601
|
}
|
|
529
602
|
|
|
530
|
-
function
|
|
531
|
-
return
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
case "Update" :
|
|
596
|
-
let f = lastAction._1;
|
|
597
|
-
let id1$1 = lastAction._0;
|
|
598
|
-
if (typeof action === "object") {
|
|
599
|
-
switch (action.TAG) {
|
|
600
|
-
case "Update" :
|
|
601
|
-
if (id1$1 === action._0) {
|
|
602
|
-
let g = action._1;
|
|
603
|
-
return previousActions.concat([{
|
|
604
|
-
TAG: "Update",
|
|
605
|
-
_0: id1$1,
|
|
606
|
-
_1: state => g(f(state))
|
|
607
|
-
}]);
|
|
608
|
-
}
|
|
609
|
-
break;
|
|
610
|
-
case "UpdateWithDefault" :
|
|
611
|
-
if (id1$1 === action._0) {
|
|
612
|
-
let g$1 = action._2;
|
|
613
|
-
return previousActions.concat([{
|
|
614
|
-
TAG: "UpdateWithDefault",
|
|
615
|
-
_0: id1$1,
|
|
616
|
-
_1: action._1,
|
|
617
|
-
_2: state => g$1(f(state))
|
|
618
|
-
}]);
|
|
619
|
-
}
|
|
620
|
-
break;
|
|
621
|
-
case "Set" :
|
|
622
|
-
if (id1$1 === action._0) {
|
|
623
|
-
log.warn("Projection", undefined, `optimizing Set after Update for id=` + id1$1 + `, ignoring the Update`);
|
|
624
|
-
return previousActions.concat([{
|
|
625
|
-
TAG: "Set",
|
|
626
|
-
_0: id1$1,
|
|
627
|
-
_1: action._1
|
|
628
|
-
}]);
|
|
629
|
-
}
|
|
630
|
-
break;
|
|
631
|
-
case "Delete" :
|
|
632
|
-
if (id1$1 === action._0) {
|
|
633
|
-
log.warn("Projection", undefined, `optimizing Delete after Update for id=` + id1$1 + `, ignoring the Update`);
|
|
634
|
-
return previousActions.concat([{
|
|
635
|
-
TAG: "Delete",
|
|
636
|
-
_0: id1$1
|
|
637
|
-
}]);
|
|
638
|
-
}
|
|
639
|
-
break;
|
|
640
|
-
}
|
|
603
|
+
function groupActionsById(actions) {
|
|
604
|
+
return groupSourcedById(actions.map(action => ({
|
|
605
|
+
action: action,
|
|
606
|
+
sources: []
|
|
607
|
+
}))).map(param => [
|
|
608
|
+
param[0],
|
|
609
|
+
param[1].map(s => s.action)
|
|
610
|
+
]);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function mergeActions(lastAction, action) {
|
|
614
|
+
if (typeof lastAction === "object") {
|
|
615
|
+
switch (lastAction.TAG) {
|
|
616
|
+
case "Create" :
|
|
617
|
+
let state1 = lastAction._1;
|
|
618
|
+
let id1 = lastAction._0;
|
|
619
|
+
if (typeof action === "object") {
|
|
620
|
+
switch (action.TAG) {
|
|
621
|
+
case "Create" :
|
|
622
|
+
if (id1 === action._0) {
|
|
623
|
+
log.warn("Projection", undefined, `optimizing 2 sequential Create for id=` + id1 + `, ignoring second: ` + Stdlib_Option.getOr(JSON.stringify(action._1), "?"));
|
|
624
|
+
return {
|
|
625
|
+
TAG: "Create",
|
|
626
|
+
_0: id1,
|
|
627
|
+
_1: state1
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
break;
|
|
631
|
+
case "Update" :
|
|
632
|
+
if (id1 === action._0) {
|
|
633
|
+
return {
|
|
634
|
+
TAG: "Create",
|
|
635
|
+
_0: id1,
|
|
636
|
+
_1: action._1(state1)
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
break;
|
|
640
|
+
case "UpdateWithDefault" :
|
|
641
|
+
if (id1 === action._0) {
|
|
642
|
+
return {
|
|
643
|
+
TAG: "Create",
|
|
644
|
+
_0: id1,
|
|
645
|
+
_1: action._2(state1)
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
break;
|
|
649
|
+
case "Set" :
|
|
650
|
+
if (id1 === action._0) {
|
|
651
|
+
log.warn("Projection", undefined, `optimizing Set after Create for id=` + id1 + `, ignoring Create: ` + Stdlib_Option.getOr(JSON.stringify(state1), "?"));
|
|
652
|
+
return {
|
|
653
|
+
TAG: "Set",
|
|
654
|
+
_0: id1,
|
|
655
|
+
_1: action._1
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
break;
|
|
659
|
+
case "Delete" :
|
|
660
|
+
if (id1 === action._0) {
|
|
661
|
+
log.warn("Projection", undefined, `optimizing Delete after Create for id=` + id1 + `, ignoring Create: ` + Stdlib_Option.getOr(JSON.stringify(state1), "?"));
|
|
662
|
+
return {
|
|
663
|
+
TAG: "Delete",
|
|
664
|
+
_0: id1
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
break;
|
|
641
668
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
}]);
|
|
690
|
-
}
|
|
691
|
-
break;
|
|
692
|
-
case "Delete" :
|
|
693
|
-
if (id1$2 === action._0) {
|
|
694
|
-
log.warn("Projection", undefined, `optimizing Delete after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
|
|
695
|
-
return previousActions.concat([{
|
|
696
|
-
TAG: "Delete",
|
|
697
|
-
_0: id1$2
|
|
698
|
-
}]);
|
|
699
|
-
}
|
|
700
|
-
break;
|
|
701
|
-
}
|
|
669
|
+
}
|
|
670
|
+
break;
|
|
671
|
+
case "Update" :
|
|
672
|
+
let f = lastAction._1;
|
|
673
|
+
let id1$1 = lastAction._0;
|
|
674
|
+
if (typeof action === "object") {
|
|
675
|
+
switch (action.TAG) {
|
|
676
|
+
case "Update" :
|
|
677
|
+
if (id1$1 === action._0) {
|
|
678
|
+
let g = action._1;
|
|
679
|
+
return {
|
|
680
|
+
TAG: "Update",
|
|
681
|
+
_0: id1$1,
|
|
682
|
+
_1: state => g(f(state))
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
break;
|
|
686
|
+
case "UpdateWithDefault" :
|
|
687
|
+
if (id1$1 === action._0) {
|
|
688
|
+
let g$1 = action._2;
|
|
689
|
+
return {
|
|
690
|
+
TAG: "UpdateWithDefault",
|
|
691
|
+
_0: id1$1,
|
|
692
|
+
_1: action._1,
|
|
693
|
+
_2: state => g$1(f(state))
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
break;
|
|
697
|
+
case "Set" :
|
|
698
|
+
if (id1$1 === action._0) {
|
|
699
|
+
log.warn("Projection", undefined, `optimizing Set after Update for id=` + id1$1 + `, ignoring the Update`);
|
|
700
|
+
return {
|
|
701
|
+
TAG: "Set",
|
|
702
|
+
_0: id1$1,
|
|
703
|
+
_1: action._1
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
break;
|
|
707
|
+
case "Delete" :
|
|
708
|
+
if (id1$1 === action._0) {
|
|
709
|
+
log.warn("Projection", undefined, `optimizing Delete after Update for id=` + id1$1 + `, ignoring the Update`);
|
|
710
|
+
return {
|
|
711
|
+
TAG: "Delete",
|
|
712
|
+
_0: id1$1
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
break;
|
|
702
716
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
717
|
+
}
|
|
718
|
+
break;
|
|
719
|
+
case "UpdateWithDefault" :
|
|
720
|
+
let f$1 = lastAction._2;
|
|
721
|
+
let defaultState1 = lastAction._1;
|
|
722
|
+
let id1$2 = lastAction._0;
|
|
723
|
+
if (typeof action === "object") {
|
|
724
|
+
switch (action.TAG) {
|
|
725
|
+
case "Create" :
|
|
726
|
+
if (id1$2 === action._0) {
|
|
727
|
+
log.warn("Projection", undefined, `optimizing Create after UpdateWithDefault for id=` + id1$2 + `, ignoring the Create`);
|
|
728
|
+
return {
|
|
729
|
+
TAG: "UpdateWithDefault",
|
|
730
|
+
_0: id1$2,
|
|
731
|
+
_1: defaultState1,
|
|
732
|
+
_2: f$1
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
break;
|
|
736
|
+
case "Update" :
|
|
737
|
+
if (id1$2 === action._0) {
|
|
738
|
+
let g$2 = action._1;
|
|
739
|
+
return {
|
|
740
|
+
TAG: "UpdateWithDefault",
|
|
741
|
+
_0: id1$2,
|
|
742
|
+
_1: g$2(defaultState1),
|
|
743
|
+
_2: state => g$2(f$1(state))
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
break;
|
|
747
|
+
case "UpdateWithDefault" :
|
|
748
|
+
if (id1$2 === action._0) {
|
|
749
|
+
let g$3 = action._2;
|
|
750
|
+
return {
|
|
751
|
+
TAG: "UpdateWithDefault",
|
|
752
|
+
_0: id1$2,
|
|
753
|
+
_1: g$3(defaultState1),
|
|
754
|
+
_2: state => g$3(f$1(state))
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
break;
|
|
758
|
+
case "Set" :
|
|
759
|
+
if (id1$2 === action._0) {
|
|
760
|
+
log.warn("Projection", undefined, `optimizing Set after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
|
|
761
|
+
return {
|
|
709
762
|
TAG: "Set",
|
|
710
|
-
_0: id1$
|
|
763
|
+
_0: id1$2,
|
|
711
764
|
_1: action._1
|
|
712
|
-
}
|
|
713
|
-
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
break;
|
|
768
|
+
case "Delete" :
|
|
769
|
+
if (id1$2 === action._0) {
|
|
770
|
+
log.warn("Projection", undefined, `optimizing Delete after UpdateWithDefault for id=` + id1$2 + `, ignoring the UpdateWithDefault`);
|
|
771
|
+
return {
|
|
772
|
+
TAG: "Delete",
|
|
773
|
+
_0: id1$2
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
break;
|
|
714
777
|
}
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
}]);
|
|
727
|
-
}
|
|
778
|
+
}
|
|
779
|
+
break;
|
|
780
|
+
case "Delete" :
|
|
781
|
+
if (typeof action === "object" && action.TAG === "Create") {
|
|
782
|
+
let id1$3 = lastAction._0;
|
|
783
|
+
if (id1$3 === action._0) {
|
|
784
|
+
return {
|
|
785
|
+
TAG: "Set",
|
|
786
|
+
_0: id1$3,
|
|
787
|
+
_1: action._1
|
|
788
|
+
};
|
|
728
789
|
}
|
|
729
|
-
|
|
730
|
-
|
|
790
|
+
}
|
|
791
|
+
break;
|
|
792
|
+
case "UpdateMultiState" :
|
|
793
|
+
if (typeof action === "object" && action.TAG === "UpdateMultiState") {
|
|
794
|
+
let id1$4 = lastAction._0;
|
|
795
|
+
if (id1$4 === action._0) {
|
|
796
|
+
let g$4 = action._1;
|
|
797
|
+
let f$2 = lastAction._1;
|
|
798
|
+
return {
|
|
799
|
+
TAG: "UpdateMultiState",
|
|
800
|
+
_0: id1$4,
|
|
801
|
+
_1: state => g$4(f$2(state))
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
break;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
log.warn("Projection", undefined, `actions not optimized: ` + Stdlib_Option.getOr(JSON.stringify(lastAction), "?") + ` + ` + Stdlib_Option.getOr(JSON.stringify(action), "?"));
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
function optimizeSourced(actions) {
|
|
812
|
+
return Stdlib_Array.reduce(actions, [], (optimized, next) => {
|
|
813
|
+
let count = optimized.length;
|
|
814
|
+
if (count === 0) {
|
|
815
|
+
return [next];
|
|
816
|
+
}
|
|
817
|
+
let last = optimized[count - 1 | 0];
|
|
818
|
+
let previous = optimized.slice(0, count - 1 | 0);
|
|
819
|
+
let action = mergeActions(last.action, next.action);
|
|
820
|
+
if (action !== undefined) {
|
|
821
|
+
return previous.concat([{
|
|
822
|
+
action: action,
|
|
823
|
+
sources: mergeSources(last.sources, next.sources)
|
|
824
|
+
}]);
|
|
825
|
+
} else {
|
|
826
|
+
return optimized.concat([next]);
|
|
731
827
|
}
|
|
732
|
-
log.warn("Projection", undefined, `actions not optimized: ` + Stdlib_Option.getOr(JSON.stringify(lastAction), "?") + ` + ` + Stdlib_Option.getOr(JSON.stringify(action), "?"));
|
|
733
|
-
return optimizedActions.concat([action]);
|
|
734
828
|
});
|
|
735
829
|
}
|
|
736
830
|
|
|
737
|
-
|
|
831
|
+
function optimizeActions(actions) {
|
|
832
|
+
return optimizeSourced(actions.map(action => ({
|
|
833
|
+
action: action,
|
|
834
|
+
sources: []
|
|
835
|
+
}))).map(s => s.action);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
async function handleSourcedActions(compOpt, onChange, actions, operations, subIdConfig) {
|
|
738
839
|
let comp = compOpt !== undefined ? compOpt : "Projection";
|
|
739
840
|
let handleActionsForId = async (actions, id) => {
|
|
740
841
|
let actionCount = actions.length;
|
|
741
842
|
if (actionCount > 1) {
|
|
742
843
|
log.debug(comp, undefined, `handleActions: optimizing ` + actionCount.toString() + ` actions for id=` + id);
|
|
743
844
|
}
|
|
744
|
-
let optimizedActions =
|
|
845
|
+
let optimizedActions = optimizeSourced(actions);
|
|
745
846
|
let optimizedActionCount = optimizedActions.length;
|
|
746
847
|
log.debug(comp, undefined, `handleActions: id=` + id + ` actions=` + optimizedActionCount.toString());
|
|
747
848
|
return await Stdlib_Array.reduce(optimizedActions, Promise.resolve({
|
|
748
849
|
TAG: "Ok",
|
|
749
850
|
_0: undefined
|
|
750
|
-
}), async (p,
|
|
851
|
+
}), async (p, param) => {
|
|
751
852
|
let err = await p;
|
|
752
853
|
if (err.TAG !== "Ok") {
|
|
753
854
|
log.error(comp, undefined, `storage error: ` + JSON.stringify(Message$ReventlessCore.encode(err._0, QueryDb$ReventlessInfra.storageErrorSchema)));
|
|
754
855
|
}
|
|
755
|
-
return await handleAction(comp, action, operations, subIdConfig);
|
|
856
|
+
return await handleAction(comp, param.sources, onChange, param.action, operations, subIdConfig);
|
|
756
857
|
});
|
|
757
858
|
};
|
|
758
|
-
let results = await Promise.all(
|
|
859
|
+
let results = await Promise.all(groupSourcedById(actions).map(param => handleActionsForId(param[1], param[0])));
|
|
759
860
|
let errors = Stdlib_Array.filterMap(results, x => {
|
|
760
861
|
if (x.TAG === "Ok") {
|
|
761
862
|
return;
|
|
@@ -770,6 +871,14 @@ async function handleActions(compOpt, actions, operations, subIdConfig) {
|
|
|
770
871
|
return Stdlib_JsError.throwWithMessage(`Projection.handleActions failed with ` + count.toString() + ` errors: ` + errors.map(QueryDb$ReventlessCore.storageErrorToString).join(","));
|
|
771
872
|
}
|
|
772
873
|
|
|
874
|
+
function handleActions(compOpt, onChange, actions, operations, subIdConfig) {
|
|
875
|
+
let comp = compOpt !== undefined ? compOpt : "Projection";
|
|
876
|
+
return handleSourcedActions(comp, onChange, actions.map(action => ({
|
|
877
|
+
action: action,
|
|
878
|
+
sources: []
|
|
879
|
+
})), operations, subIdConfig);
|
|
880
|
+
}
|
|
881
|
+
|
|
773
882
|
let $$Set;
|
|
774
883
|
|
|
775
884
|
export {
|
|
@@ -783,13 +892,18 @@ export {
|
|
|
783
892
|
trailOverlays,
|
|
784
893
|
rewriteTrail,
|
|
785
894
|
rewriteJsonAction,
|
|
895
|
+
mergeSources,
|
|
786
896
|
applyChanges,
|
|
787
897
|
stateToString,
|
|
788
898
|
statesToString,
|
|
789
899
|
handleAction,
|
|
790
900
|
actionsWithId,
|
|
901
|
+
groupSourcedById,
|
|
791
902
|
groupActionsById,
|
|
903
|
+
mergeActions,
|
|
904
|
+
optimizeSourced,
|
|
792
905
|
optimizeActions,
|
|
906
|
+
handleSourcedActions,
|
|
793
907
|
handleActions,
|
|
794
908
|
}
|
|
795
909
|
/* log Not a pure module */
|