Created
September 20, 2022 12:30
-
-
Save e-oz/deeef9630ba3729439b5eafe9ca192f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface UsersListState { | |
users: User[]; | |
columns: string[]; | |
} | |
class UsersListStore extends ComponentStore<UsersListState> { | |
// ... | |
getUsers() { | |
return this.select(state => state.users).pipe( | |
combineLatestWith(this.select(state => state.columns)), | |
map(([users, columns]) => { | |
const renderedUsersList: User[] = []; | |
for (let user of users) { | |
let mapped = {}; | |
for (let column of columns) { | |
mapped[column] = user[column]; | |
} | |
renderedUsersList.push(mapped); | |
} | |
// "users" were not modified here | |
return renderedUsersList; | |
}) | |
); | |
} | |
// ... | |
} | |
@Component({ | |
selector: 'users-list', | |
template: ` | |
<div *ngFor="let user of users$|async"> | |
{{user.name}} | |
</div> ... | |
` | |
}) | |
class UsersListComponent { | |
public readonly users$: Observable<Users[]>; | |
constructor(private readonly store: UsersListStore) { | |
this.users$ = this.store.getUsers(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment