Explorer

definy.mirror-sync-event

description
Stores one event of remote with definy.mirror-submit. When contents are missing, fetches and stores at most content_limit of them from remote and submits again; #partial means some are still missing (the next run continues). definy.mirror-receive uses it
type
Fn(
  remote: String,
  content_limit: Number,
  event: @definy.stored-event,
  then: Fn(result: Union{#stored, #partial(String), #failed(String)}) -> @definy.service-io
) -> @definy.service-io
expression
fn(remote$0, content_limit$1, event$2, then$3) =>
  let request$4 = #object<@definy.json>([{key: "signedEventBytes", value: #string<@definy.json>($2.signed_event_bytes)}]);
  @definy.sync-local(
    io: @definy.mirror-submit(request: $4),
    then: fn(result$5) =>
      match $5 {
        #error(message$6) =>
          $3(result: #failed($6)),
        #ok(json$7) =>
          match @definy.json-get-string(value: $7, key: "status") {
            #some("missing_content") =>
              @definy.sync-remote-rpc(
                remote: $0,
                method: "GetContent",
                request: #object<@definy.json>([
                  {
                    key: "contentHashes",
                    value: #array<@definy.json>(@definy.json-string-array(
                      items: @definy.strings-take(
                        items: match @definy.json-get-strings(value: $7, key: "missingContentHashes") {
                          #some(hashes$8) =>
                            $8,
                          #none(_) =>
                            []
                        },
                        count: $1,
                        index: 0,
                        acc: []
                      ),
                      index: 0
                    ))
                  }
                ]),
                then: fn(result$9) =>
                  match $9 {
                    #error(message$10) =>
                      $3(result: #failed($10)),
                    #ok(json$11) =>
                      @definy.sync-local(
                        io: @definy.upload-content(
                          request: #object<@definy.json>([{key: "items", value: #array<@definy.json>(@definy.json-get-array(value: $11, key: "items"))}])
                        ),
                        then: fn(result$12) =>
                          match $12 {
                            #error(message$13) =>
                              $3(result: #failed($13)),
                            #ok(_) =>
                              @definy.sync-local(
                                io: @definy.mirror-submit(request: $4),
                                then: fn(result$14) =>
                                  match $14 {
                                    #error(message$15) =>
                                      $3(result: #failed($15)),
                                    #ok(json$16) =>
                                      match @definy.json-get-string(value: $16, key: "status") {
                                        #some("missing_content") =>
                                          $3(result: #partial("some contents of the event are still missing (stored the first ones)")),
                                        _ =>
                                          $3(result: #stored)
                                      }
                                  }
                              )
                          }
                      )
                  }
              ),
            _ =>
              $3(result: #stored)
          }
      }
  )
visibility
public
definy.mirror-sync-event (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: --YTAKYhFhsQI13SpALhKHiy9KXEC4_pHoCs_ehIczg
content hash: brPGaVE4r4rhWKZrG10n4KDoTO_J1DEcI01O-dIv3-c