@assistant-ui/react
Version:
Typescript/React library for AI Chat
85 lines (84 loc) • 2.76 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/runtimes/remote-thread-list/OptimisticState.ts
var OptimisticState_exports = {};
__export(OptimisticState_exports, {
OptimisticState: () => OptimisticState
});
module.exports = __toCommonJS(OptimisticState_exports);
var import_BaseSubscribable = require("./BaseSubscribable.js");
var pipeTransforms = (initialState, extraParam, transforms) => {
return transforms.reduce((state, transform) => {
return transform?.(state, extraParam) ?? state;
}, initialState);
};
var OptimisticState = class extends import_BaseSubscribable.BaseSubscribable {
_pendingTransforms = [];
_baseValue;
_cachedValue;
constructor(initialState) {
super();
this._baseValue = initialState;
this._cachedValue = initialState;
}
_updateState() {
this._cachedValue = this._pendingTransforms.reduce((state, transform) => {
return pipeTransforms(state, transform.task, [
transform.loading,
transform.optimistic
]);
}, this._baseValue);
this._notifySubscribers();
}
get baseValue() {
return this._baseValue;
}
get value() {
return this._cachedValue;
}
update(state) {
this._baseValue = state;
this._updateState();
}
async optimisticUpdate(transform) {
const task = transform.execute();
const pendingTransform = { ...transform, task };
try {
this._pendingTransforms.push(pendingTransform);
this._updateState();
const result = await task;
this._baseValue = pipeTransforms(this._baseValue, result, [
transform.optimistic,
transform.then
]);
return result;
} finally {
const index = this._pendingTransforms.indexOf(pendingTransform);
if (index > -1) {
this._pendingTransforms.splice(index, 1);
}
this._updateState();
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
OptimisticState
});
//# sourceMappingURL=OptimisticState.js.map
;