@finos/legend-shared
Version:
Legend Studio shared utilities and helpers
119 lines • 3.36 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { action, computed, makeObservable, observable } from 'mobx';
var ACTION_STATE;
(function (ACTION_STATE) {
ACTION_STATE["INITIAL"] = "INITIAL";
ACTION_STATE["IN_PROGRESS"] = "IN_PROGRESS";
ACTION_STATE["SUCCEEDED"] = "SUCEEDED";
ACTION_STATE["FAILED"] = "FAILED";
})(ACTION_STATE || (ACTION_STATE = {}));
export class ActionState {
state;
_message;
_messageFormatter;
constructor() {
this.state = ACTION_STATE.INITIAL;
}
setMessage(val) {
this._message = val;
}
setMessageFormatter(val) {
this._messageFormatter = val;
}
reset() {
this.state = ACTION_STATE.INITIAL;
return this;
}
inProgress() {
this.state = ACTION_STATE.IN_PROGRESS;
return this;
}
fail() {
this.state = ACTION_STATE.FAILED;
return this;
}
pass() {
this.state = ACTION_STATE.SUCCEEDED;
return this;
}
complete(hasSucceeded = true) {
if (hasSucceeded) {
this.pass();
}
else {
this.fail();
}
return this;
}
sync(val) {
const data = val.exportData();
this.state = data.state;
this._message = data.message;
}
exportData() {
return {
state: this.state,
message: this._message,
};
}
get isInInitialState() {
return this.state === ACTION_STATE.INITIAL;
}
get isInProgress() {
return this.state === ACTION_STATE.IN_PROGRESS;
}
get hasFailed() {
return this.state === ACTION_STATE.FAILED;
}
get hasSucceeded() {
return this.state === ACTION_STATE.SUCCEEDED;
}
get message() {
return this._message
? this._messageFormatter
? this._messageFormatter(this._message)
: this._message
: undefined;
}
/**
* Use this if only the completion state of the action is of concern,
* i.e. we don't care if it fails or succeeds.
*/
get hasCompleted() {
return this.hasFailed || this.hasSucceeded;
}
static create() {
return makeObservable(new ActionState(), {
state: observable,
_message: observable,
reset: action,
inProgress: action,
pass: action,
fail: action,
complete: action,
setMessage: action,
sync: action,
isInInitialState: computed,
isInProgress: computed,
hasFailed: computed,
hasSucceeded: computed,
hasCompleted: computed,
message: computed,
});
}
}
//# sourceMappingURL=ActionState.js.map