| 461 | * @since 4.0.6 |
| 462 | */ |
| 463 | export const handler = < |
| 464 | Tag extends string, |
| 465 | Fields extends Schema.Struct.Fields, |
| 466 | Key extends keyof Fields & string, |
| 467 | Error extends Schema.Top, |
| 468 | KeySchema extends Schema.Constraint, |
| 469 | RSet = never, |
| 470 | RPatch = never, |
| 471 | RRemove = never |
| 472 | >( |
| 473 | row: EventRow<Tag, Fields, Key, Error, KeySchema>, |
| 474 | callbacks: { |
| 475 | readonly set: ( |
| 476 | row: Schema.Struct<Fields>["Type"], |
| 477 | key: Schema.Schema.Type<KeySchema> |
| 478 | ) => Effect.Effect<unknown, Error["Type"], RSet> |
| 479 | readonly patch: ( |
| 480 | key: Schema.Schema.Type<KeySchema>, |
| 481 | fields: Schema.Struct<PatchFields<Fields, Key>>["Type"] |
| 482 | ) => Effect.Effect<unknown, Error["Type"], RPatch> |
| 483 | readonly remove: (key: Schema.Schema.Type<KeySchema>) => Effect.Effect<unknown, Error["Type"], RRemove> |
| 484 | } |
| 485 | ): (options: { |
| 486 | readonly storeId: StoreId |
| 487 | readonly payload: Payload<Fields, Key, KeySchema> |
| 488 | readonly entry: Entry |
| 489 | readonly conflicts: ReadonlyArray<{ |
| 490 | readonly entry: Entry |
| 491 | readonly payload: Payload<Fields, Key, KeySchema> |
| 492 | }> |
| 493 | }) => Effect.Effect<void, Error["Type"], RSet | RPatch | RRemove | DecodingServices<Fields, Key, KeySchema>> => { |
| 494 | const decode = Schema.decodeUnknownEffect(SchemaBinary.toCodec(row.payload)) |
| 495 | const keyFrom: RowKey["fromRow"] = (row as any)[keyFromId] |
| 496 | return (options) => |
| 497 | Effect.flatMap( |
| 498 | Effect.orDie(decode(options.entry.payload)), |
| 499 | (payload): Effect.Effect<unknown, Error["Type"], RSet | RPatch | RRemove> => { |
| 500 | const op = resolve(payload as Op, options.conflicts as ReadonlyArray<{ readonly payload: Op }>) |
| 501 | if (op === undefined) return Effect.void |
| 502 | switch (op._tag) { |
| 503 | case "set": |
| 504 | return callbacks.set(op.row as any, keyFrom(op.row) as any) |
| 505 | case "patch": |
| 506 | return Object.keys(op.fields).length === 0 ? Effect.void : callbacks.patch(op.key as any, op.fields as any) |
| 507 | case "remove": |
| 508 | return callbacks.remove(op.key as any) |
| 509 | } |
| 510 | } |
| 511 | ).pipe(Effect.asVoid) |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Registers a compactor for one row event of a group. |