Last active
February 4, 2019 22:21
-
-
Save codediodeio/5d222897375fd1a8eeffa0f96f802983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { BehaviorSubject } from 'rxjs'; | |
@Component({ | |
selector: 'app-root', | |
template: ` | |
<p>You clicked {{count.value}} times</p> | |
<button (click)="setCount(count.value + 1)">Click Me</button> | |
`, | |
}) | |
export class AppComponent { | |
@UseState(0) count; setCount; | |
@UseEffect() | |
onEffect() { | |
document.title = `You clicked ${this.count.value} times`; | |
} | |
} | |
/// Implementation Details: | |
function UseEffect() { | |
return function (target, key, descriptor) { | |
target.ngOnInit = descriptor.value; | |
target.ngOnChanges = descriptor.value; | |
}; | |
} | |
function UseState(seed: any) { | |
return function (target, key) { | |
target[key] = new BehaviorSubject(seed); | |
target[`set${key.replace(/^\w/, c => c.toUpperCase())}`] = (val) => target.count.next(val); | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment