diff --git a/packages/computed_collections/lib/src/flat.dart b/packages/computed_collections/lib/src/flat.dart index 56bcc7f..0370ef1 100644 --- a/packages/computed_collections/lib/src/flat.dart +++ b/packages/computed_collections/lib/src/flat.dart @@ -28,55 +28,62 @@ class FlatComputedMap final snapshotComputation = nested.snapshot; final changeStream = nested.changes; final initialPrevToken = IMap.empty(); - return Computed>.withPrev((prev) { - final IMap snap; - final ChangeEvent? change; - try { - snap = snapshotComputation.use; - change = getIfChanged(changeStream); - } on NoValueException { - // This must be coming from the nested snapshot - // Can't do much without a snapshot - throw NoValueException(); - } catch (e) { - merger.addError(e); - // Throwing the exception causes withPrev to stop computing us - rethrow; - } - - IMap? snapPrev; - try { - snapPrev = snapshotComputation.prev; - } on NoValueException { - // Leave null - } - - if (identical(prev, initialPrevToken)) { - // This is the initial snapshot - broadcast the new key products - merger.add(KeyChanges( - snap.map((k2, v) => MapEntry((k1, k2), ChangeRecordValue(v))))); - } else if (change != null) { - switch (change) { - case KeyChanges(): - merger.add(KeyChanges( - change.changes.map((k2, r) => MapEntry((k1, k2), r)))); - case ChangeEventReplace(): - if (snapPrev != null) { - // Broadcast a deletion for all the key products of the old snapshot - merger.add(KeyChanges(snapPrev.map( - (k2, _) => MapEntry((k1, k2), ChangeRecordDelete())))); - } - // Broadcast the new key products - merger.add(KeyChanges(change.newCollection - .map((k2, v) => MapEntry((k1, k2), ChangeRecordValue(v))))); - } - } - return snap; - }, + // Keep a copy here so that onCancel can use it + IMap? snapMutable; + return Computed>.withPrev( + (prev) { + final IMap snap; + final ChangeEvent? change; + try { + snap = snapshotComputation.use; + change = getIfChanged(changeStream); + } on NoValueException { + // This must be coming from the nested snapshot + // Can't do much without a snapshot + throw NoValueException(); + } catch (e) { + merger.addError(e); + // Throwing the exception causes withPrev to stop computing us + rethrow; + } + + IMap? snapPrev; + try { + snapPrev = snapshotComputation.prev; + } on NoValueException { + // Leave null + } + + if (identical(prev, initialPrevToken)) { + // This is the initial snapshot - broadcast the new key products + merger.add(KeyChanges(snap.map( + (k2, v) => MapEntry((k1, k2), ChangeRecordValue(v))))); + } else if (change != null) { + switch (change) { + case KeyChanges(): + merger.add(KeyChanges(change.changes + .map((k2, r) => MapEntry((k1, k2), r)))); + case ChangeEventReplace(): + if (snapPrev != null) { + // Broadcast a deletion for all the key products of the old snapshot + merger.add(KeyChanges(snapPrev.map((k2, _) => + MapEntry((k1, k2), ChangeRecordDelete())))); + } + // Broadcast the new key products + merger.add(KeyChanges(change.newCollection.map((k2, v) => + MapEntry((k1, k2), ChangeRecordValue(v))))); + } + } + snapMutable = snap; + return snap; + }, initialPrev: initialPrevToken, async: true, - dispose: (snap) => merger.add(KeyChanges(snap - .map((k2, _) => MapEntry((k1, k2), ChangeRecordDelete()))))) + onCancel: () { + merger.add(KeyChanges((snapMutable ?? const IMap.empty()) + .map((k2, _) => MapEntry((k1, k2), ChangeRecordDelete())))); + snapMutable = null; + }) // Explicitly ignore exceptions thrown by the computation, // as they must be coming from the nested map and we handle // them by adding them to the merger before throwing ourselves diff --git a/packages/computed_collections/pubspec.lock b/packages/computed_collections/pubspec.lock index 46468da..3a6879e 100644 --- a/packages/computed_collections/pubspec.lock +++ b/packages/computed_collections/pubspec.lock @@ -52,10 +52,9 @@ packages: computed: dependency: "direct main" description: - name: computed - sha256: da0f24e5cb61545f5602d207348b5e35f2521d5c71095c837982195b7135c10c - url: "https://pub.dev" - source: hosted + path: "../computed" + relative: true + source: path version: "0.5.0" convert: dependency: transitive @@ -394,4 +393,4 @@ packages: source: hosted version: "3.1.2" sdks: - dart: ">=3.2.1 <4.0.0" + dart: ">=3.4.0 <4.0.0" diff --git a/packages/computed_collections/pubspec.yaml b/packages/computed_collections/pubspec.yaml index 3961599..7c33001 100644 --- a/packages/computed_collections/pubspec.yaml +++ b/packages/computed_collections/pubspec.yaml @@ -10,7 +10,8 @@ environment: dependencies: meta: ">=1.3.0 <2.0.0" fast_immutable_collections: "^10.2.4" - computed: "^0.5.0" + computed: + path: ../computed dev_dependencies: lints: ^4.0.0 matcher: ^0.12.16