respond-framework
Version:
create as fast you think
48 lines (47 loc) • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.markCached = exports.default = void 0;
var _kinds = require("../kinds.js");
const fetch0 = ({
cache = true
} = {}) => cache ? fetchWithNavigationCache : fetchWithConsistentFollowup;
var _default = exports.default = fetch0;
const markCached = (state, e) => {
if (e.kind !== _kinds.navigation) return;
e.meta.cached = state.respond.urlCache.has(e);
};
exports.markCached = markCached;
const fetchWithNavigationCache = {
enter(state, e) {
if (e.meta.cached || !e.event.fetch) return;
if (e.event.pattern) state.respond.urlCache.set(e);
return fetch(state, e);
}
};
const fetchWithConsistentFollowup = {
enter(state, e) {
if (e.event.fetch) return fetch(state, e);
if (e.event.pattern) return e.event.done.dispatch(undefined, {
from: e
}); // always dispatch a *consistent* follow-up, as app architecture desires always receiving a follow-up, while relying on dbCache instead -- can be used to display loading spinners on buttons *before* navigating; to do so switch on the .done event of navigations in a Page/Screen reducer
}
};
const fetch = async (state, e) => {
const res = await e.event.fetch.call(state, state, e);
state.respond.devtools.sendPluginNotification({
type: 'fetch',
returned: res
}, e);
if (res?.error) {
await e.event.error.dispatch(res, {
from: e
});
if (e.event.pattern) state.respond.urlCache.delete(e);
return false;
}
await e.event.done.dispatch(res, {
from: e
});
};