コード例 #1
0
        /// <summary>
        /// Makes sure that the observable is initialized against the shared state and any other objects along the tree.
        /// </summary>
        /// <typeparam name="T">The type to return.</typeparam>
        /// <param name="sharedState">The shared state to attach to.</param>
        /// <param name="initializerFunction">The initializer function.</param>
        /// <returns>The observable you wannt.</returns>
        public static T Observable <T>(this ISharedState sharedState, Func <T> initializerFunction)
        {
            if (sharedState is null)
            {
                throw new ArgumentNullException(nameof(sharedState));
            }

            if (initializerFunction is null)
            {
                throw new ArgumentNullException(nameof(initializerFunction));
            }

            try
            {
                SharedState.SetAsyncLocalState(sharedState);
                var result = initializerFunction();
                if (((IReactiveObject)result).SharedState != sharedState)
                {
                    // shared state should have been assigned.
                    throw new ArgumentOutOfRangeException(nameof(initializerFunction));
                }

                return(result);
            }
            finally
            {
                SharedState.SetAsyncLocalState(null);
            }
        }