Last active
January 26, 2023 10:26
-
-
Save dmorosinotto/62ab9acd876f394b0f557917a683ed46 to your computer and use it in GitHub Desktop.
Snippet for @input @output as Observable for Angular (VSCode Cmd+P > Snippets: Configure User Snippets)
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
"@Input Observable for Angular": { | |
"prefix": "in$", | |
"description": "Create an Observable @Input", | |
"body": [ | |
"#${1:prop} = new BehaviorSubject<${2:type}|undefined>(undefined);", | |
"protected ${1:prop}\\$ = this.#${1:prop}.pipe(filter(p=>p!==undefined));", | |
"@Input() set ${1:prop}(value: ${2:type}) {", | |
"\t$0//if (value!==this.${1:prop}) //eventual validation logic", | |
"\tthis.#${1:prop}.next(value);", | |
"}", | |
"get ${1:prop}(): ${2:type}|undefined {", | |
"\treturn this.#${1:prop}.getValue();", | |
"}" | |
] | |
}, | |
"@Output Observable for Angular [(two-way)] ": { | |
"prefix": "out$", | |
"description": "Create an Observable @Output for [(two-way)]", | |
"body": ["@Output() ${1:prop}Change=this.${1:prop}\\$.pipe(distinctUntilChanged());$0"] | |
}, | |
"[(two-way)] Observable for Angular": { | |
"prefix": "two$", | |
"description": "Create @Input+@Output Observable for [(two-way)]", | |
"body": [ | |
"#${1:prop} = new BehaviorSubject<${2:type}|undefined>(undefined);", | |
"protected ${1:prop}\\$ = this.#${1:prop}.asObservable().pipe(filter(p=>p!==undefined));", | |
"@Input() set ${1:prop}(value: ${2:type}) {", | |
"\tthis.#${1:prop}.next(value);", | |
"}", | |
"get ${1:prop}(): ${2:type}|undefined {", | |
"\treturn this.#${1:prop}.getValue();", | |
"}", | |
"@Output() ${1:prop}Change=this.${1:prop}\\$.pipe(distinctUntilChanged());$0" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment