@eclipse-emfcloud/model-service-theia
Version:
Model service Theia
144 lines • 6.12 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
// *****************************************************************************
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.FakeModelHubProtocol = void 0;
exports.bindFakeModelHubProtocol = bindFakeModelHubProtocol;
exports.connectClient = connectClient;
const inversify_1 = require("@theia/core/shared/inversify");
const common_1 = require("../../common");
/**
* All that we need for testing are the subscription-related methods and `getModel()`.
*/
let FakeModelHubProtocol = class FakeModelHubProtocol {
constructor() {
this.nextSubId = 0;
this.subscriptions = new Map();
this.models = new Map();
this.pendingSubs = [];
}
//
// Partial implementation of the `ModelHubProtocol` interface.
//
subscribe(context, ...modelIds) {
const token = {
id: ++this.nextSubId,
modelIds,
};
this.subscriptions.set(token.id, token);
const result = context === 'Boom!'
? Promise.reject('Bomb context')
: Promise.resolve(token);
this.pendingSubs.push(result);
return result;
}
closeSubscription(tokenOrId) {
const id = typeof tokenOrId === 'number' ? tokenOrId : tokenOrId.id;
this.subscriptions.delete(id);
this.client?.closeSubscription(id);
return Promise.resolve();
}
//
// Test drivers.
//
setModel(modelId, model) {
this.models.set(modelId, model);
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelLoaded(sub.id, modelId)));
}
removeModel(modelId) {
const model = this.models.get(modelId);
if (model) {
this.pendingSubsReady
.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelUnloaded(sub.id, modelId)))
.finally(() => this.models.delete(modelId));
}
}
getModel(_context, modelId) {
return this.models.has(modelId)
? Promise.resolve(this.models.get(modelId))
: Promise.reject(new Error('No such model: ' + modelId));
}
fakeModelChange(modelId, patch) {
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelChanged(sub.id, modelId, patch)));
}
fakeModelDirtyState(modelId, dirty) {
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelDirtyState(sub.id, modelId, dirty)));
}
fakeModelValidated(modelId, diagnostic) {
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelValidated(sub.id, modelId, diagnostic)));
}
fakeModelLoaded(modelId) {
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelLoaded(sub.id, modelId)));
}
fakeModelUnloaded(modelId) {
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.onModelUnloaded(sub.id, modelId)));
}
fakeModelHubDisposed() {
this.pendingSubsReady.then(() => this.lookupSubs().forEach((sub) => this.client?.onModelHubDisposed(sub.id)));
}
fakeModelHubCreated(context) {
this.client?.onModelHubCreated(context);
}
fakeModelHubDestroyed(context) {
this.client?.onModelHubDestroyed(context);
}
fakeSubscriptionClosed(modelId) {
this.pendingSubsReady.then(() => this.lookupSubs(modelId).forEach((sub) => this.client?.closeSubscription(sub.id)));
}
setClient(client) {
this.client = client;
}
get pendingSubsReady() {
return Promise.allSettled(this.pendingSubs).then(() => {
this.pendingSubs.length = 0;
});
}
lookupSubs(modelId) {
const result = [];
if (!this.client) {
// No point
return result;
}
for (const token of this.subscriptions.values()) {
if (modelId === undefined ||
token.modelIds?.length === 0 ||
token.modelIds?.includes(modelId)) {
result.push(token);
}
}
return result;
}
};
exports.FakeModelHubProtocol = FakeModelHubProtocol;
exports.FakeModelHubProtocol = FakeModelHubProtocol = __decorate([
(0, inversify_1.injectable)()
], FakeModelHubProtocol);
function bindFakeModelHubProtocol(binder) {
const bind = binder instanceof inversify_1.Container ? binder.bind.bind(binder) : binder;
bind(FakeModelHubProtocol).toSelf().inSingletonScope();
bind(common_1.ModelHubProtocol).toService(FakeModelHubProtocol);
}
function connectClient(modelHub, subscriber) {
const subscriberImpl = subscriber;
modelHub.setClient(subscriberImpl.client);
subscriberImpl.setModelHub(modelHub);
}
//# sourceMappingURL=fake-model-hub-protocol.js.map