Ejemplo n.º 1
0
        public static async Task<StatusModel> Get(TwitterStatus status)
        {
            status = await StatusStore.Get(status.Id)
                                      .DefaultIfEmpty(status)
                                      .FirstAsync();
            var rto = status.RetweetedOriginal == null
                          ? null
                          : await Get(status.RetweetedOriginal);
            var lockerobj = _generateLock.GetOrAdd(status.Id, new object());
            try
            {
                lock (lockerobj)
                {
                    StatusModel model;
                    WeakReference wr;
                    lock (_staticCacheLock)
                    {
                        _staticCache.TryGetValue(status.Id, out wr);
                    }
                    if (wr != null)
                    {
                        model = (StatusModel)wr.Target;
                        if (model != null)
                        {
                            return model;
                        }
                    }

                    // cache is dead/not cached yet
                    model = new StatusModel(status, rto);
                    wr = new WeakReference(model);
                    lock (_staticCacheLock)
                    {
                        _staticCache[status.Id] = wr;
                    }
                    return model;
                }
            }
            finally
            {
                _generateLock.TryRemove(status.Id, out lockerobj);
                // ReSharper disable InvertIf
#pragma warning disable 4014
                if (Interlocked.Increment(ref _cleanupCount) == CleanupInterval)
                {
                    Interlocked.Exchange(ref _cleanupCount, 0);
                    Task.Run((Action)CollectGarbages);
                }
#pragma warning restore 4014
                // ReSharper restore InvertIf
            }
        }
Ejemplo n.º 2
0
 private StatusModel(TwitterStatus status, StatusModel retweetedOriginal)
     : this(status)
 {
     this.RetweetedOriginal = retweetedOriginal;
 }
Ejemplo n.º 3
0
 protected StatusViewModel GenerateStatusViewModel(StatusModel status)
 {
     return new StatusViewModel(this, status, CurrentAccounts);
 }
Ejemplo n.º 4
0
        public static StatusModel Get(TwitterStatus status)
        {
            object lockerobj = _generateLock.GetOrAdd(status.Id, new object());
            try
            {
                lock (lockerobj)
                {
                    StatusModel model;
                    WeakReference wr;
                    lock (_staticCacheLock)
                    {
                        _staticCache.TryGetValue(status.Id, out wr);
                    }
                    if (wr != null)
                    {
                        model = (StatusModel)wr.Target;
                        if (model != null)
                            return model;
                    }

                    // cache is dead/not cached yet
                    model = new StatusModel(status);
                    wr = new WeakReference(model);
                    lock (_staticCacheLock)
                    {
                        _staticCache[status.Id] = wr;
                    }
                    return model;
                }
            }
            finally
            {
                _generateLock.TryRemove(status.Id, out lockerobj);
                var cc = Interlocked.Increment(ref _cleanupCount);
                if (cc == CleanupInterval)
                {
                    Interlocked.Exchange(ref _cleanupCount, 0);
                    Task.Run((Action)CollectGarbages);
                }
            }
        }
Ejemplo n.º 5
0
        public StatusViewModel(TimelineViewModelBase parent, StatusModel status,
            IEnumerable<long> initialBoundAccounts)
        {
            _parent = parent;
            // get status model
            Model = status;
            RetweetedOriginalModel = status.RetweetedOriginal;

            // bind accounts
            _bindingAccounts = initialBoundAccounts.Guard().ToArray();

            // initialize users information
            _favoritedUsers = ViewModelHelperRx.CreateReadOnlyDispatcherCollectionRx(
                Model.FavoritedUsers, user => new UserViewModel(user), DispatcherHelper.UIDispatcher);
            CompositeDisposable.Add(_favoritedUsers);
            CompositeDisposable.Add(
                _favoritedUsers.ListenCollectionChanged()
                               .Subscribe(_ =>
                               {
                                   RaisePropertyChanged(() => IsFavorited);
                                   RaisePropertyChanged(() => IsFavoritedUserExists);
                                   RaisePropertyChanged(() => FavoriteCount);
                               }));
            _retweetedUsers = ViewModelHelperRx.CreateReadOnlyDispatcherCollectionRx(
                Model.RetweetedUsers, user => new UserViewModel(user), DispatcherHelper.UIDispatcher);
            CompositeDisposable.Add(_retweetedUsers);
            CompositeDisposable.Add(
                _retweetedUsers.ListenCollectionChanged()
                               .Subscribe(_ =>
                               {
                                   RaisePropertyChanged(() => IsRetweeted);
                                   RaisePropertyChanged(() => IsRetweetedUserExists);
                                   RaisePropertyChanged(() => RetweetCount);
                               }));
            if (RetweetedOriginalModel != null)
            {
                CompositeDisposable.Add(
                            RetweetedOriginalModel.FavoritedUsers.ListenCollectionChanged()
                                                  .Subscribe(_ => this.RaisePropertyChanged(() => IsFavorited)));
                CompositeDisposable.Add(
                    RetweetedOriginalModel.RetweetedUsers.ListenCollectionChanged()
                                          .Subscribe(_ => this.RaisePropertyChanged(() => IsRetweeted)));
            }

            // resolve images
            var imgsubj = Model.ImagesSubject;
            if (imgsubj != null)
            {
                lock (imgsubj)
                {
                    var subscribe = imgsubj
                        .Finally(() =>
                        {
                            RaisePropertyChanged(() => Images);
                            RaisePropertyChanged(() => FirstImage);
                            RaisePropertyChanged(() => IsImageAvailable);
                        })
                        .Subscribe();
                    CompositeDisposable.Add(subscribe);
                }
            }

            // look-up in-reply-to
            if (!status.Status.InReplyToStatusId.HasValue) return;
            var inReplyTo = StoreHelper.GetTweet(status.Status.InReplyToStatusId.Value)
                                       .Subscribe(replyTo =>
                                       {
                                           this._inReplyTo = replyTo;
                                           this.RaisePropertyChanged(() => this.IsInReplyToExists);
                                           this.RaisePropertyChanged(() => this.InReplyToUserImage);
                                           this.RaisePropertyChanged(() => this.InReplyToUserName);
                                           this.RaisePropertyChanged(() => this.InReplyToUserScreenName);
                                           this.RaisePropertyChanged(() => this.InReplyToBody);
                                       });
            this.CompositeDisposable.Add(inReplyTo);
        }
