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

Improvements to LoadSupport #3805

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion admin/tabs/monitor/editor/MonitorEditorDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export const monitorEditorDialog = hoistCmp.factory<MonitorTabModel>(({model}) =
canOutsideClickClose: false,
onClose: () => (model.showEditorDialog = false),
item: panel({
item: restGrid({modelConfig: {...modelSpec, readonly: AppModel.readonly}}),
item: restGrid({
modelConfig: {
...modelSpec,
readonly: AppModel.readonly,
showRefreshButton: true
}
}),
bbar: [
filler(),
button({
Expand Down
13 changes: 11 additions & 2 deletions core/HoistService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Copyright © 2024 Extremely Heavy Industries Inc.
*/
import {HoistBase, managed, LoadSupport, LoadSpec, Loadable, PlainObject} from './';
import {XH, HoistBase, managed, LoadSupport, LoadSpec, Loadable, PlainObject} from './';

/**
* Core superclass for Services in Hoist. Services are special classes used in both Hoist and
Expand Down Expand Up @@ -83,10 +83,19 @@ export class HoistService extends HoistBase implements Loadable {
async autoRefreshAsync(meta?: PlainObject) {
return this.loadSupport?.autoRefreshAsync(meta);
}
async doLoadAsync(loadSpec: LoadSpec) {}
async loadAsync(loadSpec?: LoadSpec | Partial<LoadSpec>) {
return this.loadSupport?.loadAsync(loadSpec);
}

//--------------
// For override
//--------------
async doLoadAsync(loadSpec: LoadSpec) {}
async onLoadException(e: unknown, loadSpec: LoadSpec) {
if (!e['isRoutine']) {
XH.handleException(e, {showAlert: false});
}
}
}

export interface HoistServiceClass<T extends HoistService = HoistService> {
Expand Down
4 changes: 2 additions & 2 deletions core/load/LoadSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export class LoadSpec {
return this !== this.owner.lastRequested;
}

/** True if a more recent request to load this object's owner has *successfully completed*. */
/** True if a more recent request to load this object's owner has completed*. */
get isObsolete(): boolean {
return this.owner.lastSucceeded?.loadNumber > this.loadNumber;
return this.owner.lastCompleted?.loadNumber > this.loadNumber;
}

/** Display type of refresh for troubleshooting and logging. */
Expand Down
42 changes: 21 additions & 21 deletions core/load/LoadSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../';
import {LoadSpec, Loadable} from './';
import {makeObservable, observable, runInAction} from '@xh/hoist/mobx';
import {logDebug, logError, throwIf} from '@xh/hoist/utils/js';
import {logDebug, throwIf} from '@xh/hoist/utils/js';
import {isPlainObject, pull} from 'lodash';

/**
Expand All @@ -29,7 +29,7 @@ import {isPlainObject, pull} from 'lodash';
*/
export class LoadSupport extends HoistBase implements Loadable {
lastRequested: LoadSpec = null;
lastSucceeded: LoadSpec = null;
lastCompleted: LoadSpec = null;

@managed
loadModel: TaskObserver = TaskObserver.trackLast();
Expand Down Expand Up @@ -70,6 +70,10 @@ export class LoadSupport extends HoistBase implements Loadable {
return this.loadAsync({meta, isAutoRefresh: true});
}

onLoadException(exception: unknown, loadSpec: LoadSpec) {
this.target.onLoadException(exception, loadSpec);
}

async doLoadAsync(loadSpec: LoadSpec) {
let {target, loadModel} = this;

Expand All @@ -90,31 +94,27 @@ export class LoadSupport extends HoistBase implements Loadable {
.linkTo(loadModel)
.catch(e => {
exception = e;
target.onLoadException(exception, loadSpec);
throw e;
})
.finally(() => {
runInAction(() => {
this.lastLoadCompleted = new Date();
this.lastLoadException = exception;
});

if (!exception) {
this.lastSucceeded = loadSpec;
const now = new Date();

// If not obsolete, update state.
if (!loadSpec.isObsolete) {
runInAction(() => {
this.lastCompleted = loadSpec;
this.lastLoadCompleted = now;
this.lastLoadException = exception;
});
}

if (target instanceof RefreshContextModel) return;

const elapsed = this.lastLoadCompleted.getTime() - this.lastLoadRequested.getTime(),
status = exception ? 'failed' : null,
msg = pull([loadSpec.typeDisplay, status, `${elapsed}ms`, exception], null);
// Basic client-side debug logging
if (!(target instanceof RefreshContextModel)) {
const elapsed = now.getTime() - loadSpec.dateCreated.getTime(),
status = exception ? 'failed' : null,
msg = pull([loadSpec.typeDisplay, status, `${elapsed}ms`, exception], null);

if (exception) {
if (exception.isRoutine) {
logDebug(msg, target);
} else {
logError(msg, target);
}
} else {
logDebug(msg, target);
}
});
Expand Down
2 changes: 2 additions & 0 deletions core/load/Loadable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ export interface Loadable {
* `loadSupport` or when making calls to {@link FetchService} APIs.
*/
doLoadAsync(loadSpec: LoadSpec): Promise<void>;

onLoadException(e: unknown, loadSpec: LoadSpec): void;
}
13 changes: 11 additions & 2 deletions core/model/HoistModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {action, makeObservable, observable} from '@xh/hoist/mobx';
import {warnIf} from '@xh/hoist/utils/js';
import {forOwn, has, isFunction} from 'lodash';
import {DefaultHoistProps, HoistBase, LoadSpecConfig, managed, PlainObject} from '../';
import {DefaultHoistProps, HoistBase, LoadSpecConfig, managed, PlainObject, XH} from '../';
import {instanceManager} from '../impl/InstanceManager';
import {Loadable, LoadSpec, LoadSupport} from '../load';
import {ModelSelector} from './';
Expand Down Expand Up @@ -115,11 +115,20 @@ export abstract class HoistModel extends HoistBase implements Loadable {
async autoRefreshAsync(meta?: PlainObject) {
return this.loadSupport?.autoRefreshAsync(meta);
}
async doLoadAsync(loadSpec: LoadSpec) {}
async loadAsync(loadSpec?: LoadSpecConfig) {
return this.loadSupport?.loadAsync(loadSpec);
}

//--------------
// For override
//--------------
async doLoadAsync(loadSpec: LoadSpec) {}
async onLoadException(e: unknown, loadSpec: LoadSpec) {
if (!e['isRoutine']) {
XH.handleException(e, {showAlert: false});
}
}

//---------------------------
// Linked model support
//---------------------------
Expand Down
2 changes: 2 additions & 0 deletions data/UrlStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ export class UrlStore extends Store implements Loadable {
if (dataRoot) data = data[dataRoot];
this.loadData(data);
}

onLoadException(e: unknown, loadSpec: LoadSpec) {}
}
Loading