示例#1
0
        /// <summary>
        /// Subscribe an EventSystemBase to get reader updates.
        /// </summary>
        /// <param name="eventSystem"> The event system. </param>
        internal void Subscribe(EventSystemBase eventSystem)
        {
            Assert.IsFalse(this._disposed);

            Assert.IsFalse(this._subscribers.Contains(eventSystem));
            this._subscribers.Add(eventSystem);
        }
示例#2
0
        /// <summary>
        /// Unsubscribe an EventSystemBase to stop getting reader updates.
        /// </summary>
        /// <param name="eventSystem"> The event system. </param>
        internal void Unsubscribe(EventSystemBase eventSystem)
        {
            Assert.IsFalse(this._disposed);
            Assert.IsTrue(this._subscribers.Contains(eventSystem));
            this._subscribers.Remove(eventSystem);

            // No more subscribers, clean up the bus
            if (this._subscribers.Count == 0)
            {
                Instances.Remove(this._key);
                this._disposed = true;
            }

            // Must release streams before call unsubscribe.
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            foreach (var streamPair in this._streams)
            {
                Assert.IsFalse(streamPair.Value.Systems.Remove(eventSystem));
            }
#endif
        }
示例#3
0
        /// <summary>
        /// Return a set of streams that the system has finished reading.
        /// </summary>
        /// <param name="owner"> The system the streams are coming from. </param>
        /// <param name="streamsToRelease"> The collection of streams to be released. </param>
        /// <param name="inputHandle"> The dependency handle. </param>
        /// <returns> New dependency handle. </returns>
        internal JobHandle ReleaseStreams(EventSystemBase owner, IReadOnlyList <NativeEventStream> streamsToRelease, JobHandle inputHandle)
        {
            Assert.IsFalse(this._disposed);

            var outputHandle = inputHandle;

            for (var index = 0; index < streamsToRelease.Count; index++)
            {
                var stream = streamsToRelease[index];

                Assert.IsTrue(this._streams.ContainsKey(stream));

                var handles = this._streams[stream];

                // Remove the owner handle
                bool result = handles.Systems.Remove(owner);
                Assert.IsTrue(result);

                var handle = JobHandle.CombineDependencies(handles.Handle, inputHandle);

                // No one else using stream, need to dispose
                if (handles.Systems.Count == 0)
                {
                    this._pool.Return(handles);
                    this._streams.Remove(stream);

                    stream.Dispose(handle);
                }
                else
                {
                    handles.Handle = handle;
                }

                outputHandle = JobHandle.CombineDependencies(outputHandle, handle);
            }

            return(outputHandle);
        }
示例#4
0
        /// <summary> Add a set of event streams to be shared with other systems. </summary>
        /// <param name="owner"> The system that owns the streams. </param>
        /// <param name="type"> The type of the event. </param>
        /// <param name="newStreams"> The streams. </param>
        /// <param name="consumerHandle"> The dependency handle for these streams. </param>
        /// <returns> The new dependency handle. </returns>
        /// <exception cref="ArgumentException"> Thrown  if this owner is not subscribed. </exception>
        internal JobHandle AddStreams(EventSystemBase owner, Type type, IReadOnlyList <NativeEventStream> newStreams, JobHandle consumerHandle)
        {
            Assert.IsFalse(this._disposed);

            if (!this._subscribers.Contains(owner))
            {
                throw new ArgumentException("Owner not subscribed");
            }

            if (newStreams.Count == 0)
            {
                return(consumerHandle);
            }

            if (this._subscribers.Count == 1)
            {
                // No subscribers other than ourselves, just dispose the streams
                JobHandle handle = consumerHandle;

                for (var index = 0; index < newStreams.Count; index++)
                {
                    // var stream = newStreams[index];
                    // handle = JobHandle.CombineDependencies(handle, stream.Dispose(consumerHandle));
                    newStreams[index].Dispose(consumerHandle);
                }

                return(handle);
            }

            for (var index = 0; index < newStreams.Count; index++)
            {
                var stream  = newStreams[index];
                var handles = this._pool.Get();
                handles.Handle = consumerHandle;

                this._streams.Add(stream, handles);

                for (var i = 0; i < this._subscribers.Count; i++)
                {
                    var subscriber = this._subscribers[i];
                    if (subscriber == owner)
                    {
                        continue;
                    }

                    handles.Systems.Add(subscriber);
                }
            }

            // Fire off these new readers
            for (var i = 0; i < this._subscribers.Count; i++)
            {
                var subscriber = this._subscribers[i];
                if (subscriber == owner)
                {
                    continue;
                }

                subscriber.AddExternalReaders(type, newStreams, consumerHandle);
            }

            return(consumerHandle);
        }
 /// <summary> Initializes a new instance of the <see cref="Extensions{T}"/> struct. </summary>
 /// <param name="eventSystem"> Event system parent. </param>
 internal Extensions(EventSystemBase eventSystem)
 {
     this._eventSystem = eventSystem;
 }