diff --git a/package-lock.json b/package-lock.json index 2e2091d..278c151 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,6 @@ "just-debounce-it": "~3.2.0", "lz-string": "~1.5.0", "masto": "~6.3.1", - "mem": "~9.0.2", "moize": "~6.1.6", "p-retry": "~6.1.0", "p-throttle": "~5.1.0", @@ -5274,17 +5273,6 @@ "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": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/masto/-/masto-6.3.1.tgz", @@ -5297,21 +5285,6 @@ "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": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -5366,17 +5339,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": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -5499,14 +5461,6 @@ "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": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-6.1.0.tgz", @@ -11265,14 +11219,6 @@ "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": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/masto/-/masto-6.3.1.tgz", @@ -11285,15 +11231,6 @@ "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": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -11336,11 +11273,6 @@ "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": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -11430,11 +11362,6 @@ "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": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-6.1.0.tgz", diff --git a/package.json b/package.json index 7b78c25..38e5621 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "just-debounce-it": "~3.2.0", "lz-string": "~1.5.0", "masto": "~6.3.1", - "mem": "~9.0.2", "moize": "~6.1.6", "p-retry": "~6.1.0", "p-throttle": "~5.1.0", diff --git a/src/components/account-info.jsx b/src/components/account-info.jsx index dcc5768..74d92a2 100644 --- a/src/components/account-info.jsx +++ b/src/components/account-info.jsx @@ -1,7 +1,6 @@ import './account-info.css'; import { Menu, MenuDivider, MenuItem, SubMenu } from '@szhsin/react-menu'; -import mem from 'mem'; import { useCallback, useEffect, @@ -16,6 +15,7 @@ import enhanceContent from '../utils/enhance-content'; import getHTMLText from '../utils/getHTMLText'; import handleContentLinks from '../utils/handle-content-links'; import niceDateTime from '../utils/nice-date-time'; +import pmem from '../utils/pmem'; import shortenNumber from '../utils/shorten-number'; import showToast from '../utils/show-toast'; import states, { hideAllModals } from '../utils/states'; @@ -63,7 +63,7 @@ function fetchFamiliarFollowers(currentID, masto) { id: [currentID], }); } -const memFetchFamiliarFollowers = mem(fetchFamiliarFollowers, { +const memFetchFamiliarFollowers = pmem(fetchFamiliarFollowers, { maxAge: ACCOUNT_INFO_MAX_AGE, }); @@ -112,7 +112,7 @@ async function fetchPostingStats(accountID, masto) { console.log('posting stats', stats); return stats; } -const memFetchPostingStats = mem(fetchPostingStats, { +const memFetchPostingStats = pmem(fetchPostingStats, { maxAge: ACCOUNT_INFO_MAX_AGE, }); diff --git a/src/components/media.jsx b/src/components/media.jsx index 7861c21..6699fc4 100644 --- a/src/components/media.jsx +++ b/src/components/media.jsx @@ -1,5 +1,4 @@ import { getBlurHashAverageColor } from 'fast-blurhash'; -import mem from 'mem'; import { Fragment } from 'preact'; import { useCallback, @@ -10,6 +9,7 @@ import { } from 'preact/hooks'; import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom'; +import mem from '../utils/mem'; import states from '../utils/states'; import Icon from './icon'; diff --git a/src/components/shortcuts-settings.jsx b/src/components/shortcuts-settings.jsx index cb6105a..c6ecfd3 100644 --- a/src/components/shortcuts-settings.jsx +++ b/src/components/shortcuts-settings.jsx @@ -4,7 +4,6 @@ import { compressToEncodedURIComponent, decompressFromEncodedURIComponent, } from 'lz-string'; -import mem from 'mem'; import { useEffect, useMemo, useRef, useState } from 'preact/hooks'; import { useSnapshot } from 'valtio'; @@ -13,6 +12,7 @@ import multiColumnUrl from '../assets/multi-column.svg'; import tabMenuBarUrl from '../assets/tab-menu-bar.svg'; import { api } from '../utils/api'; +import pmem from '../utils/pmem'; import showToast from '../utils/show-toast'; import states from '../utils/states'; @@ -133,15 +133,10 @@ export const SHORTCUTS_META = { }, list: { id: 'list', - title: mem( - async ({ id }) => { - const list = await api().masto.v1.lists.$select(id).fetch(); - return list.title; - }, - { - cacheKey: ([{ id }]) => id, - }, - ), + title: pmem(async ({ id }) => { + const list = await api().masto.v1.lists.$select(id).fetch(); + return list.title; + }), path: ({ id }) => `/l/${id}`, icon: 'list', }, @@ -167,15 +162,10 @@ export const SHORTCUTS_META = { }, 'account-statuses': { id: 'account-statuses', - title: mem( - async ({ id }) => { - const account = await api().masto.v1.accounts.$select(id).fetch(); - return account.username || account.acct || account.displayName; - }, - { - cacheKey: ([{ id }]) => id, - }, - ), + title: pmem(async ({ id }) => { + const account = await api().masto.v1.accounts.$select(id).fetch(); + return account.username || account.acct || account.displayName; + }), path: ({ id }) => `/a/${id}`, icon: 'user', }, diff --git a/src/components/status.jsx b/src/components/status.jsx index 12625b2..45fa827 100644 --- a/src/components/status.jsx +++ b/src/components/status.jsx @@ -9,7 +9,6 @@ import { MenuItem, } from '@szhsin/react-menu'; import { decodeBlurHash } from 'fast-blurhash'; -import mem from 'mem'; import pThrottle from 'p-throttle'; import { memo } from 'preact/compat'; import { @@ -42,6 +41,7 @@ import htmlContentLength from '../utils/html-content-length'; import isMastodonLinkMaybe from '../utils/isMastodonLinkMaybe'; import localeMatch from '../utils/locale-match'; import niceDateTime from '../utils/nice-date-time'; +import pmem from '../utils/pmem'; import safeBoundingBoxPadding from '../utils/safe-bounding-box-padding'; import shortenNumber from '../utils/shorten-number'; import showToast from '../utils/show-toast'; @@ -67,13 +67,9 @@ const throttle = pThrottle({ }); function fetchAccount(id, masto) { - try { - return masto.v1.accounts.$select(id).fetch(); - } catch (e) { - return Promise.reject(e); - } + return masto.v1.accounts.$select(id).fetch(); } -const memFetchAccount = mem(fetchAccount); +const memFetchAccount = pmem(fetchAccount); const visibilityText = { public: 'Public', @@ -2133,11 +2129,7 @@ function nicePostURL(url) { ); } -const unfurlMastodonLink = throttle( - mem(_unfurlMastodonLink, { - cacheKey: (instance, url) => `${instance}:${url}`, - }), -); +const unfurlMastodonLink = throttle(pmem(_unfurlMastodonLink)); function FilteredStatus({ status, filterInfo, instance, containerProps = {} }) { const { diff --git a/src/utils/locale-match.jsx b/src/utils/locale-match.jsx index e901e9f..7376882 100644 --- a/src/utils/locale-match.jsx +++ b/src/utils/locale-match.jsx @@ -1,5 +1,6 @@ import { match } from '@formatjs/intl-localematcher'; -import mem from 'mem'; + +import mem from './mem'; function _localeMatch(...args) { // Wrap in try/catch because localeMatcher throws on invalid locales @@ -10,8 +11,6 @@ function _localeMatch(...args) { return defaultLocale || false; } } -const localeMatch = mem(_localeMatch, { - cacheKey: (args) => args.join(), -}); +const localeMatch = mem(_localeMatch); export default localeMatch; diff --git a/src/utils/mem.js b/src/utils/mem.js new file mode 100644 index 0000000..773a492 --- /dev/null +++ b/src/utils/mem.js @@ -0,0 +1,5 @@ +import moize from 'moize'; + +export default function mem(fn, opts = {}) { + return moize(fn, { ...opts, maxSize: 100 }); +} diff --git a/src/utils/pmem.js b/src/utils/pmem.js index 8cd0be9..e8041c3 100644 --- a/src/utils/pmem.js +++ b/src/utils/pmem.js @@ -1,5 +1,5 @@ -import moize from 'moize'; +import mem from './mem'; export default function pmem(fn, opts = {}) { - return moize(fn, { isPromise: true, ...opts, maxSize: Infinity }); + return mem(fn, { isPromise: true, ...opts }); } diff --git a/src/utils/states.js b/src/utils/states.js index 3c61bfb..580d02a 100644 --- a/src/utils/states.js +++ b/src/utils/states.js @@ -1,8 +1,8 @@ -import mem from 'mem'; import { proxy, subscribe } from 'valtio'; import { subscribeKey } from 'valtio/utils'; import { api } from './api'; +import pmem from './pmem'; import store from './store'; 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(); });