Skip to content

Instantly share code, notes, and snippets.

@e-oz
Created September 20, 2022 12:30
Show Gist options
  • Save e-oz/deeef9630ba3729439b5eafe9ca192f2 to your computer and use it in GitHub Desktop.
Save e-oz/deeef9630ba3729439b5eafe9ca192f2 to your computer and use it in GitHub Desktop.
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