@eclipse-emfcloud/model-service
Version:
Model service framework
60 lines • 2.66 kB
JavaScript
;
// *****************************************************************************
// Copyright (C) 2023-2024 STMicroelectronics.
//
// 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: MIT License which is
// available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: EPL-2.0 OR MIT
// *****************************************************************************
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelTriggerEngine = void 0;
const trigger_engine_1 = require("@eclipse-emfcloud/trigger-engine");
/**
* A customized trigger engine that wraps the models it operates on
* to decorate them with the `modelId`, which is subsequently extracted
* to pass along to the `ModelTrigger`s that are adapted for that usage.
*/
class ModelTriggerEngine extends trigger_engine_1.TriggerEngineImpl {
/**
* Compute triggers for changes to a `model`.
*
* @param modelId the model ID for which to compute triggers
* @param model the model document for which to compute triggers
* @param delta the changes for which to compute triggers
* @param previousModel the previous state of the model document before the `delta`
* @returns the new changes provided by triggers to follow up the `delta`, if any
*/
async applyModelTriggers(modelId, model, delta, previousModel) {
if (!delta.length) {
return undefined;
}
const modelWithId = { modelId, model };
const prevModelWithId = { modelId, model: previousModel };
return this.applyTriggers(modelWithId, delta, prevModelWithId);
}
addModelTrigger(modelTrigger) {
this.addTrigger(this.adaptTrigger(modelTrigger));
}
adaptTrigger(trigger) {
return async (document, delta, previousDocument) => {
const { modelId, model } = document;
const { model: previousModel } = previousDocument;
return trigger.getPatch(modelId, model, delta, previousModel);
};
}
applyPatch(workingCopy, patch) {
super.applyPatch(workingCopy.model, patch);
}
compare(document, workingCopy) {
return super.compare(document.model, workingCopy.model);
}
}
exports.ModelTriggerEngine = ModelTriggerEngine;
//# sourceMappingURL=model-trigger-engine.js.map