This commit is contained in:
Conduitry 2021-02-26 10:49:05 -05:00
parent 5959fd4520
commit dfcc711817
6 changed files with 29 additions and 36 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@
/package-lock.json
/pnpm-lock.yaml
/shrinkwrap.yaml
/types/
/yarn.lock

View File

@ -6,7 +6,7 @@
"cache"
],
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"module": "src/index.js",
"types": "types/index.d.ts",
"files": [
"dist",
@ -25,12 +25,12 @@
"devDependencies": {
"@types/node": "=12",
"rollup": "^2",
"rollup-plugin-cheap-ts": "Conduitry/rollup-plugin-cheap-ts#semver:^1",
"typescript": "^4"
},
"scripts": {
"build": "rollup -c",
"dev": "rollup -cw",
"prepublishOnly": "tsc src/*.js --declaration --allowJs --emitDeclarationOnly --outDir types --lib esnext --downlevelIteration",
"prepare": "npm run build",
"pretest": "npm run build",
"test": "node test.js"

View File

@ -1,11 +1,5 @@
import cheap_ts from 'rollup-plugin-cheap-ts';
export default {
input: './src/index',
input: './src/index.js',
external: name => /^[a-z]/.test(name),
plugins: [cheap_ts()],
output: [
{ file: 'dist/index.cjs.js', format: 'cjs', sourcemap: true, interop: false, preferConst: true },
{ file: 'dist/index.esm.js', format: 'esm', sourcemap: true, preferConst: true },
],
output: { file: 'dist/index.cjs.js', format: 'cjs', sourcemap: true, interop: false, preferConst: true },
};

View File

@ -1,16 +1,28 @@
// @ts-check
import { createHash } from 'crypto';
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { serialize, deserialize } from 'v8';
type Mode = string | number | boolean | null | undefined;
/**
* @typedef {string | number | boolean | null | undefined} Mode
*/
export const autocache = (path: string, mode: Mode) => {
/**
* @param {string} path
* @param {Mode} mode
*/
export const autocache = (path, mode) => {
path = resolve(path);
let cache = new Map<Mode, Map<string, any>>();
const all_entries = new Map<string, any>();
const used_entries = new Map<string, any>();
const pending_entries = new Map<string, Promise<any>>();
/** @type Map<Mode, Map<string, any>> */
let cache = new Map();
/** @type Map<string, any> */
const all_entries = new Map();
/** @type Map<string, any> */
const used_entries = new Map();
/** @type Map<string, Promise<any>> */
const pending_entries = new Map();
try {
const data = deserialize(readFileSync(path));
if (data && typeof data === 'object' && data.schema === 2 && data.cache instanceof Map) {
@ -24,9 +36,13 @@ export const autocache = (path: string, mode: Mode) => {
} catch {}
cache.set(mode, used_entries);
return {
async cache(key: string, compute_value: () => Promise<any>) {
/**
* @param {string} key
* @param {() => Promise<any>} compute_value
*/
async cache(key, compute_value) {
const key_hashed = createHash('sha1').update(key).digest('hex');
let value: any;
let value;
if (all_entries.has(key_hashed)) {
value = all_entries.get(key_hashed);
} else if (pending_entries.has(key_hashed)) {

View File

@ -1,11 +0,0 @@
{
"compilerOptions": {
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"removeComments": true,
"sourceMap": true,
"target": "esnext"
},
"include": ["src"]
}

7
types/index.d.ts vendored
View File

@ -1,7 +0,0 @@
export function autocache(
path: string,
mode: string | number | boolean | null | undefined,
): {
cache: <T>(key: string, compute_value: () => T | Promise<T>) => Promise<T>;
close: () => void;
};