import EventEmitter from "eventemitter3"; /** * Creates an event-sourced Entity. * * @class {Function} Entity * @param {Object} [snapshot] A previously saved snapshot of an Entity. * @param {Array} [events] An array of events to apply on instantiation. * @requires eventemitter3 * @requires debug * @requires lodash.clonedeep * @license MIT */ export declare class Entity extends EventEmitter { eventsToEmit: any[]; newEvents: any[]; replaying: boolean; snapshotVersion: number; timestamp: number; version: number; constructor(); /** * Rehydrates by merging a snapshot, and replaying events on top. */ rehydrate(snapshot: any, events: any[]): void; /** * Wrapper around the EventEmitter.emit method that adds a condition so events * are not fired during replay. */ emit(event: string | symbol, ...args: any[]): boolean; /** * Add events to the queue of events to emit. If called during replay, this * method does nothing. */ enqueue(...args: any[]): void; /** * Digest a command with given data.This is called whenever you want to record * a command into the events for the Entity. If called during replay, this * method does nothing. * * @param {String} method the name of the method/command you want to digest. * @param {Object} data the data that should be passed to the replay. */ digest(method: string, data: any): void; /** * Merge a snapshot onto the Entity. * * For every property passed in the snapshot, the value is deep-cloned and then * merged into the instance through mergeProperty. See mergeProperty for details. * * @param {Object} snapshot snapshot object. * @see Entity.mergeProperty */ merge(snapshot: any): this; /** * Merge a property onto the instance. * * Given a name and a value, mergeProperty checks first attempt to find the * property in the mergeProperties map using the constructor name as key. If it * is found and it is a function, the function is called. If it is NOT found * we check if the property is an object. If so, we merge. If not, we simply * assign the passed value to the instance. * * @param {String} name the name of the property being merged. * @param {Object} value the value of the property being merged. * @see mergeProperties * @see Entity.mergeProperty */ mergeProperty(name: string, value: any): any; /** * Replay an array of events onto the instance. * * The goal here is to allow application of events without emitting, enqueueing * nor digesting the replayed events. This is done by setting this.replaying to * true which emit, enqueue and digest check for. * * If the method in the event being replayed exists in the instance, we call * the mathod with the data in the event and set the version of the instance to * the version of the event. If the method is not found, we attempt to parse the * constructor to give a more descriptive error. * * @param {Array} events an array of events to be replayed. */ replay(events: any): void; /** * Returns a clone of the entity's current state without the event sourcing properties * * Here the instance's snapshotVersion property is set to the current version, * then the instance is deep-cloned and the clone is trimmed of the internal * sourced attributes using trimSnapshot and returned. * * @returns {Object} */ state(): Omit; /** * Create a snapshot of the current state of the entity instance. * * Here the instance's snapshotVersion property is set to the current version, * then the instance is deep-cloned and the clone is trimmed of the internal * sourced attributes using trimSnapshot and returned. * * @returns {Object} */ snapshot(): Omit; /** * Remove the internal sourced properties from the passed snapshot. * * Snapshots are to contain only entity data properties. This trims all other * properties from the snapshot. * * @param {Object} snapshot the snapshot to be trimmed. * @see Entity.prototype.snapshot */ trimSnapshot(snapshot: any): Omit; /** * Helper function to automatically create a method that calls digest on the * param provided. Use it to add methods that automatically call digest. * * @param {Object} type the entity class to which the method will be added. * @param {Function} fn the actual function to be added. * @example * * Entity.digestMethod(Car, function clearSettings (param) { * * const self = this; * * this.settings.get(param.name).forEach((name, config) => { * * config.sources.forEach((source) => { * * source.remove(); * * }); * * }); * * return this.settings; * * }); * */ static digestMethod(type: any, fn: any): void; /** * Convenience function to store references to functions that should be run * when merging a particular property. * * @param {Object} type the entity class to which the property->fn belongs to. * @param {String} name the name of the property that holds the fn. * @param {Function} fn the function to execute when merging the property. * @see mergeProperties * @example * function Wheel (status) { * this.status = status; * } * * Wheel.prototype.go = function () { * this.status = 'going'; * } * * function Car () { * this.id = null; * this.wheel = new Wheel(); // for instantiating our default wheel, when we first 'new' up a Car * * Entity.apply(this, arguments); * } * * util.inherits(Car, Entity); * * Entity.mergeProperty(Car, 'wheels', function (obj) { * this.wheel = new Wheel(); // for instantiating our wheel from saved values in a database * }); */ static mergeProperty(type: any, name: any, fn: any): void; }