the useWaveObject() setter now writes back to the DB. make metadata setting generic

This commit is contained in:
sawka
2024-05-28 15:41:03 -07:00
parent f184723354
commit 609f2ec85c
8 changed files with 467 additions and 314 deletions
+3 -3
View File
@@ -27,21 +27,21 @@ const clientAtom: jotai.Atom<Client> = jotai.atom((get) => {
if (clientId == null) {
return null;
}
return WOS.getStaticObjectValue(WOS.makeORef("client", clientId), get);
return WOS.getObjectValue(WOS.makeORef("client", clientId), get);
});
const windowDataAtom: jotai.Atom<WaveWindow> = jotai.atom((get) => {
const windowId = get(windowIdAtom);
if (windowId == null) {
return null;
}
return WOS.getStaticObjectValue(WOS.makeORef("window", windowId), get);
return WOS.getObjectValue(WOS.makeORef("window", windowId), get);
});
const workspaceAtom: jotai.Atom<Workspace> = jotai.atom((get) => {
const windowData = get(windowDataAtom);
if (windowData == null) {
return null;
}
return WOS.getStaticObjectValue(WOS.makeORef("workspace", windowData.workspaceid), get);
return WOS.getObjectValue(WOS.makeORef("workspace", windowData.workspaceid), get);
});
const atoms = {
+35 -5
View File
@@ -147,7 +147,7 @@ function useWaveObjectValue<T>(oref: string): [T, boolean] {
return [atomVal.value, atomVal.loading];
}
function useWaveObject<T>(oref: string): [T, boolean, (T) => void] {
function useWaveObject<T extends WaveObj>(oref: string): [T, boolean, (T) => void] {
let wov = waveObjectValueCache.get(oref);
if (wov == null) {
wov = createWaveValueObject(oref, true);
@@ -162,6 +162,7 @@ function useWaveObject<T>(oref: string): [T, boolean, (T) => void] {
const [atomVal, setAtomVal] = jotai.useAtom(wov.dataAtom);
const simpleSet = (val: T) => {
setAtomVal({ value: val, loading: false });
UpdateObject(val, false);
};
return [atomVal.value, atomVal.loading, simpleSet];
}
@@ -243,15 +244,39 @@ function wrapObjectServiceCall<T>(fnName: string, ...args: any[]): Promise<T> {
return prtn;
}
function getStaticObjectValue<T>(oref: string, getFn: jotai.Getter): T {
// 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
function getObjectValue<T>(oref: string, getFn?: jotai.Getter): T {
let wov = waveObjectValueCache.get(oref);
if (wov == null) {
return null;
}
if (getFn == null) {
getFn = globalStore.get;
}
const atomVal = getFn(wov.dataAtom);
return atomVal.value;
}
// 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
function setObjectValue<T>(value: WaveObj, setFn?: jotai.Setter, pushToServer?: boolean) {
const oref = makeORef(value.otype, value.oid);
let wov = waveObjectValueCache.get(oref);
if (wov == null) {
return;
}
if (setFn == null) {
setFn = globalStore.set;
}
setFn(wov.dataAtom, { value: value, loading: false });
if (pushToServer) {
UpdateObject(value, false);
}
}
export function AddTabToWorkspace(tabName: string, activateTab: boolean): Promise<{ tabId: string }> {
return wrapObjectServiceCall("AddTabToWorkspace", tabName, activateTab);
}
@@ -272,16 +297,21 @@ export function CloseTab(tabId: string): Promise<void> {
return wrapObjectServiceCall("CloseTab", tabId);
}
export function UpdateBlockMeta(blockId: string, meta: MetadataType): Promise<void> {
return wrapObjectServiceCall("UpdateBlockMeta", blockId, meta);
export function UpdateObjectMeta(blockId: string, meta: MetadataType): Promise<void> {
return wrapObjectServiceCall("UpdateObjectMeta", blockId, meta);
}
export function UpdateObject(waveObj: WaveObj, returnUpdates: boolean): Promise<WaveObjUpdate[]> {
return wrapObjectServiceCall("UpdateObject", waveObj, returnUpdates);
}
export {
cleanWaveObjectCache,
clearWaveObjectCache,
getStaticObjectValue,
getObjectValue,
loadAndPinWaveObject,
makeORef,
setObjectValue,
updateWaveObject,
updateWaveObjects,
useWaveObject,