checkpoint on new objectservice

This commit is contained in:
sawka
2024-05-26 23:05:11 -07:00
parent b1aaba2a37
commit 95ce1cc86d
9 changed files with 387 additions and 47 deletions
+94 -2
View File
@@ -1,15 +1,18 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import * as React from "react";
import * as jotai from "jotai";
import { atomFamily } from "jotai/utils";
import * as jotaiUtils from "jotai/utils";
import { v4 as uuidv4 } from "uuid";
import * as rxjs from "rxjs";
import type { WailsEvent } from "@wailsio/runtime/types/events";
import { Events } from "@wailsio/runtime";
import { produce } from "immer";
import { BlockService } from "@/bindings/blockservice";
import { ObjectService } from "@/bindings/objectservice";
import * as wstore from "@/gopkg/wstore";
import { Call as $Call } from "@wailsio/runtime";
const globalStore = jotai.createStore();
@@ -105,4 +108,93 @@ function removeBlockFromTab(tabId: string, blockId: string) {
BlockService.CloseBlock(blockId);
}
export { globalStore, atoms, getBlockSubject, addBlockIdToTab, blockDataMap, useBlockAtom, removeBlockFromTab };
function GetObject(oref: string): Promise<any> {
let prtn = $Call.ByName(
"github.com/wavetermdev/thenextwave/pkg/service/objectservice.ObjectService.GetObject",
oref
);
return prtn;
}
type WaveObjectHookData = {
oref: string;
};
type WaveObjectValue<T> = {
pendingPromise: Promise<any>;
value: T;
loading: boolean;
};
const waveObjectValueCache = new Map<string, WaveObjectValue<any>>();
let waveObjectAtomCache = new WeakMap<WaveObjectHookData, jotai.Atom<any>>();
function clearWaveObjectCache() {
waveObjectValueCache.clear();
waveObjectAtomCache = new WeakMap<WaveObjectHookData, jotai.Atom<any>>();
}
function createWaveObjectAtom<T>(oref: string): jotai.Atom<[T, boolean]> {
let cacheVal: WaveObjectValue<T> = waveObjectValueCache.get(oref);
if (cacheVal == null) {
cacheVal = { pendingPromise: null, value: null, loading: true };
cacheVal.pendingPromise = GetObject(oref).then((val) => {
cacheVal.value = val;
cacheVal.loading = false;
cacheVal.pendingPromise = null;
});
waveObjectValueCache.set(oref, cacheVal);
}
return jotai.atom(
(get) => {
return [cacheVal.value, cacheVal.loading];
},
(get, set, newVal: T) => {
cacheVal.value = newVal;
}
);
}
function useWaveObjectValue<T>(oref: string): [T, boolean] {
const objRef = React.useRef<WaveObjectHookData>(null);
if (objRef.current == null) {
objRef.current = { oref: oref };
}
const objHookData = objRef.current;
let objAtom = waveObjectAtomCache.get(objHookData);
if (objAtom == null) {
objAtom = createWaveObjectAtom(oref);
waveObjectAtomCache.set(objHookData, objAtom);
}
const atomVal = jotai.useAtomValue(objAtom);
return [atomVal[0], atomVal[1]];
}
function useWaveObject<T>(oref: string): [T, boolean, (T) => void] {
const objRef = React.useRef<WaveObjectHookData>(null);
if (objRef.current == null) {
objRef.current = { oref: oref };
}
const objHookData = objRef.current;
let objAtom = waveObjectAtomCache.get(objHookData);
if (objAtom == null) {
objAtom = createWaveObjectAtom(oref);
waveObjectAtomCache.set(objHookData, objAtom);
}
const [atomVal, setAtomVal] = jotai.useAtom(objAtom);
return [atomVal[0], atomVal[1], setAtomVal];
}
export {
globalStore,
atoms,
getBlockSubject,
addBlockIdToTab,
blockDataMap,
useBlockAtom,
removeBlockFromTab,
GetObject,
useWaveObject,
useWaveObjectValue,
clearWaveObjectCache,
};
+92 -1
View File
@@ -1,6 +1,97 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
declare global {}
declare global {
type ORef = {
otype: string;
oid: string;
};
type Block = {
otype: string;
oid: string;
version: number;
blockdef: BlockDef;
controller: string;
view: string;
meta?: { [key: string]: any };
runtimeopts?: RuntimeOpts;
};
type BlockDef = {
controller: string;
view?: string;
files?: { [key: string]: FileDef };
meta?: { [key: string]: any };
};
type FileDef = {
filetype?: string;
path?: string;
url?: string;
content?: string;
meta?: { [key: string]: any };
};
type TermSize = {
rows: number;
cols: number;
};
type Client = {
otype: string;
oid: string;
version: number;
mainwindowid: string;
};
type Tab = {
otype: string;
oid: string;
version: number;
name: string;
blockids: string[];
};
type Point = {
x: number;
y: number;
};
type WinSize = {
width: number;
height: number;
};
type Workspace = {
otype: string;
oid: string;
version: number;
name: string;
tabids: string[];
};
type RuntimeOpts = {
termsize?: TermSize;
winsize?: WinSize;
};
type WaveObj = {
otype: string;
oid: string;
};
type Window = {
otype: string;
oid: string;
version: number;
workspaceid: string;
activetabid: string;
activeblockmap: { [key: string]: string };
pos: Point;
winsize: WinSize;
lastfocusts: number;
};
}
export {};