Don't you hate it to see something is promises twice at the same time? If functions are requesting a model, its sad it retrieves it twice from the database. So, @PromiseOnce ensures you it only fetches it once
Take a look at the example. While only 2 requests will take place, /model/1 and /model/7. While model 1 is called twice
import{PromiseOnce}from'ts-common-decorators';
importAxiosfrom'axios';
classFoo{
@PromiseOnce
bar(id?:number){
returnAxios({
method:'get',
url:'/model/'+id,
})
}
}
constfoo=newFoo();
constfetch1=foo.bar(1),
fetch2=foo.bar(1),
// Fetching another model
fetch3=foo.bar(7);
Promise.all([fetch1,fetch2,fetch3]).then(data=>{
// Both fetch 1 and 2 are a request for model's instance 1.
console.log('fetch 1 and 2 refer to the same object?',data[0]===data[1]);// true
// fetch 3 is a request for model's instance 7.
console.log('fetch 1 and 3 refer to the same object?',data[0]===data[2]);// false
});
Static
Sometimes you see these purely static classes. Its better to make them safe so they cant be created
import{Static}from'ts-common-decorators';
@Static
classFoo{
staticbar(){
}
}
// Error, can't call new on a static class
constfoo=newFoo;
Metadata
Give a method some metadata
import{MetaData}from'ts-common-decorators';
classFoo{
@MetaData({something:'special'})
bar(){
}
}
constfoo=newFoo();
console.log(MetaData.get(foo.bar).something);// special
Logger
Getting tired of figuring out if a method gets called while changing it?
import{Logger}from'ts-common-decorators'
classFoo{
@Logger(()=>console.log('called Foo.bar'))
bar(){
}
}
constfoo=newFoo();
foo.bar();// Logs it on the console. Handy debug function