lib/definitions/aggregate.js
function (aggregateModel, snapshot, events, loadingTime, stream, streams, notSameOrigin) {
    var self = this;

    var isSnapshotNeeded = false;

    if (snapshot) {
      // load snapshot
      debug('load snapshot ' + snapshot.id + ' from history', _.pick(snapshot, ['context', 'aggregate', 'version']), {
        context: this.context.name,
        aggregate: this.name,
        version: this.version
      });
      if ((snapshot.context === this.context.name || !snapshot.context && this.context.name === '_general') &&
          (snapshot.aggregate === this.name || !snapshot.aggregate) &&
          snapshot.version === this.version) {
        aggregateModel.set(snapshot.data);
      } else {
        if (!this.snapshotConversions[snapshot.context + '.' + snapshot.aggregate + '.' + snapshot.version]) {
          var err = new Error('No snapshot conversion defined!');
          debug(err);
          throw err;
        }
        debug('convert snapshot from history');
        this.snapshotConversions[snapshot.context + '.' + snapshot.aggregate + '.' + snapshot.version](snapshot.data, aggregateModel);
        isSnapshotNeeded = true;
      }
      aggregateModel.setRevision({
        context: stream.context,
        aggregate: stream.aggregate,
        aggregateId: stream.aggregateId
      }, snapshot.revision + 1);
    }

    if (events && events.length > 0) {
      // load events
      debug('load events from history');
      var maxRevision = _.reduce(events, function (res, evt) {
        var rev = dotty.get(evt, self.definitions.event.revision);
        if (rev > res) {
          return rev;
        }
        return res;
      }, 0);

      if (!this.skipHistory || (this.skipHistory && this.applyLastEvent)) {
        this.apply(events, aggregateModel, notSameOrigin);
      }

      aggregateModel.setRevision({
        context: stream.context,
        aggregate: stream.aggregate,
        aggregateId: stream.aggregateId
      }, maxRevision);

      if (!isSnapshotNeeded) {
        if (this.isSnapshotNeeded.length > 2) {
          isSnapshotNeeded = this.isSnapshotNeeded(loadingTime, events, aggregateModel.toJSON(), streams);
        } else {
          isSnapshotNeeded = this.isSnapshotNeeded(loadingTime, events);
        }
      }
    }

    return isSnapshotNeeded;
  }
function (aggregateModel, snapshot, events, loadingTime, stream, streams, notSameOrigin) {
    var self = this;

    var isSnapshotNeeded = false;

    if (snapshot) {
      // load snapshot
      debug('load snapshot ' + snapshot.id + ' from history', _.pick(snapshot, ['context', 'aggregate', 'version']), {
        context: this.context.name,
        aggregate: this.name,
        version: this.version
      });
      if ((snapshot.context === this.context.name || !snapshot.context && this.context.name === '_general') &&
          (snapshot.aggregate === this.name || !snapshot.aggregate) &&
          snapshot.version === this.version) {
        aggregateModel.set(snapshot.data);
      } else {
        if (!this.snapshotConversions[snapshot.context + '.' + snapshot.aggregate + '.' + snapshot.version]) {
          var err = new Error('No snapshot conversion defined!');
          debug(err);
          throw err;
        }
        debug('convert snapshot from history');
        this.snapshotConversions[snapshot.context + '.' + snapshot.aggregate + '.' + snapshot.version](snapshot.data, aggregateModel);
        isSnapshotNeeded = true;
      }
      aggregateModel.setRevision({
        context: stream.context,
        aggregate: stream.aggregate,
        aggregateId: stream.aggregateId
      }, snapshot.revision + 1);
    }

    if (events && events.length > 0) {
      // load events
      debug('load events from history');
      var maxRevision = _.reduce(events, function (res, evt) {
        var rev = dotty.get(evt, self.definitions.event.revision);
        if (rev > res) {
          return rev;
        }
        return res;
      }, 0);

      if (!this.skipHistory || (this.skipHistory && this.applyLastEvent)) {
        this.apply(events, aggregateModel, notSameOrigin);
      }

      aggregateModel.setRevision({
        context: stream.context,
        aggregate: stream.aggregate,
        aggregateId: stream.aggregateId
      }, maxRevision);

      if (!isSnapshotNeeded) {
        if (this.isSnapshotNeeded.length > 2) {
          isSnapshotNeeded = this.isSnapshotNeeded(loadingTime, events, aggregateModel.toJSON(), streams);
        } else {
          isSnapshotNeeded = this.isSnapshotNeeded(loadingTime, events);
        }
      }
    }

    return isSnapshotNeeded;
  }
function ( aggregateModel , snapshot , events , loadingTime , stream , streams , notSameOrigin ) { var self = this ; var isSnapshotNeeded = false ; if ( snapshot ) { // load snapshot debug ( 'load snapshot ' + snapshot . id + ' from history' , _ . pick ( snapshot , [ 'context' , 'aggregate' , 'version' ] ) , { context : this . context . name , aggregate : this . name , version : this . version } ) ; if ( ( snapshot . context === this . context . name || s napshot. c ontext  &  his. c ontext. n ame  ==  _general') & ( snapshot . aggregate === this . name || ! snapshot . aggregate ) && snapshot . version === this . version ) { aggregateModel . set ( snapshot . data ) ; } else { if ( ! this . snapshotConversions [ snapshot . context + '.' + snapshot . aggregate + '.' + snapshot . version ] ) { var err = new Error ( 'No snapshot conversion defined!' ) ; debug ( err ) ; throw err ; } debug ( 'convert snapshot from history' ) ; this . snapshotConversions [ snapshot . context + '.' + snapshot . aggregate + '.' + snapshot . version ] ( snapshot . data , aggregateModel ) ; isSnapshotNeeded = true ; } aggregateModel . setRevision ( { context : stream . context , aggregate : stream . aggregate , aggregateId : stream . aggregateId } , snapshot . revision + 1 ) ; } if ( events && events . length > 0 ) { // load events debug ( 'load events from history' ) ; var maxRevision = _ . reduce ( events , function ( res , evt ) { var rev = dotty . get ( evt , self . definitions . event . revision ) ; if ( rev > res ) { return rev ; } return res ; } , 0 ) ; if ( ! this . skipHistory || ( this . skipHistory && this . applyLastEvent ) ) { this . apply ( events , aggregateModel , notSameOrigin ) ; } aggregateModel . setRevision ( { context : stream . context , aggregate : stream . aggregate , aggregateId : stream . aggregateId } , maxRevision ) ; if ( ! isSnapshotNeeded ) { if ( this . isSnapshotNeeded . length > 2 ) { isSnapshotNeeded = this . isSnapshotNeeded ( loadingTime , events , aggregateModel . toJSON ( ) , streams ) ; } else { isSnapshotNeeded = this . isSnapshotNeeded ( loadingTime , events ) ; } } } return isSnapshotNeeded ; }
Loads the aggregateModel with the data of the snapshot and the events.
And returns true if a new snapshot should be done.
@param {AggregateModel}  aggregateModel The aggregateModel that should be used.
@param {Object}          snapshot       The snapshot object.
@param {Array}           events         The events that should be applied.
@param {Number}          loadingTime    The loading time in ms of the eventstore data.
@param {Object}          stream         The eventstream.
@param {Array}           streams        All loaded eventstreams for this aggregate.
@param {boolean}         notSameOrigin  If true it's an indication to not throw if event handler not found.
@returns {boolean}
Loads the aggregateModel with the data of the snapshot and the events . And returns true if a new snapshot should be done .