Exemplo n.º 1
0
// TODO extract out into `pull-difference`
function difference () {
  var lastValue
  return filter(function (value) {
    var condition = value !== lastValue
    lastValue = value
    return condition
  })
}
Exemplo n.º 2
0
test('emits entities but also component updates', async t => {
    try {
        const { registry, entitySet } = await initialise({ loadEntities: true });

        Pull(
            entitySet.source({ emitEntities: true, closeAfterExisting: false }),
            // PullMap(val => Array.isArray(val) ? val[0] : val),
            PullMap(val => (Array.isArray(val) ? val[0] : val)),
            // PullMap(val => {
            //     Log.debug('[PullMap][<]', stringify(val));
            //     return val;
            // }),
            PullFilter(val => {
                return isEntity(val);
            }),
            Pull.collect((err, entities) => {
                // Log.debug('[collect]', entityToString(entities));
                t.equals(entities.length, 5);
                t.end();
            })
        );

        // Log.debug( entityToString(entitySet) );

        // adding this entity should only trigger an entity:add evt, not a component:add evt
        entitySet.addComponent([{ '@c': '/name', name: 'circle3' }]);

        let entity = entitySet.at(0, true);
        entity.removeComponent('/ttl');
        entity.addComponent({ '@c': '/name', name: 'box1' });

        // Log.debug('>--');

        entitySet.addEntity(entity);

        // Log.debug( entityToString(entity) );
        // Log.debug( entityToString(entitySet) );

        t.end();
    } catch (err) {
        Log.error(err.stack);
    }
});
Exemplo n.º 3
0
test('source emits entities', async t => {
    try {
        const { registry, entitySet } = await initialise({ loadEntities: true });

        Pull(
            entitySet.source({ emitEntities: true, closeAfterExisting: true }),
            PullMap(val => (Array.isArray(val) ? val[0] : val)),
            PullFilter(val => {
                return isEntity(val);
            }),
            Pull.collect((err, entities) => {
                // Log.debug('[collect]', entityToString(entities));
                t.equals(entities.length, 5);
                t.end();
            })
        );
    } catch (err) {
        Log.error(err.stack);
    }
});
Exemplo n.º 4
0
function start (app) {
  app = defined(app, {})

  var init = defined(app.init, defaults.init)
  var update = defined(app.update, defaults.update)
  var view = defined(app.view, defaults.view)
  var run = defined(app.run, defaults.run)

  var actions = notify()

  function dispatch (nextAction) {
    actions(nextAction)
  }

  var initialState = init.call(app)
  var states = notify()
  pull(
    actions.listen(),
    scan(function (state, action) {
      return update.call(app, state.model, action)
    }, initialState),
    drain(states)
  )

  var models = notify()
  pull(
    states.listen(),
    map(function (state) {
      return state.model
    }),
    difference(),
    drain(models)
  )

  var views = notify()
  pull(
    models.listen(),
    map(function (model) {
      return view.call(app, model, dispatch)
    }),
    filter(isNotNil),
    drain(views)
  )

  var effects = notify()
  pull(
    states.listen(),
    map(function (state) {
      return state.effect
    }),
    filter(isNotNil),
    drain(effects)
  )

  var effectActionsSources = notify()

  var notifys = {
    actions: actions,
    states: states,
    models: models,
    views: views,
    effects: effects,
    effectActionsSources: effectActionsSources
  }

  var sources = {}
  Object.keys(notifys).forEach(function (name) {
    var listen = notifys[name].listen
    sources[name] = (
      ['states', 'models', 'effects', 'views'].indexOf(name) !== -1
    ) ? replayLastValue(listen) : listen
  })

  pull(
    effects.listen(),
    map(function (effect) {
      return run.call(app, effect, sources)
    }),
    filter(isNotNil),
    drain(effectActionsSources)
  )

  pull(
    effectActionsSources.listen(),
    drainMany(actions)
  )

  process.nextTick(function () {
    states(initialState)
  })

  return Object.assign({}, sources, { stop: stop })

  function stop () {
    Object.keys(notifys).forEach(function (name) {
      notifys[name].end()
    })
  }
}