-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefer.linq
29 lines (23 loc) · 1.01 KB
/
Defer.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Query Kind="Statements">
<Reference><ApplicationData>\LINQPad\Samples\Programming Reactive Extensions and LINQ\System.Reactive.dll</Reference>
<Namespace>System.Reactive</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
</Query>
/* Defer
*
* Defer is a way to take a possibly Hot Observable and make it Cold via a Func.
* This means, that we are creating an Observable that is only calculated when
* someone actually Subscribes to it.
*
* Why would I want to do this? Well, remember that using Observable.Return
* means that the value is calculated immediately, and maybe we want to be more
* lazy about it (think perhaps looking up something in a database).
*/
int i = 2;
var input1 = Observable.Return(i);
var input2 = Observable.Defer(() => Observable.Return(i));
i = 10;
"Without Defer - captured 'i' when it was created".Dump();
input1.Subscribe(Console.WriteLine);
"Using Defer - we didn't capture 'i' until Dump()".Dump();
input2.Subscribe(Console.WriteLine);