Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Destructive: Publish all histories per type when subscribe #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Assets/Scripts/InstanceBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum InjectId
BrokableInstanceType,
}

private IDictionary<Type, IReactiveProperty<object>> ReactivePropertyMap { get; } = new Dictionary<Type, IReactiveProperty<object>>();
private IDictionary<Type, ISubject<object>> SubjectMap { get; } = new Dictionary<Type, ISubject<object>>();

[Inject]
public void Construct(
Expand Down Expand Up @@ -67,28 +67,28 @@ public IObservable<T> TryReceive<T>()

private void DeclareInternal(Type type)
{
if (ReactivePropertyMap.ContainsKey(type))
if (SubjectMap.ContainsKey(type))
{
((ReactiveProperty<object>) ReactivePropertyMap[type]).Dispose();
((ReplaySubject<object>) SubjectMap[type]).Dispose();
}

ReactivePropertyMap[type] = new ReactiveProperty<object>();
SubjectMap[type] = new ReplaySubject<object>();
}

private void ReleaseInternal(Type type)
{
if (!ReactivePropertyMap.ContainsKey(type))
if (!SubjectMap.ContainsKey(type))
{
Debug.LogWarning($"`{type}' does not declared.");
return;
}

ReactivePropertyMap.Remove(type);
SubjectMap.Remove(type);
}

private void PublishInternal<T>(T instance, bool assert)
{
if (!ReactivePropertyMap.ContainsKey(typeof(T)))
if (!SubjectMap.ContainsKey(typeof(T)))
{
if (assert)
{
Expand All @@ -98,12 +98,12 @@ private void PublishInternal<T>(T instance, bool assert)
return;
}

ReactivePropertyMap[typeof(T)].Value = instance;
SubjectMap[typeof(T)].OnNext(instance);
}

private IObservable<T> ReceiveInternal<T>(bool assert)
{
if (!ReactivePropertyMap.ContainsKey(typeof(T)))
if (!SubjectMap.ContainsKey(typeof(T)))
{
if (assert)
{
Expand All @@ -113,7 +113,7 @@ private IObservable<T> ReceiveInternal<T>(bool assert)
return Observable.Never<T>();
}

return ReactivePropertyMap[typeof(T)]
return SubjectMap[typeof(T)]
.Where(x => (!(x is Object) && x != null) || (Object) x != null)
.Select(x => (T) x);
}
Expand All @@ -134,4 +134,4 @@ public static void DeclareBrokableInstance<T>(this DiContainer container)
}
}
}
}
}