src/core/entity.js
/**
 * Creates a function with initial (default) state that is given
 * to optional components when invoked as default argument state.
 *
 * Entity(ctx, initialState, ...components) -> (args, scope) -> Any
 * Entity(ctx, initialState, [...components]) -> (args, scope) -> Any
 * Entity(ctx, ...components) -> (args, scope) -> Any
 * Entity(ctx, [...components]) -> (args, scope) -> Any
 *
 * @public
 * @function
 * @param {Context} ctx
 * @param {Object} initialState
 * @return {Function}
 * @throws MissingContextError
 * @throws BadArgumentError
 */
Creates a function with initial (default) state that is given
to optional components when invoked as default argument state.




Creates a function with initial ( default ) state that is given to optional components when invoked as default argument state .
function Entity(ctx, initialState, ...components) {
  if (undefined === ctx) {
    throw new MissingContextError('Entity')
  } else if (null === ctx || 'object' != typeof ctx || Array.isArray(ctx)) {
    throw new BadArgumentError(0, 'ctx', ctx, 'object')
  }
  if (initialState) {
    if (Array.isArray(initialState)) {
      components = initialState
      initialState = {}
    } else if ('function' == typeof initialState) {
      components.unshift(initialState)
    } else if ('object' != typeof initialState) {
      throw new BadArgumentError(1, 'initialState', initialState, 'object')
    }
  }
  if (Array.isArray(components[0])) {
    components = components[0]
  }
  initialState = { ...initialState }
  const entityId = generateEntityId()
  const entityContext = ctx.regl({context: new EntityContext(entityId)})
  const flatComponents = flattenComponents(initialState, components)
  const combinedComponents = combine(ctx.regl, flatComponents.components)
  return Object.assign((...vargs) => {
    const [kargs, next] = parseArguments(initialState, ...vargs)
    return entityContext(kargs, ({}, args, batchId) => {
      return combinedComponents(args, (ctx, cargs) => {
        return next(ctx, cargs, batchId)
      })
    })
  }, {
    combinedComponents,
    initialState,
    components,
    entityId,
  })
}
function Entity ( ctx , initialState , ... components ) { if ( undefined === ctx ) { throw new MissingContextError ( 'Entity' ) } else if ( null === ctx || 'object' != typeof ctx || Array . isArray ( ctx ) ) { throw new BadArgumentError ( 0 , 'ctx' , ctx , 'object' ) } if ( initialState ) { if ( Array . isArray ( initialState ) ) { components = initialState initialState = { } } else if ( 'function' == typeof initialState ) { components . unshift ( initialState ) } else if ( 'object' != typeof initialState ) { throw new BadArgumentError ( 1 , 'initialState' , initialState , 'object' ) } } if ( Array . isArray ( components [ 0 ] ) ) { components = components [ 0 ] } initialState = { ... initialState } const entityId = generateEntityId ( ) const entityContext = ctx . regl ( { context : new EntityContext ( entityId ) } ) const flatComponents = flattenComponents ( initialState , components ) const combinedComponents = combine ( ctx . regl , flatComponents . components ) return Object . assign ( ( ... vargs ) => { const [ kargs , next ] = parseArguments ( initialState , ... vargs ) return entityContext ( kargs , ( { } , args , batchId ) => { return combinedComponents ( args , ( ctx , cargs ) => { return next ( ctx , cargs , batchId ) } ) } ) } , { combinedComponents , initialState , components , entityId , } ) }
Creates a function with initial (default) state that is given
 to optional components when invoked as default argument state.
Creates a function with initial ( default ) state that is given to optional components when invoked as default argument state .