Version 1.0.2 mit node_modules Verzeichnis

This commit is contained in:
ISA
2024-10-02 07:58:24 +02:00
parent f353a06b1b
commit 62b6e55a0a
68228 changed files with 4548477 additions and 651 deletions

View File

@@ -0,0 +1,133 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall recoil
*/
'use strict';
const {
getRecoilTestFn,
} = require('recoil-shared/__test_utils__/Recoil_TestingUtils');
let LRUCache;
const testRecoil = getRecoilTestFn(() => {
({LRUCache} = require('../Recoil_LRUCache'));
});
describe('LRUCache', () => {
testRecoil('setting and getting (without hitting max size)', () => {
const cache = new LRUCache({
maxSize: 10,
});
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.size()).toBe(3);
expect(cache.get('a')).toBe(1);
expect(cache.get('b')).toBe(2);
expect(cache.get('c')).toBe(3);
cache.delete('a');
cache.delete('b');
expect(cache.size()).toBe(1);
});
testRecoil('setting and getting (hitting max size)', () => {
const cache = new LRUCache({
maxSize: 2,
});
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.size()).toBe(2);
expect(cache.get('a')).toBe(undefined);
expect(cache.get('b')).toBe(2);
expect(cache.get('c')).toBe(3);
cache.delete('a');
cache.delete('b');
expect(cache.size()).toBe(1);
cache.set('d', 4);
cache.set('e', 5);
expect(cache.size()).toBe(2);
expect(cache.get('b')).toBe(undefined);
expect(cache.get('c')).toBe(undefined);
});
testRecoil('manually deleting LRU', () => {
const cache = new LRUCache({
maxSize: 10,
});
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.size()).toBe(3);
expect(cache.get('a')).toBe(1);
expect(cache.get('b')).toBe(2);
expect(cache.get('c')).toBe(3);
cache.deleteLru(); // delete 'a'
expect(cache.get('a')).toBe(undefined);
expect(cache.size()).toBe(2);
cache.deleteLru(); // delete 'b'
expect(cache.get('b')).toBe(undefined);
expect(cache.size()).toBe(1);
});
testRecoil('head() and tail()', () => {
const cache = new LRUCache({
maxSize: 10,
});
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.size()).toBe(3);
expect(cache.tail()).toBeDefined();
expect(cache.tail()?.value).toBe(1);
expect(cache.head()?.value).toBe(3);
expect(cache.get('c')).toBe(3);
expect(cache.get('b')).toBe(2);
expect(cache.get('a')).toBe(1);
expect(cache.tail()?.value).toBe(3);
expect(cache.head()?.value).toBe(1);
cache.delete('a');
cache.delete('b');
expect(cache.tail()?.value).toBe(3);
expect(cache.head()?.value).toBe(3);
expect(cache.size()).toBe(1);
});
});

View File

@@ -0,0 +1,53 @@
/**
* (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
*
* @flow strict-local
* @format
* @oncall recoil
*/
'use strict';
const {
getRecoilTestFn,
} = require('recoil-shared/__test_utils__/Recoil_TestingUtils');
let MapCache;
const testRecoil = getRecoilTestFn(() => {
({MapCache} = require('../Recoil_MapCache'));
});
describe('MapCache', () => {
testRecoil('setting and getting', () => {
const cache = new MapCache<string, number>();
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.size()).toBe(3);
expect(cache.get('a')).toBe(1);
expect(cache.get('b')).toBe(2);
expect(cache.get('c')).toBe(3);
});
testRecoil('deleting', () => {
const cache = new MapCache<string, number>();
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3);
expect(cache.size()).toBe(3);
cache.delete('a');
expect(cache.size()).toBe(2);
expect(cache.get('a')).toBe(undefined);
expect(cache.get('b')).toBe(2);
expect(cache.has('a')).toBe(false);
});
});

View File

