Explorer

definy.mirror-events-loop

description
Stores events with definy.mirror-sync-event in order until limit of them are stored, or one has contents still missing (events that fail are skipped and not counted). This keeps one Cron run of definy.mirror-receive within the CPU time limit of Cloudflare Workers
type
Fn(
  remote: String,
  content_limit: Number,
  events: List<@definy.stored-event>,
  index: Number,
  limit: Number,
  count: Number,
  errors: List<String>,
  then: Fn(summary: Record{count: Number, errors: List<String>}) -> @definy.service-io
) -> @definy.service-io
expression
fn(remote$0, content_limit$1, events$2, index$3, limit$4, count$5, errors$6, then$7) =>
  match @core.less-than(left: $5, right: $4) {
    false =>
      $7(summary: {count: $5, errors: $6}),
    true =>
      match @core.list-get(list: $2, index: $3) {
        #none(_) =>
          $7(summary: {count: $5, errors: $6}),
        #some(event$8) =>
          @definy.mirror-fetch-and-sync-event(
            remote: $0,
            content_limit: $1,
            event: $8,
            then: fn(result$9) =>
              let label$10 = @std.string-join(items: [$8.event_type, " ", $8.event_hash, ": "], separator: "");
              match $9 {
                #stored(_) =>
                  @definy.mirror-events-loop(
                    remote: $0,
                    content_limit: $1,
                    events: $2,
                    index: @core.plus(left: $3, right: 1),
                    limit: $4,
                    count: @core.plus(left: $5, right: 1),
                    errors: $6,
                    then: fn(summary$13) =>
                      $7(summary: $13)
                  ),
                #partial(message$11) =>
                  $7(
                    summary: {
                      count: $5,
                      errors: @core.list-append(list: $6, item: @std.string-join(items: [$10, $11], separator: ""))
                    }
                  ),
                #failed(message$12) =>
                  @definy.mirror-events-loop(
                    remote: $0,
                    content_limit: $1,
                    events: $2,
                    index: @core.plus(left: $3, right: 1),
                    limit: $4,
                    count: $5,
                    errors: @core.list-append(list: $6, item: @std.string-join(items: [$10, $12], separator: "")),
                    then: fn(summary$14) =>
                      $7(summary: $14)
                  )
              }
          )
      }
  }
visibility
public
definy.mirror-events-loop (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: HkH4HVRb6H50zJ1hHxe257h65l_yWjoqSKSH9rZilTg
content hash: a9qoAFtRMmvK33zzm7epaTJhoIyeCnGcN_lGCdDPiRI