Download this file
| 1 | import Foundation |
| 2 | |
| 3 | // Network awaits alone do not move JSON processing off the main actor. |
| 4 | actor SheetWireWorker { |
| 5 | func decode(_ data: Data) throws -> GridResponse { |
| 6 | try JSONDecoder().decode(GridResponse.self, from: data) |
| 7 | } |
| 8 | |
| 9 | func encode(_ writes: [CellWrite], startingAt offset: Int) throws -> (data: Data, count: Int) { |
| 10 | struct Batch: Encodable { let cells: [CellWrite] } |
| 11 | var count = min(500, writes.count - offset) |
| 12 | var data = try JSONEncoder().encode(Batch(cells: Array(writes[offset..<offset + count]))) |
| 13 | while data.count > 1 << 20 && count > 1 { |
| 14 | count /= 2 |
| 15 | data = try JSONEncoder().encode(Batch(cells: Array(writes[offset..<offset + count]))) |
| 16 | } |
| 17 | return (data, count) |
| 18 | } |
| 19 | } |
| 20 | |