@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.
Files changed (40) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/package.json +7 -7
  3. package/src/Message.res +46 -0
  4. package/src/Message.res.mjs +45 -0
  5. package/src/Projection.res +269 -131
  6. package/src/Projection.res.mjs +338 -224
  7. package/src/ViewChange.res +194 -0
  8. package/src/ViewChange.res.mjs +235 -0
  9. package/src/components/Aggregate/Aggregate_Callback.res +11 -3
  10. package/src/components/Aggregate/Aggregate_Callback.res.mjs +8 -6
  11. package/src/components/AutomationSlice/AutomationSlice_Callback.res +21 -9
  12. package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +19 -8
  13. package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
  14. package/src/components/CommandTopic/CommandTopic_Helpers.res +56 -26
  15. package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +35 -16
  16. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
  17. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +6 -2
  18. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +25 -12
  19. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +20 -7
  20. package/src/components/ReadModel/ReadModel_Callback.res +23 -3
  21. package/src/components/ReadModel/ReadModel_Callback.res.mjs +14 -2
  22. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +7 -0
  23. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +7 -7
  24. package/src/components/StateViewSlice/StateViewSlice_Builder.res +14 -2
  25. package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +13 -2
  26. package/src/components/StateViewSlice/StateViewSlice_Callback.res +29 -4
  27. package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +9 -2
  28. package/src/util/EffectLogger.res +30 -29
  29. package/src/util/EffectLogger.res.mjs +13 -22
  30. package/src/util/Logger.res +231 -35
  31. package/src/util/Logger.res.mjs +263 -42
  32. package/tests/commandgenerator/OwnerActingTest.res +205 -1
  33. package/tests/commandgenerator/OwnerActingTest.res.mjs +361 -0
  34. package/tests/commandtopic/CommandOutcomeHookTest.res +74 -2
  35. package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +92 -0
  36. package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
  37. package/tests/logger/LoggerSinkTest.res +205 -0
  38. package/tests/logger/LoggerSinkTest.res.mjs +209 -0
  39. package/tests/projection/ViewChangeTest.res +173 -0
  40. package/tests/projection/ViewChangeTest.res.mjs +292 -0
@@ -34,7 +34,8 @@ let todoRowSchema = Sury.$schema(s => ({
34
34
  processedAt: s.m(Sury.$option(Sury.string)),
35
35
  completedAt: s.m(Sury.$option(Sury.string)),
36
36
  retryCount: s.m(Sury.int32),
37
- maxRetries: s.m(Sury.$option(Sury.int32))
37
+ maxRetries: s.m(Sury.$option(Sury.int32)),
38
+ source: s.m(Sury.$option(Message$ReventlessCore.flowSourceSchema))
38
39
  }));
39
40
 
40
41
  function todoRowSchemaFor(itemSchema) {
@@ -45,7 +46,8 @@ function todoRowSchemaFor(itemSchema) {
45
46
  processedAt: s.m(Sury.$option(Sury.string)),
46
47
  completedAt: s.m(Sury.$option(Sury.string)),
47
48
  retryCount: s.m(Sury.int32),
48
- maxRetries: s.m(Sury.$option(Sury.int32))
49
+ maxRetries: s.m(Sury.$option(Sury.int32)),
50
+ source: s.m(Sury.$option(Message$ReventlessCore.flowSourceSchema))
49
51
  }));
50
52
  }
51
53
 
@@ -84,7 +86,14 @@ function Make(Spec) {
84
86
  return changes;
85
87
  };
86
88
  let readsClock = Automation.mappings.some(M => M.sourceName === Clock$Reventless.name);
87
- let makeMeta = () => Message$ReventlessCore.generateMeta(Spec.targetName, undefined, undefined, undefined, undefined, undefined, undefined);
89
+ let makeMeta = row => {
90
+ let source = row.source;
91
+ if (source !== undefined) {
92
+ return Message$ReventlessCore.deriveMetaFromSource(source, Spec.targetName);
93
+ } else {
94
+ return Message$ReventlessCore.generateMeta(Spec.targetName, undefined, undefined, undefined, undefined, undefined, undefined);
95
+ }
96
+ };
88
97
  let personalFields = Pii$Reventless.recordFields(Spec.todoItemSchema);
89
98
  let keyField = "$key";
90
99
  let fingerprintPurpose = `automationWorkItem:` + Spec.name;
@@ -199,7 +208,7 @@ function Make(Spec) {
199
208
  }
200
209
  return {
201
210
  id: param[0],
202
- meta: makeMeta(),
211
+ meta: makeMeta(row),
203
212
  commandJson: commandJson
204
213
  };
205
214
  });
@@ -219,7 +228,7 @@ function Make(Spec) {
219
228
  return millis;
220
229
  }
221
230
  };
222
- let apply = async (param, capabilities) => {
231
+ let apply = async (param, capabilities, source) => {
223
232
  let collected = param.collected;
224
233
  for (let i = 0, i_finish = collected.length; i < i_finish; ++i) {
225
234
  let match = collected[i];
@@ -236,7 +245,8 @@ function Make(Spec) {
236
245
  status: "Pending",
237
246
  createdAt: row_createdAt,
238
247
  retryCount: 0,
239
- maxRetries: row_maxRetries
248
+ maxRetries: row_maxRetries,
249
+ source: source
240
250
  };
241
251
  todoItems[id] = row;
242
252
  } else {
@@ -284,8 +294,9 @@ function Make(Spec) {
284
294
  let dict = Stdlib_JSON.Decode.object(json);
285
295
  let eventPayload = dict !== undefined ? Stdlib_Option.getOr(dict["event"], json) : json;
286
296
  let routed = Route.route(eventPayload, sourceName, context.id, ctx);
297
+ let source = sourceName === Clock$Reventless.name ? undefined : Message$ReventlessCore.flowSourceOf(context.meta);
287
298
  for (let j = 0, j_finish = routed.length; j < j_finish; ++j) {
288
- await apply(routed[j], capabilities);
299
+ await apply(routed[j], capabilities, source);
289
300
  }
290
301
  reschedule(routed, context.meta.msgId, Stdlib_Option.getOr(ctx.recordedAt, Date.now()));
291
302
  if (sourceName === Clock$Reventless.name) {
@@ -385,7 +396,7 @@ function Make(Spec) {
385
396
  commandJson = undefined;
386
397
  }
387
398
  Stdlib_Option.forEach(commandJson, commandJson => {
388
- let msg_meta = makeMeta();
399
+ let msg_meta = makeMeta(row);
389
400
  let msg = {
390
401
  id: targetId,
391
402
  meta: msg_meta,
@@ -25,7 +25,9 @@ let componentType = "CommandTopic";
25
25
 
26
26
  let refusalCauseToGraphQL = CommandTopic_Helpers$ReventlessCore.refusalCauseToGraphQL;
27
27
 
28
- let commandOutcomeHook = CommandTopic_Helpers$ReventlessCore.commandOutcomeHook;
28
+ let commandOutcomeHooks = CommandTopic_Helpers$ReventlessCore.commandOutcomeHooks;
29
+
30
+ let nextOutcomeHookId = CommandTopic_Helpers$ReventlessCore.nextOutcomeHookId;
29
31
 
30
32
  let registerCommandOutcome = CommandTopic_Helpers$ReventlessCore.registerCommandOutcome;
31
33
 
@@ -63,7 +65,8 @@ export {
63
65
  componentType,
64
66
  NotPublishedToChannel,
65
67
  refusalCauseToGraphQL,
66
- commandOutcomeHook,
68
+ commandOutcomeHooks,
69
+ nextOutcomeHookId,
67
70
  registerCommandOutcome,
68
71
  clearCommandOutcome,
69
72
  fireCommandOutcome,
@@ -48,51 +48,73 @@ type observedOutcome =
48
48
  | OutcomeAccepted({entityId: option<string>, eventCount: int})
49
49
  | OutcomeRejected({errorCode: string, errorDetail: string, cause: refusalCause})
50
50
 
51
- /** `reference` is the command's `meta.msgId` on every current transport. */
51
+ /** `reference` is the command's `meta.msgId` on every current transport.
52
+ `correlationId` is the command's, where the reporting handler had its meta. */
52
53
  type commandOutcomeReport = {
53
54
  component: string,
54
55
  reference: string,
56
+ correlationId?: string,
55
57
  outcome: observedOutcome,
56
58
  }
57
59
 
58
60
  type commandOutcomeHook = commandOutcomeReport => unit
59
61
 
62
+ type registeredOutcomeHook = {id: int, hook: commandOutcomeHook}
63
+
60
64
  /**
61
- Module-level outcome hook, in the shape the `RuntimeExtension` cold-start seam already reaches
62
- — so an extension registers it from `onColdStart` alongside the interception and publish hooks,
63
- and no new registration path is needed. None = no-op (default).
65
+ Module-level outcome listeners, in the shape the `RuntimeExtension` cold-start seam already
66
+ reaches — so an extension registers one from `onColdStart` alongside the interception and
67
+ publish hooks, and no new registration path is needed. Several may listen at once: an
68
+ extension and the platform that watches itself, say. None registered = no-op (default).
64
69
  */
65
- let commandOutcomeHook: ref<option<commandOutcomeHook>> = ref(None)
70
+ let commandOutcomeHooks: ref<array<registeredOutcomeHook>> = ref([])
71
+ let nextOutcomeHookId = ref(0)
66
72
 
67
- let registerCommandOutcome = (hook: commandOutcomeHook) => {
68
- commandOutcomeHook.contents = Some(hook)
73
+ /** Adds a listener and returns the function that removes it. */
74
+ let registerCommandOutcome = (hook: commandOutcomeHook): (unit => unit) => {
75
+ nextOutcomeHookId := nextOutcomeHookId.contents + 1
76
+ let id = nextOutcomeHookId.contents
77
+ commandOutcomeHooks := commandOutcomeHooks.contents->Array.concat([{id, hook}])
78
+ () => commandOutcomeHooks := commandOutcomeHooks.contents->Array.filter(h => h.id != id)
69
79
  }
70
80
 
81
+ /** Removes every listener. */
71
82
  let clearCommandOutcome = () => {
72
- commandOutcomeHook.contents = None
83
+ commandOutcomeHooks := []
73
84
  }
74
85
 
75
86
  // Fired from reportAccepted/reportRejected below, which both write-side kinds call
76
87
  // unconditionally — so this sees fire-and-forget dispatches too, not just the ones a
77
88
  // producer awaited on the side-channels.
78
89
  //
79
- // A throwing hook is logged and swallowed: the outcome it observes has already happened,
80
- // and letting an observer fail a completed command would make an extension an outage
81
- // (the same failure policy the cold-start seam settled on).
82
- let fireCommandOutcome = (~component: string, ~reference: string, outcome: observedOutcome) =>
83
- commandOutcomeHook.contents->Option.forEach(hook =>
84
- switch hook({component, reference, outcome}) {
85
- | () => ()
86
- | exception err =>
87
- EffectLogger.logError(
88
- ~comp=`CommandTopic(${component})`,
89
- `command-outcome hook threw (ignored): ${err
90
- ->JsExn.fromException
91
- ->Option.flatMap(JsExn.message)
92
- ->Option.getOr("unknown")}`,
93
- )->Effect.runSync
94
- }
95
- )
90
+ // A throwing listener is logged and swallowed, and the others still hear: the outcome
91
+ // it observes has already happened, and letting an observer fail a completed command
92
+ // would make an extension an outage (the same failure policy the cold-start seam
93
+ // settled on).
94
+ let fireCommandOutcome = (
95
+ ~component: string,
96
+ ~reference: string,
97
+ ~correlationId: option<string>=?,
98
+ outcome: observedOutcome,
99
+ ) =>
100
+ switch commandOutcomeHooks.contents {
101
+ | [] => ()
102
+ | hooks =>
103
+ let report = {component, reference, ?correlationId, outcome}
104
+ hooks->Array.forEach(({hook}) =>
105
+ switch hook(report) {
106
+ | () => ()
107
+ | exception err =>
108
+ EffectLogger.logError(
109
+ ~comp=`CommandTopic(${component})`,
110
+ `command-outcome hook threw (ignored): ${err
111
+ ->JsExn.fromException
112
+ ->Option.flatMap(JsExn.message)
113
+ ->Option.getOr("unknown")}`,
114
+ )->Effect.runSync
115
+ }
116
+ )
117
+ }
96
118
 
97
119
  // Side-channel for publishJsonsAndWait result propagation.
98
120
  // Set by runInlineAndCollect before invoking the handler; cleared after.
@@ -143,11 +165,17 @@ let ownershipRefusal = (
143
165
  // knowing which component produced it, and the side-channel (keyed per inline dispatch) never
144
166
  // had to say. `~cause` is required rather than defaulted so a new rejection site has to state
145
167
  // which kind it is instead of silently reporting infrastructure as a domain decision.
146
- let reportAccepted = (~component: string, reference: string, result: acceptedResult) => {
168
+ let reportAccepted = (
169
+ ~component: string,
170
+ ~correlationId: option<string>=?,
171
+ reference: string,
172
+ result: acceptedResult,
173
+ ) => {
147
174
  acceptedResultChannel.contents->Option.forEach(cb => cb(reference, result))
148
175
  fireCommandOutcome(
149
176
  ~component,
150
177
  ~reference,
178
+ ~correlationId?,
151
179
  OutcomeAccepted({entityId: result.entityId, eventCount: result.eventCount}),
152
180
  )
153
181
  }
@@ -155,6 +183,7 @@ let reportAccepted = (~component: string, reference: string, result: acceptedRes
155
183
  let reportRejected = (
156
184
  ~component: string,
157
185
  ~cause: refusalCause,
186
+ ~correlationId: option<string>=?,
158
187
  reference: string,
159
188
  result: rejectedResult,
160
189
  ) => {
@@ -162,6 +191,7 @@ let reportRejected = (
162
191
  fireCommandOutcome(
163
192
  ~component,
164
193
  ~reference,
194
+ ~correlationId?,
165
195
  OutcomeRejected({errorCode: result.errorCode, errorDetail: result.errorDetail, cause}),
166
196
  )
167
197
  }
@@ -22,26 +22,44 @@ function refusalCauseToGraphQL(cause) {
22
22
  }
23
23
  }
24
24
 
25
- let commandOutcomeHook = {
26
- contents: undefined
25
+ let commandOutcomeHooks = {
26
+ contents: []
27
+ };
28
+
29
+ let nextOutcomeHookId = {
30
+ contents: 0
27
31
  };
28
32
 
29
33
  function registerCommandOutcome(hook) {
30
- commandOutcomeHook.contents = hook;
34
+ nextOutcomeHookId.contents = nextOutcomeHookId.contents + 1 | 0;
35
+ let id = nextOutcomeHookId.contents;
36
+ commandOutcomeHooks.contents = commandOutcomeHooks.contents.concat([{
37
+ id: id,
38
+ hook: hook
39
+ }]);
40
+ return () => {
41
+ commandOutcomeHooks.contents = commandOutcomeHooks.contents.filter(h => h.id !== id);
42
+ };
31
43
  }
32
44
 
33
45
  function clearCommandOutcome() {
34
- commandOutcomeHook.contents = undefined;
46
+ commandOutcomeHooks.contents = [];
35
47
  }
36
48
 
37
- function fireCommandOutcome(component, reference, outcome) {
38
- Stdlib_Option.forEach(commandOutcomeHook.contents, hook => {
49
+ function fireCommandOutcome(component, reference, correlationId, outcome) {
50
+ let hooks = commandOutcomeHooks.contents;
51
+ if (hooks.length === 0) {
52
+ return;
53
+ }
54
+ let report = {
55
+ component: component,
56
+ reference: reference,
57
+ correlationId: correlationId,
58
+ outcome: outcome
59
+ };
60
+ hooks.forEach(param => {
39
61
  try {
40
- hook({
41
- component: component,
42
- reference: reference,
43
- outcome: outcome
44
- });
62
+ param.hook(report);
45
63
  return;
46
64
  } catch (raw_err) {
47
65
  let err = Primitive_exceptions.internalToException(raw_err);
@@ -86,18 +104,18 @@ function ownershipRefusal(verdict, component, command) {
86
104
  }
87
105
  }
88
106
 
89
- function reportAccepted(component, reference, result) {
107
+ function reportAccepted(component, correlationId, reference, result) {
90
108
  Stdlib_Option.forEach(acceptedResultChannel.contents, cb => cb(reference, result));
91
- fireCommandOutcome(component, reference, {
109
+ fireCommandOutcome(component, reference, correlationId, {
92
110
  TAG: "OutcomeAccepted",
93
111
  entityId: result.entityId,
94
112
  eventCount: result.eventCount
95
113
  });
96
114
  }
97
115
 
98
- function reportRejected(component, cause, reference, result) {
116
+ function reportRejected(component, cause, correlationId, reference, result) {
99
117
  Stdlib_Option.forEach(rejectedResultChannel.contents, cb => cb(reference, result, cause));
100
- fireCommandOutcome(component, reference, {
118
+ fireCommandOutcome(component, reference, correlationId, {
101
119
  TAG: "OutcomeRejected",
102
120
  errorCode: result.errorCode,
103
121
  errorDetail: result.errorDetail,
@@ -272,7 +290,8 @@ function getHandlers(typeName) {
272
290
 
273
291
  export {
274
292
  refusalCauseToGraphQL,
275
- commandOutcomeHook,
293
+ commandOutcomeHooks,
294
+ nextOutcomeHookId,
276
295
  registerCommandOutcome,
277
296
  clearCommandOutcome,
278
297
  fireCommandOutcome,
@@ -177,7 +177,7 @@ module Make = (
177
177
  ->Option.getOr("")
178
178
  let (eventType, dataDict) = rawEvent->Message.splitMessage
179
179
  switch decoder.decode(~eventType, ~data=dataDict) {
180
- | Some(event) => [(sourceId, event)]
180
+ | Some(event) => [(sourceId, event, Message.metaOfEnvelope(envelope))]
181
181
  | None => []
182
182
  }
183
183
  },
@@ -189,7 +189,11 @@ module Make = (
189
189
  Effect.promise(
190
190
  async () => {
191
191
  // Phase 1: collect outbound items
192
- await Callback.phase1(eventsArr, ~capabilities=Capabilities.capabilities())
192
+ await Callback.phase1(
193
+ eventsArr->Array.map(((sourceId, event, _)) => (sourceId, event)),
194
+ ~metas=eventsArr->Array.map(((_, _, meta)) => meta),
195
+ ~capabilities=Capabilities.capabilities(),
196
+ )
193
197
  // Sync the post-phase-1 TODO state so consumers awaiting the
194
198
  // originating publishEvent observe Pending rows immediately.
195
199
  await syncToQueryDb(queryDbOps)
@@ -102,13 +102,17 @@ function Make(RuntimeEnvironment) {
102
102
  if (event !== undefined) {
103
103
  return [[
104
104
  sourceId,
105
- Primitive_option.valFromOption(event)
105
+ Primitive_option.valFromOption(event),
106
+ Message$ReventlessCore.metaOfEnvelope(envelope)
106
107
  ]];
107
108
  } else {
108
109
  return [];
109
110
  }
110
111
  })), events => Stream$1.fromIterable(events))), eventsArr => Effect.promise(async () => {
111
- await Callback.phase1(eventsArr, Capabilities.capabilities());
112
+ await Callback.phase1(eventsArr.map(param => [
113
+ param[0],
114
+ param[1]
115
+ ]), eventsArr.map(param => param[2]), Capabilities.capabilities());
112
116
  await syncToQueryDb(queryDbOps);
113
117
  Stdlib_Promise.$$catch(Callback.phase2(publishJsonsFn, Capabilities.capabilities()).then(() => syncToQueryDb(queryDbOps)), exn => {
114
118
  let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
@@ -29,6 +29,9 @@ type todoRow = {
29
29
  decode — silently, since the restore swallows it per row. */
30
30
  maxRetries?: int,
31
31
  lastError?: string,
32
+ /** The event that created the row, so the command sent for it continues its
33
+ flow. Absent on rows written before it existed. */
34
+ source?: Message.flowSource,
32
35
  }
33
36
 
34
37
  /** The item schema seen through the `JSON.t` the row actually stores.
@@ -62,6 +65,7 @@ let todoRowSchemaFor = (itemSchema: S.t<'item>): S.t<todoRow> =>
62
65
  retryCount: s.matches(S.int),
63
66
  maxRetries: ?s.matches(S.option(S.int)),
64
67
  lastError: ?s.matches(S.option(S.string)),
68
+ source: ?s.matches(S.option(Message.flowSourceSchema)),
65
69
  })
66
70
 
67
71
  /**
@@ -81,6 +85,7 @@ runtime, as `undefined` destructured from a tuple that was never a tuple.
81
85
  */
82
86
  type phase1<'event> = (
83
87
  array<(string, 'event)>,
88
+ ~metas: array<option<Reventless.Message.meta>>=?,
84
89
  ~capabilities: Reventless.Capabilities.t,
85
90
  ) => promise<unit>
86
91
 
@@ -112,8 +117,9 @@ module type T = {
112
117
 
113
118
  /** Phase 1: update TODO list from a batch of `(sourceId, event)` pairs
114
119
  (collect only, no resolve). `sourceId` is the envelope id of the entity the
115
- event was published for. `~capabilities` supplies the key a personal item's
116
- row id is fingerprinted with. */
120
+ event was published for. `~metas`, by the same index, are the events' metas
121
+ where their envelopes had one: a row keeps its event's flow from them. `~capabilities` supplies the key a
122
+ personal item's row id is fingerprinted with. */
117
123
  let phase1: phase1<Spec.consumedEvent>
118
124
 
119
125
  /** Phase 2: translate all pending items, optionally publishing commands via publishJsons. */
@@ -131,10 +137,10 @@ module Make = (
131
137
 
132
138
  let now = () => Date.make()->Date.toISOString
133
139
 
134
- // Root meta for messages emitted by this OutboundTranslationSlice. Source-event
135
- // meta is not threaded from phase1 → phase2 today, so causation across the
136
- // translation hop is not preserved. If sources later carry meta into todoItems,
137
- // switch to Message.deriveMeta.
140
+ // Meta for a command emitted for a row: derived from the event that created
141
+ // the row, so the command continues its flow (its correlation id, the event's
142
+ // message id as causation, its user and trace parent). A row without a source
143
+ // (written before rows kept one, or given no meta) starts a new flow.
138
144
  //
139
145
  // `service` names the command's TARGET, matching what the API path does
140
146
  // (`CommandGenerator_Callback` passes `~serviceName=AggregateSpec.name`). It is
@@ -145,10 +151,13 @@ module Make = (
145
151
  // no error. Falls back to the slice's own name only when there is no target, in
146
152
  // which case the command goes to the plugin's DCB topic and nothing dispatches
147
153
  // on it.
148
- let makeMeta = (): Reventless.Message.meta =>
149
- Message.generateMeta(
150
- ~service=Spec.targetName->Option.getOr(`OutboundTranslationSlice:${Spec.name}`),
151
- )
154
+ let makeMeta = (row: todoRow): Reventless.Message.meta => {
155
+ let service = Spec.targetName->Option.getOr(`OutboundTranslationSlice:${Spec.name}`)
156
+ switch row.source {
157
+ | Some(source) => Message.deriveMetaFromSource(source, ~service)
158
+ | None => Message.generateMeta(~service)
159
+ }
160
+ }
152
161
 
153
162
  /** Record a failed attempt. The row is marked `Abandoned` at the moment the
154
163
  budget runs out rather than being left to fall out of the next pass's
@@ -184,7 +193,7 @@ module Make = (
184
193
  try {
185
194
  let msg: Reventless.Message.commandJson = {
186
195
  id: targetId,
187
- meta: makeMeta(),
196
+ meta: makeMeta(row),
188
197
  commandJson: cmd->Reventless.Util_Sury.toJson(Spec.inboundCommandSchema),
189
198
  }
190
199
  await publishJsons([msg])
@@ -242,10 +251,13 @@ module Make = (
242
251
 
243
252
  let phase1 = async (
244
253
  events: array<(string, Spec.consumedEvent)>,
254
+ ~metas: array<option<Reventless.Message.meta>>=[],
245
255
  ~capabilities: Reventless.Capabilities.t,
246
256
  ) =>
247
257
  for i in 0 to events->Array.length - 1 {
248
258
  let (sourceId, event) = events->Array.getUnsafe(i)
259
+ let source =
260
+ metas->Array.get(i)->Option.flatMap(meta => meta)->Option.map(Message.flowSourceOf)
249
261
  // Collect new outbound items
250
262
  let collected = Translation.collect(event, ~sourceId)
251
263
  for j in 0 to collected->Array.length - 1 {
@@ -275,6 +287,7 @@ module Make = (
275
287
  createdAt: now(),
276
288
  retryCount: 0,
277
289
  maxRetries: Spec.maxRetries,
290
+ ?source,
278
291
  }
279
292
  todoItems->Dict.set(id, row)
280
293
  }
@@ -396,7 +409,7 @@ module Make = (
396
409
  try {
397
410
  let msg: Reventless.Message.commandJson = {
398
411
  id: targetId,
399
- meta: makeMeta(),
412
+ meta: makeMeta(row),
400
413
  commandJson,
401
414
  }
402
415
  await publishJsons([msg])
@@ -30,7 +30,8 @@ let todoRowSchema = Sury.$schema(s => ({
30
30
  completedAt: s.m(Sury.$option(Sury.string)),
31
31
  retryCount: s.m(Sury.int32),
32
32
  maxRetries: s.m(Sury.$option(Sury.int32)),
33
- lastError: s.m(Sury.$option(Sury.string))
33
+ lastError: s.m(Sury.$option(Sury.string)),
34
+ source: s.m(Sury.$option(Message$ReventlessCore.flowSourceSchema))
34
35
  }));
35
36
 
36
37
  function todoRowSchemaFor(itemSchema) {
@@ -42,14 +43,23 @@ function todoRowSchemaFor(itemSchema) {
42
43
  completedAt: s.m(Sury.$option(Sury.string)),
43
44
  retryCount: s.m(Sury.int32),
44
45
  maxRetries: s.m(Sury.$option(Sury.int32)),
45
- lastError: s.m(Sury.$option(Sury.string))
46
+ lastError: s.m(Sury.$option(Sury.string)),
47
+ source: s.m(Sury.$option(Message$ReventlessCore.flowSourceSchema))
46
48
  }));
47
49
  }
48
50
 
49
51
  function Make(Spec) {
50
52
  return Translation => {
51
53
  let todoItems = {};
52
- let makeMeta = () => Message$ReventlessCore.generateMeta(Stdlib_Option.getOr(Spec.targetName, `OutboundTranslationSlice:` + Spec.name), undefined, undefined, undefined, undefined, undefined, undefined);
54
+ let makeMeta = row => {
55
+ let service = Stdlib_Option.getOr(Spec.targetName, `OutboundTranslationSlice:` + Spec.name);
56
+ let source = row.source;
57
+ if (source !== undefined) {
58
+ return Message$ReventlessCore.deriveMetaFromSource(source, service);
59
+ } else {
60
+ return Message$ReventlessCore.generateMeta(service, undefined, undefined, undefined, undefined, undefined, undefined);
61
+ }
62
+ };
53
63
  let afterFailedAttempt = (row, lastError) => {
54
64
  let retryCount = row.retryCount + 1 | 0;
55
65
  let newrecord = {...row};
@@ -71,7 +81,7 @@ function Make(Spec) {
71
81
  }
72
82
  try {
73
83
  let msg_id = match[0];
74
- let msg_meta = makeMeta();
84
+ let msg_meta = makeMeta(row);
75
85
  let msg_commandJson = Util_Sury$Reventless.toJson(match[1], Spec.inboundCommandSchema);
76
86
  let msg = {
77
87
  id: msg_id,
@@ -110,9 +120,11 @@ function Make(Spec) {
110
120
  return key;
111
121
  }
112
122
  };
113
- let phase1 = async (events, capabilities) => {
123
+ let phase1 = async (events, metasOpt, capabilities) => {
124
+ let metas = metasOpt !== undefined ? metasOpt : [];
114
125
  for (let i = 0, i_finish = events.length; i < i_finish; ++i) {
115
126
  let match = events[i];
127
+ let source = Stdlib_Option.map(Stdlib_Option.flatMap(metas[i], meta => meta), Message$ReventlessCore.flowSourceOf);
116
128
  let collected = Translation.collect(match[1], match[0]);
117
129
  for (let j = 0, j_finish = collected.length; j < j_finish; ++j) {
118
130
  let match$1 = collected[j];
@@ -133,7 +145,8 @@ function Make(Spec) {
133
145
  status: "Pending",
134
146
  createdAt: row_createdAt,
135
147
  retryCount: 0,
136
- maxRetries: row_maxRetries
148
+ maxRetries: row_maxRetries,
149
+ source: source
137
150
  };
138
151
  todoItems[id] = row;
139
152
  } else {
@@ -265,7 +278,7 @@ function Make(Spec) {
265
278
  if (commandJson !== undefined) {
266
279
  try {
267
280
  let msg_id = match$1[0];
268
- let msg_meta = makeMeta();
281
+ let msg_meta = makeMeta(row);
269
282
  let msg$1 = {
270
283
  id: msg_id,
271
284
  meta: msg_meta,
@@ -12,6 +12,11 @@ module Make = (
12
12
 
13
13
  let comp = `ReadModel(${ReadModelSpec.name})`
14
14
 
15
+ // Each row the events change is logged, and handed to whoever listens.
16
+ let onChange = ViewChange.reporter(~comp, ~view=ReadModelSpec.name, ~stateToJson=state =>
17
+ state->Reventless.Util_Sury.toJson(ReadModelSpec.stateSchema)
18
+ )
19
+
15
20
  let handleJsonEvents: EventCollector.jsonEventsHandler = stream =>
16
21
  stream
17
22
  ->Stream.runCollect
@@ -31,19 +36,34 @@ module Make = (
31
36
  json,
32
37
  )} actions:${actionsStr}`,
33
38
  )->Effect.runSync
39
+ // The event each action came from, kept for the change it makes: the
40
+ // entity is the aggregate the event was published for.
41
+ let sources = [
42
+ ViewChange.sourceOf(~meta=context.meta, ~entityId=context.id, ~entityType=sourceName, ()),
43
+ ]
34
44
  // Rewritten here rather than at the apply below: a trail entry is
35
45
  // stamped with this envelope's own time.
36
46
  actions->Array.map(
37
- Projection.rewriteAction(_, ~at=context.meta.time, ReadModelSpec.stateSchema),
47
+ action => (
48
+ action->Projection.rewriteAction(~at=context.meta.time, ReadModelSpec.stateSchema),
49
+ sources,
50
+ ),
38
51
  )
39
52
  })
40
53
  ->Array.flat
41
- ->Array.reduce(Effect.succeed(), (acc, action) =>
54
+ ->Array.reduce(Effect.succeed(), (acc, (action, sources)) =>
42
55
  acc->Effect.flatMap(
43
56
  _ => {
44
57
  Effect.promise(
45
58
  () =>
46
- Projection.handleAction(~comp, action, Spec.operations, ReadModelSpec.subIdConfig),
59
+ Projection.handleAction(
60
+ ~comp,
61
+ ~sources,
62
+ ~onChange,
63
+ action,
64
+ Spec.operations,
65
+ ReadModelSpec.subIdConfig,
66
+ ),
47
67
  )->Effect.map(_ => ())
48
68
  },
49
69
  )
@@ -4,9 +4,11 @@ import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
4
4
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
5
5
  import * as Effect from "effect/Effect";
6
6
  import * as Message$Reventless from "@reventlessdev/reventless-spec/src/types/Message.res.mjs";
7
+ import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
7
8
  import * as Message$ReventlessCore from "../../Message.res.mjs";
8
9
  import * as LogFormat$ReventlessCore from "../../util/LogFormat.res.mjs";
9
10
  import * as Projection$ReventlessCore from "../../Projection.res.mjs";
11
+ import * as ViewChange$ReventlessCore from "../../ViewChange.res.mjs";
10
12
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
11
13
  import * as ProjectionMapper$ReventlessCore from "../../ProjectionMapper.res.mjs";
12
14
 
@@ -19,6 +21,7 @@ function Make(ReadModelSpec) {
19
21
  subIdConfig: ReadModelSpec.subIdConfig
20
22
  })(Mappings);
21
23
  let comp = `ReadModel(` + ReadModelSpec.name + `)`;
24
+ let onChange = ViewChange$ReventlessCore.reporter(comp, ReadModelSpec.name, state => Util_Sury$Reventless.toJson(state, ReadModelSpec.stateSchema));
22
25
  let handleJsonEvents = stream => Effect.flatMap(Stream.runCollect(stream), events => {
23
26
  let total = events.length.toString();
24
27
  return Stdlib_Array.reduce(events.map((json, idx) => {
@@ -28,12 +31,21 @@ function Make(ReadModelSpec) {
28
31
  let actionsStr = LogFormat$ReventlessCore.actionNames(actions);
29
32
  let idxStr = (idx + 1 | 0).toString();
30
33
  Effect.runSync(EffectLogger$ReventlessCore.logInfo(comp, json, `handling event ` + idxStr + `/` + total + ` from ` + sourceName + `: ` + LogFormat$ReventlessCore.eventDetail(json) + ` actions:` + actionsStr));
31
- return actions.map(__x => Projection$ReventlessCore.rewriteAction(__x, context.meta.time, ReadModelSpec.stateSchema));
32
- }).flat(), Effect.succeed(), (acc, action) => Effect.flatMap(acc, () => Effect.map(Effect.promise(() => Projection$ReventlessCore.handleAction(comp, action, Spec.operations, ReadModelSpec.subIdConfig)), param => {})));
34
+ let sources = [ViewChange$ReventlessCore.sourceOf(context.meta, context.id, sourceName, undefined)];
35
+ return actions.map(action => [
36
+ Projection$ReventlessCore.rewriteAction(action, context.meta.time, ReadModelSpec.stateSchema),
37
+ sources
38
+ ]);
39
+ }).flat(), Effect.succeed(), (acc, param) => {
40
+ let sources = param[1];
41
+ let action = param[0];
42
+ return Effect.flatMap(acc, () => Effect.map(Effect.promise(() => Projection$ReventlessCore.handleAction(comp, sources, onChange, action, Spec.operations, ReadModelSpec.subIdConfig)), param => {}));
43
+ });
33
44
  });
34
45
  return {
35
46
  EventProjector: EventProjector,
36
47
  comp: comp,
48
+ onChange: onChange,
37
49
  handleJsonEvents: handleJsonEvents
38
50
  };
39
51
  });