-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPartialArray.cs
More file actions
87 lines (72 loc) · 2.21 KB
/
Copy pathPartialArray.cs
File metadata and controls
87 lines (72 loc) · 2.21 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Date: 21.11.2017, Time: 23:05 */
using System;
using System.Collections.Generic;
namespace IllidanS4.SharpUtils.ObjectModel
{
public sealed class PartialArray<T> : Partial<T[]>
{
readonly Dictionary<int, PartialElement> parts = new Dictionary<int, PartialElement>();
public PartialArray(ICompletable completable, out Partial<T[]> property) : base(completable, out property)
{
}
public PartialArray(ICompletable completable) : base(completable)
{
}
public Partial<T> this[int index]
{
get{
PartialElement part;
if(!parts.TryGetValue(index, out part))
{
part = parts[index] = new PartialElement(index, this);
part.MarkCreated();
}
return part;
}
}
public override void RegisterReceiver<TArg>(Partial<TArg> property, Action<TArg> valueReceiver)
{
if(base.ContainsProperty(property))
{
base.RegisterReceiver(property, valueReceiver);
return;
}
if(!ContainsProperty(property)) throw Completable.InvalidProperty("property");
Container.RegisterReceiver(property, valueReceiver);
this.RegisterReceiver(this, arr => valueReceiver(To<TArg>.Cast(arr[((PartialElement)(object)property).Index])));
}
public override TArg WaitForProperty<TArg>(Partial<TArg> property)
{
if(base.ContainsProperty(property)) return base.WaitForProperty(property);
if(!ContainsProperty(property)) throw Completable.InvalidProperty("property");
try{
return Container.WaitForProperty(property);
}catch(PropertyIncompleteException)
{
return To<TArg>.Cast(Value[((PartialElement)(object)property).Index]);
}
}
public override bool ContainsProperty<TArg>(Partial<TArg> property)
{
if(base.ContainsProperty(property)) return true;
var prop = property as PartialElement;
if(prop != null)
{
return parts.ContainsValue(prop);
}
return false;
}
private class PartialElement : Partial<T>
{
public int Index{get; private set;}
public PartialElement(int index, ICompletable completable, out Partial<T> property) : base(completable, out property)
{
Index = index;
}
public PartialElement(int index, ICompletable completable) : base(completable)
{
Index = index;
}
}
}
}