<commit_before>'use strict'

var Struct = require('observ-struct')
var Observ = require('observ')
var Path = require('observ-path')
var watch = require('observ/watch')
var createStore = require('weakmap-shim/create-store')
var Event = require('weakmap-event')
var series = require('run-series')
var partial = require('ap').partial
var Table = require('./table')
var View = require('./view')

module.exports = Router

function Router (data) {
  data = data || {}

  var state = Struct({
    path: Path(data.path),
    listening: Observ(false),
    active: Observ()
  })

  store(state).table = Table()

  return state
}

Router.listen = function listen (state) {
  if (state.listening()) return

  var table = routes(state)

  return watch(state.path, function onPath (path) {
    var match = routes(state).match(path)

    if (!match) return NotFoundEvent.broadcast(state, {
      path: path
    })

    var hooks = table.get(match.key).hooks()

    series(hooks.map(function (hook) {
      return partial(hook, match.params)
    }), done)

    function done (err) {
      if (err) return ErrorEvent.broadcast(state, err)
      state.active.set({render: match.render})
    }
  })
}

var NotFoundEvent = Event()
Router.onNotFound = NotFoundEvent.listen

var ErrorEvent = Event()
Router.onError = ErrorEvent.listen

var store = createStore()

Router.route = function route (state, options) {
  return routes(state).add(options)
}

Router.hook = function hook (state, route, callback) {
  routes(state).get(route).hook(callback)
}

Router.render = function render (state) {
  if (!state.active) return
  return state.active.render()
}

function routes (state) {
  return store(state).table
}<commit_msg>Expose page render fn to Sour.render via weak store<commit_after>'use strict'

var Struct = require('observ-struct')
var Observ = require('observ')
var Path = require('observ-path')
var watch = require('observ/watch')
var createStore = require('weakmap-shim/create-store')
var Event = require('weakmap-event')
var series = require('run-series')
var partial = require('ap').partial
var Table = require('./table')
var View = require('./view')

module.exports = Router

function Router (data) {
  data = data || {}

  var state = Struct({
    path: Path(data.path),
    listening: Observ(false),
    active: Observ()
  })

  store(state).table = Table()

  return state
}

Router.listen = function listen (state) {
  if (state.listening()) return

  var table = routes(state)

  return watch(state.path, function onPath (path) {
    var match = routes(state).match(path)

    if (!match) return NotFoundEvent.broadcast(state, {
      path: path
    })

    var hooks = table.get(match.key).hooks()

    series(hooks.map(function (hook) {
      return partial(hook, match.params)
    }), done)

    function done (err) {
      if (err) return ErrorEvent.broadcast(state, err)
      store(match.key).render = match.render
      state.active.set(match.key)
    }
  })
}

var NotFoundEvent = Event()
Router.onNotFound = NotFoundEvent.listen

var ErrorEvent = Event()
Router.onError = ErrorEvent.listen

var store = createStore()

Router.route = function route (state, options) {
  return routes(state).add(options)
}

Router.hook = function hook (state, route, callback) {
  routes(state).get(route).hook(callback)
}

Router.render = function render (state) {
  if (!state.active) return
  return store(state.active).render()
}

function routes (state) {
  return store(state).table
}
{'fraction_non_alphanumeric': 0.14207650273224043, 'fraction_numerical': 0.0, 'mean_word_length': 3.9055007052186177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2413797', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1068, 'n_words': 281}