aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-02-13 14:27:35 +0100
committerChocobozzz <me@florianbigard.com>2023-02-13 14:27:48 +0100
commit57d64f30e30b57ba5dc035fcaf38895d775412bd (patch)
tree596f3f416576598be4d1b640d54df0de401b2ce5 /client/src/app/helpers
parent587aa74ac3d8f2e1fdd8754d60c7a8c38ce30b88 (diff)
downloadPeerTube-57d64f30e30b57ba5dc035fcaf38895d775412bd.tar.gz
PeerTube-57d64f30e30b57ba5dc035fcaf38895d775412bd.tar.zst
PeerTube-57d64f30e30b57ba5dc035fcaf38895d775412bd.zip
Improve channel's avatar display performance
Diffstat (limited to 'client/src/app/helpers')
-rw-r--r--client/src/app/helpers/utils/index.ts1
-rw-r--r--client/src/app/helpers/utils/simple-memoize.ts49
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'
2export * from './date' 2export * from './date'
3export * from './html' 3export * from './html'
4export * from './object' 4export * from './object'
5export * from './simple-memoize'
5export * from './dom' 6export * from './dom'
6export * from './upload' 7export * from './upload'
7export * from './url' 8export * 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
8export 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
21function 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}