@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
77 lines • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.repeatOnMessagesUpdated = exports.updateMessages = exports.onMessagesUpdated = exports.messages = void 0;
/********************************************************************************
* Copyright (c) 2025 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 sprotty_1 = require("@eclipse-glsp/sprotty");
const rawMessages = require("./messages.json");
const deepUpdate = (target, updates) => {
for (const key in updates) {
// Guard against prototype pollution and block __proto__ and constructor
if (!Object.prototype.hasOwnProperty.call(updates, key) || key === '__proto__' || key === 'constructor') {
continue;
}
if (updates[key] && typeof updates[key] === 'object' && !Array.isArray(updates[key])) {
if (!target[key]) {
target[key] = {};
}
deepUpdate(target[key], updates[key]);
}
else {
target[key] = updates[key];
}
}
};
/**
* The messages object containing all known messages.
*/
exports.messages = rawMessages;
const messagesUpdatedEmitter = new sprotty_1.Emitter();
/**
* Event that is fired when the messages are updated.
*/
exports.onMessagesUpdated = messagesUpdatedEmitter.event;
/**
* Update the messages with the given set of messages. This may include overwriting existing messages or adding new ones.
*
* @param updates The updates to apply to the messages object.
*/
const updateMessages = (updates) => {
deepUpdate(exports.messages, updates);
messagesUpdatedEmitter.fire(exports.messages);
};
exports.updateMessages = updateMessages;
/**
* Executes the given listener with the current messages and re-executes it whenever the messages are updated.
* If the listener returns a disposable, it will be disposed before the listener is called again.
*
* @param listener The listener to re-execute when the messages are updated.
* @param options Options to control the behavior of the listener execution.
* @returns A disposable that can be used to clean up the listener.
*/
function repeatOnMessagesUpdated(listener, options = { initial: true }) {
let cleanup = options.initial ? listener(exports.messages) : {};
const updateListener = (0, exports.onMessagesUpdated)(() => {
sprotty_1.Disposable.dispose(cleanup);
cleanup = listener();
});
return sprotty_1.Disposable.create(() => {
sprotty_1.Disposable.dispose(cleanup);
updateListener.dispose();
});
}
exports.repeatOnMessagesUpdated = repeatOnMessagesUpdated;
//# sourceMappingURL=messages.js.map