applyPatch static method

Future<PatchApplyResult> applyPatch(
  1. PatchInfo patchInfo, {
  2. void onProgress(
    1. PatchApplyProgress
    )?,
})

Applies the update checkUpdate returned: downloads the patch file, then native code verifies and installs it for the next cold start, as applyPatchBytes does. A withdrawal removes the installed patch.

Returns a PatchApplyResult. ok=true means the patch is installed and will take effect on the next cold start.

Implementation

static Future<PatchApplyResult> applyPatch(
  PatchInfo patchInfo, {
  void Function(PatchApplyProgress)? onProgress,
}) async {
  if (_unsupportedGuard('applyPatch')) {
    return _unsupported();
  }
  final url = patchInfo.raw['url'], size = patchInfo.raw['size'];
  if (url is! String || size is! int) {
    return PatchApplyResult.failure(
      PatchApplyError.invalidArgs,
      'Pass the PatchInfo returned by checkUpdate',
    );
  }
  StreamSubscription<PatchApplyProgress>? sub;
  if (onProgress != null) {
    sub = applyProgress.listen(onProgress);
  }
  String? path;
  try {
    _downloads.add(
      PatchApplyProgress(
        phase: PatchApplyPhase.downloading,
        totalBytes: size,
      ),
    );
    path = await platform_io.downloadPayload(
      url,
      size,
      (received) => _downloads.add(
        PatchApplyProgress(
          phase: PatchApplyPhase.downloading,
          bytesReceived: received,
          totalBytes: size,
        ),
      ),
    );
    return await applyPatchFile(path);
  } on platform_io.PayloadDownloadException catch (e) {
    return PatchApplyResult.failure(PatchApplyError.network, e.toString());
  } catch (e, s) {
    _log('applyPatch failed: $e', s);
    return PatchApplyResult.failure(PatchApplyError.unknown, e.toString());
  } finally {
    if (path != null) await platform_io.deletePayload(path);
    await sub?.cancel();
  }
}