예제 #1
0
        private void SetupStreams()
        {
            _refreshButtonStream = Observable.FromEventPattern(RefreshButton, "Click");
            _close1ButtonStream  = Observable.FromEventPattern(Suggestion1CloseButton, "Click");
            _close2ButtonStream  = Observable.FromEventPattern(Suggestion2CloseButton, "Click");
            _close3ButtonStream  = Observable.FromEventPattern(Suggestion3CloseButton, "Click");

            _requestStream = _refreshButtonStream.StartWith((EventPattern <object>)null)
                             .Select((x) =>
            {
                var randomOffset = _random.Next();
                return((long)randomOffset);
            });

            _responseStream = _requestStream.SelectMany((sinceId) => _gitHubApi.GetGitHubUsers(_sinceId));

            var suggestion1Stream = CreateSuggestionStream(_close1ButtonStream);
            var suggestion2Stream = CreateSuggestionStream(_close2ButtonStream);
            var suggestion3Stream = CreateSuggestionStream(_close3ButtonStream);


            suggestion1Stream.ObserveOn(Dispatcher.CurrentDispatcher).Subscribe((user) =>
            {
                if (user != null)
                {
                    Suggestion1Name.Text = user.Login;
                }
            });

            suggestion2Stream.ObserveOn(Dispatcher.CurrentDispatcher).Subscribe((user) =>
            {
                if (user != null)
                {
                    Suggestion2Name.Text = user.Login;
                }
            });

            suggestion3Stream.ObserveOn(Dispatcher.CurrentDispatcher).Subscribe((user) =>
            {
                if (user != null)
                {
                    Suggestion3Name.Text = user.Login;
                }
            });
        }
        private void SetupStreams()
        {
            //this is the refresh button stream
            var refreshButtonStream = Observable.FromEventPattern(RefreshButton, "Click");
            //_responseStream = _gitHubApi.GetGitHubUsers(1);

            /**for each click of Refresh button we need to do following -
             * Check if buffer has requested number of users if not then request from server and fill the buffer
             */
            var refreshClickStream = refreshButtonStream.StartWith((EventPattern <object>)null).Select(click =>
            {
                if (_buffer.Count >= RefreshButonUserCount)
                {
                    //yes buffer has requested number of user
                    //deque those number of user
                    return(Observable.Return(_buffer.DequeueChunk(RefreshButonUserCount).ToList()));
                }

                return(_gitHubApi.GetGitHubUsers(_sinceId)
                       .Where(x => x != null && x.Count > 0)
                       .Select(users =>
                {
                    users.ForEach(u => _buffer.Enqueue(u));

                    _sinceId = users[users.Count - 1].Id;

                    if (_buffer.Count >= RefreshButonUserCount)
                    {
                        //yes buffer has requested number of user
                        //deque those number of user
                        return _buffer.DequeueChunk(RefreshButonUserCount).ToList();
                    }

                    return new List <GitHubUser>();
                }));
            })
                                     .SelectMany(x => x);


            refreshClickStream.ObserveOn(Dispatcher.CurrentDispatcher).Subscribe((users) =>
            {
                _users.Clear();
                users.ForEach(u => _users.Add(u));
            });
        }