UNPKG

@pkerschbaum/code-oss-file-service

Version:

VS Code ([microsoft/vscode](https://github.com/microsoft/vscode)) includes a rich "`FileService`" and "`DiskFileSystemProvider`" abstraction built on top of Node.js core modules (`fs`, `path`) and Electron's `shell` module. This package allows to use that

53 lines 1.77 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.LRUCachedComputed = exports.Cache = void 0; const cancellation_1 = require("../../base/common/cancellation"); class Cache { constructor(task) { this.task = task; this.result = null; } get() { if (this.result) { return this.result; } const cts = new cancellation_1.CancellationTokenSource(); const promise = this.task(cts.token); this.result = { promise, dispose: () => { this.result = null; cts.cancel(); cts.dispose(); } }; return this.result; } } exports.Cache = Cache; /** * Uses a LRU cache to make a given parametrized function cached. * Caches just the last value. * The key must be JSON serializable. */ class LRUCachedComputed { constructor(computeFn) { this.computeFn = computeFn; this.lastCache = undefined; this.lastArgKey = undefined; } get(arg) { const key = JSON.stringify(arg); if (this.lastArgKey !== key) { this.lastArgKey = key; this.lastCache = this.computeFn(arg); } return this.lastCache; } } exports.LRUCachedComputed = LRUCachedComputed; //# sourceMappingURL=cache.js.map