respond-framework
Version:
create as fast you think
32 lines (31 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _trySync = require("../../utils/trySync.js");
const optimistic0 = (state, e) => {
if (!e.event.optimistic) return;
const onError = error => state.respond.onError({
error,
kind: 'optimistic',
e
});
return state.respond.awaitInReplaysOnly(() => {
const res = e.event.optimistic.call(state, state, e); // optimistic is for fast syncronous routines like providing another event, which will be dispatched in parallel below
return (0, _trySync.default)(res, r => optimistic(state, e, r)); // however awaiting event.optimistic is supported; the idea is at least the subsequent dispatch will run in parallel
}, onError);
};
var _default = exports.default = optimistic0;
const optimistic = (state, e, res) => {
state.respond.devtools.sendPluginNotification({
type: 'optimistic',
returned: res
}, e);
const meta = {
from: e
};
return res?.dispatch?.({
meta
}) ?? (res && e.event.data.dispatch(res, meta));
};