Ejemplo n.º 6
0
 public StatusViewModel(StatusModel status)
     : this(null, status, null)
 {
 }
Ejemplo n.º 7
0
        public StatusViewModel(TimelineViewModelBase parent, StatusModel status,
            IEnumerable<long> initialBoundAccounts)
        {
            Interlocked.Increment(ref _instanceCount);
            CompositeDisposable.Add(() => Interlocked.Decrement(ref _instanceCount));
            _parent = parent;
            // get status model
            Model = status;
            RetweetedOriginalModel = status.RetweetedOriginal;

            // bind accounts
            _bindingAccounts = initialBoundAccounts.Guard().ToArray();

            // initialize users information
            _favoritedUsers = ViewModelHelperRx.CreateReadOnlyDispatcherCollectionRx(
                Model.FavoritedUsers, user => new UserViewModel(user),
                DispatcherHelper.UIDispatcher, DispatcherPriority.Background);
            CompositeDisposable.Add(_favoritedUsers);
            CompositeDisposable.Add(
                _favoritedUsers.ListenCollectionChanged()
                               .Subscribe(_ =>
                               {
                                   RaisePropertyChanged(() => IsFavorited);
                                   RaisePropertyChanged(() => IsFavoritedUserExists);
                                   RaisePropertyChanged(() => FavoriteCount);
                               }));
            _retweetedUsers = ViewModelHelperRx.CreateReadOnlyDispatcherCollectionRx(
                Model.RetweetedUsers, user => new UserViewModel(user),
                DispatcherHelper.UIDispatcher, DispatcherPriority.Background);
            CompositeDisposable.Add(_retweetedUsers);
            CompositeDisposable.Add(
                _retweetedUsers.ListenCollectionChanged()
                               .Subscribe(_ =>
                               {
                                   RaisePropertyChanged(() => IsRetweeted);
                                   RaisePropertyChanged(() => IsRetweetedUserExists);
                                   RaisePropertyChanged(() => RetweetCount);
                               }));
            if (RetweetedOriginalModel != null)
            {
                CompositeDisposable.Add(
                            RetweetedOriginalModel.FavoritedUsers.ListenCollectionChanged()
                                                  .Subscribe(_ => this.RaisePropertyChanged(() => IsFavorited)));
                CompositeDisposable.Add(
                    RetweetedOriginalModel.RetweetedUsers.ListenCollectionChanged()
                                          .Subscribe(_ => this.RaisePropertyChanged(() => IsRetweeted)));
            }

            // resolve images
            var imgsubj = Model.ImagesSubject;
            if (imgsubj != null)
            {
                lock (imgsubj)
                {
                    var subscribe = imgsubj
                        .Finally(() =>
                        {
                            RaisePropertyChanged(() => Images);
                            RaisePropertyChanged(() => FirstImage);
                            RaisePropertyChanged(() => IsImageAvailable);
                        })
                        .Subscribe();
                    CompositeDisposable.Add(subscribe);
                }
            }

            // look-up in-reply-to
            _isInReplyToExists = status.Status.InReplyToStatusId.HasValue;
        }