Beispiel #1
0
    /// <summary>
    ///     Subscribes the given callback on the event. The callback will be invoked every time the event
    ///     is raised. This method establishes a weak reference between the event source and the object
    ///     holding the supplied callback, aka subscriber. That means that the subscription is kept alive
    ///     only as long as both event source and subscriber are kept in memory via strong references from
    ///     other objects. The event source alone doesn't keep the subscriber in memory. You have to keep a
    ///     strong reference to the returned subscription object to achieve this. The subscription can be
    ///     canceled at any time by disposing the returned subscription object. Otherwise, the subscription
    ///     is automatically canceled if the subscriber is being garbage collected. For this to happen no
    ///     other strong reference to the returned subscription must exist.
    /// </summary>
    ///
    /// <param name="action">
    ///     The callback to subscribe on the event.
    /// </param>
    ///
    /// <returns>
    ///     An object that represents the newly created subscription. Disposing this object will cancel the
    ///     subscription and remove the callback from the event source's subscription list.
    /// </returns>
    ///
    /// <exception cref="ArgumentNullException">
    ///     A null reference was passed to a method that did not accept it as a valid argument.
    /// </exception>
    public readonly IDisposable SubscribeWeak(Action <T> action)
    {
        if (action == null)
        {
            throw new ArgumentNullException(nameof(action));
        }

        if (mSource == null)
        {
            return(NullSubscription.Instance);
        }

        return(mSource.AddWeak(action));
    }