cancel method
void
cancel()
Cancel observation. No-op if this StoreObserver is already cancelled.
The handler that was passed in when calling Store.registerObserver will no longer be called, and changes will no longer emit any new events.
A StoreObserver is considered cancelled when the owning Ditto object is closed.
Implementation
void cancel() {
// Guard on isCancelled (not the raw _cancelled field) so a post-close
// cancel() honors the docstring's no-op contract — `_ditto.ptr` below
// would otherwise throw DittoClosedException. StoreObserverV2.cancel()
// already uses this shape; SDKS-3916 brings V1 into line.
if (isCancelled) return;
_cancelled = true;
// Only stop the live query if it was actually started. In the lazy-start
// path, an observer cancelled before any listener attached has no live
// query to stop (SDKS-3878).
if (_liveQueryStarted) {
core.dittoLiveQueryStop(_ditto.ptr, _liveQueryId);
}
// Close the stream first so no further results surface to listeners.
_controller.close();
// Drop this observer from the owning store's active-observer set so it no
// longer appears in [Store.observers] (mirrors AttachmentFetcher.stop).
_ditto.store.removeObserver(this);
// Defer freeing the native resources. As of SDKS-4719 the legacy observer
// is delivered through a `RawReceivePort` + a `Dart_PostCObject` C
// trampoline (see `dittoffiTryExperimentalRegisterChangeObserverStr`), not
// a `NativeCallable`. The freeable here just closes that port. Closing it
// is always safe, and a callback the Rust tokio runtime has already
// dispatched then posts to a closed port (a no-op) instead of aborting the
// Dart VM with "Callback invoked after it has been deleted" — the crash
// reported with active sync over a large collection (SPO-1022) and, on hot
// restart, SDKS-4719. The native `ctx` cell holding the port is
// `Arc`-refcounted via the retain/release pair given at registration, so a
// late trampoline invocation reads a still-live cell and posts to a closed
// port (a no-op); see the registration wrapper.
//
// The `drainThenClose` delay is retained for defense-in-depth and
// consistency with the sibling observer paths; it is no longer load-bearing
// for crash-safety on THIS path (closing/posting is now abort-proof). Do
// not drop the empirical drains on the sibling `NativeCallable.listener`
// paths (V2, logger, presence) without per-platform — especially Windows —
// evidence; that assumption has regressed before (SDKS-4362). Those paths
// still need the same port-based treatment: SDKS-4734. On web the freeable
// is a no-op.
unawaited(core.drainThenClose(_freeable));
}