diff options
author | Chocobozzz <me@florianbigard.com> | 2023-02-13 14:27:35 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-02-13 14:27:48 +0100 |
commit | 57d64f30e30b57ba5dc035fcaf38895d775412bd (patch) | |
tree | 596f3f416576598be4d1b640d54df0de401b2ce5 /client/src/app/helpers/utils | |
parent | 587aa74ac3d8f2e1fdd8754d60c7a8c38ce30b88 (diff) | |
download | PeerTube-57d64f30e30b57ba5dc035fcaf38895d775412bd.tar.gz PeerTube-57d64f30e30b57ba5dc035fcaf38895d775412bd.tar.zst PeerTube-57d64f30e30b57ba5dc035fcaf38895d775412bd.zip |
Improve channel's avatar display performance
Diffstat (limited to 'client/src/app/helpers/utils')
-rw-r--r-- | client/src/app/helpers/utils/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/helpers/utils/simple-memoize.ts | 49 |
2 files changed, 50 insertions, 0 deletions
diff --git a/client/src/app/helpers/utils/index.ts b/client/src/app/helpers/utils/index.ts index f821985c9..afa8f8f5c 100644 --- a/client/src/app/helpers/utils/index.ts +++ b/client/src/app/helpers/utils/index.ts | |||
@@ -2,6 +2,7 @@ export * from './channel' | |||
2 | export * from './date' | 2 | export * from './date' |
3 | export * from './html' | 3 | export * from './html' |
4 | export * from './object' | 4 | export * from './object' |
5 | export * from './simple-memoize' | ||
5 | export * from './dom' | 6 | export * from './dom' |
6 | export * from './upload' | 7 | export * from './upload' |
7 | export * from './url' | 8 | export * from './url' |
diff --git a/client/src/app/helpers/utils/simple-memoize.ts b/client/src/app/helpers/utils/simple-memoize.ts new file mode 100644 index 000000000..e8292793f --- /dev/null +++ b/client/src/app/helpers/utils/simple-memoize.ts | |||
@@ -0,0 +1,49 @@ | |||
1 | /** | ||
2 | * | ||
3 | * Simple memoize only support methods that accept 0 or 1 argument | ||
4 | * You can easily use it adding @SimpleMemoize just above the method name | ||
5 | * | ||
6 | */ | ||
7 | |||
8 | export function SimpleMemoize () { | ||
9 | const store = new Map() | ||
10 | |||
11 | return (_target: object, _propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => { | ||
12 | if (descriptor.value != null) { | ||
13 | descriptor.value = getNewFunction(descriptor.value, store) | ||
14 | return | ||
15 | } | ||
16 | |||
17 | throw new Error('Only put a Memoize() decorator on a method accessor.') | ||
18 | } | ||
19 | } | ||
20 | |||
21 | function getNewFunction (originalMethod: () => void, store: Map<any, any>) { | ||
22 | return function (this: any, ...args: any[]) { | ||
23 | if (args.length > 1) { | ||
24 | throw new Error('Simple memoize only support 0 or 1 argument') | ||
25 | } | ||
26 | |||
27 | let returnedValue: any | ||
28 | |||
29 | if (args.length > 0) { | ||
30 | const hashKey = args[0] | ||
31 | |||
32 | if (store.has(hashKey)) { | ||
33 | returnedValue = store.get(hashKey) | ||
34 | } else { | ||
35 | returnedValue = originalMethod.apply(this, args) | ||
36 | store.set(hashKey, returnedValue) | ||
37 | } | ||
38 | } else { | ||
39 | if (store.has(this)) { | ||
40 | returnedValue = store.get(this) | ||
41 | } else { | ||
42 | returnedValue = originalMethod.apply(this, args) | ||
43 | store.set(this, returnedValue) | ||
44 | } | ||
45 | } | ||
46 | |||
47 | return returnedValue | ||
48 | } | ||
49 | } | ||