diff options
| author | eunhhu <[email protected]> | 2026-09-25 15:49:48 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-09-25 15:49:48 +0100 |
| commit | 18675f3e45fa0896efd777bf66ad0ec4c8157d36 (patch) | |
| tree | 058a22fc929e974cef70cc1ddbd5dc54f56bf55c /prototypes/connections/module.h | |
| parent | 49c29a1255e1ad3f7fde6c64910c7d2c38edad78 (diff) | |
| download | pure-18675f3e45fa0896efd777bf66ad0ec4c8157d36.tar.gz pure-18675f3e45fa0896efd777bf66ad0ec4c8157d36.zip | |
Keep app states in the tree store
When the data host keeps states (PureData.state and .save, implemented by
pure_tree_data), every state of an app lives in the store: a text state's
draft is its stored value, a list mirrors each change into a stored draft
(record header, then items), and numbers, cursors and indices are stored
together by name. An app starts from what is stored, moves list fields by
name when a record changed, resets cursors and indices that no longer fit,
and refuses damaged stored values.
Each key, text and message event publishes the states it changed together
with the message's document in one store commit, or fails and leaves
memory and store as they were. Frame changes go with the next event or
pure_app_save. pure-run takes --store PATH.
Tests run the same random events on an app in memory and on a stored app
that restarts every few events and require equal states after every step;
kill a process at every commit stage of an event that changes a number, a
text and a list; and check frames, message atomicity and migration by
restart and by replacement. A stored event costs about 10.6 ms more on the
Pi's SD card, mostly two fsyncs.
Diffstat (limited to 'prototypes/connections/module.h')
| -rw-r--r-- | prototypes/connections/module.h | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/prototypes/connections/module.h b/prototypes/connections/module.h index dd20d4a..d2d4b7a 100644 --- a/prototypes/connections/module.h +++ b/prototypes/connections/module.h @@ -121,6 +121,12 @@ typedef struct { PureResult (*splice)(void *context, void *document, size_t offset, size_t remove, PureSpan insert); PureResult (*commit)(void *context, void *document, int64_t *revision); void (*close)(void *context, void *document); + /* Optional: the host keeps the app's states. state opens a runtime-owned + * value by name as a draft (a missing one is empty at revision 0); save + * publishes the given state drafts together with document (or NULL), + * all or nothing, and names the document's new revision. */ + PureResult (*state)(void *context, const char *name, PureDraft **out); + PureResult (*save)(void *context, void *document, PureDraft *const *states, size_t count, int64_t *revision); } PureData; /* A byte or text value inside one event or view: a range of the payload, a @@ -131,7 +137,11 @@ typedef struct { unsigned char *bytes; size_t used, capacity; } PureArena; /* One app instance: its states and at most one open event. Text states are * runtime-owned drafts; cursors are positions the runtime keeps on character * boundaries; lists are runtime-owned and an index always names an item - * (or 0 in an empty list). Every event is atomic: on failure nothing changes. */ + * (or 0 in an empty list). Every event is atomic: on failure nothing changes. + * When the data host keeps states, the app starts from the stored ones and + * every key, text and message event stores what it changed with its own + * commit (with the message's document); frame events are stored with the + * next one, or by pure_app_save. */ typedef struct { const PureModule *module; PureData data; @@ -139,6 +149,9 @@ typedef struct { PureDraft *texts[PURE_MODULE_STATES]; PureList *lists[PURE_MODULE_STATES]; PureUndo undo; /* list changes of the current event */ + PureDraft *list_drafts[PURE_MODULE_STATES]; /* stored lists, when kept */ + PureDraft *stored_values; /* numbers, cursors and indices, when kept */ + PureVersion saved[PURE_MODULE_STATES]; /* text and list versions at the last save */ PureFault fault; uint32_t fault_pc; uint64_t ticks, events; @@ -153,6 +166,9 @@ typedef struct { } PureApp; PureResult pure_app_start(PureApp *, const PureModule *, PureData data); +/* Store every state changed since the last save (after frame events). */ +PureResult pure_app_save(PureApp *); +/* Leaves unsaved frame changes unsaved: call pure_app_save first to keep them. */ void pure_app_stop(PureApp *); PureResult pure_app_frame(PureApp *); PureResult pure_app_input(PureApp *, uint32_t input, PureEdge); |
