Complete transition from mem to moize

This commit is contained in:
Lim Chee Aun 2023-10-14 20:33:40 +08:00
parent 0a7f158b70
commit f56a44ac97
10 changed files with 29 additions and 117 deletions

73
package-lock.json generated
View file

@ -22,7 +22,6 @@
"just-debounce-it": "~3.2.0", "just-debounce-it": "~3.2.0",
"lz-string": "~1.5.0", "lz-string": "~1.5.0",
"masto": "~6.3.1", "masto": "~6.3.1",
"mem": "~9.0.2",
"moize": "~6.1.6", "moize": "~6.1.6",
"p-retry": "~6.1.0", "p-retry": "~6.1.0",
"p-throttle": "~5.1.0", "p-throttle": "~5.1.0",
@ -5274,17 +5273,6 @@
"sourcemap-codec": "^1.4.8" "sourcemap-codec": "^1.4.8"
} }
}, },
"node_modules/map-age-cleaner": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz",
"integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==",
"dependencies": {
"p-defer": "^1.0.0"
},
"engines": {
"node": ">=6"
}
},
"node_modules/masto": { "node_modules/masto": {
"version": "6.3.1", "version": "6.3.1",
"resolved": "https://registry.npmjs.org/masto/-/masto-6.3.1.tgz", "resolved": "https://registry.npmjs.org/masto/-/masto-6.3.1.tgz",
@ -5297,21 +5285,6 @@
"ws": "^8.13.0" "ws": "^8.13.0"
} }
}, },
"node_modules/mem": {
"version": "9.0.2",
"resolved": "https://registry.npmjs.org/mem/-/mem-9.0.2.tgz",
"integrity": "sha512-F2t4YIv9XQUBHt6AOJ0y7lSmP1+cY7Fm1DRh9GClTGzKST7UWLMx6ly9WZdLH/G/ppM5RL4MlQfRT71ri9t19A==",
"dependencies": {
"map-age-cleaner": "^0.1.3",
"mimic-fn": "^4.0.0"
},
"engines": {
"node": ">=12.20"
},
"funding": {
"url": "https://github.com/sindresorhus/mem?sponsor=1"
}
},
"node_modules/merge-stream": { "node_modules/merge-stream": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@ -5366,17 +5339,6 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/mimic-fn": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz",
"integrity": "sha512-vqiC06CuhBTUdZH+RYl8sFrL096vA45Ok5ISO6sE/Mr1jRbGH4Csnhi8f3wKVl7x8mO4Au7Ir9D3Oyv1VYMFJw==",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/minimatch": { "node_modules/minimatch": {
"version": "3.1.2", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
@ -5499,14 +5461,6 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"node_modules/p-defer": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz",
"integrity": "sha512-wB3wfAxZpk2AzOfUMJNL+d36xothRSyj8EXOa4f6GMqYDN9BJaaSISbsk+wS9abmnebVw95C2Kb5t85UmpCxuw==",
"engines": {
"node": ">=4"
}
},
"node_modules/p-retry": { "node_modules/p-retry": {
"version": "6.1.0", "version": "6.1.0",
"resolved": "https://registry.npmjs.org/p-retry/-/p-retry-6.1.0.tgz", "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-6.1.0.tgz",
@ -11265,14 +11219,6 @@
"sourcemap-codec": "^1.4.8" "sourcemap-codec": "^1.4.8"
} }
}, },
"map-age-cleaner": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz",
"integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==",
"requires": {
"p-defer": "^1.0.0"
}
},
"masto": { "masto": {
"version": "6.3.1", "version": "6.3.1",
"resolved": "https://registry.npmjs.org/masto/-/masto-6.3.1.tgz", "resolved": "https://registry.npmjs.org/masto/-/masto-6.3.1.tgz",
@ -11285,15 +11231,6 @@
"ws": "^8.13.0" "ws": "^8.13.0"
} }
}, },
"mem": {
"version": "9.0.2",
"resolved": "https://registry.npmjs.org/mem/-/mem-9.0.2.tgz",
"integrity": "sha512-F2t4YIv9XQUBHt6AOJ0y7lSmP1+cY7Fm1DRh9GClTGzKST7UWLMx6ly9WZdLH/G/ppM5RL4MlQfRT71ri9t19A==",
"requires": {
"map-age-cleaner": "^0.1.3",
"mimic-fn": "^4.0.0"
}
},
"merge-stream": { "merge-stream": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@ -11336,11 +11273,6 @@
"mime-db": "1.52.0" "mime-db": "1.52.0"
} }
}, },
"mimic-fn": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz",
"integrity": "sha512-vqiC06CuhBTUdZH+RYl8sFrL096vA45Ok5ISO6sE/Mr1jRbGH4Csnhi8f3wKVl7x8mO4Au7Ir9D3Oyv1VYMFJw=="
},
"minimatch": { "minimatch": {
"version": "3.1.2", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
@ -11430,11 +11362,6 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"p-defer": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz",
"integrity": "sha512-wB3wfAxZpk2AzOfUMJNL+d36xothRSyj8EXOa4f6GMqYDN9BJaaSISbsk+wS9abmnebVw95C2Kb5t85UmpCxuw=="
},
"p-retry": { "p-retry": {
"version": "6.1.0", "version": "6.1.0",
"resolved": "https://registry.npmjs.org/p-retry/-/p-retry-6.1.0.tgz", "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-6.1.0.tgz",

View file

@ -24,7 +24,6 @@
"just-debounce-it": "~3.2.0", "just-debounce-it": "~3.2.0",
"lz-string": "~1.5.0", "lz-string": "~1.5.0",
"masto": "~6.3.1", "masto": "~6.3.1",
"mem": "~9.0.2",
"moize": "~6.1.6", "moize": "~6.1.6",
"p-retry": "~6.1.0", "p-retry": "~6.1.0",
"p-throttle": "~5.1.0", "p-throttle": "~5.1.0",

View file

@ -1,7 +1,6 @@
import './account-info.css'; import './account-info.css';
import { Menu, MenuDivider, MenuItem, SubMenu } from '@szhsin/react-menu'; import { Menu, MenuDivider, MenuItem, SubMenu } from '@szhsin/react-menu';
import mem from 'mem';
import { import {
useCallback, useCallback,
useEffect, useEffect,
@ -16,6 +15,7 @@ import enhanceContent from '../utils/enhance-content';
import getHTMLText from '../utils/getHTMLText'; import getHTMLText from '../utils/getHTMLText';
import handleContentLinks from '../utils/handle-content-links'; import handleContentLinks from '../utils/handle-content-links';
import niceDateTime from '../utils/nice-date-time'; import niceDateTime from '../utils/nice-date-time';
import pmem from '../utils/pmem';
import shortenNumber from '../utils/shorten-number'; import shortenNumber from '../utils/shorten-number';
import showToast from '../utils/show-toast'; import showToast from '../utils/show-toast';
import states, { hideAllModals } from '../utils/states'; import states, { hideAllModals } from '../utils/states';
@ -63,7 +63,7 @@ function fetchFamiliarFollowers(currentID, masto) {
id: [currentID], id: [currentID],
}); });
} }
const memFetchFamiliarFollowers = mem(fetchFamiliarFollowers, { const memFetchFamiliarFollowers = pmem(fetchFamiliarFollowers, {
maxAge: ACCOUNT_INFO_MAX_AGE, maxAge: ACCOUNT_INFO_MAX_AGE,
}); });
@ -112,7 +112,7 @@ async function fetchPostingStats(accountID, masto) {
console.log('posting stats', stats); console.log('posting stats', stats);
return stats; return stats;
} }
const memFetchPostingStats = mem(fetchPostingStats, { const memFetchPostingStats = pmem(fetchPostingStats, {
maxAge: ACCOUNT_INFO_MAX_AGE, maxAge: ACCOUNT_INFO_MAX_AGE,
}); });

