Unverified Commit d0a5b1d9 authored by Tasso Evangelista's avatar Tasso Evangelista
Browse files

Add @rocket.chat/memo

parent 24eb5d7a
......@@ -66,6 +66,7 @@
},
"dependencies": {
"@emotion/hash": "^0.8.0",
"@rocket.chat/memo": "^0.20.1",
"stylis": "^4.0.6"
}
}
import { memoize } from './memoize';
import { memoize } from '@rocket.chat/memo';
/**
* Memoized and SSR-compatible facade of CSS.supports API
......
const store = new WeakMap<(arg: unknown) => unknown, Map<unknown, unknown>>();
export const memoize = <A, R>(fn: (arg: A) => R): typeof fn => {
const cache = new Map<A, R>();
const memoized: typeof fn = function (this: unknown, arg) {
if (cache.has(arg)) {
return cache.get(arg);
}
const result = fn.call(this, arg);
cache.set(arg, result);
return result;
};
store.set(memoized, cache);
return memoized;
};
export const clear = <A, R>(fn: (arg: A) => R): void => {
const x = store.get(fn);
if (x) {
x.clear();
}
};
import { memoize } from './memoize';
import { memoize } from '@rocket.chat/memo';
import { createAnimationName, escapeName } from './names';
/**
......
......@@ -6,7 +6,10 @@
"fuselage",
"box"
],
"author": "Tasso Evangelista <tasso.evangelista@rocket.chat>",
"author": {
"name": "Rocket.Chat",
"url": "https://rocket.chat/"
},
"homepage": "https://github.com/RocketChat/Rocket.Chat.Fuselage#readme",
"license": "MIT",
"publishConfig": {
......
......@@ -4,3 +4,4 @@ export { useArrayLikeClassNameProp } from './useArrayLikeClassNameProp';
export { BoxTransform, BoxTransforms } from './BoxTransforms';
export { useComposedBoxTransform } from './useComposedBoxTransform';
export * from './styles';
export * from './theming';
......@@ -53,6 +53,7 @@
"@rocket.chat/css-in-js": "^0.20.1",
"@rocket.chat/fuselage-box": "^0.20.1",
"@rocket.chat/fuselage-tokens": "^0.20.1",
"@rocket.chat/memo": "^0.20.1",
"invariant": "^2.2.4",
"react-keyed-flatten-children": "^1.2.0"
},
......
const store = new WeakMap();
export const memoize = (fn) => {
const cache = new Map();
const memoized = function (arg) {
if (cache.has(arg)) {
return cache.get(arg);
}
const result = fn.call(this, arg);
cache.set(arg, result);
return result;
};
store.set(memoized, cache);
return memoized;
};
export const clear = (fn) => {
if (store.has(fn)) {
store.get(fn).clear();
}
};
import { cssSupports } from '@rocket.chat/css-in-js';
import tokenColors from '@rocket.chat/fuselage-tokens/colors.json';
import tokenTypography from '@rocket.chat/fuselage-tokens/typography.json';
import { memoize } from '@rocket.chat/memo';
import invariant from 'invariant';
import { memoize } from './helpers/memoize';
const measure = (computeSpecialValue) =>
memoize((value) => {
if (typeof value === 'number') {
......@@ -213,23 +212,4 @@ export const fontFamily = memoize((value) => {
return fontFamily;
});
export const fontScale = (value) => {
if (!tokenTypography.fontScales[value]) {
return;
}
const {
fontSize,
fontWeight,
lineHeight,
letterSpacing,
} = tokenTypography.fontScales[value];
return {
fontSize: `${fontSize / 16}rem`,
fontWeight,
lineHeight: `${lineHeight / 16}rem`,
letterSpacing: `${letterSpacing / 16}rem`,
};
};
fontScale.values = Object.keys(tokenTypography.fontScales);
export { fontScale } from '@rocket.chat/fuselage-box';
module.exports = {
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/eslint-recommended',
'@rocket.chat/eslint-config',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018,
warnOnUnsupportedTypeScriptVersion: false,
ecmaFeatures: {
experimentalObjectRestSpread: true,
legacyDecorators: true,
},
},
plugins: ['@typescript-eslint', 'prettier'],
rules: {
'func-call-spacing': 'off',
'indent': 'off',
'import/order': [
'error',
{
'newlines-between': 'always',
'groups': [
'builtin',
'external',
'internal',
['parent', 'sibling', 'index'],
],
'alphabetize': {
order: 'asc',
},
},
],
'jsx-quotes': ['error', 'prefer-single'],
'no-empty-function': 'off',
'no-extra-parens': 'off',
'no-redeclare': 'off',
'no-spaced-func': 'off',
'no-undef': 'off',
'no-unused-vars': 'off',
'no-useless-constructor': 'off',
'no-use-before-define': 'off',
'operator-linebreak': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/func-call-spacing': 'error',
'@typescript-eslint/indent': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-extra-parens': 'off',
'@typescript-eslint/no-redeclare': ['error'],
'@typescript-eslint/no-use-before-define': ['error'],
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': [
'warn',
{
allowExpressions: true,
},
],
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars-experimental': 'warn',
'prettier/prettier': 2,
},
env: {
browser: true,
commonjs: true,
es6: true,
node: true,
jest: true,
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.ts', '.tsx'],
},
},
},
};
{
"singleQuote": true,
"semi": true,
"quoteProps": "consistent",
"jsxSingleQuote": true,
"printWidth": 80
}
# `@rocket.chat/memo`
> TODO: description
## Usage
```
import { memoize, clear } from '@rocket.chat/memo';
// TODO: DEMONSTRATE API
```
{
"name": "@rocket.chat/memo",
"version": "0.20.1",
"description": "Memoization utilities",
"keywords": [
"memoize"
],
"author": {
"name": "Rocket.Chat",
"url": "https://rocket.chat/"
},
"homepage": "https://github.com/RocketChat/Rocket.Chat.Fuselage#readme",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RocketChat/Rocket.Chat.Fuselage.git"
},
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"files": [
"./dist/"
],
"scripts": {
"build:esm": "tsc -p tsconfig.json",
"build:cjs": "tsc -p tsconfig-cjs.json",
"build": "run-p build:esm build:cjs",
"eslint": "eslint src",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/RocketChat/Rocket.Chat.Fuselage/issues"
},
"devDependencies": {
"@types/react": "^16.14.0",
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"prettier": "^2.2.1",
"typescript": "^4.1.3"
}
}
export { memoize, clear } from './memoize';
type MemoizableFunction<T, A, R> = (this: T, arg: A) => R;
type MemoizedFunction<T, A, R> = (this: T, arg: A) => R;
const store = new WeakMap<
MemoizableFunction<unknown, unknown, unknown>,
Map<unknown, unknown>
>();
const isCachedValue = <A, R>(
cachedValue: R | undefined,
arg: A,
cache: Map<A, R>
): cachedValue is R => cache.has(arg) && cache.get(arg) === cachedValue;
export const memoize = <T, A, R>(
fn: MemoizableFunction<T, A, R>
): MemoizedFunction<T, A, R> => {
const cache = new Map<A, R>();
const memoized: MemoizedFunction<T, A, R> = function (this, arg) {
const cachedValue = cache.get(arg);
if (isCachedValue(cachedValue, arg, cache)) {
return cachedValue;
}
const result = fn.call(this, arg);
cache.set(arg, result);
return result;
};
store.set(memoized as MemoizableFunction<unknown, unknown, unknown>, cache);
return memoized;
};
export const clear = (
fn: MemoizableFunction<unknown, unknown, unknown>
): void => {
const x = store.get(fn);
if (x) {
x.clear();
}
};
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "./dist/cjs"
}
}
{
"compilerOptions": {
"target": "es5",
"module": "ES2020",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist/esm",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment