(ctx context.Context, c *websocket.Conn, v any)
| 50 | } |
| 51 | |
| 52 | func write(ctx context.Context, c *websocket.Conn, v any) (err error) { |
| 53 | defer errd.Wrap(&err, "failed to write JSON message") |
| 54 | |
| 55 | // json.Marshal cannot reuse buffers between calls as it has to return |
| 56 | // a copy of the byte slice but Encoder does as it directly writes to w. |
| 57 | err = json.NewEncoder(util.WriterFunc(func(p []byte) (int, error) { |
| 58 | err := c.Write(ctx, websocket.MessageText, p) |
| 59 | if err != nil { |
| 60 | return 0, err |
| 61 | } |
| 62 | return len(p), nil |
| 63 | })).Encode(v) |
| 64 | if err != nil { |
| 65 | return fmt.Errorf("failed to marshal JSON: %w", err) |
| 66 | } |
| 67 | return nil |
| 68 | } |
no test coverage detected
searching dependent graphs…