View file

@ -1,5 +1,4 @@
import { getBlurHashAverageColor } from 'fast-blurhash'; import { getBlurHashAverageColor } from 'fast-blurhash';
import mem from 'mem';
import { Fragment } from 'preact'; import { Fragment } from 'preact';
import { import {
useCallback, useCallback,
@ -10,6 +9,7 @@ import {
} from 'preact/hooks'; } from 'preact/hooks';
import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom'; import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom';
import mem from '../utils/mem';
import states from '../utils/states'; import states from '../utils/states';
import Icon from './icon'; import Icon from './icon';

View file

@ -4,7 +4,6 @@ import {
compressToEncodedURIComponent, compressToEncodedURIComponent,
decompressFromEncodedURIComponent, decompressFromEncodedURIComponent,
} from 'lz-string'; } from 'lz-string';
import mem from 'mem';
import { useEffect, useMemo, useRef, useState } from 'preact/hooks'; import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
import { useSnapshot } from 'valtio'; import { useSnapshot } from 'valtio';
@ -13,6 +12,7 @@ import multiColumnUrl from '../assets/multi-column.svg';
import tabMenuBarUrl from '../assets/tab-menu-bar.svg'; import tabMenuBarUrl from '../assets/tab-menu-bar.svg';
import { api } from '../utils/api'; import { api } from '../utils/api';
import pmem from '../utils/pmem';
import showToast from '../utils/show-toast'; import showToast from '../utils/show-toast';
import states from '../utils/states'; import states from '../utils/states';
@ -133,15 +133,10 @@ export const SHORTCUTS_META = {
}, },
list: { list: {
id: 'list', id: 'list',
title: mem( title: pmem(async ({ id }) => {
async ({ id }) => {
const list = await api().masto.v1.lists.$select(id).fetch(); const list = await api().masto.v1.lists.$select(id).fetch();
return list.title; return list.title;
}, }),
{
cacheKey: ([{ id }]) => id,
},
),
path: ({ id }) => `/l/${id}`, path: ({ id }) => `/l/${id}`,
icon: 'list', icon: 'list',
}, },
@ -167,15 +162,10 @@ export const SHORTCUTS_META = {
}, },
'account-statuses': { 'account-statuses': {
id: 'account-statuses', id: 'account-statuses',
title: mem( title: pmem(async ({ id }) => {
async ({ id }) => {
const account = await api().masto.v1.accounts.$select(id).fetch(); const account = await api().masto.v1.accounts.$select(id).fetch();
return account.username || account.acct || account.displayName; return account.username || account.acct || account.displayName;
}, }),
{
cacheKey: ([{ id }]) => id,
},
),
path: ({ id }) => `/a/${id}`, path: ({ id }) => `/a/${id}`,
icon: 'user', icon: 'user',
}, },

