@envelop/core
Advanced tools
Comparing version
44
index.js
@@ -302,2 +302,45 @@ 'use strict'; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function getInMemoryLRUCache({ max = 1000, onDelete = () => { }, } = {}) { | ||
let storage = {}; | ||
let keys = []; | ||
return { | ||
get(key) { | ||
return storage[key]; | ||
}, | ||
set(key, value) { | ||
queueMicrotask(() => { | ||
if (storage[key] != null) { | ||
if (keys.indexOf(key)) { | ||
keys.splice(keys.indexOf(key), 1); | ||
} | ||
} | ||
keys.push(key); | ||
storage[key] = value; | ||
while (keys.length > max) { | ||
const key = keys.shift(); | ||
if (key != null) { | ||
onDelete(key, storage[key]); | ||
delete storage[key]; | ||
} | ||
} | ||
}); | ||
}, | ||
delete(key) { | ||
queueMicrotask(() => { | ||
if (storage[key] != null) { | ||
keys.splice(keys.indexOf(key), 1); | ||
onDelete(key, storage[key]); | ||
delete storage[key]; | ||
} | ||
}); | ||
}, | ||
clear() { | ||
queueMicrotask(() => { | ||
storage = {}; | ||
keys = []; | ||
}); | ||
}, | ||
}; | ||
} | ||
@@ -1293,2 +1336,3 @@ function createEnvelopOrchestrator(plugins) { | ||
exports.formatError = formatError; | ||
exports.getInMemoryLRUCache = getInMemoryLRUCache; | ||
exports.handleStreamOrSingleExecutionResult = handleStreamOrSingleExecutionResult; | ||
@@ -1295,0 +1339,0 @@ exports.isAsyncIterable = isAsyncIterable; |
{ | ||
"name": "@envelop/core", | ||
"version": "2.3.1", | ||
"version": "2.4.0-alpha-3936561.0", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
@@ -34,1 +34,10 @@ import { ASTNode, DocumentNode, OperationDefinitionNode, Source, ExecutionResult, SubscriptionArgs, ExecutionArgs } from 'graphql'; | ||
export declare function errorAsyncIterator<TInput>(source: AsyncIterable<TInput>, onError: (err: unknown) => void): AsyncGenerator<TInput>; | ||
export declare function getInMemoryLRUCache<V>({ max, onDelete, }?: { | ||
max?: number; | ||
onDelete?: (key: string, value: V) => void; | ||
}): { | ||
get(key: string): V | undefined; | ||
set(key: string, value: V): void; | ||
delete(key: string): void; | ||
clear(): void; | ||
}; |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
113299
2.72%2834
3.51%2
100%