ui_app

source of persisted

1// The app's store (decisions/F12.md, "Storage"): a std::storage2// store named "app" under the app's name, opened the first time the Ui3// is asked for it. What a handler set in it is written back once the4// event is handled, at most once a second, natively on a worker; and as5// the app ends, after a write under way, never the older over the newer.6// On the web each set reaches the page's localStorage at once. None of7// it is in a program not asking for the store: its state is an extension8// installed as it opens.910import std::string;11import std::vector;12import std::option;13import std::sync;14import std::time;15import std::process;16import std::json;17import std::storage;1819import pkg::alloy::ui;20import pkg::alloy::ui::store;21import pkg::alloy::ui::tree;22import pkg::alloy::ui::layout;23import pkg::alloy::ui::painting;24import pkg::alloy::ui::events;25import pkg::alloy::ui::widgets;26import pkg::alloy::ui::fields;27import pkg::alloy::ui::scrolling;28import pkg::alloy::ui::running;29import pkg::alloy::ui::timers;30import pkg::alloy::ui::layers;31import pkg::alloy::ui::motion;32import pkg::alloy::ui::transitions;33import pkg::alloy::ui::languages;34import pkg::alloy::ui::extensions;35import pkg::alloy::ui::views;36import ui_app;37import network;3839// The store and its writing, written back after each event and as the40// app ends (natively: the page keeps each set at once).41pub type KeptState : Extension = struct {42    pub store: KeptStore,43    pub writing: bool, // on a worker44    pub last: Option<u64>, // when the last write started, on the window's clock45    pub waiting: bool, // for a second to pass since then46};4748pub fn handled(self k: &var KeptState, ui: &var Ui) {49    if (target() != "web-wasm32") {50        ui.write_store();51    }52}5354pub fn closing(self k: &var KeptState, ui: &var Ui) {55    if (target() != "web-wasm32") {56        ui.end_store();57    }58}5960// Writes no more often than this.61const SPACING: u64 = 1000000000;6263pub fn kept_state(self ui: &var Ui) -> &var KeptState {64    if (ui.extension<KeptState>() is ::None) {65        var store = KeptStore::open(&ui.app.view(), &"app");66        ui.install(new KeptState {67            .store = move store,68            .writing = false,69            .last = ::None,70            .waiting = false71        });72    }73    if (ui.extension_var<KeptState>() is ::Some |&var k|) {74        return &var k;75    }76    panic "pkg::alloy::ui: no store";77}7879// After an event is handled: the store's changes written, a second after80// the last write began at the earliest.81fn write_store(self ui: &var Ui) {82    const state = &var ui.kept_state();83    if (state.writing || state.waiting || state.store.saved()) {84        return;85    }86    const now = ui.time.nanoseconds;87    if (state.last is ::Some |last| && now - last < SPACING) {88        state.waiting = true;89        const wait = Duration::from_nanoseconds(SPACING - (now - last));90        // the Ui's own timer, which no owner going takes91        const owner = ui.owner;92        ui.owner = 0;93        ui.after(wait, (ui: &var Ui) {94            ui.kept_state().waiting = false;95        });96        ui.owner = owner;97        return;98    }99    var w = if (state.store.unsaved() is ::Some |move w|) move w else {100        return;101    };102    state.writing = true;103    state.last = ::Some(now);104    if (ui.network_of() is ::Some |&network|) {105        network.store_writing.store(true);106        ui.jobs.push(|move w| (workers: &Workers) -> |=| (&var Ui) {107            const ok = w.write();108            if (network_in(&workers) is ::Some |&n|) {109                n.store_writing.store(false);110            }111            return |move w, ok| (ui: &var Ui) => ui.store_wrote(&w, ok);112        });113    } else {114        const ok = w.write();115        ui.store_wrote(&w, ok);116    }117}118119fn store_wrote(self ui: &var Ui, w: &StoreWrite, ok: bool) {120    const state = &var ui.kept_state();121    state.writing = false;122    state.store.wrote(&w, ok);123}124125// As the app ends: what is left written now, after the write a worker126// may still be making.127fn end_store(self ui: &var Ui) {128    if (ui.network_of() is ::Some |&network|) {129        while (network.store_writing.load()) {130            sleep(Duration::from_milliseconds(1));131        }132    }133    const state = &var ui.kept_state();134    if (state.store.unsaved() is ::Some |&w|) {135        const ok = w.write();136        state.store.wrote(&w, ok);137    }138}139140// A signal holding what the store keeps under the key, written back as141// it changes.142pub fn persist<T>(143    self ui: &var Ui,144    key: &[u8],145    initial: T,146    write: |=| (&T) -> Json,147    read: |=| (&Json) -> Option<T>148) -> Signal<T> {149    var value = move initial;150    if (ui.store().get(&key) is ::Some |&json| && read(&json) is ::Some |move v|) {151        value = move v;152    }153    const signal = ui.signal(move value);154    var name = String::from(&key);155    const effect = ui.new_effect();156    // the first run only reads, what was read being kept already157    ui.tracked(effect, &|signal| (ui: &Ui) -> bool {158        signal.get(&ui);159        return true;160    });161    ui.set_effect(effect, ::None, |move name, move write, signal| (ui: &var Ui) {162        var json = write(&signal.get(&ui));163        ui.store().set(&name.view(), move json);164    });165    return signal;166}