View file

@ -9,7 +9,6 @@ import {
MenuItem, MenuItem,
} from '@szhsin/react-menu'; } from '@szhsin/react-menu';
import { decodeBlurHash } from 'fast-blurhash'; import { decodeBlurHash } from 'fast-blurhash';
import mem from 'mem';
import pThrottle from 'p-throttle'; import pThrottle from 'p-throttle';
import { memo } from 'preact/compat'; import { memo } from 'preact/compat';
import { import {
@ -42,6 +41,7 @@ import htmlContentLength from '../utils/html-content-length';
import isMastodonLinkMaybe from '../utils/isMastodonLinkMaybe'; import isMastodonLinkMaybe from '../utils/isMastodonLinkMaybe';
import localeMatch from '../utils/locale-match'; import localeMatch from '../utils/locale-match';
import niceDateTime from '../utils/nice-date-time'; import niceDateTime from '../utils/nice-date-time';
import pmem from '../utils/pmem';
import safeBoundingBoxPadding from '../utils/safe-bounding-box-padding'; import safeBoundingBoxPadding from '../utils/safe-bounding-box-padding';
import shortenNumber from '../utils/shorten-number'; import shortenNumber from '../utils/shorten-number';
import showToast from '../utils/show-toast'; import showToast from '../utils/show-toast';
@ -67,13 +67,9 @@ const throttle = pThrottle({
}); });
function fetchAccount(id, masto) { function fetchAccount(id, masto) {
try {
return masto.v1.accounts.$select(id).fetch(); return masto.v1.accounts.$select(id).fetch();
} catch (e) {
return Promise.reject(e);
}
} }
const memFetchAccount = mem(fetchAccount); const memFetchAccount = pmem(fetchAccount);
const visibilityText = { const visibilityText = {
public: 'Public', public: 'Public',
@ -2133,11 +2129,7 @@ function nicePostURL(url) {
); );
} }
const unfurlMastodonLink = throttle( const unfurlMastodonLink = throttle(pmem(_unfurlMastodonLink));
mem(_unfurlMastodonLink, {
cacheKey: (instance, url) => `${instance}:${url}`,
}),
);
function FilteredStatus({ status, filterInfo, instance, containerProps = {} }) { function FilteredStatus({ status, filterInfo, instance, containerProps = {} }) {
const { const {

View file

@ -1,5 +1,6 @@
import { match } from '@formatjs/intl-localematcher'; import { match } from '@formatjs/intl-localematcher';
import mem from 'mem';
import mem from './mem';
function _localeMatch(...args) { function _localeMatch(...args) {
// Wrap in try/catch because localeMatcher throws on invalid locales // Wrap in try/catch because localeMatcher throws on invalid locales
@ -10,8 +11,6 @@ function _localeMatch(...args) {
return defaultLocale || false; return defaultLocale || false;
} }
} }
const localeMatch = mem(_localeMatch, { const localeMatch = mem(_localeMatch);
cacheKey: (args) => args.join(),
});
export default localeMatch; export default localeMatch;

5
src/utils/mem.js Normal file
View file

@ -0,0 +1,5 @@
import moize from 'moize';
export default function mem(fn, opts = {}) {
return moize(fn, { ...opts, maxSize: 100 });
}

View file

@ -1,5 +1,5 @@
import moize from 'moize'; import mem from './mem';
export default function pmem(fn, opts = {}) { export default function pmem(fn, opts = {}) {
return moize(fn, { isPromise: true, ...opts, maxSize: Infinity }); return mem(fn, { isPromise: true, ...opts });
} }

View file

@ -1,8 +1,8 @@
import mem from 'mem';
import { proxy, subscribe } from 'valtio'; import { proxy, subscribe } from 'valtio';
import { subscribeKey } from 'valtio/utils'; import { subscribeKey } from 'valtio/utils';
import { api } from './api'; import { api } from './api';
import pmem from './pmem';
import store from './store'; import store from './store';
const states = proxy({ const states = proxy({
@ -229,6 +229,6 @@ export function threadifyStatus(status, propInstance) {
}); });
} }
const fetchStatus = mem((statusID, masto) => { const fetchStatus = pmem((statusID, masto) => {
return masto.v1.statuses.$select(statusID).fetch(); return masto.v1.statuses.$select(statusID).fetch();
}); });