Observers


How to use the observers and async browser APIs of the bit Butil?

Usage
These reactive and async browser APIs are spread across a few classes and element extension methods. Inject the ones you need (the element observers take the injected IJSRuntime) and use them like this:
@inject Bit.Butil.Performance performance
@inject Bit.Butil.StorageManager storageManager
@inject Bit.Butil.NetworkInformation networkInformation
@inject Bit.Butil.BroadcastChannel broadcastChannel
@inject Bit.Butil.IndexedDb indexedDb
@inject IJSRuntime js

@code {
    var sub = await element.ObserveIntersection(js, entries => { /* ... */ });
    await using var db = await indexedDb.Open("my-db", 1, [new IndexedDbStoreSchema { Name = "items", KeyPath = "id" }]);
}
Element observers
The ObserveIntersection, ObserveResize and ObserveMutations extension methods wire the matching browser observers onto an ElementReference. Each returns a ButilSubscription you dispose to stop observing. The samples below all watch this shared target element:

target

ObserveIntersection:
Fires when the target enters or leaves the viewport, reporting the intersection ratio and visibility (MDN).



ObserveResize:
Reports size changes when the target is resized, surfacing the new content-rect dimensions (MDN).



ObserveMutations:
Logs DOM changes on the target element, such as attribute mutations (MDN).

Performance
The Performance class wraps the Performance timing and marker API.

Mark / Measure / GetEntries:
Adds named marks to the timeline, measures the elapsed time between them, and reads back the recorded entries (MDN).



SubscribeObserver:
Subscribes a PerformanceObserver to one or more entry types and reports each batch as it arrives (MDN).

StorageManager & NetworkInformation
The StorageManager class wraps navigator.storage and the NetworkInformation class wraps the Network Information API.

Estimate:
Reports an estimate of the storage quota and current usage for the origin (MDN).



GetStatus:
Returns the online flag, effective connection type, downlink and round-trip-time estimates (MDN).

BroadcastChannel
The BroadcastChannel class wraps the BroadcastChannel API for cross-tab pub/sub on the same origin.

Subscribe / Post:
Subscribes to a named channel and posts messages to every other listener on that channel in the same origin (the sender does not receive its own message). Open a second tab to see messages arrive (MDN).

IndexedDB
The IndexedDb class is a lightweight wrapper over IndexedDB.

Open / Put / Get:
Opens (and upgrades if needed) a database, stores a record and reads it back by key (MDN).