Compress / Decompress

What it is

compress wraps a Func1 or Func2 so that its argument is compressed before being passed in, and decompress wraps it so the argument is decompressed. They are typically used with data pipelines or transport layers.

When to use it

  • Sending large payloads over the network.
  • Persisting data where size matters.
  • Wrapping compression libraries transparently.

Async / sync support

WrapperSupport
Func<R>❌ No
Func1<T, R>✅ Async
Func2<T1, T2, R>✅ Async
FuncSync<R>❌ No

API reference

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// api-reference
// On Func1<T, R>
Func1<T, R> compress<T>({
  required T Function(T data) compressor,
});

// On Func2<T1, T2, R>
Func2<T1, T2, R> compress<T>({
  required T Function(T data) compressor,
});

// On Func1<T, R>
Func1<T, R> decompress<T>({
  required T Function(T data) decompressor,
});

// On Func2<T1, T2, R>
Func2<T1, T2, R> decompress<T>({
  required T Function(T data) decompressor,
});

Parameters

ParameterTypeDescription
compressor / decompressorT Function(T)Transformation applied to the argument(s).

Examples

Basic example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
final send = Func1<String, void>((payload) async {
  await api.send(payload);
}).compress(
  algorithm: CompressionAlgorithm.gzip,
  level: CompressionLevel.balanced,
);

void main() async {
  await send('hello');
}

Real-world example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
final upload = Func1<Uint8List, void>((bytes) async {
  await httpClient.post(body: bytes);
}).compressBytes(
  threshold: 1024,
  algorithm: CompressionAlgorithm.gzip,
);

void main() async {
  await upload(Uint8List(0));
}

Best practices

  • Use compress on the producer side and decompress on the consumer side.
  • Choose a compression algorithm appropriate to your data type.
  • For large objects, consider streaming instead of in-memory compression.

Common pitfalls

  • Type mismatch: compress<T> must match the argument type of the wrapped function.
  • Decompressor exceptions: Malformed data will throw; pair with fallback or recover if needed.