chore(xo-server/decorateWith): use fn wrappers as method decorator

This commit is contained in:
Julien Fontanet 2020-04-27 09:35:05 +02:00
parent b50e3aec5f
commit 317a020841
3 changed files with 16 additions and 6 deletions

View File

@ -0,0 +1,14 @@
// Creates a decorator from a function wrapper.
//
// For instance, allows using Lodash's functions as decorators:
//
// ```js
// @decorateWith(lodash.debounce, 150)
// myMethod() {
// // body
// }
// ```
export const decorateWith = (fn, ...args) => (target, name, descriptor) => ({
...descriptor,
value: fn(descriptor.value, ...args),
})

View File

@ -52,8 +52,3 @@ export const debounceWithKey = (fn, delay, keyFn = defaultKeyFn) => {
return promise
}
}
debounceWithKey.decorate = (...params) => (target, name, descriptor) => ({
...descriptor,
value: debounceWithKey(descriptor.value, ...params),
})

View File

@ -5,6 +5,7 @@ import { filter, find, pickBy, some } from 'lodash'
import ensureArray from '../../_ensureArray'
import { debounceWithKey } from '../../_pDebounceWithKey'
import { decorateWith } from '../../_decorateWith'
import { forEach, mapFilter, mapToArray, parseXml } from '../../utils'
import { extractOpaqueRef, useUpdateSystem } from '../utils'
@ -55,7 +56,7 @@ const listMissingPatches = debounceWithKey(
export default {
// raw { uuid: patch } map translated from updates.xensource.com/XenServer/updates.xml
// FIXME: should be static
@debounceWithKey.decorate(24 * 60 * 60 * 1000, function() {
@decorateWith(debounceWithKey, 24 * 60 * 60 * 1000, function() {
return this
})
async _getXenUpdates() {