-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from lukepighetti/named-imports
Use named imports in example
- Loading branch information
Showing
2 changed files
with
34 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,69 @@ | ||
library rx.example.example; | ||
|
||
import 'package:more/collection.dart'; | ||
import 'package:rx/constructors.dart'; | ||
import 'package:rx/core.dart'; | ||
import 'package:rx/operators.dart'; | ||
import 'package:rx/rx.dart' as rx; | ||
import 'package:rx/operators.dart' as ops; | ||
|
||
Observer<T> printObserver<T>(String name) => Observer( | ||
rx.Observer<T> printObserver<T>(String name) => rx.Observer( | ||
next: (value) => print('$name.next($value)'), | ||
error: (error, [stackTrace]) => print('$name.error($error)'), | ||
complete: () => print('$name.complete()'), | ||
); | ||
|
||
void main() { | ||
// create | ||
final createObservable = create((subscriber) { | ||
final create = rx.create((subscriber) { | ||
for (var i = 0; i < 3; i++) { | ||
subscriber.next(i); | ||
} | ||
subscriber.complete(); | ||
}); | ||
createObservable.subscribe(printObserver('createObservable')); | ||
create.subscribe(printObserver('create')); | ||
|
||
// empty | ||
final emptyObservable = empty(); | ||
emptyObservable.subscribe(printObserver('emptyObservable')); | ||
final empty = rx.empty(); | ||
empty.subscribe(printObserver('empty')); | ||
|
||
// future | ||
final futureObservable = fromFuture(Future.value(42)); | ||
futureObservable.subscribe(printObserver('futureObservable')); | ||
final future = rx.fromFuture(Future.value(42)); | ||
future.subscribe(printObserver('future')); | ||
|
||
// just | ||
final justObservable = just(42); | ||
justObservable.subscribe(printObserver('justObservable')); | ||
final just = rx.just(42); | ||
just.subscribe(printObserver('just')); | ||
|
||
// never | ||
final neverObservable = never(); | ||
neverObservable.subscribe(printObserver('neverObservable')); | ||
final never = rx.never(); | ||
never.subscribe(printObserver('never')); | ||
|
||
// stream | ||
final streamObservable = fromStream(Stream.fromIterable([1, 2, 3])); | ||
streamObservable.subscribe(printObserver('streamObservable')); | ||
final stream = rx.fromStream(Stream.fromIterable([1, 2, 3])); | ||
stream.subscribe(printObserver('stream')); | ||
|
||
// throw | ||
final throwObservable = throwError(Exception('Hello World')); | ||
throwObservable.subscribe(printObserver('throwObservable')); | ||
final throwError = rx.throwError(Exception('Hello World')); | ||
throwError.subscribe(printObserver('throw')); | ||
|
||
// Other: | ||
final transformed = fromIterable(IntegerRange(0, 100)) | ||
.lift(filter((value) => value.isEven)) | ||
.lift(map((value) => 'Observable{value * value}')) | ||
.lift(filter((value) => value.length < 3)); | ||
final transformed = rx | ||
.fromIterable(IntegerRange(0, 100)) | ||
.lift(ops.filter((value) => value.isEven)) | ||
.lift(ops.map((value) => '{value * value}')) | ||
.lift(ops.filter((value) => value.length < 3)); | ||
|
||
transformed.subscribe(printObserver('one')); | ||
transformed.subscribe(printObserver('two')); | ||
|
||
final obs = timer( | ||
final obs = rx.timer( | ||
delay: const Duration(seconds: 2), | ||
period: const Duration(milliseconds: 500)); | ||
final subs1 = obs.subscribe(printObserver('first')); | ||
final subs2 = obs.subscribe(printObserver('second')); | ||
|
||
timer(delay: const Duration(seconds: 3)) | ||
.subscribe(Observer(complete: () => subs1.unsubscribe())); | ||
timer(delay: const Duration(seconds: 5)) | ||
.subscribe(Observer(complete: () => subs2.unsubscribe())); | ||
rx | ||
.timer(delay: const Duration(seconds: 3)) | ||
.subscribe(rx.Observer(complete: () => subs1.unsubscribe())); | ||
rx | ||
.timer(delay: const Duration(seconds: 5)) | ||
.subscribe(rx.Observer(complete: () => subs2.unsubscribe())); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
library rx; | ||
|
||
export 'constructors.dart'; | ||
export 'core.dart'; | ||
export 'operators.dart'; |