@statezero/core
Version:
The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate
30 lines (29 loc) • 752 B
JavaScript
/**
* Given a live reactive handle and a Promise, wire them together so
* the handle is also thenable.
*
* @template T
* @param {LiveQueryset} live
* @param {Promise<T>} promise
* @returns {LiveQueryset & Promise<T>}
*/
export function makeLiveThenable(live, promise) {
live.isOptimistic = true;
live.then = promise.then.bind(promise);
live.catch = promise.catch.bind(promise);
return live;
}
/**
* Remove the thenable hooks and clear the optimistic flag in-place, so the object
* can be returned without causing a recursive await loop.
*
* @template T
* @param {T} live
* @returns {T}
*/
export function breakThenable(live) {
delete live.isOptimistic;
delete live.then;
delete live.catch;
return live;
}