Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View state storage #3865

Merged
merged 7 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions cmp/viewmanager/DataAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
*
* Copyright © 2024 Extremely Heavy Industries Inc.
*/

import {PlainObject, XH} from '@xh/hoist/core';
import {pluralize, throwIf} from '@xh/hoist/utils/js';
import {map} from 'lodash';
import {ViewInfo} from './ViewInfo';
import {View} from './View';
import {ViewManagerModel} from './ViewManagerModel';

export interface ViewCreateSpec {
name: string;
group: string;
description: string;
isShared: boolean;
value?: PlainObject;
}

export interface ViewUpdateSpec {
name: string;
group: string;
description: string;
isShared?: boolean;
isDefaultPinned?: boolean;
}

export interface ViewUserState {
currentView?: string;
userPinned: Record<string, boolean>;
autoSave: boolean;
}

/**
* Supporting class for accessing and updating ViewManager and View data.
*
* @internal
*/
export class DataAccess<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general question - do we have any other classes like this, or named like this? Just trying to relate this to any patterns we already have for breaking up functionality along these lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a direct analog, and as you might have noticed, I have been playing around with various names for it.

The class gets a lot thinner with the move to a server-side view service, so probably less necessary but thought it was worth it to keep it around, with the main class being so large.

private readonly model: ViewManagerModel<T>;

constructor(model: ViewManagerModel<T>) {
this.model = model;
}

//---------------
// Load/search.
//---------------
/** Fetch metadata for all views accessible by current user. */
async fetchDataAsync(): Promise<{views: ViewInfo[]; state: ViewUserState}> {
const {typeDisplayName, type, instance} = this.model;
try {
const ret = await XH.fetchJson({
url: 'xhView/allData',
params: {type, viewInstance: instance}
});
return {
views: ret.views.map(v => new ViewInfo(v, this.model)),
state: ret.state
};
} catch (e) {
throw XH.exception({
message: `Unable to fetch ${pluralize(typeDisplayName)}`,
cause: e
});
}
}

/** Fetch the latest version of a view. */
async fetchViewAsync(info: ViewInfo): Promise<View<T>> {
const {model} = this;
if (!info) return View.createDefault(model);
try {
const raw = await XH.fetchJson({url: 'xhView/get', params: {token: info.token}});
return View.fromBlob(raw, model);
} catch (e) {
throw XH.exception({message: `Unable to fetch ${info.typedName}`, cause: e});
}
}

/** Create a new view, owned by the current user.*/
async createViewAsync(spec: ViewCreateSpec): Promise<View<T>> {
const {model} = this;
try {
const raw = await XH.postJson({
url: 'xhView/create',
body: {type: model.type, ...spec}
});
return View.fromBlob(raw, model);
} catch (e) {
throw XH.exception({message: `Unable to create ${model.typeDisplayName}`, cause: e});
}
}

/** Update all aspects of a view's metadata.*/
async updateViewInfoAsync(view: ViewInfo, updates: ViewUpdateSpec): Promise<View<T>> {
try {
this.ensureEditable(view);
const raw = await XH.postJson({
url: 'xhView/updateViewInfo',
params: {token: view.token},
body: updates
});
return View.fromBlob(raw, this.model);
} catch (e) {
throw XH.exception({message: `Unable to update ${view.typedName}`, cause: e});
}
}

/** Promote a view to global visibility/ownership status. */
async makeViewGlobalAsync(view: ViewInfo): Promise<View<T>> {
try {
this.ensureEditable(view);
const raw = await XH.fetchJson({url: 'xhView/makeGlobal', params: {token: view.token}});
return View.fromBlob(raw, this.model);
} catch (e) {
throw XH.exception({message: `Unable to update ${view.typedName}`, cause: e});
}
}

/** Update a view's value. */
async updateViewValueAsync(view: View<T>, value: Partial<T>): Promise<View<T>> {
try {
this.ensureEditable(view.info);
const raw = await XH.postJson({
url: 'xhView/updateValue',
params: {token: view.token},
body: value
});
return View.fromBlob(raw, this.model);
} catch (e) {
throw XH.exception({
message: `Unable to update value for ${view.typedName}`,
cause: e
});
}
}

async deleteViewsAsync(views: ViewInfo[]) {
views.forEach(v => this.ensureEditable(v));
try {
await XH.postJson({
url: 'xhView/delete',
params: {tokens: map(views, 'token').join(',')}
});
} catch (e) {
throw XH.exception({
message: `Failed to delete ${pluralize(this.model.typeDisplayName)}`,
cause: e
});
}
}

//--------------------------
// State related changes
//--------------------------
async updateStateAsync(update: Partial<ViewUserState>) {
const {type, instance} = this.model;
await XH.postJson({
url: 'xhView/updateState',
params: {type, viewInstance: instance},
body: update
});
}

//------------------
// Implementation
//------------------
private ensureEditable(view: ViewInfo) {
const {model} = this;
throwIf(
!view.isEditable,
`Cannot save changes to ${model.typeDisplayName} - missing required permission.`
);
}
}
Loading
Loading