Created
November 6, 2022 09:43
-
-
Save pr0xyMity/bbb26bb861390f0f5fe73dbf5bfc2fae to your computer and use it in GitHub Desktop.
Repositories + InMemoryStore
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
| import 'package:rxdart/rxdart.dart'; | |
| class InMemoryStore<T> { | |
| InMemoryStore(T initial) : _subject = BehaviorSubject<T>.seeded(initial); | |
| // INIT | |
| final BehaviorSubject<T> _subject; | |
| // GET - synchronous | |
| T get value => _subject.value; | |
| // SET | |
| set value(T value) => _subject.add(value); | |
| // STREAM | |
| Stream<T> get stream => _subject.stream; | |
| // CLOSE | |
| void close() => _subject.close(); | |
| } | |
| class AppUser { | |
| final String email; | |
| final String password; | |
| AppUser({required this.email, required this.password}); | |
| } | |
| class AuthRepository { | |
| final _authState = InMemoryStore<AppUser?>(null); | |
| void createUser(String email, String password) { | |
| _authState.value = AppUser(email: email, password: password); | |
| } | |
| Stream<AppUser?> authStream() => _authState.stream; | |
| } | |
| void main() { | |
| final repository = AuthRepository(); | |
| repository.authStream().listen((data) => print(data?.email)); | |
| repository.createUser('johnDoe@gmail.com', 'Password123'); | |
| repository.createUser('maxDoe@gmail.com', 'Verstappen33'); | |
| repository.createUser('sergioDoe@gmail.com', 'Perzez11'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment