model: add onSuccess/onFailure lifecycle hooks to modelRegister
This commit is contained in:
+5
-1
@@ -144,6 +144,8 @@ modelRegister('firewall', {
|
||||
return r.data;
|
||||
},
|
||||
defaultData: null, // optional, initial data value
|
||||
// onSuccess: (name, data, param?) => { }, // optional — after model.data is set (also for null)
|
||||
// onFailure: (name, error) => { }, // optional — after model.error is set (real throws only)
|
||||
});
|
||||
|
||||
// Parameterized example — tab-aware fetch:
|
||||
@@ -164,6 +166,8 @@ modelRegister('logs', {
|
||||
| `definition.subsystem` | WS topic string. Use `'firewall'`, `'dnsmasq'`, etc. Use `'*'` to match all topics. |
|
||||
| `definition.fetch(signal?, param?)` | Async function that fetches and returns data. Throws on error. Receives optional `AbortSignal` and optional parameter (e.g., tab key). |
|
||||
| `definition.defaultData` | Optional initial data value (default: `null`) |
|
||||
| `definition.onSuccess(name, data, param?)` | Optional lifecycle hook called after `model.data` is assigned — including `data === null` (a resolved `null` is normal, not an error). `param` is the action object passed to `fetch` (or `undefined`), so hooks can tell which action produced the data. Fire-and-forget: hook errors are caught and logged via `console.warn`; they never clobber `model.error`, the returned promise, or the `finally` flag clearing. |
|
||||
| `definition.onFailure(name, error)` | Optional lifecycle hook called after `model.error` is assigned. Only reachable on a real throw from `fetch` (e.g., network error). Same fire-and-forget error isolation as `onSuccess`. |
|
||||
|
||||
### `getModel(name)`
|
||||
|
||||
@@ -212,7 +216,7 @@ modelFetch('logs', 'nginx-access');
|
||||
- On failure, stores error in `model.error`.
|
||||
- Flags cleared in `finally` block.
|
||||
- Does not abort in-progress fetches — other consumers may still need the data.
|
||||
- The `param` argument is passed to `fetch(signal, param)` for parameterized models. Dedup key is `name: param`.
|
||||
- The `param` argument is passed to `fetch(signal, param)` for parameterized models. Dedup key is `name` (no param) or `name: JSON.stringify(param)` (with param) — object params (e.g. `{ action: 'refresh' }` vs `{ action: 'check' }`) therefore get distinct keys, and param-less `modelFetch(name)` calls retain the bare `name` key.
|
||||
|
||||
### `refreshByTopic(topic)`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user