<commit_before>import { login } from "../login";
import { settings } from "../settings";
import { tea } from "../tea";
import { coffee } from "../coffee";
import { beer } from "../beer";

import { caseOf, get, head, tail, onChange } from "../util";

const routings = {
  Login: login.routing,
  Settings: settings.routing,
  Tea: tea.routing,
  Coffee: coffee.routing,
  Beer: beer.routing
};

const validateLeave = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["ValidateLeave"]);

  if (fn) {
    const result = fn({ state, update, value: head(routes).value });
    if (!result) {
      return false;
    }
  }
  if (routing) {
    return validateLeave({ routings: routing, routes: tail(routes), state, update });
  }
  return true;
};


const leaving = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["Leaving"]);

  if (fn) {
    fn({ state, update, value: head(routes).value });
  }
  if (routing) {
    leaving({ routings: routing, routes: tail(routes), state, update });
  }
};

const validateArrive = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["ValidateArrive"]);

  if (fn) {
    const result = fn({ state, update, value: head(routes).value });
    if (!result) {
      return false;
    }
  }
  if (routing) {
    return validateArrive({ routings: routing, routes: tail(routes), state, update });
  }
  return true;
};

const arriving = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["Arriving"]);

  if (fn) {
    fn({ state, update, value: head(routes).value });
  }
  if (routing) {
    arriving({ routings: routing, routes: tail(routes), state, update });
  }
};

export const root = {
  actions: update => ({
    navigateTo: (id, value) => update({
      routeNext: [caseOf(id, value)]
    }),

    deepLink: (routes, id, value) => update({
      routeNext: routes.concat([caseOf(id, value)])
    })
  }),

  service: (states, update) => {
    onChange(states, ["routeNext"], state => {
      const routes = state.routeNext;
      const transition = {
        from: state.routeCurrent,
        to: routes
      };

      const okLeave = validateLeave({ routings, routes: transition.from, state, update });
      if (okLeave) {
        leaving({ routings, routes: transition.from, state, update });

        const okArrive = validateArrive({ routings, routes, state, update });
        if (okArrive) {
          arriving({ routings, routes, state, update });
          update({ routeCurrent: routes });
        }
      }
    });
  }
};<commit_msg>working on routing<commit_after>import { login } from "../login";
import { settings } from "../settings";
import { tea } from "../tea";
import { coffee } from "../coffee";
import { beer } from "../beer";

import { caseOf, get, head, tail, onChange } from "../util";

const routings = {
  Login: login.routing,
  Settings: settings.routing,
  Tea: tea.routing,
  Coffee: coffee.routing,
  Beer: beer.routing
};

const validateLeave = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["ValidateLeave"]);

  if (fn) {
    const result = fn({ state, update, value: head(routes).value });
    if (!result) {
      return false;
    }
  }
  if (routing) {
    return validateLeave({ routings: routing, routes: tail(routes), state, update });
  }
  return true;
};


const leaving = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["Leaving"]);

  if (fn) {
    fn({ state, update, value: head(routes).value });
  }
  if (routing) {
    leaving({ routings: routing, routes: tail(routes), state, update });
  }
};

const validateArrive = ({ routings, routes, state, update }) => {
  const routing = get(routings, [get(head(routes), ["case"])]);
  const fn = get(routing, ["ValidateArrive"]);

  if (fn) {
    const result = fn({ state, update, value: head(routes).value });
    if (!result) {
      return false;
    }
  }
  if (routing) {
    return validateArrive({ routings: routing, routes: tail(routes), state, update });
  }
  return true;
};

const arriving = ({ routings, transition, state, update }) => {
  const from = head(transition.from);
  const to = head(transition.to);
  const routing = get(routings, [get(to, ["case"])]);
  const fn = get(routing, ["Arriving"]);

  if (fn && (from !== to)) {
    fn({ state, update, value: to.value });
  }
  if (routing) {
    arriving({
      routings: routing,
      transition: {
        from: tail(transition.from),
        to: tail(transition.to)
      },
      state,
      update
    });
  }
};

export const root = {
  actions: update => ({
    navigateTo: (id, value) => update({
      routeNext: [caseOf(id, value)]
    }),

    deepLink: (routes, id, value) => update({
      routeNext: routes.concat([caseOf(id, value)])
    })
  }),

  service: (states, update) => {
    onChange(states, ["routeNext"], state => {
      const routes = state.routeNext;
      const transition = {
        from: state.routeCurrent,
        to: routes
      };

      const okLeave = validateLeave({ routings, routes: transition.from, state, update });
      if (okLeave) {
        leaving({ routings, routes: transition.from, state, update });

        const okArrive = validateArrive({ routings, routes, state, update });
        if (okArrive) {
          arriving({ routings, transition, state, update });
          update({ routeCurrent: routes });
        }
      }
    });
  }
};
{'fraction_non_alphanumeric': 0.1753962784286699, 'fraction_numerical': 0.0, 'mean_word_length': 3.0651260504201683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '134273', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1854, 'n_words': 513}