Improve channel's avatar display performance
This commit is contained in:
parent
587aa74ac3
commit
57d64f30e3
|
@ -5,6 +5,7 @@ import { ComponentPagination, hasMoreItems, MarkdownService, User, UserService }
|
||||||
import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
|
import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
|
||||||
import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
|
import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
|
||||||
import { NSFWPolicyType, VideoSortField } from '@shared/models'
|
import { NSFWPolicyType, VideoSortField } from '@shared/models'
|
||||||
|
import { SimpleMemoize } from '@app/helpers'
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'my-account-video-channels',
|
selector: 'my-account-video-channels',
|
||||||
|
@ -145,6 +146,7 @@ export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
|
||||||
this.loadMoreChannels()
|
this.loadMoreChannels()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SimpleMemoize()
|
||||||
getVideoChannelLink (videoChannel: VideoChannel) {
|
getVideoChannelLink (videoChannel: VideoChannel) {
|
||||||
return [ '/c', videoChannel.nameWithHost ]
|
return [ '/c', videoChannel.nameWithHost ]
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ export * from './channel'
|
||||||
export * from './date'
|
export * from './date'
|
||||||
export * from './html'
|
export * from './html'
|
||||||
export * from './object'
|
export * from './object'
|
||||||
|
export * from './simple-memoize'
|
||||||
export * from './dom'
|
export * from './dom'
|
||||||
export * from './upload'
|
export * from './upload'
|
||||||
export * from './url'
|
export * from './url'
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Simple memoize only support methods that accept 0 or 1 argument
|
||||||
|
* You can easily use it adding @SimpleMemoize just above the method name
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function SimpleMemoize () {
|
||||||
|
const store = new Map()
|
||||||
|
|
||||||
|
return (_target: object, _propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
|
||||||
|
if (descriptor.value != null) {
|
||||||
|
descriptor.value = getNewFunction(descriptor.value, store)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Only put a Memoize() decorator on a method accessor.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNewFunction (originalMethod: () => void, store: Map<any, any>) {
|
||||||
|
return function (this: any, ...args: any[]) {
|
||||||
|
if (args.length > 1) {
|
||||||
|
throw new Error('Simple memoize only support 0 or 1 argument')
|
||||||
|
}
|
||||||
|
|
||||||
|
let returnedValue: any
|
||||||
|
|
||||||
|
if (args.length > 0) {
|
||||||
|
const hashKey = args[0]
|
||||||
|
|
||||||
|
if (store.has(hashKey)) {
|
||||||
|
returnedValue = store.get(hashKey)
|
||||||
|
} else {
|
||||||
|
returnedValue = originalMethod.apply(this, args)
|
||||||
|
store.set(hashKey, returnedValue)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (store.has(this)) {
|
||||||
|
returnedValue = store.get(this)
|
||||||
|
} else {
|
||||||
|
returnedValue = originalMethod.apply(this, args)
|
||||||
|
store.set(this, returnedValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnedValue
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue