1.0.31 • Published 6 years ago

decorator-class-update v1.0.31

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Update class by decorator method

install: npm i -D decorator-class-update reflect-metadata

import { UpdateClass } from 'decorator-class-update';
import 'reflect-metadata';


function DecoratorClass() {
	return (target) => {

		// observe decorator method and returns a list with anything
		// sent by the UpdateClass.subscriber method
		const list = UpdateClass.watch('my-event', target.prototype);
		// static method scientist
		Object.defineProperty(target, 'scientist', {
			get:  function  get() {
					return  list[0];
				}
	    });
	    return target
	}
}

function DecoratorMethod(options) {
	return (target, key, property) => {
		// Sent to class decorator
		UpdateClass.subscribe('my-event', target, () =>  `${options} Eureka!`)
	}
}

@DecoratorClass()
class Test{
	
	@DecoratorMethod('Arquimedes said')
	exclamation() {
        // static method Test.scientist was added to class was instantiated
        // and its value when DecoratorMethod was declared
		console.log(`phrases ${Test.scientist}`)
	}
}

new Test().exclamation(); // out: Phrases: Arquimedes said Eureka!