Last active
September 6, 2021 09:26
-
-
Save antischematic/4101b73b7dd01522f53cc9dce33a7762 to your computer and use it in GitHub Desktop.
Class vs Function composition
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
@Component({ | |
template: ` | |
<div> | |
<show-results *ngIf="results$ | async as results else loading" [results]="results"></show-results> | |
<ng-template #loading> | |
<spinner></spinner> | |
</ng-template> | |
</div> | |
<pagination [page]="page" [pageSize]="10" [total]="(results$ | async)?.length""></pagination> | |
` | |
}) | |
export class MyComponent { | |
page$ = new BehaviorSubject(0) | |
results$ = this.page$.pipe( | |
switchMap((page) => this.api.getSomething({ page })), | |
share() | |
) | |
@Input() | |
get page() { | |
return this.page$.value | |
} | |
set page(value) { | |
this.page$.next(value) | |
this.pageChange.emit(value) | |
} | |
@Output() | |
pageChange = new EventEmitter() | |
constructor(private api: Api) {} | |
} |
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
function setup() { | |
const api = inject(Api) | |
const [page, pageChange] = use(0) | |
const results = use() | |
subscribe(page, (page) => | |
subscribe(api.getSomething({ page }), results) | |
) | |
return { | |
page, | |
pageChange, | |
results, | |
} | |
} | |
@Component({ | |
template: ` | |
<div> | |
<show-results *ngIf="results else loading" [results]="results"></show-results> | |
<ng-template #loading> | |
<spinner></spinner> | |
</ng-template> | |
</div> | |
<pagination [page]="page" [pageSize]="10" [total]="results?.length"></pagination> | |
`, | |
inputs: ["page"], | |
outputs: ["pageChange"] | |
}) | |
export class MyComponent extends ViewDef(setup) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment