Skip to content

Commit

Permalink
computed_collections: Fix flat for the new dispose semantics
Browse files Browse the repository at this point in the history
  Use the local computed, instead of the published version
  • Loading branch information
mstniy committed Sep 17, 2024
1 parent a172e68 commit bbad7d9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
101 changes: 54 additions & 47 deletions packages/computed_collections/lib/src/flat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,62 @@ class FlatComputedMap<K1, K2, V>
final snapshotComputation = nested.snapshot;
final changeStream = nested.changes;
final initialPrevToken = IMap<K2, V>.empty();
return Computed<IMap<K2, V>>.withPrev((prev) {
final IMap<K2, V> snap;
final ChangeEvent<K2, V>? 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<K2, V>? 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<K2, V>():
merger.add(KeyChanges(
change.changes.map((k2, r) => MapEntry((k1, k2), r))));
case ChangeEventReplace<K2, V>():
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<V>()))));
}
// 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<K2, V>? snapMutable;
return Computed<IMap<K2, V>>.withPrev(
(prev) {
final IMap<K2, V> snap;
final ChangeEvent<K2, V>? 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<K2, V>? 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<K2, V>():
merger.add(KeyChanges(change.changes
.map((k2, r) => MapEntry((k1, k2), r))));
case ChangeEventReplace<K2, V>():
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<V>()))));
}
// 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
Expand Down
9 changes: 4 additions & 5 deletions packages/computed_collections/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
3 changes: 2 additions & 1 deletion packages/computed_collections/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbad7d9

Please sign in to comment.