@@ -0,0 +1,264 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall recoil
*/
'use strict';
import type {NodeKey} from 'Recoil_Keys';
const {
getRecoilTestFn,
} = require('recoil-shared/__test_utils__/Recoil_TestingUtils');
let TreeCache, loadableWithValue, nullthrows;
const testRecoil = getRecoilTestFn(() => {
({TreeCache} = require('../Recoil_TreeCache'));
nullthrows = require('recoil-shared/util/Recoil_nullthrows');
({loadableWithValue} = require('../../adt/Recoil_Loadable'));
});
describe('TreeCache', () => {
testRecoil('setting and getting values', () => {
const cache = new TreeCache();
const [route1, loadable1] = [
[
['a', 2],
['b', 3],
],
loadableWithValue('value1'),
];
const [route2, loadable2] = [
[
['a', 3],
['b', 4],
],
loadableWithValue('value2'),
];
const [route3, loadable3] = [[['a', 4]], loadableWithValue('value3')];
cache.set(route1, loadable1);
cache.set(route2, loadable2);
cache.set(route3, loadable3);
expect(
cache.get(nodeKey => route1.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable1);
expect(
cache.get(nodeKey => route2.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable2);
expect(
cache.get(nodeKey => route3.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable3);
expect(cache.size()).toBe(3);
});
testRecoil('deleting values', () => {
const cache = new TreeCache();
const [route1, loadable1] = [
[
['a', 2],
['b', 3],
],
loadableWithValue('value1'),
];
const [route2, loadable2] = [
[
['a', 2],
['b', 4],
['c', 5],
],
loadableWithValue('value2'),
];
const [route3, loadable3] = [[['a', 6]], loadableWithValue('value3')];
cache.set(route1, loadable1);
cache.set(route2, loadable2);
cache.set(route3, loadable3);
const leaf1 = cache.getLeafNode(
nodeKey => route1.find(([key]) => key === nodeKey)?.[1],
);
const leaf2 = cache.getLeafNode(
nodeKey => route2.find(([key]) => key === nodeKey)?.[1],
);
const leaf3 = cache.getLeafNode(
nodeKey => route3.find(([key]) => key === nodeKey)?.[1],
);
expect(leaf1).toBeDefined();
expect(leaf2).toBeDefined();
expect(leaf3).toBeDefined();
const leaf1Node = nullthrows(leaf1);
const leaf2Node = nullthrows(leaf2);
const leaf3Node = nullthrows(leaf3);
expect(cache.size()).toBe(3);
const deleted1 = cache.delete(leaf1Node);
expect(deleted1).toBe(true);
expect(cache.size()).toBe(2);
const deleted2 = cache.delete(leaf2Node);
expect(deleted2).toBe(true);
expect(cache.size()).toBe(1);
const deleted3 = cache.delete(leaf3Node);
expect(deleted3).toBe(true);
expect(cache.size()).toBe(0);
expect(cache.root()).toBeNull();
const deletedAgain = cache.delete(leaf1Node);
expect(deletedAgain).toBe(false);
});
testRecoil('onHit() handler', () => {
const [route1, loadable1] = [
[
['a', 2],
['b', 3],
],
loadableWithValue('value1'),
];
const onHit = jest.fn();
const cache = new TreeCache({
onHit,
});
const getter = (nodeKey: NodeKey) =>
route1.find(([key]) => key === nodeKey)?.[1];
cache.set(route1, loadable1);
// hit
cache.get(getter);
// miss
cache.get(() => {});
// hit
cache.get(getter);
expect(onHit).toHaveBeenCalledTimes(2);
});
testRecoil('onSet() handler', () => {
const onSet = jest.fn();
const cache = new TreeCache({
onSet,
});
const [route1, loadable1] = [
[
['a', 2],
['b', 3],
],
loadableWithValue('value1'),
];
const [route2, loadable2] = [
[
['a', 3],
['b', 4],
],
loadableWithValue('value2'),
];
const [route3, loadable3] = [[['a', 4]], loadableWithValue('value3')];
cache.set(route1, loadable1);
cache.set(route2, loadable2);
cache.set(route3, loadable3);
expect(onSet).toHaveBeenCalledTimes(3);
});
testRecoil('default key generation uses reference equality', () => {
const [route1, loadable1] = [
[
['a', [2]],
['b', [3]],
],
loadableWithValue('value1'),
];
const cache = new TreeCache();
cache.set(route1, loadable1);
const resultWithKeyCopy = cache.get(nodeKey => [
...(route1.find(([key]) => key === nodeKey)?.[1] ?? []),
]);
expect(resultWithKeyCopy).toBeUndefined();
const result = cache.get(
nodeKey => route1.find(([key]) => key === nodeKey)?.[1],
);
expect(result).toBe(loadable1);
});
testRecoil('mapNodeValue() to implement value equality keys', () => {
const cache = new TreeCache({
mapNodeValue: value => JSON.stringify(value),
});
const [route1, loadable1] = [
[
['a', [2]],
['b', [3]],
],
loadableWithValue('value1'),
];
cache.set(route1, loadable1);
const resultWithKeyCopy = cache.get(nodeKey => [
...(route1.find(([key]) => key === nodeKey)?.[1] ?? []),
]);
expect(resultWithKeyCopy).toBe(loadable1);
});
// Test ability to scale cache to large number of entries.
// Use more dependencies than the JavaScript callstack depth limit to ensure
// we are not using a recursive algorithm.
testRecoil('Scalability', () => {
const cache = new TreeCache();
const route = Array.from(Array(10000).keys()).map(i => [
String(i),
String(i),
]);
// $FlowFixMe[incompatible-call]
cache.set(route, 'VALUE');
expect(cache.get(x => x)).toBe('VALUE');
const leafNode = cache.getLeafNode(x => x);
expect(cache.delete(nullthrows(leafNode))).toBe(true);
});
});

View File

@@ -0,0 +1,203 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall recoil
*/
'use strict';
const {
getRecoilTestFn,
} = require('recoil-shared/__test_utils__/Recoil_TestingUtils');
let cacheFromPolicy;
const testRecoil = getRecoilTestFn(() => {
cacheFromPolicy = require('../Recoil_cacheFromPolicy');
});
describe('cacheFromPolicy()', () => {
testRecoil('equality: reference, eviction: keep-all', () => {
const policy = {equality: 'reference', eviction: 'keep-all'};
const cache = cacheFromPolicy<{[string]: number}, boolean>(policy);
const obj1 = {a: 1};
const obj2 = {b: 2};
const obj3 = {c: 3};
cache.set(obj1, true);
cache.set(obj2, true);
cache.set(obj3, true);
expect(cache.size()).toBe(3);
expect(cache.get(obj1)).toBe(true);
expect(cache.get(obj2)).toBe(true);
expect(cache.get(obj3)).toBe(true);
expect(cache.get({...obj1})).toBe(undefined);
expect(cache.get({...obj2})).toBe(undefined);
expect(cache.get({...obj3})).toBe(undefined);
});
testRecoil('equality: value, eviction: keep-all', () => {
const policy = {equality: 'value', eviction: 'keep-all'};
const cache = cacheFromPolicy<{[string]: number}, boolean>(policy);
const obj1 = {a: 1};
const obj2 = {b: 2};
const obj3 = {c: 3};
cache.set(obj1, true);
cache.set(obj2, true);
cache.set(obj3, true);
expect(cache.size()).toBe(3);
expect(cache.get(obj1)).toBe(true);
expect(cache.get(obj2)).toBe(true);
expect(cache.get(obj3)).toBe(true);
expect(cache.get({...obj1})).toBe(true);
expect(cache.get({...obj2})).toBe(true);
expect(cache.get({...obj3})).toBe(true);
});
testRecoil('equality: reference, eviction: lru', () => {
const policy = {equality: 'reference', eviction: 'lru', maxSize: 2};
const cache = cacheFromPolicy<{[string]: number}, boolean>(policy);
const obj1 = {a: 1};
const obj2 = {b: 2};
const obj3 = {c: 3};
cache.set(obj1, true);
cache.set(obj2, true);
cache.set(obj3, true);
expect(cache.size()).toBe(2);
expect(cache.get(obj1)).toBe(undefined);
expect(cache.get(obj2)).toBe(true);
expect(cache.get(obj3)).toBe(true);
cache.set(obj1, true);
expect(cache.size()).toBe(2);
expect(cache.get(obj2)).toBe(undefined);
expect(cache.get(obj1)).toBe(true);
expect(cache.get(obj3)).toBe(true);
expect(cache.get({...obj1})).toBe(undefined);
expect(cache.get({...obj3})).toBe(undefined);
});
testRecoil('equality: value, eviction: lru', () => {
const policy = {equality: 'value', eviction: 'lru', maxSize: 2};
const cache = cacheFromPolicy<{[string]: number}, boolean>(policy);
const obj1 = {a: 1};
const obj2 = {b: 2};
const obj3 = {c: 3};
cache.set(obj1, true);
cache.set(obj2, true);
cache.set(obj3, true);
expect(cache.size()).toBe(2);
expect(cache.get(obj1)).toBe(undefined);
expect(cache.get(obj2)).toBe(true);
expect(cache.get(obj3)).toBe(true);
cache.set(obj1, true);
expect(cache.size()).toBe(2);
expect(cache.get(obj2)).toBe(undefined);
expect(cache.get(obj1)).toBe(true);
expect(cache.get(obj3)).toBe(true);
expect(cache.get({...obj2})).toBe(undefined);
expect(cache.get({...obj1})).toBe(true);
expect(cache.get({...obj3})).toBe(true);
});
testRecoil('equality: reference, eviction: most-recent', () => {
const policy = {equality: 'reference', eviction: 'most-recent'};
const cache = cacheFromPolicy<{[string]: number}, boolean>(policy);
const obj1 = {a: 1};
const obj2 = {b: 2};
const obj3 = {c: 3};
cache.set(obj1, true);
cache.set(obj2, true);
cache.set(obj3, true);
expect(cache.size()).toBe(1);
expect(cache.get(obj1)).toBe(undefined);
expect(cache.get(obj2)).toBe(undefined);
expect(cache.get(obj3)).toBe(true);
cache.set(obj1, true);
expect(cache.size()).toBe(1);
expect(cache.get(obj2)).toBe(undefined);
expect(cache.get(obj3)).toBe(undefined);
expect(cache.get(obj1)).toBe(true);
expect(cache.get({...obj2})).toBe(undefined);
expect(cache.get({...obj1})).toBe(undefined);
expect(cache.get({...obj3})).toBe(undefined);
});
testRecoil('equality: value, eviction: most-recent', () => {
const policy = {equality: 'value', eviction: 'most-recent'};
const cache = cacheFromPolicy<{[string]: number}, boolean>(policy);
const obj1 = {a: 1};
const obj2 = {b: 2};
const obj3 = {c: 3};
cache.set(obj1, true);
cache.set(obj2, true);
cache.set(obj3, true);
expect(cache.size()).toBe(1);
expect(cache.get(obj1)).toBe(undefined);
expect(cache.get(obj2)).toBe(undefined);
expect(cache.get(obj3)).toBe(true);
cache.set(obj1, true);
expect(cache.size()).toBe(1);
expect(cache.get(obj2)).toBe(undefined);
expect(cache.get(obj3)).toBe(undefined);
expect(cache.get(obj1)).toBe(true);
expect(cache.get({...obj2})).toBe(undefined);
expect(cache.get({...obj3})).toBe(undefined);
expect(cache.get({...obj1})).toBe(true);
});
});

View File

@@ -0,0 +1,276 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall recoil
*/
'use strict';
import type {NodeKey} from 'Recoil_Keys';
const {
getRecoilTestFn,
} = require('recoil-shared/__test_utils__/Recoil_TestingUtils');
let treeCacheFromPolicy;
const testRecoil = getRecoilTestFn(() => {
treeCacheFromPolicy = require('../Recoil_treeCacheFromPolicy');
});
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
const valGetterFromPath = path => (nodeKey: NodeKey) =>
path.find(([k]) => k === nodeKey)?.[1];
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
const clonePath = path => JSON.parse(JSON.stringify(path));
describe('treeCacheFromPolicy()', () => {
testRecoil('equality: reference, eviction: keep-all', () => {
const policy = {equality: 'reference', eviction: 'keep-all'};
const cache = treeCacheFromPolicy<{[string]: number}>(policy);
const path1 = [
['a', [1]],
['b', [2]],
];
const obj1 = {a: 1};
const path2 = [['a', [2]]];
const obj2 = {b: 2};
const path3 = [
['a', [3]],
['c', [4]],
];
const obj3 = {c: 3};
cache.set(path1, obj1);
cache.set(path2, obj2);
cache.set(path3, obj3);
expect(cache.size()).toBe(3);
expect(cache.get(valGetterFromPath(path1))).toBe(obj1);
expect(cache.get(valGetterFromPath(path2))).toBe(obj2);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
expect(cache.get(valGetterFromPath(clonePath(path1)))).toBe(undefined);
expect(cache.get(valGetterFromPath(clonePath(path2)))).toBe(undefined);
expect(cache.get(valGetterFromPath(clonePath(path3)))).toBe(undefined);
});
testRecoil('equality: value, eviction: keep-all', () => {
const policy = {equality: 'value', eviction: 'keep-all'};
const cache = treeCacheFromPolicy<{[string]: number}>(policy);
const path1 = [
['a', [1]],
['b', [2]],
];
const obj1 = {a: 1};
const path2 = [['a', [2]]];
const obj2 = {b: 2};
const path3 = [
['a', [3]],
['c', [4]],
];
const obj3 = {c: 3};
cache.set(path1, obj1);
cache.set(path2, obj2);
cache.set(path3, obj3);
expect(cache.size()).toBe(3);
expect(cache.get(valGetterFromPath(path1))).toBe(obj1);
expect(cache.get(valGetterFromPath(path2))).toBe(obj2);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
expect(cache.get(valGetterFromPath(clonePath(path1)))).toBe(obj1);
expect(cache.get(valGetterFromPath(clonePath(path2)))).toBe(obj2);
expect(cache.get(valGetterFromPath(clonePath(path3)))).toBe(obj3);
});
testRecoil('equality: reference, eviction: lru', () => {
const policy = {equality: 'reference', eviction: 'lru', maxSize: 2};
const cache = treeCacheFromPolicy<{[string]: number}>(policy);
const path1 = [
['a', [1]],
['b', [2]],
];
const obj1 = {a: 1};
const path2 = [['a', [2]]];
const obj2 = {b: 2};
const path3 = [
['a', [3]],
['c', [4]],
];
const obj3 = {c: 3};
cache.set(path1, obj1);
cache.set(path2, obj2);
cache.set(path3, obj3);
expect(cache.size()).toBe(2);
expect(cache.get(valGetterFromPath(path1))).toBe(undefined);
expect(cache.get(valGetterFromPath(path2))).toBe(obj2);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
cache.set(path1, obj1);
expect(cache.size()).toBe(2);
expect(cache.get(valGetterFromPath(path2))).toBe(undefined);
expect(cache.get(valGetterFromPath(path1))).toBe(obj1);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
expect(cache.get(valGetterFromPath(clonePath(path1)))).toBe(undefined);
expect(cache.get(valGetterFromPath(clonePath(path3)))).toBe(undefined);
});
testRecoil('equality: value, eviction: lru', () => {
const policy = {equality: 'value', eviction: 'lru', maxSize: 2};
const cache = treeCacheFromPolicy<{[string]: number}>(policy);
const path1 = [
['a', [1]],
['b', [2]],
];
const obj1 = {a: 1};
const path2 = [['a', [2]]];
const obj2 = {b: 2};
const path3 = [
['a', [3]],
['c', [4]],
];
const obj3 = {c: 3};
cache.set(path1, obj1);
cache.set(path2, obj2);
cache.set(path3, obj3);
expect(cache.size()).toBe(2);
expect(cache.get(valGetterFromPath(path1))).toBe(undefined);
expect(cache.get(valGetterFromPath(path2))).toBe(obj2);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
cache.set(path1, obj1);
expect(cache.size()).toBe(2);
expect(cache.get(valGetterFromPath(path2))).toBe(undefined);
expect(cache.get(valGetterFromPath(path1))).toBe(obj1);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
expect(cache.get(valGetterFromPath(clonePath(path1)))).toBe(obj1);
expect(cache.get(valGetterFromPath(clonePath(path3)))).toBe(obj3);
});
testRecoil('equality: reference, eviction: most-recent', () => {
const policy = {equality: 'reference', eviction: 'most-recent'};
const cache = treeCacheFromPolicy<{[string]: number}>(policy);
const path1 = [
['a', [1]],
['b', [2]],
];
const obj1 = {a: 1};
const path2 = [['a', [2]]];
const obj2 = {b: 2};
const path3 = [
['a', [3]],
['c', [4]],
];
const obj3 = {c: 3};
cache.set(path1, obj1);
cache.set(path2, obj2);
cache.set(path3, obj3);
expect(cache.size()).toBe(1);
expect(cache.get(valGetterFromPath(path1))).toBe(undefined);
expect(cache.get(valGetterFromPath(path2))).toBe(undefined);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
cache.set(path1, obj1);
expect(cache.size()).toBe(1);
expect(cache.get(valGetterFromPath(path2))).toBe(undefined);
expect(cache.get(valGetterFromPath(path3))).toBe(undefined);
expect(cache.get(valGetterFromPath(path1))).toBe(obj1);
expect(cache.get(valGetterFromPath(clonePath(path1)))).toBe(undefined);
expect(cache.get(valGetterFromPath(clonePath(path2)))).toBe(undefined);
expect(cache.get(valGetterFromPath(clonePath(path3)))).toBe(undefined);
});
testRecoil('equality: value, eviction: most-recent', () => {
const policy = {equality: 'value', eviction: 'most-recent'};
const cache = treeCacheFromPolicy<{[string]: number}>(policy);
const path1 = [
['a', [1]],
['b', [2]],
];
const obj1 = {a: 1};
const path2 = [['a', [2]]];
const obj2 = {b: 2};
const path3 = [
['a', [3]],
['c', [4]],
];
const obj3 = {c: 3};
cache.set(path1, obj1);
cache.set(path2, obj2);
cache.set(path3, obj3);
expect(cache.size()).toBe(1);
expect(cache.get(valGetterFromPath(path1))).toBe(undefined);
expect(cache.get(valGetterFromPath(path2))).toBe(undefined);
expect(cache.get(valGetterFromPath(path3))).toBe(obj3);
cache.set(path1, obj1);
expect(cache.size()).toBe(1);
expect(cache.get(valGetterFromPath(path2))).toBe(undefined);
expect(cache.get(valGetterFromPath(path3))).toBe(undefined);
expect(cache.get(valGetterFromPath(path1))).toBe(obj1);
expect(cache.get(valGetterFromPath(clonePath(path1)))).toBe(obj1);
expect(cache.get(valGetterFromPath(clonePath(path2)))).toBe(undefined);
expect(cache.get(valGetterFromPath(clonePath(path3)))).toBe(undefined);
});
});

View File

@@ -0,0 +1,117 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall recoil
*/
'use strict';
const {
getRecoilTestFn,
} = require('recoil-shared/__test_utils__/Recoil_TestingUtils');
let treeCacheLRU, loadableWithValue;
const testRecoil = getRecoilTestFn(() => {
treeCacheLRU = require('../Recoil_treeCacheLRU');
({loadableWithValue} = require('../../adt/Recoil_Loadable'));
});
describe('treeCacheLRU()', () => {
testRecoil('getting and setting cache', () => {
const cache = treeCacheLRU<$FlowFixMe>({maxSize: 10});
const [route1, loadable1] = [
[
['a', 2],
['b', 3],
],
loadableWithValue('value1'),
];
const [route2, loadable2] = [
[
['a', 3],
['b', 4],
],
loadableWithValue('value2'),
];
const [route3, loadable3] = [[['a', 4]], loadableWithValue('value3')];
cache.set(route1, loadable1);
cache.set(route2, loadable2);
cache.set(route3, loadable3);
expect(
cache.get(nodeKey => route1.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable1);
expect(
cache.get(nodeKey => route2.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable2);
expect(
cache.get(nodeKey => route3.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable3);
expect(cache.size()).toBe(3);
});
testRecoil('getting and setting cache (hitting max size)', () => {
const cache = treeCacheLRU<$FlowFixMe>({maxSize: 2});
const [route1, loadable1] = [
[
['a', 2],
['b', 3],
],
loadableWithValue('value1'),
];
const [route2, loadable2] = [
[
['a', 3],
['b', 4],
],
loadableWithValue('value2'),
];
const [route3, loadable3] = [[['a', 4]], loadableWithValue('value3')];
cache.set(route1, loadable1);
cache.set(route2, loadable2);
cache.set(route3, loadable3);
expect(
cache.get(nodeKey => route1.find(([key]) => key === nodeKey)?.[1]),
).toBe(undefined);
expect(
cache.get(nodeKey => route2.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable2);
expect(
cache.get(nodeKey => route3.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable3);
expect(cache.size()).toBe(2);
cache.set(route1, loadable1);
expect(
cache.get(nodeKey => route1.find(([key]) => key === nodeKey)?.[1]),
).toBe(loadable1);
expect(
cache.get(nodeKey => route2.find(([key]) => key === nodeKey)?.[1]),
).toBe(undefined);
expect(cache.size()).toBe(2);
});
});