-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
873 additions
and
67 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using R3; | ||
using System.Collections; | ||
using System.Runtime.InteropServices; | ||
|
||
public sealed class LiveList<T> : IReadOnlyList<T>, IDisposable | ||
{ | ||
readonly List<T> list = new List<T>(); | ||
readonly IDisposable sourceSubscription; | ||
|
||
public LiveList(Event<T> source) | ||
{ | ||
sourceSubscription = source.Subscribe(new ListSubscriber(list)); | ||
} | ||
|
||
public T this[int index] | ||
{ | ||
get | ||
{ | ||
lock (list) | ||
{ | ||
return list[index]; | ||
} | ||
} | ||
} | ||
|
||
public int Count | ||
{ | ||
get | ||
{ | ||
lock (list) | ||
{ | ||
return list.Count; | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
sourceSubscription.Dispose(); | ||
} | ||
|
||
public void ForEach(Action<T> action) | ||
{ | ||
lock (list) | ||
{ | ||
var span = CollectionsMarshal.AsSpan(list); | ||
foreach (ref var item in span) | ||
{ | ||
action(item); | ||
} | ||
} | ||
} | ||
|
||
public void ForEach<TState>(Action<T, TState> action, TState state) | ||
{ | ||
lock (list) | ||
{ | ||
var span = CollectionsMarshal.AsSpan(list); | ||
foreach (ref var item in span) | ||
{ | ||
action(item, state); | ||
} | ||
} | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
lock (list) | ||
{ | ||
// snapshot | ||
return CollectionsMarshal.AsSpan(list).ToArray().AsEnumerable().GetEnumerator(); | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
lock (list) | ||
{ | ||
// snapshot | ||
return CollectionsMarshal.AsSpan(list).ToArray().AsEnumerable().GetEnumerator(); | ||
} | ||
} | ||
|
||
sealed class ListSubscriber(List<T> list) : Subscriber<T> | ||
{ | ||
protected override void OnNextCore(T message) | ||
{ | ||
lock (list) | ||
{ | ||
list.Add(message); | ||
} | ||
} | ||
|
||
protected override void OnErrorResumeCore(Exception error) | ||
{ | ||
EventSystem.GetUnhandledExceptionHandler().Invoke(error); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.