Created
December 13, 2019 06:31
-
-
Save frencojobs/02be45911b8b60293321c8b939ea0602 to your computer and use it in GitHub Desktop.
Me explaining observables to my friends
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:async'; | |
class Observable { | |
Function subscriberFunction; | |
Observable(this.subscriberFunction); | |
subscribe(observer) { | |
this.subscriberFunction(observer); | |
} | |
pipe(fun) { | |
return Observable((observer) { | |
return this.subscribe({ | |
'observe': (v) { | |
observer['observe'](fun(v)); | |
} | |
}); | |
}); | |
} | |
} | |
main() { | |
var observable = new Observable((observer) { | |
observer['observe']('🐨'); | |
Timer(Duration(seconds: 10), () { | |
observer['observe']('🐈'); | |
}); | |
}).pipe((v) => 'i love $v'); | |
observable.subscribe({ | |
'observe': (v) { | |
print('subscriber #1: $v'); | |
} | |
}); | |
observable.subscribe({ | |
'observe': (v) { | |
print('subscriber #2: $v'); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment