@webjsdev/core 0.7.45 → 0.7.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/optimistic.js CHANGED
@@ -76,13 +76,18 @@ async function runLegacyOptimistic(signal, value, action) {
76
76
  * class TodoList extends WebComponent({ todos: prop(Array) }) {
77
77
  * optimisticTodos = optimistic(this, {
78
78
  * source: () => this.todos,
79
- * update: (state, title) => [...state, { id: 'tmp', title, pending: true }]
79
+ * // PURE: the row is derived from the payload, nothing is minted here.
80
+ * // `.value` re-folds the queue on every read (see Behaviour 1), so a
81
+ * // minted id would differ per render and a hardcoded 'tmp' would
82
+ * // collide across two concurrent adds.
83
+ * update: (state, add) => [...state, { id: add.tempId, title: add.title, pending: true }]
80
84
  * });
81
85
  *
82
86
  * async handleSubmit(e) {
83
87
  * const title = e.target.querySelector('input').value;
88
+ * const tempId = crypto.randomUUID();
84
89
  * const promise = createTodo({ title });
85
- * this.optimisticTodos.add(title, promise);
90
+ * this.optimisticTodos.add({ tempId, title }, promise);
86
91
  * const result = await promise;
87
92
  * if (result.success) this.todos = [...this.todos, result.data];
88
93
  * }