Skip to content

Commit

Permalink
feat: @rocket.chat/memo (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Feb 5, 2021
1 parent 9436d1c commit 2425c04
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 61 deletions.
1 change: 1 addition & 0 deletions packages/css-in-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
},
"dependencies": {
"@emotion/hash": "^0.8.0",
"@rocket.chat/memo": "^0.21.0",
"stylis": "^4.0.6"
}
}
2 changes: 1 addition & 1 deletion packages/css-in-js/src/cssSupports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memoize } from './memoize';
import { memoize } from '@rocket.chat/memo';

/**
* Memoized and SSR-compatible facade of CSS.supports API
Expand Down
29 changes: 0 additions & 29 deletions packages/css-in-js/src/memoize.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/css-in-js/src/tags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { memoize } from './memoize';
import { memoize } from '@rocket.chat/memo';

import { createAnimationName, escapeName } from './names';

/**
Expand Down
1 change: 1 addition & 0 deletions packages/fuselage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"react-dom": "^16.13.1"
},
"dependencies": {
"@rocket.chat/memo": "^0.21.0",
"@rocket.chat/css-in-js": "^0.21.0",
"@rocket.chat/fuselage-tokens": "^0.21.0",
"invariant": "^2.2.4",
Expand Down
27 changes: 0 additions & 27 deletions packages/fuselage/src/helpers/memoize.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/fuselage/src/styleTokens.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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') {
Expand Down
2 changes: 2 additions & 0 deletions packages/memo/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/dist
79 changes: 79 additions & 0 deletions packages/memo/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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'],
},
},
},
};
1 change: 1 addition & 0 deletions packages/memo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
2 changes: 2 additions & 0 deletions packages/memo/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/dist
7 changes: 7 additions & 0 deletions packages/memo/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"semi": true,
"quoteProps": "consistent",
"jsxSingleQuote": true,
"printWidth": 80
}
46 changes: 46 additions & 0 deletions packages/memo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@rocket.chat/memo",
"version": "0.21.0",
"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"
}
}
1 change: 1 addition & 0 deletions packages/memo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { memoize, clear } from './memoize';
47 changes: 47 additions & 0 deletions packages/memo/src/memoize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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();
}
};
7 changes: 7 additions & 0 deletions packages/memo/tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "./dist/cjs"
}
}
16 changes: 16 additions & 0 deletions packages/memo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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
}
}

0 comments on commit 2425c04

Please sign in to comment.