waft-foundation 0.18.2

App, Handle, and Entity for Waft
Documentation

waft-foundation

Flutter's foundation library, and the App every other framework crate runs on. It takes waft-embedder (the host traits and dart:ui's value types) and web-time (the current time on every target, a browser's included), and provides:

  • App and AppCell. The App holds every Flutter object, the singletons, the timers, the microtasks and the async tasks; every framework call takes it as &mut App. AppCell owns it and runs the checkpoint, between turns, where the dropped objects are freed and the microtasks and tasks run.
  • The arena: Ref<T> and Handle<T>. A constructor is new. One that makes an arena object takes &mut App and returns a Ref; one that makes a plain value returns the value, and takes the App only to read it. App::create is the arena call those constructors make. A Ref keeps its object as a Dart reference does; the object is freed at the checkpoint after its last Ref drops. A Handle is the Copy id that keeps nothing: the method receiver (self: Handle<Self>), read through app.get / app.get_mut, and turned back into a Ref with upgrade while something keeps the object, or with keep where something is known to (it panics naming the object if nothing does). A Ref and a bare handle of the same object compare equal. The erased form of a typed Ref is erase / into_erased; AnyRef keeps an object of any type; erased_handle! gives an erased type (Dart's abstract class used as a type) the items every erased type shares. Tree nodes are kept by the tree code (into_tree_node, release_tree_node).
  • ChangeNotifier, Listenable, ValueListenable, ValueNotifier, Listener. A notifier is a ChangeNotifierData field; a tear-off (Listener::handle_method) keeps nothing and is skipped once its receiver is freed; Listener::downgrade gives a listener that removes itself.
  • Entity<T>, GPUI's store for state an app writer owns, which is not where Flutter objects live.
  • DateTime, in UTC or in the local time zone, which the app's platform gives (Platform::time_zone_offset and time_zone_name, the two calls Dart's natives make of the VM), so what makes a local value takes the App; Dart's equivalent-year mapping and its probing for a wall time's offset are done here, over the offsets the platform answers, with no time zone database. SignedDuration, Dart's Duration where it can be negative, such as a zone's offset west of UTC, is waft-embedder's, re-exported here.
  • Timer, Completer, Task, keys, ObserverList and Flutter's foundation constants.
  • Debug tooling, in debug builds: the stale-read message names a freed object's type, creation site and checkpoint; debug_live_objects lists what the arena holds, which waft_test::object_snapshot / assert_no_new_objects count by type and creation site; debug_take_not_disposed and debug_take_not_collected report Flutter's two leak kinds. "Not disposed" covers a ChangeNotifier from its first listener, Ticker, AnimationController, CurvedAnimation and RestorationBucket, and not yet the gesture recognizers or the image types; "not collected" sees the types whose dispose calls mark_disposed and skips tree nodes, which the hit-test path keeps legitimately after the tree releases them; neither sees a type with no dispose, such as a tween, which only a test's waft_test::object_snapshot / assert_no_new_objects catches; debug_take_skipped_tear_offs reports a listener whose receiver was freed without removing it. A creation site is the caller's line only for a constructor marked #[track_caller], and the constructor's own line otherwise.

An app writer needs four rules. Store a Ref for anything you keep — fields of your state and your widgets — and never free anything. Call dispose where Flutter does, for what your state created; it frees nothing, and the object stays readable while something keeps it. A closure you register on an object must not capture a Ref to that same object: capture the state's bare handle and read the field inside, or use a tear-off; breaking this leaks until dispose and does not crash. After an await or a timer, ask mounted before using a state or a BuildContext.

Every framework crate above it uses it: scheduler, gestures, services, painting, rendering, widgets and the kits. The porting rules for its types are in .agents/skills/porting-flutter/patterns/ownership.md and handle-receiver.md.