monaco-editor-core
Version:
A browser based code editor
75 lines • 2.82 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { BugIndicatingError } from './commonFacade/deps.js';
/**
* Subscribes to and records changes and the last value of the given observables.
* Don't use the key "changes", as it is reserved for the changes array!
*/
export function recordChanges(obs) {
return {
createChangeSummary: (_previousChangeSummary) => {
// eslint-disable-next-line local/code-no-any-casts
return {
changes: [],
};
},
handleChange(ctx, changeSummary) {
for (const key in obs) {
if (ctx.didChange(obs[key])) {
// eslint-disable-next-line local/code-no-any-casts
changeSummary.changes.push({ key, change: ctx.change });
}
}
return true;
},
beforeUpdate(reader, changeSummary) {
for (const key in obs) {
if (key === 'changes') {
throw new BugIndicatingError('property name "changes" is reserved for change tracking');
}
changeSummary[key] = obs[key].read(reader);
}
}
};
}
/**
* Subscribes to and records changes and the last value of the given observables.
* Don't use the key "changes", as it is reserved for the changes array!
*/
export function recordChangesLazy(getObs) {
let obs = undefined;
return {
createChangeSummary: (_previousChangeSummary) => {
// eslint-disable-next-line local/code-no-any-casts
return {
changes: [],
};
},
handleChange(ctx, changeSummary) {
if (!obs) {
obs = getObs();
}
for (const key in obs) {
if (ctx.didChange(obs[key])) {
// eslint-disable-next-line local/code-no-any-casts
changeSummary.changes.push({ key, change: ctx.change });
}
}
return true;
},
beforeUpdate(reader, changeSummary) {
if (!obs) {
obs = getObs();
}
for (const key in obs) {
if (key === 'changes') {
throw new BugIndicatingError('property name "changes" is reserved for change tracking');
}
changeSummary[key] = obs[key].read(reader);
}
}
};
}
//# sourceMappingURL=changeTracker.js.map