2024-05-27 22:59:58 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// WaveObjectStore
|
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
import { sendRpcCommand } from "@/app/store/wshrpc";
|
2024-07-18 03:49:27 +02:00
|
|
|
import { getWebServerEndpoint } from "@/util/endpoints";
|
2024-05-27 22:59:58 +02:00
|
|
|
import * as jotai from "jotai";
|
2024-05-28 21:12:28 +02:00
|
|
|
import * as React from "react";
|
2024-07-18 00:24:43 +02:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2024-07-18 03:42:49 +02:00
|
|
|
import { atoms, globalStore } from "./global";
|
2024-06-12 02:42:10 +02:00
|
|
|
import * as services from "./services";
|
|
|
|
|
|
|
|
const IsElectron = true;
|
2024-05-27 22:59:58 +02:00
|
|
|
|
|
|
|
type WaveObjectDataItemType<T extends WaveObj> = {
|
|
|
|
value: T;
|
|
|
|
loading: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type WaveObjectValue<T extends WaveObj> = {
|
|
|
|
pendingPromise: Promise<T>;
|
|
|
|
dataAtom: jotai.PrimitiveAtom<WaveObjectDataItemType<T>>;
|
|
|
|
refCount: number;
|
|
|
|
holdTime: number;
|
|
|
|
};
|
|
|
|
|
2024-07-30 00:09:07 +02:00
|
|
|
type WritableWaveObjectAtom<T extends WaveObj> = jotai.WritableAtom<T, [value: T], void>;
|
|
|
|
|
2024-05-27 22:59:58 +02:00
|
|
|
function splitORef(oref: string): [string, string] {
|
2024-06-11 22:19:29 +02:00
|
|
|
const parts = oref.split(":");
|
2024-05-27 22:59:58 +02:00
|
|
|
if (parts.length != 2) {
|
|
|
|
throw new Error("invalid oref");
|
|
|
|
}
|
|
|
|
return [parts[0], parts[1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
function isBlank(str: string): boolean {
|
|
|
|
return str == null || str == "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isBlankNum(num: number): boolean {
|
|
|
|
return num == null || isNaN(num) || num == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValidWaveObj(val: WaveObj): boolean {
|
|
|
|
if (val == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-27 23:31:12 +02:00
|
|
|
if (isBlank(val.otype) || isBlank(val.oid) || isBlankNum(val.version)) {
|
2024-05-27 22:59:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeORef(otype: string, oid: string): string {
|
|
|
|
if (isBlank(otype) || isBlank(oid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return `${otype}:${oid}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function GetObject<T>(oref: string): Promise<T> {
|
2024-06-12 02:42:10 +02:00
|
|
|
return callBackendService("object", "GetObject", [oref], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function callBackendService(service: string, method: string, args: any[], noUIContext?: boolean): Promise<any> {
|
|
|
|
const startTs = Date.now();
|
|
|
|
let uiContext: UIContext = null;
|
|
|
|
if (!noUIContext) {
|
|
|
|
uiContext = globalStore.get(atoms.uiContext);
|
|
|
|
}
|
2024-07-18 03:42:49 +02:00
|
|
|
const waveCall: WebCallType = {
|
2024-06-12 02:42:10 +02:00
|
|
|
service: service,
|
|
|
|
method: method,
|
|
|
|
args: args,
|
|
|
|
uicontext: uiContext,
|
|
|
|
};
|
|
|
|
// usp is just for debugging (easier to filter URLs)
|
2024-07-18 03:42:49 +02:00
|
|
|
const methodName = `${service}.${method}`;
|
|
|
|
const usp = new URLSearchParams();
|
2024-06-12 02:42:10 +02:00
|
|
|
usp.set("service", service);
|
|
|
|
usp.set("method", method);
|
2024-07-18 03:49:27 +02:00
|
|
|
const url = getWebServerEndpoint() + "/wave/service?" + usp.toString();
|
2024-07-18 03:42:49 +02:00
|
|
|
const fetchPromise = fetch(url, {
|
2024-06-12 02:42:10 +02:00
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(waveCall),
|
|
|
|
});
|
2024-07-18 03:42:49 +02:00
|
|
|
const prtn = fetchPromise
|
2024-06-12 02:42:10 +02:00
|
|
|
.then((resp) => {
|
|
|
|
if (!resp.ok) {
|
|
|
|
throw new Error(`call ${methodName} failed: ${resp.status} ${resp.statusText}`);
|
|
|
|
}
|
|
|
|
return resp.json();
|
|
|
|
})
|
|
|
|
.then((respData: WebReturnType) => {
|
|
|
|
if (respData == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (respData.updates != null) {
|
|
|
|
updateWaveObjects(respData.updates);
|
|
|
|
}
|
|
|
|
if (respData.error != null) {
|
|
|
|
throw new Error(`call ${methodName} error: ${respData.error}`);
|
|
|
|
}
|
|
|
|
console.log("Call", methodName, Date.now() - startTs + "ms");
|
|
|
|
return respData.data;
|
|
|
|
});
|
|
|
|
return prtn;
|
2024-05-27 22:59:58 +02:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:56:04 +02:00
|
|
|
function wshServerRpcHelper_responsestream(
|
2024-07-18 00:24:43 +02:00
|
|
|
command: string,
|
|
|
|
data: any,
|
|
|
|
opts: WshRpcCommandOpts
|
2024-07-19 00:56:04 +02:00
|
|
|
): AsyncGenerator<any, void, boolean> {
|
|
|
|
if (opts?.noresponse) {
|
|
|
|
throw new Error("noresponse not supported for responsestream calls");
|
|
|
|
}
|
2024-07-23 21:46:29 +02:00
|
|
|
const msg: RpcMessage = {
|
2024-07-19 00:56:04 +02:00
|
|
|
command: command,
|
|
|
|
data: data,
|
|
|
|
reqid: uuidv4(),
|
|
|
|
};
|
|
|
|
if (opts?.timeout) {
|
|
|
|
msg.timeout = opts.timeout;
|
|
|
|
}
|
|
|
|
const rpcGen = sendRpcCommand(msg);
|
|
|
|
return rpcGen;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wshServerRpcHelper_call(command: string, data: any, opts: WshRpcCommandOpts): Promise<any> {
|
2024-07-23 21:46:29 +02:00
|
|
|
const msg: RpcMessage = {
|
2024-07-18 00:24:43 +02:00
|
|
|
command: command,
|
|
|
|
data: data,
|
|
|
|
};
|
|
|
|
if (!opts?.noresponse) {
|
|
|
|
msg.reqid = uuidv4();
|
|
|
|
}
|
|
|
|
if (opts?.timeout) {
|
|
|
|
msg.timeout = opts.timeout;
|
|
|
|
}
|
|
|
|
const rpcGen = sendRpcCommand(msg);
|
|
|
|
if (rpcGen == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-07-19 00:56:04 +02:00
|
|
|
const respMsgPromise = rpcGen.next(true); // pass true to force termination of rpc after 1 response (not streaming)
|
|
|
|
return respMsgPromise.then((msg: IteratorResult<any, void>) => {
|
|
|
|
return msg.value;
|
2024-07-18 00:24:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-27 22:59:58 +02:00
|
|
|
const waveObjectValueCache = new Map<string, WaveObjectValue<any>>();
|
|
|
|
|
|
|
|
function clearWaveObjectCache() {
|
|
|
|
waveObjectValueCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultHoldTime = 5000; // 5-seconds
|
|
|
|
|
|
|
|
function createWaveValueObject<T extends WaveObj>(oref: string, shouldFetch: boolean): WaveObjectValue<T> {
|
|
|
|
const wov = { pendingPromise: null, dataAtom: null, refCount: 0, holdTime: Date.now() + 5000 };
|
|
|
|
wov.dataAtom = jotai.atom({ value: null, loading: true });
|
|
|
|
if (!shouldFetch) {
|
|
|
|
return wov;
|
|
|
|
}
|
2024-06-11 22:19:29 +02:00
|
|
|
const startTs = Date.now();
|
|
|
|
const localPromise = GetObject<T>(oref);
|
2024-05-27 22:59:58 +02:00
|
|
|
wov.pendingPromise = localPromise;
|
|
|
|
localPromise.then((val) => {
|
|
|
|
if (wov.pendingPromise != localPromise) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [otype, oid] = splitORef(oref);
|
|
|
|
if (val != null) {
|
|
|
|
if (val["otype"] != otype) {
|
|
|
|
throw new Error("GetObject returned wrong type");
|
|
|
|
}
|
|
|
|
if (val["oid"] != oid) {
|
|
|
|
throw new Error("GetObject returned wrong id");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wov.pendingPromise = null;
|
|
|
|
globalStore.set(wov.dataAtom, { value: val, loading: false });
|
2024-05-28 00:44:57 +02:00
|
|
|
console.log("WaveObj resolved", oref, Date.now() - startTs + "ms");
|
2024-05-27 22:59:58 +02:00
|
|
|
});
|
|
|
|
return wov;
|
|
|
|
}
|
|
|
|
|
2024-07-30 00:09:07 +02:00
|
|
|
function getWaveObjectValue<T extends WaveObj>(oref: string, createIfMissing = true): WaveObjectValue<T> {
|
2024-05-27 22:59:58 +02:00
|
|
|
let wov = waveObjectValueCache.get(oref);
|
2024-07-30 00:09:07 +02:00
|
|
|
if (wov === undefined && createIfMissing) {
|
2024-05-27 22:59:58 +02:00
|
|
|
wov = createWaveValueObject(oref, true);
|
|
|
|
waveObjectValueCache.set(oref, wov);
|
|
|
|
}
|
2024-07-30 00:09:07 +02:00
|
|
|
return wov;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadAndPinWaveObject<T extends WaveObj>(oref: string): Promise<T> {
|
|
|
|
const wov = getWaveObjectValue<T>(oref);
|
2024-05-27 22:59:58 +02:00
|
|
|
wov.refCount++;
|
|
|
|
if (wov.pendingPromise == null) {
|
|
|
|
const dataValue = globalStore.get(wov.dataAtom);
|
|
|
|
return Promise.resolve(dataValue.value);
|
|
|
|
}
|
|
|
|
return wov.pendingPromise;
|
|
|
|
}
|
|
|
|
|
2024-07-30 00:09:07 +02:00
|
|
|
function getWaveObjectAtom<T extends WaveObj>(oref: string): WritableWaveObjectAtom<T> {
|
|
|
|
const wov = getWaveObjectValue<T>(oref);
|
2024-06-04 22:05:44 +02:00
|
|
|
return jotai.atom(
|
2024-06-11 22:19:29 +02:00
|
|
|
(get) => get(wov.dataAtom).value,
|
2024-06-04 22:05:44 +02:00
|
|
|
(_get, set, value: T) => {
|
|
|
|
setObjectValue(value, set, true);
|
2024-05-29 02:17:52 +02:00
|
|
|
}
|
2024-06-04 22:05:44 +02:00
|
|
|
);
|
2024-05-29 02:17:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-11 22:19:29 +02:00
|
|
|
function getWaveObjectLoadingAtom(oref: string): jotai.Atom<boolean> {
|
2024-07-30 00:09:07 +02:00
|
|
|
const wov = getWaveObjectValue(oref);
|
2024-05-29 02:17:52 +02:00
|
|
|
return jotai.atom((get) => {
|
2024-06-11 22:19:29 +02:00
|
|
|
const dataValue = get(wov.dataAtom);
|
2024-05-29 02:17:52 +02:00
|
|
|
if (dataValue.loading) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return dataValue.loading;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-30 00:09:07 +02:00
|
|
|
function useWaveObjectValue<T extends WaveObj>(oref: string): [T, boolean] {
|
|
|
|
const wov = getWaveObjectValue<T>(oref);
|
2024-05-27 22:59:58 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
wov.refCount++;
|
|
|
|
return () => {
|
|
|
|
wov.refCount--;
|
|
|
|
};
|
|
|
|
}, [oref]);
|
|
|
|
const atomVal = jotai.useAtomValue(wov.dataAtom);
|
|
|
|
return [atomVal.value, atomVal.loading];
|
|
|
|
}
|
|
|
|
|
2024-05-27 23:31:12 +02:00
|
|
|
function updateWaveObject(update: WaveObjUpdate) {
|
|
|
|
if (update == null) {
|
2024-05-27 22:59:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-06-11 22:19:29 +02:00
|
|
|
const oref = makeORef(update.otype, update.oid);
|
2024-07-30 00:09:07 +02:00
|
|
|
const wov = getWaveObjectValue(oref);
|
2024-05-27 23:31:12 +02:00
|
|
|
if (update.updatetype == "delete") {
|
2024-05-28 01:33:31 +02:00
|
|
|
console.log("WaveObj deleted", oref);
|
2024-05-27 22:59:58 +02:00
|
|
|
globalStore.set(wov.dataAtom, { value: null, loading: false });
|
|
|
|
} else {
|
2024-05-27 23:31:12 +02:00
|
|
|
if (!isValidWaveObj(update.obj)) {
|
|
|
|
console.log("invalid wave object update", update);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-11 22:19:29 +02:00
|
|
|
const curValue: WaveObjectDataItemType<WaveObj> = globalStore.get(wov.dataAtom);
|
2024-05-27 23:31:12 +02:00
|
|
|
if (curValue.value != null && curValue.value.version >= update.obj.version) {
|
2024-05-27 22:59:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-05-28 01:33:31 +02:00
|
|
|
console.log("WaveObj updated", oref);
|
2024-05-27 23:31:12 +02:00
|
|
|
globalStore.set(wov.dataAtom, { value: update.obj, loading: false });
|
2024-05-27 22:59:58 +02:00
|
|
|
}
|
|
|
|
wov.holdTime = Date.now() + defaultHoldTime;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-27 23:31:12 +02:00
|
|
|
function updateWaveObjects(vals: WaveObjUpdate[]) {
|
2024-06-11 22:19:29 +02:00
|
|
|
for (const val of vals) {
|
2024-05-27 22:59:58 +02:00
|
|
|
updateWaveObject(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanWaveObjectCache() {
|
2024-06-11 22:19:29 +02:00
|
|
|
const now = Date.now();
|
|
|
|
for (const [oref, wov] of waveObjectValueCache) {
|
2024-05-27 22:59:58 +02:00
|
|
|
if (wov.refCount == 0 && wov.holdTime < now) {
|
|
|
|
waveObjectValueCache.delete(oref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 00:41:03 +02:00
|
|
|
// gets the value of a WaveObject from the cache.
|
|
|
|
// should provide getFn if it is available (e.g. inside of a jotai atom)
|
|
|
|
// otherwise it will use the globalStore.get function
|
2024-07-30 00:09:07 +02:00
|
|
|
function getObjectValue<T extends WaveObj>(oref: string, getFn?: jotai.Getter): T {
|
|
|
|
const wov = getWaveObjectValue<T>(oref);
|
2024-06-12 02:42:10 +02:00
|
|
|
if (getFn == null) {
|
2024-05-29 00:41:03 +02:00
|
|
|
getFn = globalStore.get;
|
|
|
|
}
|
2024-05-28 00:44:57 +02:00
|
|
|
const atomVal = getFn(wov.dataAtom);
|
|
|
|
return atomVal.value;
|
|
|
|
}
|
|
|
|
|
2024-05-29 00:41:03 +02:00
|
|
|
// sets the value of a WaveObject in the cache.
|
|
|
|
// should provide setFn if it is available (e.g. inside of a jotai atom)
|
|
|
|
// otherwise it will use the globalStore.set function
|
2024-06-06 02:21:40 +02:00
|
|
|
function setObjectValue<T extends WaveObj>(value: T, setFn?: jotai.Setter, pushToServer?: boolean) {
|
2024-05-29 00:41:03 +02:00
|
|
|
const oref = makeORef(value.otype, value.oid);
|
2024-07-30 00:09:07 +02:00
|
|
|
const wov = getWaveObjectValue(oref, false);
|
2024-06-11 22:19:29 +02:00
|
|
|
if (wov === undefined) {
|
2024-05-29 00:41:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-06-11 22:19:29 +02:00
|
|
|
if (setFn === undefined) {
|
2024-05-29 00:41:03 +02:00
|
|
|
setFn = globalStore.set;
|
|
|
|
}
|
|
|
|
setFn(wov.dataAtom, { value: value, loading: false });
|
|
|
|
if (pushToServer) {
|
2024-06-12 02:42:10 +02:00
|
|
|
services.ObjectService.UpdateObject(value, false);
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 22:59:58 +02:00
|
|
|
export {
|
2024-06-12 02:42:10 +02:00
|
|
|
callBackendService,
|
2024-05-28 21:12:28 +02:00
|
|
|
cleanWaveObjectCache,
|
|
|
|
clearWaveObjectCache,
|
2024-05-29 00:41:03 +02:00
|
|
|
getObjectValue,
|
2024-05-29 02:17:52 +02:00
|
|
|
getWaveObjectAtom,
|
|
|
|
getWaveObjectLoadingAtom,
|
2024-05-28 21:12:28 +02:00
|
|
|
loadAndPinWaveObject,
|
2024-05-27 22:59:58 +02:00
|
|
|
makeORef,
|
2024-05-29 00:41:03 +02:00
|
|
|
setObjectValue,
|
2024-05-28 21:12:28 +02:00
|
|
|
updateWaveObject,
|
|
|
|
updateWaveObjects,
|
2024-05-27 22:59:58 +02:00
|
|
|
useWaveObjectValue,
|
2024-07-19 00:56:04 +02:00
|
|
|
wshServerRpcHelper_call,
|
|
|
|
wshServerRpcHelper_responsestream,
|
2024-05-27 22:59:58 +02:00
|
|
|
};
|