<commit_before>import {
  Context,
  InputData,
  EventOptions,
  EventData,
} from './core'
import {
  HandlerMsg,
} from './handler'
import { toIt } from './module'
import { _stateOf, CtxStateOf, _componentHelpers } from './input'

export interface InterfaceHelpers {
  ctx: Context
  interfaceOf: CtxInterfaceOf
  stateOf: CtxStateOf
  ev: CtxEv
  act: CtxAct
  vw: CtxVw
  vws: CtxVws
}

export const makeInterfaceHelpers = (ctx: Context): InterfaceHelpers => ({
  ctx,
  interfaceOf: _interfaceOf(ctx),
  stateOf: _stateOf(ctx),
  ev: _ev(ctx),
  act: _act(ctx),
  vw: _vw(ctx),
  vws: _vws(ctx),
})

export interface CtxInterfaceOf {
  (name: string, interfaceName: string): any
}

// gets an interface message from a certain component
export const _interfaceOf = (ctx: Context) => (name: string, interfaceName) => {
  let id = `${ctx.id}$${name}`
  let compCtx = ctx.components[id]
  if (!compCtx) {
    ctx.error('interfaceOf', `there are no component space '${id}'`)
    return {}
  }
  if (!compCtx.interfaces[interfaceName]) {
    ctx.error(
      'interfaceOf',
      `there are no interface '${interfaceName}' in component '${compCtx.name}' from space '${id}'`
    )
    return {}
  }
  // search in interface cache
  let cache = compCtx.interfaceValues[interfaceName]
  if (cache) {
    return cache
  } else {
    // caches interface
    compCtx.interfaceValues[interfaceName] = compCtx.interfaces[interfaceName](compCtx.state)
    return compCtx.interfaceValues[interfaceName]
  }
}

export interface CtxAct {
  (actionName: string, context?: any, param?: any, options?: EventOptions): InputData
}

// generic action dispatcher
export const _act = (ctx: Context): CtxAct => {
  let _evCtx = _ev(ctx)
  return (actionName, context, param, options): InputData =>
    _evCtx('_action', [actionName, context], param, options)
}

export interface CtxVw {
  (componentName: string): HandlerMsg
}

// extract component view interface, sintax sugar
export const _vw = (ctx: Context): CtxVw => {
  let _interfaceOfCtx = _interfaceOf(ctx)
  return componentName => _interfaceOfCtx(componentName, 'view')
}

export interface CtxVws {
  (groupName: string): HandlerMsg[]
}

// extract view interfaces from a component group
export const _vws = (ctx: Context): CtxVws => {
  let _interfaceOfCtx = _interfaceOf(ctx)
  let comps = _componentHelpers(ctx)
  return groupName => {
    let views = []
    let componentNames = comps(groupName).getNames()
    for (let i = 0, len = componentNames.length; i < len; i++) {
      views.push(_interfaceOfCtx(componentNames[i], 'view'))
    }
    return views
  }
}

export interface CtxEv {
  (inputName: string, context?: any, param?: any, options?: EventOptions): InputData
}

// create an InputData array
export const _ev = (ctx: Context): CtxEv => (inputName, context, param, options) => {
  return [ctx.id, inputName, context, param, options]
}
<commit_msg>Interfaces are now async<commit_after>import {
  Context,
  InputData,
  EventOptions,
  EventData,
} from './core'
import {
  HandlerMsg,
} from './handler'
import { toIt } from './module'
import { _stateOf, CtxStateOf, _componentHelpers } from './input'

export interface InterfaceHelpers {
  ctx: Context
  interfaceOf: CtxInterfaceOf
  stateOf: CtxStateOf
  ev: CtxEv
  act: CtxAct
  vw: CtxVw
  vws: CtxVws
}

export const makeInterfaceHelpers = (ctx: Context): InterfaceHelpers => ({
  ctx,
  interfaceOf: _interfaceOf(ctx),
  stateOf: _stateOf(ctx),
  ev: _ev(ctx),
  act: _act(ctx),
  vw: _vw(ctx),
  vws: _vws(ctx),
})

export interface CtxInterfaceOf {
  (name: string, interfaceName: string): Promise<any>
}

// gets an interface message from a certain component
export const _interfaceOf = (ctx: Context) => async (name: string, interfaceName) => {
  let id = `${ctx.id}$${name}`
  let compCtx = ctx.components[id]
  if (!compCtx) {
    ctx.error('interfaceOf', `there are no component space '${id}'`)
    return {}
  }
  if (!compCtx.interfaces[interfaceName]) {
    ctx.error(
      'interfaceOf',
      `there are no interface '${interfaceName}' in component '${compCtx.name}' from space '${id}'`
    )
    return {}
  }
  // search in interface cache
  let cache = compCtx.interfaceValues[interfaceName]
  if (cache) {
    return cache
  } else {
    // caches interface
    compCtx.interfaceValues[interfaceName] = await compCtx.interfaces[interfaceName](compCtx.state)
    return compCtx.interfaceValues[interfaceName]
  }
}

export interface CtxAct {
  (actionName: string, context?: any, param?: any, options?: EventOptions): InputData
}

// generic action dispatcher
export const _act = (ctx: Context): CtxAct => {
  let _evCtx = _ev(ctx)
  return (actionName, context, param, options): InputData =>
    _evCtx('_action', [actionName, context], param, options)
}

export interface CtxVw {
  (componentName: string): HandlerMsg
}

// extract component view interface, sintax sugar
export const _vw = (ctx: Context): CtxVw => {
  let _interfaceOfCtx = _interfaceOf(ctx)
  return async componentName => await _interfaceOfCtx(componentName, 'view')
}

export interface CtxVws {
  (groupName: string): Promise<HandlerMsg[]>
}

// extract view interfaces from a component group
export const _vws = (ctx: Context): CtxVws => {
  let _interfaceOfCtx = _interfaceOf(ctx)
  let comps = _componentHelpers(ctx)
  return async groupName => {
    let views = []
    let componentNames = comps(groupName).getCompleteNames()
    for (let i = 0, len = componentNames.length; i < len; i++) {
      views.push(await _interfaceOfCtx(componentNames[i], 'view'))
    }
    return views
  }
}

export interface CtxEv {
  (inputName: string, context?: any, param?: any, options?: EventOptions): InputData
}

// create an InputData array
export const _ev = (ctx: Context): CtxEv => (inputName, context, param, options) => {
  return [ctx.id, inputName, context, param, options]
}

{'fraction_non_alphanumeric': 0.12825310426943357, 'fraction_numerical': 0.0003401939105290015, 'mean_word_length': 4.130890052356021, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '1319264', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1923, 'n_words': 545}