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",
"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",

View file

@ -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",

View file

@ -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,
});

View file

@ -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';

View file

@ -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',
},

View file

@ -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 {

View file

@ -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;

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 = {}) {
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 { 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();
});