Created
April 24, 2020 14:24
-
-
Save frencojobs/cd88f1870d34dbf34a794f3e777d20ec to your computer and use it in GitHub Desktop.
Reactivity explained in dart
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
import 'dart:html'; | |
class Storage { | |
Map db = {'count': 0}; | |
Map deps = {'count': []}; | |
void add(String key, dynamic value) { | |
db[key] = value; | |
} | |
dynamic getter(String key) { | |
print('$key was used.'); | |
return db[key]; | |
} | |
dynamic setter(String key, dynamic value) { | |
db[key] = value; | |
print('$key was changed.'); | |
for (var fn in deps[key]) { | |
fn(db[key]); | |
} | |
} | |
void depend(String key, Function dep) { | |
deps[key].add(dep); | |
dep(db[key]); | |
} | |
int get count => getter('count'); | |
set count(value) => setter('count', value); | |
} | |
void render(int count) { | |
querySelector('#counter').text = '$count'; | |
} | |
void main() { | |
var storage = Storage(); | |
storage.depend('count', render); | |
querySelector('#increment').onClick.listen((e) { | |
++storage.count; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment