MCPcopy Create free account
hub / github.com/Effect-TS/effect / groupCompaction

Function groupCompaction

packages/effect/src/unstable/eventlog/EventLog.ts:570–655  ·  view source on GitHub ↗
(
  group: EventGroup.EventGroup<Events>,
  effect: (options: {
    readonly primaryKey: string
    readonly entries: ReadonlyArray<Entry>
    readonly events: ReadonlyArray<Event.TaggedPayload<Events>>
    readonly write: <Tag extends Event.Tag<Events>>(
      tag: Tag,
      payload: Event.PayloadWithTag<Events, Tag>
    ) => Effect.Effect<void, never, Event.PayloadSchemaWithTag<Events, Tag>["EncodingServices"]>
  }) => Effect.Effect<void, never, R>
)

Source from the content-addressed store, hash-verified

568 * @since 4.0.0
569 */
570export const groupCompaction = <Events extends Event.Any, R>(
571 group: EventGroup.EventGroup<Events>,
572 effect: (options: {
573 readonly primaryKey: string
574 readonly entries: ReadonlyArray<Entry>
575 readonly events: ReadonlyArray<Event.TaggedPayload<Events>>
576 readonly write: <Tag extends Event.Tag<Events>>(
577 tag: Tag,
578 payload: Event.PayloadWithTag<Events, Tag>
579 ) => Effect.Effect<void, never, Event.PayloadSchemaWithTag<Events, Tag>["EncodingServices"]>
580 }) => Effect.Effect<void, never, R>
581): Layer.Layer<never, never, R | Event.PayloadSchema<Events>["DecodingServices"] | Registry> =>
582 Layer.effectDiscard(
583 Effect.gen(function*() {
584 const registry = yield* Registry
585 const services = yield* Effect.context<R | Event.PayloadSchema<Events>["DecodingServices"]>()
586
587 yield* registry.registerCompaction({
588 events: Object.keys(group.events),
589 effect: Effect.fnUntraced(function*({ entries, write }): Effect.fn.Return<void> {
590 const isEventTag = (tag: string): tag is Event.Tag<Events> => Object.hasOwn(group.events, tag)
591 const decodePayload = <Tag extends Event.Tag<Events>>(tag: Tag, payload: Uint8Array) =>
592 Schema.decodeUnknownEffect(group.events[tag].payloadMsgPack)(payload).pipe(
593 Effect.updateContext((input) => Context.merge(services, input)),
594 Effect.orDie
595 ) as unknown as Effect.Effect<Event.PayloadWithTag<Events, Tag>>
596 const writePayload = Effect.fnUntraced(function*<Tag extends Event.Tag<Events>>(
597 timestamp: number,
598 tag: Tag,
599 payload: Event.PayloadWithTag<Events, Tag>
600 ): Effect.fn.Return<void, never, Event.PayloadSchemaWithTag<Events, Tag>["EncodingServices"]> {
601 const event = Object.hasOwn(group.events, tag) ? group.events[tag] : undefined!
602 const entry = new Entry({
603 id: makeEntryIdUnsafe({ msecs: timestamp }),
604 event: tag,
605 payload: yield* Schema.encodeUnknownEffect(event.payloadMsgPack)(payload).pipe(
606 Effect.orDie
607 ) as any,
608 primaryKey: event.primaryKey(payload)
609 }, { disableChecks: true })
610 yield* write(entry)
611 })
612
613 const byPrimaryKey = new Map<
614 string,
615 {
616 readonly entries: Array<Entry>
617 readonly taggedPayloads: Array<Event.TaggedPayload<Events>>
618 }
619 >()
620 for (const entry of entries) {
621 if (!isEventTag(entry.event)) {
622 continue
623 }
624 const payload = yield* decodePayload(entry.event, entry.payload)
625 const record = byPrimaryKey.get(entry.primaryKey)
626 const taggedPayload = { _tag: entry.event, payload } as unknown as Event.TaggedPayload<Events>
627 if (record) {

Callers

nothing calls this directly

Calls 10

makeEntryIdUnsafeFunction · 0.90
isEventTagFunction · 0.85
contextMethod · 0.80
pushMethod · 0.80
mergeMethod · 0.80
writeFunction · 0.70
decodePayloadFunction · 0.70
pipeMethod · 0.65
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected