-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathReactiveValue.cs
47 lines (39 loc) · 1018 Bytes
/
ReactiveValue.cs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using ReactUnity.Helpers;
namespace ReactUnity.Reactive
{
public class ReactiveValue<T> : IReactive<T>
{
private event Action<T> changed;
private T current;
public T Value
{
get => current;
set
{
current = value;
Change();
}
}
public ReactiveValue() { }
public ReactiveValue(T value)
{
current = value;
}
public void Change()
{
changed?.Invoke(current);
}
public Action AddListener(object cb)
{
var callback = Callback.From(cb);
var listener = new Action<T>((val) => callback.Call(val));
return AddListener(listener);
}
public Action AddListener(Action<T> listener)
{
changed += listener;
return () => changed -= listener;
}
}
}