UNPKG

ts-japi

Version:

A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification

47 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Cache { /** * The default max for document storage */ static defaultLimit = 10; /** @internal The storage for the cache */ storage = []; /** * The maximum amount of documents that can be storage before erasure. */ limit = Cache.defaultLimit; /** * The method to use in determining data equality */ resolver = Object.is; /** * Creates a {@link Cache} * * @param limit - The maximum amount of documents that can be stored before erasure. */ constructor(options = {}) { if (options.limit) this.limit = options.limit; if (options.resolver) this.resolver = options.resolver; } /** @internal Gets a document in the cache */ get(data, options) { const document = this.storage.find(([storedData, storedOptions]) => this.resolver(storedData, data) && Object.is(storedOptions, options)); if (document) return document[2]; else return false; } /** @internal Sets a document in the cache */ set(data, document, options) { if (this.storage.length > this.limit) { this.storage.shift(); } this.storage.push([data, options, document]); return document; } } exports.default = Cache; //# sourceMappingURL=cache.js.map