From 9bc741ebaf6bec85ec7e7d276987d90da8cf280d Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 20 Jul 2017 10:09:43 +0200 Subject: [PATCH] introduce createDecorator --- src/vs/base/common/decorators.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/vs/base/common/decorators.ts b/src/vs/base/common/decorators.ts index e7330957a46..8b83c71bafa 100644 --- a/src/vs/base/common/decorators.ts +++ b/src/vs/base/common/decorators.ts @@ -5,6 +5,27 @@ 'use strict'; +export function createDecorator(mapFn: (fn: Function) => Function): Function { + return (target: any, key: string, descriptor: any) => { + let fnKey: string = null; + let fn: Function = null; + + if (typeof descriptor.value === 'function') { + fnKey = 'value'; + fn = descriptor.value; + } else if (typeof descriptor.get === 'function') { + fnKey = 'get'; + fn = descriptor.get; + } + + if (!fn) { + throw new Error('not supported'); + } + + descriptor[fnKey] = mapFn(fn); + }; +} + export function memoize(target: any, key: string, descriptor: any) { let fnKey: string = null; let fn: Function = null;