Skip to content

Commit 2425c04

Browse files
authored
feat: @rocket.chat/memo (#380)
1 parent 9436d1c commit 2425c04

18 files changed

+286
-61
lines changed

packages/css-in-js/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
},
6767
"dependencies": {
6868
"@emotion/hash": "^0.8.0",
69+
"@rocket.chat/memo": "^0.21.0",
6970
"stylis": "^4.0.6"
7071
}
7172
}

packages/css-in-js/src/cssSupports.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memoize } from './memoize';
1+
import { memoize } from '@rocket.chat/memo';
22

33
/**
44
* Memoized and SSR-compatible facade of CSS.supports API

packages/css-in-js/src/memoize.ts

-29
This file was deleted.

packages/css-in-js/src/tags.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { memoize } from './memoize';
1+
import { memoize } from '@rocket.chat/memo';
2+
23
import { createAnimationName, escapeName } from './names';
34

45
/**

packages/fuselage/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"react-dom": "^16.13.1"
5151
},
5252
"dependencies": {
53+
"@rocket.chat/memo": "^0.21.0",
5354
"@rocket.chat/css-in-js": "^0.21.0",
5455
"@rocket.chat/fuselage-tokens": "^0.21.0",
5556
"invariant": "^2.2.4",

packages/fuselage/src/helpers/memoize.js

-27
This file was deleted.

packages/fuselage/src/styleTokens.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { cssSupports } from '@rocket.chat/css-in-js';
22
import tokenColors from '@rocket.chat/fuselage-tokens/colors.json';
33
import tokenTypography from '@rocket.chat/fuselage-tokens/typography.json';
4+
import { memoize } from '@rocket.chat/memo';
45
import invariant from 'invariant';
56

6-
import { memoize } from './helpers/memoize';
7-
87
const measure = (computeSpecialValue) =>
98
memoize((value) => {
109
if (typeof value === 'number') {

packages/memo/.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/dist

packages/memo/.eslintrc.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module.exports = {
2+
extends: [
3+
'plugin:@typescript-eslint/recommended',
4+
'plugin:@typescript-eslint/eslint-recommended',
5+
'@rocket.chat/eslint-config',
6+
'prettier',
7+
],
8+
parser: '@typescript-eslint/parser',
9+
parserOptions: {
10+
sourceType: 'module',
11+
ecmaVersion: 2018,
12+
warnOnUnsupportedTypeScriptVersion: false,
13+
ecmaFeatures: {
14+
experimentalObjectRestSpread: true,
15+
legacyDecorators: true,
16+
},
17+
},
18+
plugins: ['@typescript-eslint', 'prettier'],
19+
rules: {
20+
'func-call-spacing': 'off',
21+
'indent': 'off',
22+
'import/order': [
23+
'error',
24+
{
25+
'newlines-between': 'always',
26+
'groups': [
27+
'builtin',
28+
'external',
29+
'internal',
30+
['parent', 'sibling', 'index'],
31+
],
32+
'alphabetize': {
33+
order: 'asc',
34+
},
35+
},
36+
],
37+
'jsx-quotes': ['error', 'prefer-single'],
38+
'no-empty-function': 'off',
39+
'no-extra-parens': 'off',
40+
'no-redeclare': 'off',
41+
'no-spaced-func': 'off',
42+
'no-undef': 'off',
43+
'no-unused-vars': 'off',
44+
'no-useless-constructor': 'off',
45+
'no-use-before-define': 'off',
46+
'operator-linebreak': 'off',
47+
'@typescript-eslint/ban-ts-ignore': 'off',
48+
'@typescript-eslint/func-call-spacing': 'error',
49+
'@typescript-eslint/indent': 'off',
50+
'@typescript-eslint/no-explicit-any': 'off',
51+
'@typescript-eslint/no-extra-parens': 'off',
52+
'@typescript-eslint/no-redeclare': ['error'],
53+
'@typescript-eslint/no-use-before-define': ['error'],
54+
'@typescript-eslint/interface-name-prefix': 'off',
55+
'@typescript-eslint/explicit-function-return-type': [
56+
'warn',
57+
{
58+
allowExpressions: true,
59+
},
60+
],
61+
'@typescript-eslint/no-unused-vars': 'off',
62+
'@typescript-eslint/no-unused-vars-experimental': 'warn',
63+
'prettier/prettier': 2,
64+
},
65+
env: {
66+
browser: true,
67+
commonjs: true,
68+
es6: true,
69+
node: true,
70+
jest: true,
71+
},
72+
settings: {
73+
'import/resolver': {
74+
node: {
75+
extensions: ['.js', '.ts', '.tsx'],
76+
},
77+
},
78+
},
79+
};

packages/memo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dist

packages/memo/.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/dist

packages/memo/.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"singleQuote": true,
3+
"semi": true,
4+
"quoteProps": "consistent",
5+
"jsxSingleQuote": true,
6+
"printWidth": 80
7+
}

packages/memo/package.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@rocket.chat/memo",
3+
"version": "0.21.0",
4+
"description": "Memoization utilities",
5+
"keywords": [
6+
"memoize"
7+
],
8+
"author": {
9+
"name": "Rocket.Chat",
10+
"url": "https://rocket.chat/"
11+
},
12+
"homepage": "https://github.com/RocketChat/Rocket.Chat.Fuselage#readme",
13+
"license": "MIT",
14+
"publishConfig": {
15+
"access": "public"
16+
},
17+
"repository": {
18+
"type": "git",
19+
"url": "git+https://github.com/RocketChat/Rocket.Chat.Fuselage.git"
20+
},
21+
"main": "./dist/cjs/index.js",
22+
"module": "./dist/esm/index.js",
23+
"types": "./dist/esm/index.d.ts",
24+
"files": [
25+
"./dist/"
26+
],
27+
"scripts": {
28+
"build:esm": "tsc -p tsconfig.json",
29+
"build:cjs": "tsc -p tsconfig-cjs.json",
30+
"build": "run-p build:esm build:cjs",
31+
"eslint": "eslint src",
32+
"test": "echo \"Error: run tests from root\" && exit 1"
33+
},
34+
"bugs": {
35+
"url": "https://github.com/RocketChat/Rocket.Chat.Fuselage/issues"
36+
},
37+
"devDependencies": {
38+
"@types/react": "^16.14.0",
39+
"eslint": "^7.18.0",
40+
"eslint-config-prettier": "^7.2.0",
41+
"eslint-plugin-import": "^2.22.1",
42+
"eslint-plugin-prettier": "^3.3.1",
43+
"prettier": "^2.2.1",
44+
"typescript": "^4.1.3"
45+
}
46+
}

packages/memo/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { memoize, clear } from './memoize';

packages/memo/src/memoize.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
type MemoizableFunction<T, A, R> = (this: T, arg: A) => R;
2+
type MemoizedFunction<T, A, R> = (this: T, arg: A) => R;
3+
4+
const store = new WeakMap<
5+
MemoizableFunction<unknown, unknown, unknown>,
6+
Map<unknown, unknown>
7+
>();
8+
9+
const isCachedValue = <A, R>(
10+
cachedValue: R | undefined,
11+
arg: A,
12+
cache: Map<A, R>
13+
): cachedValue is R => cache.has(arg) && cache.get(arg) === cachedValue;
14+
15+
export const memoize = <T, A, R>(
16+
fn: MemoizableFunction<T, A, R>
17+
): MemoizedFunction<T, A, R> => {
18+
const cache = new Map<A, R>();
19+
20+
const memoized: MemoizedFunction<T, A, R> = function (this, arg) {
21+
const cachedValue = cache.get(arg);
22+
23+
if (isCachedValue(cachedValue, arg, cache)) {
24+
return cachedValue;
25+
}
26+
27+
const result = fn.call(this, arg);
28+
29+
cache.set(arg, result);
30+
31+
return result;
32+
};
33+
34+
store.set(memoized as MemoizableFunction<unknown, unknown, unknown>, cache);
35+
36+
return memoized;
37+
};
38+
39+
export const clear = (
40+
fn: MemoizableFunction<unknown, unknown, unknown>
41+
): void => {
42+
const x = store.get(fn);
43+
44+
if (x) {
45+
x.clear();
46+
}
47+
};

packages/memo/tsconfig-cjs.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "./dist/cjs"
6+
}
7+
}

packages/memo/tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "ES2020",
5+
"declaration": true,
6+
"declarationMap": true,
7+
"sourceMap": true,
8+
"outDir": "./dist/esm",
9+
"strict": true,
10+
"esModuleInterop": true,
11+
"skipLibCheck": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true
15+
}
16+
}

0 commit comments

Comments
 (0)