Skip to content

Commit

Permalink
feat: entity.state() returns clone of the entity state
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Lee Scott <[email protected]>
  • Loading branch information
patrickleet committed Aug 24, 2021
1 parent 5d3a1f2 commit ce2504f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ constructor to give a more descriptive error.
| --- | --- | --- |
| events | <code>Array</code> | an array of events to be replayed. |

<a name="Entity+state"></a>

### entity.state() ⇒ <code>Object</code>
Return a clone of current state of the entity instance without event sourcing properties. Does not modify anything.

**Kind**: instance method of <code>[Entity](#Entity)</code>

<a name="Entity+snapshot"></a>

### entity.snapshot() ⇒ <code>Object</code>
Expand Down
14 changes: 14 additions & 0 deletions src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ export class Entity extends EventEmitter {
this.replaying = false
}

/**
* 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() {
const state = cloneDeep(this, true)
return this.trimSnapshot(state)
}

/**
* Create a snapshot of the current state of the Entity instance.
*
Expand Down

0 comments on commit ce2504f

Please sign in to comment.