-
Notifications
You must be signed in to change notification settings - Fork 13.8k
chore: migrate @rocket.chat/memo from Fuselage
#41769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tassoevan
wants to merge
3
commits into
develop
Choose a base branch
from
chore/memo
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import server from '@rocket.chat/jest-presets/server'; | ||
| import type { Config } from 'jest'; | ||
|
|
||
| export default { | ||
| preset: server.preset, | ||
| } satisfies Config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| { | ||
| "name": "@rocket.chat/memo", | ||
| "version": "0.31.25", | ||
| "description": "Memoization utilities", | ||
| "keywords": [ | ||
| "memoize" | ||
| ], | ||
| "bugs": { | ||
| "url": "https://github.com/RocketChat/Rocket.Chat/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/RocketChat/Rocket.Chat.git", | ||
| "directory": "packages/memo" | ||
| }, | ||
| "license": "MIT", | ||
| "author": { | ||
| "name": "Rocket.Chat", | ||
| "url": "https://rocket.chat/" | ||
| }, | ||
| "main": "dist/cjs/index.js", | ||
| "module": "dist/esm/index.js", | ||
| "types": "dist/esm/index.d.ts", | ||
| "files": [ | ||
| "/dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "rm -rf dist && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint --fix .", | ||
| "test": "jest", | ||
| "testunit": "jest", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "devDependencies": { | ||
| "@rocket.chat/jest-presets": "workspace:~", | ||
| "@rocket.chat/tsconfig": "workspace:~", | ||
| "eslint": "~9.39.5", | ||
| "jest": "~30.2.0", | ||
| "prettier": "~3.3.3", | ||
| "ts-jest": "~29.4.11", | ||
| "typescript": "~5.9.3" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { memoize, clear, type MemoizableFunction, type MemoizedFunction, type Options } from './memoize'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { memoize, clear } from './memoize'; | ||
|
|
||
| it('should memoize a function that takes no parameter', () => { | ||
| const fn = jest.fn(() => 'foo'); | ||
| const memoized = jest.fn(memoize(fn)); | ||
|
|
||
| memoized(undefined); | ||
| memoized(undefined); | ||
|
|
||
| expect(memoized).toHaveBeenCalledTimes(2); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
|
|
||
| expect(fn).toHaveReturnedWith('foo'); | ||
| expect(memoized).toHaveReturnedWith('foo'); | ||
| }); | ||
|
|
||
| it('should memoize a function that takes one parameter', () => { | ||
| const fn = jest.fn((i: number) => i + 1); | ||
| const memoized = jest.fn(memoize(fn)); | ||
|
|
||
| memoized(5); | ||
| memoized(5); | ||
| memoized(2); | ||
|
|
||
| expect(memoized).toHaveBeenCalledTimes(3); | ||
| expect(fn).toHaveBeenCalledTimes(2); | ||
|
|
||
| expect(fn).toHaveNthReturnedWith(1, 6); | ||
| expect(fn).toHaveNthReturnedWith(2, 3); | ||
|
|
||
| expect(memoized).toHaveNthReturnedWith(1, 6); | ||
| expect(memoized).toHaveNthReturnedWith(2, 6); | ||
| expect(memoized).toHaveNthReturnedWith(3, 3); | ||
| }); | ||
|
|
||
| describe('clear', () => { | ||
| it('should discard cached values of a memoized function', () => { | ||
| const fn = jest.fn(() => 'foo'); | ||
| const memoized = memoize(fn); | ||
| const spiedMemoized = jest.fn(memoized); | ||
|
|
||
| spiedMemoized(undefined); | ||
| spiedMemoized(undefined); | ||
| clear(memoized); | ||
| spiedMemoized(undefined); | ||
|
|
||
| expect(spiedMemoized).toHaveBeenCalledTimes(3); | ||
| expect(fn).toHaveBeenCalledTimes(2); | ||
|
|
||
| expect(fn).toHaveReturnedWith('foo'); | ||
| expect(spiedMemoized).toHaveReturnedWith('foo'); | ||
| }); | ||
|
|
||
| it('should do nothing when a non-memoized function is passed', () => { | ||
| const fn = jest.fn(() => 'foo'); | ||
|
|
||
| expect(() => clear(fn)).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('timeout', () => { | ||
| it('should memoize a function that takes one parameter and clear after x ms', () => { | ||
| jest.useFakeTimers(); | ||
|
|
||
| const fn = jest.fn((i: number) => i + 1); | ||
| const memoized = jest.fn(memoize(fn, { maxAge: 3000 })); | ||
|
|
||
| memoized(5); | ||
| jest.advanceTimersByTime(2000); | ||
| memoized(5); | ||
| jest.advanceTimersByTime(2000); | ||
| memoized(5); | ||
| jest.advanceTimersByTime(3000); | ||
| memoized(5); | ||
|
|
||
| expect(fn).toHaveBeenCalledTimes(2); | ||
|
|
||
| expect(memoized).toHaveNthReturnedWith(1, 6); | ||
| expect(memoized).toHaveNthReturnedWith(2, 6); | ||
| expect(memoized).toHaveNthReturnedWith(3, 6); | ||
| expect(memoized).toHaveNthReturnedWith(4, 6); | ||
| }); | ||
|
|
||
| it('should memoize a function caching for two parameters and clearing both after x ms each one', () => { | ||
| jest.useFakeTimers(); | ||
|
|
||
| const fn = jest.fn((i: number) => i + 1); | ||
| const memoized = jest.fn(memoize(fn, { maxAge: 3000 })); | ||
|
|
||
| memoized(5); | ||
| jest.advanceTimersByTime(2000); | ||
| memoized(6); | ||
| jest.advanceTimersByTime(2000); | ||
|
|
||
| memoized(6); | ||
| memoized(5); | ||
|
|
||
| expect(fn).toHaveBeenCalledTimes(3); | ||
|
|
||
| expect(memoized).toHaveNthReturnedWith(1, 6); | ||
| expect(memoized).toHaveNthReturnedWith(2, 7); | ||
| expect(memoized).toHaveNthReturnedWith(3, 7); | ||
| expect(memoized).toHaveNthReturnedWith(4, 6); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| export type MemoizableFunction<T, A, R> = (this: T, arg: A) => R; | ||
| export type MemoizedFunction<T, A, R> = (this: T, arg: A) => R; | ||
|
|
||
| export type Options = { | ||
| maxAge: number; | ||
| }; | ||
|
|
||
| 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; | ||
|
tassoevan marked this conversation as resolved.
|
||
|
|
||
| export const memoize = <T, A, R>(fn: MemoizableFunction<T, A, R>, _options?: Options): MemoizedFunction<T, A, R> => { | ||
| const cache = new Map<A, R>(); | ||
| const cacheTimers = new Map<A, ReturnType<typeof setTimeout>>(); | ||
|
|
||
| const memoized: MemoizedFunction<T, A, R> = function (this, arg) { | ||
| const cleanUp = (): void => { | ||
| cache.delete(arg); | ||
| cacheTimers.delete(arg); | ||
| }; | ||
|
|
||
| const cachedValue = cache.get(arg); | ||
|
|
||
| if (isCachedValue(cachedValue, arg, cache)) { | ||
| const oldTimer = cacheTimers.get(arg); | ||
| if (oldTimer) { | ||
| clearTimeout(oldTimer); | ||
| } | ||
|
|
||
| if (_options) { | ||
| const timer = setTimeout(cleanUp, _options.maxAge); | ||
| cacheTimers.set(arg, timer); | ||
| } | ||
|
|
||
| return cachedValue; | ||
| } | ||
|
|
||
| const result = fn.call(this, arg); | ||
|
|
||
| cache.set(arg, result); | ||
|
|
||
| if (_options) { | ||
| const timer = setTimeout(cleanUp, _options.maxAge); | ||
| cacheTimers.set(arg, timer); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| store.set(memoized as MemoizableFunction<unknown, unknown, unknown>, cache); | ||
|
|
||
| return memoized; | ||
| }; | ||
|
|
||
| export const clear = (fn: MemoizedFunction<unknown, unknown, unknown>): void => { | ||
|
tassoevan marked this conversation as resolved.
|
||
| const cache = store.get(fn); | ||
| cache?.clear(); | ||
|
tassoevan marked this conversation as resolved.
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "module": "commonjs", | ||
| "moduleResolution": "node", | ||
| "declaration": true, | ||
| "declarationMap": true, | ||
| "sourceMap": true, | ||
| "outDir": "./dist/cjs" | ||
| }, | ||
| "exclude": ["**/*.spec.*", "jest.config.*"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "module": "esnext", | ||
| "moduleResolution": "node", | ||
| "declaration": true, | ||
| "declarationMap": true, | ||
| "sourceMap": true, | ||
| "outDir": "./dist/esm" | ||
| }, | ||
| "exclude": ["**/*.spec.*", "jest.config.*"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "@rocket.chat/tsconfig/base.json", | ||
| "include": ["src/**/*"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.