@lowdefy/helpers 4.0.0-alpha.35 → 4.0.0-alpha.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // https://stackoverflow.com/a/46432113/8295949
16
+ // Implementation of Least Recently Updated cache.
17
+ // It uses the fact that the implementation of Map keep track of item insertion order
18
+ let LRUCache = class LRUCache {
19
+ get(key) {
20
+ const item = this.cache.get(key);
21
+ if (item) {
22
+ // Refresh key.
23
+ this.cache.delete(key);
24
+ this.cache.set(key, item);
25
+ }
26
+ return item;
27
+ }
28
+ set(key, val) {
29
+ if (this.cache.has(key)) {
30
+ // Refresh key
31
+ this.cache.delete(key);
32
+ } else if (this.cache.size === this.maxSize) {
33
+ // Evict oldest
34
+ this.cache.delete(this.first());
35
+ }
36
+ this.cache.set(key, val);
37
+ }
38
+ first() {
39
+ return this.cache.keys().next().value;
40
+ }
41
+ constructor({ maxSize =100 } = {}){
42
+ this.maxSize = maxSize;
43
+ this.cache = new Map();
44
+ }
45
+ };
46
+ export default LRUCache;
@@ -12,11 +12,11 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ function createCachedPromises(getter) {
16
- const cache = new Map();
15
+ */ function createCachedPromises({ getter , cache }) {
17
16
  function cachedPromises(key) {
18
- if (cache.has(key)) {
19
- return Promise.resolve(cache.get(key));
17
+ const cached = cache.get(key);
18
+ if (cached) {
19
+ return Promise.resolve(cached);
20
20
  }
21
21
  const promise = getter(key);
22
22
  cache.set(key, promise);
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@
15
15
  */ import applyArrayIndices from './applyArrayIndices.js';
16
16
  import cachedPromises from './cachedPromises.js';
17
17
  import get from './get.js';
18
+ import LRUCache from './LRUCache.js';
18
19
  import mergeObjects from './mergeObjects.js';
19
20
  import omit from './omit.js';
20
21
  import serializer from './serializer.js';
@@ -25,4 +26,4 @@ import type from './type.js';
25
26
  import unset from './unset.js';
26
27
  import urlQuery from './urlQuery.js';
27
28
  import wait from './wait.js';
28
- export { applyArrayIndices, cachedPromises, get, mergeObjects, omit, serializer, set, stableStringify, swap, type, unset, urlQuery, wait };
29
+ export { applyArrayIndices, cachedPromises, get, LRUCache, mergeObjects, omit, serializer, set, stableStringify, swap, type, unset, urlQuery, wait };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/helpers",
3
- "version": "4.0.0-alpha.35",
3
+ "version": "4.0.0-alpha.37",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "d1cfcdb0b425ca9eb1259bdabe9cdf51df7f900c"
53
+ "gitHead": "855e81894be16837be556de36a036215f1efe021"
54
54
  }