From abedca2364a878a6b524f56bb8b1a9999b1ba668 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 27 May 2024 14:31:12 -0700 Subject: [PATCH] setactivetab working, removed tombstones, created updatetype --- frontend/app/store/wos.ts | 37 +++++++------ frontend/app/workspace/workspace.tsx | 2 +- frontend/types/custom.d.ts | 19 ++++--- pkg/service/objectservice/objectservice.go | 20 ++++--- pkg/waveobj/waveobj.go | 61 ++-------------------- pkg/wstore/wstore.go | 53 +++++++++++++++---- pkg/wstore/wstore_dbops.go | 12 ++--- 7 files changed, 89 insertions(+), 115 deletions(-) diff --git a/frontend/app/store/wos.ts b/frontend/app/store/wos.ts index bd845f60..f9c625b4 100644 --- a/frontend/app/store/wos.ts +++ b/frontend/app/store/wos.ts @@ -41,10 +41,7 @@ function isValidWaveObj(val: WaveObj): boolean { if (val == null) { return false; } - if (isBlank(val.otype) || isBlank(val.oid)) { - return false; - } - if (!val.deleted && isBlankNum(val.version)) { + if (isBlank(val.otype) || isBlank(val.oid) || isBlankNum(val.version)) { return false; } return true; @@ -151,34 +148,34 @@ function useWaveObject(oref: string): [T, boolean, (T) => void] { return [atomVal.value, atomVal.loading, simpleSet]; } -function updateWaveObject(val: WaveObj) { - if (val == null) { +function updateWaveObject(update: WaveObjUpdate) { + if (update == null) { return; } - if (!isValidWaveObj(val)) { - console.log("invalid wave object", val); - return; - } - let oref = makeORef(val.otype, val.oid); + let oref = makeORef(update.otype, update.oid); let wov = waveObjectValueCache.get(oref); if (wov == null) { wov = createWaveValueObject(oref, false); waveObjectValueCache.set(oref, wov); } - if (val.deleted) { + if (update.updatetype == "delete") { globalStore.set(wov.dataAtom, { value: null, loading: false }); } else { - let curValue: WaveObjectDataItemType = globalStore.get(wov.dataAtom); - if (curValue.value != null && curValue.value.version >= val.version) { + if (!isValidWaveObj(update.obj)) { + console.log("invalid wave object update", update); return; } - globalStore.set(wov.dataAtom, { value: val, loading: false }); + let curValue: WaveObjectDataItemType = globalStore.get(wov.dataAtom); + if (curValue.value != null && curValue.value.version >= update.obj.version) { + return; + } + globalStore.set(wov.dataAtom, { value: update.obj, loading: false }); } wov.holdTime = Date.now() + defaultHoldTime; return; } -function updateWaveObjects(vals: WaveObj[]) { +function updateWaveObjects(vals: WaveObjUpdate[]) { for (let val of vals) { updateWaveObject(val); } @@ -194,7 +191,7 @@ function cleanWaveObjectCache() { } Events.On("waveobj:update", (event: any) => { - const data: WaveObj[] = event?.data; + const data: WaveObjUpdate[] = event?.data; if (data == null) { return; } @@ -217,6 +214,7 @@ function wrapObjectServiceCall(fnName: string, ...args: any[]): Promise { ); prtn = prtn.then((val) => { if (val.updates) { + console.log(val.updates); updateWaveObjects(val.updates); } return val; @@ -228,6 +226,10 @@ function AddTabToWorkspace(tabName: string, activateTab: boolean): Promise<{ tab return wrapObjectServiceCall("AddTabToWorkspace", tabName, activateTab); } +function SetActiveTab(tabId: string): Promise { + return wrapObjectServiceCall("SetActiveTab", tabId); +} + function getStaticObjectValue(oref: string, getFn: jotai.Getter): T { let wov = waveObjectValueCache.get(oref); if (wov == null) { @@ -248,4 +250,5 @@ export { cleanWaveObjectCache, getStaticObjectValue, AddTabToWorkspace, + SetActiveTab, }; diff --git a/frontend/app/workspace/workspace.tsx b/frontend/app/workspace/workspace.tsx index b00428c9..7c986643 100644 --- a/frontend/app/workspace/workspace.tsx +++ b/frontend/app/workspace/workspace.tsx @@ -24,7 +24,7 @@ function Tab({ tabId }: { tabId: string }) { if (tabId == null) { return; } - // TODO + WOS.SetActiveTab(tabId); } return (
= 0; idx-- { if obj, ok := updates.UpdatesStack[idx][oref]; ok { - return obj + return &obj } } return nil } -func ContextAddUpdate(ctx context.Context, obj waveobj.WaveObj) { +func ContextAddUpdate(ctx context.Context, update WaveObjUpdate) { updatesVal := ctx.Value(waveObjUpdateKey) if updatesVal == nil { return } updates := updatesVal.(*contextUpdatesType) oref := waveobj.ORef{ - OType: obj.GetOType(), - OID: waveobj.GetOID(obj), + OType: update.OType, + OID: update.OID, } - updates.UpdatesStack[len(updates.UpdatesStack)-1][oref] = obj + updates.UpdatesStack[len(updates.UpdatesStack)-1][oref] = update } func ContextUpdatesBeginTx(ctx context.Context) context.Context { @@ -102,7 +103,7 @@ func ContextUpdatesBeginTx(ctx context.Context) context.Context { return ctx } updates := updatesVal.(*contextUpdatesType) - updates.UpdatesStack = append(updates.UpdatesStack, make(map[waveobj.ORef]waveobj.WaveObj)) + updates.UpdatesStack = append(updates.UpdatesStack, make(map[waveobj.ORef]WaveObjUpdate)) return ctx } @@ -136,6 +137,36 @@ func ContextUpdatesRollbackTx(ctx context.Context) { updates.UpdatesStack = updates.UpdatesStack[:len(updates.UpdatesStack)-1] } +type WaveObjTombstone struct { + OType string `json:"otype"` + OID string `json:"oid"` +} + +const ( + UpdateType_Update = "update" + UpdateType_Delete = "delete" +) + +type WaveObjUpdate struct { + UpdateType string `json:"updatetype"` + OType string `json:"otype"` + OID string `json:"oid"` + Obj waveobj.WaveObj `json:"obj,omitempty"` +} + +func (update WaveObjUpdate) MarshalJSON() ([]byte, error) { + rtn := make(map[string]any) + rtn["updatetype"] = update.UpdateType + rtn["otype"] = update.OType + rtn["oid"] = update.OID + var err error + rtn["obj"], err = waveobj.ToJsonMap(update.Obj) + if err != nil { + return nil, err + } + return json.Marshal(rtn) +} + type UIContext struct { WindowId string `json:"windowid"` } diff --git a/pkg/wstore/wstore_dbops.go b/pkg/wstore/wstore_dbops.go index ec7a6d3e..e912fe8d 100644 --- a/pkg/wstore/wstore_dbops.go +++ b/pkg/wstore/wstore_dbops.go @@ -159,15 +159,12 @@ func DBDelete(ctx context.Context, otype string, id string) error { table := tableNameFromOType(otype) query := fmt.Sprintf("DELETE FROM %s WHERE oid = ?", table) tx.Exec(query, id) - ContextAddUpdate(ctx, &waveobj.WaveObjTombstone{OType: otype, OID: id}) + ContextAddUpdate(ctx, WaveObjUpdate{UpdateType: UpdateType_Delete, OType: otype, OID: id}) return nil }) } func DBUpdate(ctx context.Context, val waveobj.WaveObj) error { - if waveobj.IsTombstone(val) { - return fmt.Errorf("cannot update deleted object") - } oid := waveobj.GetOID(val) if oid == "" { return fmt.Errorf("cannot update %T value with empty id", val) @@ -181,15 +178,12 @@ func DBUpdate(ctx context.Context, val waveobj.WaveObj) error { query := fmt.Sprintf("UPDATE %s SET data = ?, version = version+1 WHERE oid = ? RETURNING version", table) newVersion := tx.GetInt(query, jsonData, oid) waveobj.SetVersion(val, newVersion) - ContextAddUpdate(ctx, val) + ContextAddUpdate(ctx, WaveObjUpdate{UpdateType: UpdateType_Update, OType: val.GetOType(), OID: oid, Obj: val}) return nil }) } func DBInsert(ctx context.Context, val waveobj.WaveObj) error { - if waveobj.IsTombstone(val) { - return fmt.Errorf("cannot insert deleted object") - } oid := waveobj.GetOID(val) if oid == "" { return fmt.Errorf("cannot insert %T value with empty id", val) @@ -203,7 +197,7 @@ func DBInsert(ctx context.Context, val waveobj.WaveObj) error { waveobj.SetVersion(val, 1) query := fmt.Sprintf("INSERT INTO %s (oid, version, data) VALUES (?, ?, ?)", table) tx.Exec(query, oid, 1, jsonData) - ContextAddUpdate(ctx, val) + ContextAddUpdate(ctx, WaveObjUpdate{UpdateType: UpdateType_Update, OType: val.GetOType(), OID: oid, Obj: val}) return nil }) }