Example #1
0
        public void ConnectToStore <T>(ReduxStore <T> store)
            where T : class, IImmutable, new()
        {
            var rootSelector = Selectors.CreateSelector((T state) => state);

            var onHistory = store.ObserveHistory()
                            .WithLatestFrom(store.Select(rootSelector), (history, state) => (history, state))
                            .Select(pair => _selectHistoryRecords(pair.history, pair.state));

            var onLatestEnsureHistory = EnsureManager
                                        .GetHistory()
                                        .Select(history => history.ToImmutableDictionary(session => session.Action));

            onLatestEnsureHistory
            .Subscribe(val => _ensureSessions = val)
            .DisposedBy(this);

            MvvmRx.ApplyOnCollection(onHistory, this, Records,
                                     factory: Resolver.Resolve <HistoryRecordVm>,
                                     syncer: (model, vm) => vm.ReadModel(model, _ensureSessions),
                                     onRemove: vm => vm.Dispose());

            var onCurrentHistoryRecord = Observable.CombineLatest(
                onHistory,
                MvvmRx.ObservePropertyValues(this, x => x.SelectedItem),
                (records, index) => (records, index))
                                         .Select(pair => _pure_getSelectedHistoryRecord(pair.records, pair.index))
                                         .Where(record => record != null);

            onCurrentHistoryRecord
            .Select(record => _pure_createJsonHierarchy(record.Action))
            .ApplyOnProperty(this, x => x.Action);

            onCurrentHistoryRecord
            .Select(record => _pure_createJsonHierarchy(record.NextState))
            .ApplyOnProperty(this, x => x.State);

            onCurrentHistoryRecord
            .Subscribe(record => Differ.ReadModel(record.PreviousState.ToJson(), record.NextState.ToJson()))
            .DisposedBy(this);

            var onEnsureSession = Observable.CombineLatest(onLatestEnsureHistory, onCurrentHistoryRecord,
                                                           (history, record) => _pure_getEnsureSessionHistory(record, history));

            onEnsureSession
            .Select(session => session?.Items
                    ?.Select(item => Resolver.Resolve <EnsureItemVm>().ReadModel(item))
                    ?.ToObservableCollection()
                    ?? new ObservableCollection <EnsureItemVm>()
                    )
            .ApplyOnProperty(this, x => x.EnsureItems);

            var onEnsureItem = Observable.CombineLatest(
                onEnsureSession,
                MvvmRx.ObservePropertyValues(this, x => x.SelectedEnsureItem),
                (session, index) => (session, index))
                               .Select(pair => _pure_getSelectedEnsureItem(pair.session, pair.index));

            onEnsureItem.Select(item => _pure_createJsonHierarchy(item?.Context.Entity))
            .ApplyOnProperty(this, x => x.EnsureItemState);

            onEnsureItem
            .Subscribe(item => EnsureDiffer.ReadModel(item?.Before.ToJson(), item?.After.ToJson()))
            .DisposedBy(this);
        }