Replace lets with const where possible (#35)

This commit is contained in:
Evan Simkowitz 2024-06-11 13:19:29 -07:00 committed by GitHub
parent 97ca430884
commit 92dc82967c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 40 deletions

View File

@ -18,7 +18,7 @@ interface BlockProps {
}
const BlockHeader = ({ blockId, onClose }: BlockProps) => {
const [blockData, blockDataLoading] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
return (
<div key="header" className="block-header">

View File

@ -21,7 +21,7 @@ type WaveObjectValue<T extends WaveObj> = {
};
function splitORef(oref: string): [string, string] {
let parts = oref.split(":");
const parts = oref.split(":");
if (parts.length != 2) {
throw new Error("invalid oref");
}
@ -54,11 +54,7 @@ function makeORef(otype: string, oid: string): string {
}
function GetObject<T>(oref: string): Promise<T> {
let prtn = $Call.ByName(
"github.com/wavetermdev/thenextwave/pkg/service/objectservice.ObjectService.GetObject",
oref
);
return prtn;
return $Call.ByName("github.com/wavetermdev/thenextwave/pkg/service/objectservice.ObjectService.GetObject", oref);
}
const waveObjectValueCache = new Map<string, WaveObjectValue<any>>();
@ -75,8 +71,8 @@ function createWaveValueObject<T extends WaveObj>(oref: string, shouldFetch: boo
if (!shouldFetch) {
return wov;
}
let startTs = Date.now();
let localPromise = GetObject<T>(oref);
const startTs = Date.now();
const localPromise = GetObject<T>(oref);
wov.pendingPromise = localPromise;
localPromise.then((val) => {
if (wov.pendingPromise != localPromise) {
@ -138,24 +134,21 @@ function getWaveObjectAtom<T extends WaveObj>(oref: string): jotai.WritableAtom<
waveObjectValueCache.set(oref, wov);
}
return jotai.atom(
(get) => {
let dataValue = get(wov.dataAtom);
return dataValue.value;
},
(get) => get(wov.dataAtom).value,
(_get, set, value: T) => {
setObjectValue(value, set, true);
}
);
}
function getWaveObjectLoadingAtom<T extends WaveObj>(oref: string): jotai.Atom<boolean> {
function getWaveObjectLoadingAtom(oref: string): jotai.Atom<boolean> {
let wov = waveObjectValueCache.get(oref);
if (wov == null) {
wov = createWaveValueObject(oref, true);
waveObjectValueCache.set(oref, wov);
}
return jotai.atom((get) => {
let dataValue = get(wov.dataAtom);
const dataValue = get(wov.dataAtom);
if (dataValue.loading) {
return null;
}
@ -203,7 +196,7 @@ function updateWaveObject(update: WaveObjUpdate) {
if (update == null) {
return;
}
let oref = makeORef(update.otype, update.oid);
const oref = makeORef(update.otype, update.oid);
let wov = waveObjectValueCache.get(oref);
if (wov == null) {
wov = createWaveValueObject(oref, false);
@ -217,7 +210,7 @@ function updateWaveObject(update: WaveObjUpdate) {
console.log("invalid wave object update", update);
return;
}
let curValue: WaveObjectDataItemType<WaveObj> = globalStore.get(wov.dataAtom);
const curValue: WaveObjectDataItemType<WaveObj> = globalStore.get(wov.dataAtom);
if (curValue.value != null && curValue.value.version >= update.obj.version) {
return;
}
@ -229,14 +222,14 @@ function updateWaveObject(update: WaveObjUpdate) {
}
function updateWaveObjects(vals: WaveObjUpdate[]) {
for (let val of vals) {
for (const val of vals) {
updateWaveObject(val);
}
}
function cleanWaveObjectCache() {
let now = Date.now();
for (let [oref, wov] of waveObjectValueCache) {
const now = Date.now();
for (const [oref, wov] of waveObjectValueCache) {
if (wov.refCount == 0 && wov.holdTime < now) {
waveObjectValueCache.delete(oref);
}
@ -280,11 +273,11 @@ function wrapObjectServiceCall<T>(fnName: string, ...args: any[]): Promise<T> {
// 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) {
const wov = waveObjectValueCache.get(oref);
if (wov === undefined) {
return null;
}
if (getFn == null) {
if (getFn === undefined) {
getFn = globalStore.get;
}
const atomVal = getFn(wov.dataAtom);
@ -296,11 +289,11 @@ function getObjectValue<T>(oref: string, getFn?: jotai.Getter): T {
// otherwise it will use the globalStore.set function
function setObjectValue<T extends WaveObj>(value: T, setFn?: jotai.Setter, pushToServer?: boolean) {
const oref = makeORef(value.otype, value.oid);
let wov = waveObjectValueCache.get(oref);
if (wov == null) {
const wov = waveObjectValueCache.get(oref);
if (wov === undefined) {
return;
}
if (setFn == null) {
if (setFn === undefined) {
setFn = globalStore.set;
}
setFn(wov.dataAtom, { value: value, loading: false });

View File

@ -61,11 +61,11 @@ function loadFiraCodeFont() {
return;
}
isFiraCodeLoaded = true;
let firaCodeRegular = new FontFace("Fira Code", "url('/fonts/firacode-regular.woff2')", {
const firaCodeRegular = new FontFace("Fira Code", "url('/fonts/firacode-regular.woff2')", {
style: "normal",
weight: "400",
});
let firaCodeBold = new FontFace("Fira Code", "url('/fonts/firacode-bold.woff2')", {
const firaCodeBold = new FontFace("Fira Code", "url('/fonts/firacode-bold.woff2')", {
style: "normal",
weight: "700",
});
@ -80,19 +80,19 @@ function loadHackFont() {
return;
}
isHackFontLoaded = true;
let hackRegular = new FontFace("Hack", "url('/fonts/hack-regular.woff2')", {
const hackRegular = new FontFace("Hack", "url('/fonts/hack-regular.woff2')", {
style: "normal",
weight: "400",
});
let hackBold = new FontFace("Hack", "url('/fonts/hack-bold.woff2')", {
const hackBold = new FontFace("Hack", "url('/fonts/hack-bold.woff2')", {
style: "normal",
weight: "700",
});
let hackItalic = new FontFace("Hack", "url('/fonts/hack-italic.woff2')", {
const hackItalic = new FontFace("Hack", "url('/fonts/hack-italic.woff2')", {
style: "italic",
weight: "400",
});
let hackBoldItalic = new FontFace("Hack", "url('/fonts/hack-bolditalic.woff2')", {
const hackBoldItalic = new FontFace("Hack", "url('/fonts/hack-bolditalic.woff2')", {
style: "italic",
weight: "700",
});
@ -111,7 +111,7 @@ function loadBaseFonts() {
return;
}
isBaseFontsLoaded = true;
let mmFont = new FontFace("Martian Mono", "url(/fonts/MartianMono-VariableFont_wdth,wght.ttf)", {
const mmFont = new FontFace("Martian Mono", "url(/fonts/MartianMono-VariableFont_wdth,wght.ttf)", {
style: "normal",
weight: "normal",
});

View File

@ -13,7 +13,7 @@ function formatPath(path: PathType): string {
return "$";
}
let pathStr = "$";
for (let pathPart of path) {
for (const pathPart of path) {
if (typeof pathPart === "string") {
if (simplePathStrRe.test(pathPart)) {
pathStr += "." + pathPart;
@ -39,7 +39,7 @@ function isObject(obj: any): boolean {
function getPath(obj: any, path: PathType): any {
let cur = obj;
for (let pathPart of path) {
for (const pathPart of path) {
if (cur == null) {
return null;
}
@ -86,7 +86,7 @@ function checkPath(path: PathType): boolean {
if (!isArray(path)) {
return false;
}
for (let pathPart of path) {
for (const pathPart of path) {
if (typeof pathPart !== "string" && typeof pathPart !== "number") {
return false;
}
@ -118,7 +118,7 @@ function isEmpty(obj: any): boolean {
return obj.length == 0;
}
if (isObject(obj)) {
for (let key in obj) {
for (const _ in obj) {
return false;
}
return true;

View File

@ -33,9 +33,9 @@ document.addEventListener("DOMContentLoaded", async () => {
await WOS.loadAndPinWaveObject<Client>(WOS.makeORef("client", clientId));
const waveWindow = await WOS.loadAndPinWaveObject<WaveWindow>(WOS.makeORef("window", windowId));
await WOS.loadAndPinWaveObject<Workspace>(WOS.makeORef("workspace", waveWindow.workspaceid));
let reactElem = React.createElement(App, null, null);
let elem = document.getElementById("main");
let root = createRoot(elem);
const reactElem = React.createElement(App, null, null);
const elem = document.getElementById("main");
const root = createRoot(elem);
document.fonts.ready.then(() => {
console.log("Wave First Render");
root.render(reactElem);