<commit_before>'use strict';

import * as wx from 'webrx';

import BaseViewModel from '../React/BaseViewModel';
import { IRoutedViewModel } from '../React/BaseRoutableViewModel';
import { RouteManager, IRoute } from '../../Routing/RouteManager';
import { default as PubSub, ISubscriptionHandle } from '../../Utils/PubSub';
import { RoutingStateChangedKey, IRoutingStateChanged } from '../../Events/RoutingStateChanged';

export interface IViewModelActivator {
  (route: IRoute): IRoutedViewModel;
}

export interface IRoutingMap {
  [path: string]: IViewModelActivator | string;
}

export class RouteHandlerViewModel extends BaseViewModel {
  constructor(public manager: RouteManager, public routingMap: IRoutingMap) {
    super();
  }

  private currentPath: string;

  public currentViewModel = this.manager.currentRoute.changed
    .select(x => this.getRoutedViewModel(x))
    .toProperty();

  private routingStateChangedHandle: ISubscriptionHandle;

  private getRoutedViewModel(route: IRoute): IRoutedViewModel {
    let viewModel = this.currentViewModel();

    if (route.path !== this.currentPath) {
      let activator = this.routingMap[route.path];

      if (activator == null) {
        activator = this.routingMap['*'];
      }

      if (activator == null) {
        viewModel = null;
      } else {
        if (activator instanceof Function) {
          if (RouteManager.EnableRouteDebugging) {
            console.log(String.format('[Route    ] Routing to Path: {0}', route.path));

            if (route.state != null) {
              console.log(JSON.stringify(route.state, null, 2));
            }
          }

          viewModel = (activator as IViewModelActivator).apply(this, [route]);

          this.currentPath = route.path;

          if (viewModel) {
            viewModel.setRoutingState(route.state);
          }
        } else {
          this.manager.navTo(activator.toString());
        }
      }
    } else if (viewModel) {
      if (RouteManager.EnableRouteDebugging) {
        console.log(String.format('[Route    ] Updating Routing State: {0}', route.path));

        if (route.state != null) {
          console.log(JSON.stringify(route.state, null, 2));
        }
      }

      viewModel.setRoutingState(route.state || {});
    }

    return viewModel;
  }

  initialize() {
    super.initialize();

    this.routingStateChangedHandle = PubSub.subscribe<IRoutingStateChanged>(RoutingStateChangedKey, x => {
      if (this.currentViewModel() != null) {
        let state = this.currentViewModel().getRoutingState(x);<commit_msg>fixing bug where currentPath only gets updated when a view model mapping exists for a route path<commit_after>'use strict';

import * as wx from 'webrx';

import BaseViewModel from '../React/BaseViewModel';
import { IRoutedViewModel } from '../React/BaseRoutableViewModel';
import { RouteManager, IRoute } from '../../Routing/RouteManager';
import { default as PubSub, ISubscriptionHandle } from '../../Utils/PubSub';
import { RoutingStateChangedKey, IRoutingStateChanged } from '../../Events/RoutingStateChanged';

export interface IViewModelActivator {
  (route: IRoute): IRoutedViewModel;
}

export interface IRoutingMap {
  [path: string]: IViewModelActivator | string;
}

export class RouteHandlerViewModel extends BaseViewModel {
  constructor(public manager: RouteManager, public routingMap: IRoutingMap) {
    super();
  }

  private currentPath: string;

  public currentViewModel = this.manager.currentRoute.changed
    .select(x => this.getRoutedViewModel(x))
    .toProperty();

  private routingStateChangedHandle: ISubscriptionHandle;

  private getRoutedViewModel(route: IRoute): IRoutedViewModel {
    let viewModel = this.currentViewModel();

    if (route.path !== this.currentPath) {
      this.currentPath = route.path;
      
      let activator = this.routingMap[route.path];

      if (activator == null) {
        activator = this.routingMap['*'];
      }

      if (activator == null) {
        viewModel = null;
      } else {
        if (activator instanceof Function) {
          if (RouteManager.EnableRouteDebugging) {
            console.log(String.format('[Route    ] Routing to Path: {0}', route.path));

            if (route.state != null) {
              console.log(JSON.stringify(route.state, null, 2));
            }
          }

          viewModel = (activator as IViewModelActivator).apply(this, [route]);

          if (viewModel) {
            viewModel.setRoutingState(route.state);
          }
        } else {
          this.manager.navTo(activator.toString());
        }
      }
    } else if (viewModel) {
      if (RouteManager.EnableRouteDebugging) {
        console.log(String.format('[Route    ] Updating Routing State: {0}', route.path));

        if (route.state != null) {
          console.log(JSON.stringify(route.state, null, 2));
        }
      }

      viewModel.setRoutingState(route.state || {});
    }

    return viewModel;
  }

  initialize() {
    super.initialize();

    this.routingStateChangedHandle = PubSub.subscribe<IRoutingStateChanged>(RoutingStateChangedKey, x => {
      if (this.currentViewModel() != null) {
        let state = this.currentViewModel().getRoutingState(x);
{'fraction_non_alphanumeric': 0.1230120712780226, 'fraction_numerical': 0.001532860701283771, 'mean_word_length': 2.9336850037678976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1558101', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1452, 'n_words': 323}