@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
114 lines • 5.13 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultModelInitializationConstraint = exports.ModelInitializationConstraint = void 0;
/********************************************************************************
* Copyright (c) 2020-2023 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
const inversify_1 = require("inversify");
const sprotty_1 = require("@eclipse-glsp/sprotty");
/**
* The constraint defining when the initialization of the GLSP model is completed.
*
* Many actions, such as the `CenterAction`, can only be successfully processed if
* the GLSP model initialization is completed, that is, the model has been set,
* its bounds have been computed, the canvas bounds are available, etc.
*
* An injectable implementation of this constraint will be used by the
* `GLSPActionDispatcher` to determine when the initialization is completed.
* The action dispatcher therefore provides a promise via `onceInitialized()`
* to trigger clients that want to dispatch an action, once the initialization
* is done.
*
* For most of the cases `DefaultInitializationConstraint` can be used. In fact,
* it is bound by default. However, custom implementations can rebind other
* implementations of this constraint to, for instance, delay further before the
* `onceInitialized()` promise is fulfilled by the `GLSPActionDispatcher`.
*/
let ModelInitializationConstraint = class ModelInitializationConstraint {
constructor() {
this.completion = new sprotty_1.Deferred();
this._isCompleted = false;
this.onInitializedEmitter = new sprotty_1.Emitter();
}
get isCompleted() {
return this._isCompleted;
}
onInitialized(listener) {
if (!listener) {
return this.completion.promise;
}
if (this.isCompleted) {
listener();
return sprotty_1.Disposable.empty();
}
return this.onInitializedEmitter.event(listener);
}
setCompleted() {
if (!this.isCompleted) {
this._isCompleted = true;
this.completion.resolve();
this.onInitializedEmitter.fire();
this.onInitializedEmitter.dispose();
}
}
notifyDispatched(action) {
if (this.isCompleted) {
return;
}
if (this.isInitializedAfter(action)) {
this.setCompleted();
}
}
};
exports.ModelInitializationConstraint = ModelInitializationConstraint;
exports.ModelInitializationConstraint = ModelInitializationConstraint = __decorate([
(0, inversify_1.injectable)()
], ModelInitializationConstraint);
/**
* Default initialization constraint triggers after a non-empty `UpdateModelAction`
* and a subsequent `InitializeCanvasBoundsAction`.
*/
let DefaultModelInitializationConstraint = class DefaultModelInitializationConstraint extends ModelInitializationConstraint {
constructor() {
super(...arguments);
this.seenNonEmptyModelAction = false;
}
isInitializedAfter(action) {
if (this.isNonEmptyModelAction(action)) {
this.seenNonEmptyModelAction = true;
}
else if (this.seenNonEmptyModelAction && action.kind === sprotty_1.InitializeCanvasBoundsAction.KIND) {
return true;
}
return false;
}
isNonEmptyModelAction(action) {
if (sprotty_1.SetModelAction.is(action) || sprotty_1.UpdateModelAction.is(action)) {
return action.newRoot.type !== 'NONE';
}
return false;
}
};
exports.DefaultModelInitializationConstraint = DefaultModelInitializationConstraint;
exports.DefaultModelInitializationConstraint = DefaultModelInitializationConstraint = __decorate([
(0, inversify_1.injectable)()
], DefaultModelInitializationConstraint);
//# sourceMappingURL=model-initialization-constraint.js.map