Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace history.invoke with event emissions #293

Merged
merged 1 commit into from Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/emitter.js
Expand Up @@ -100,5 +100,23 @@ Emitter.prototype = {
}

return this
},

/**
* Remove all events for a given scope
*/
_removeScope (scope) {
let i = 0
while (i < this._events.length) {
var cb = this._events[i]

if (scope === cb.scope) {
this._events.splice(i, 1)
continue
}

i += 1
}
}

}
37 changes: 8 additions & 29 deletions src/history.js
Expand Up @@ -21,7 +21,6 @@ export default function History (limit) {

this.size = 0
this.limit = Math.max(1, limit || 1)
this.repos = []

this.begin()
}
Expand Down Expand Up @@ -122,27 +121,6 @@ inherit(History, Emitter, {
this.append(START, 'resolve')
},

addRepo (repo) {
this.repos.push(repo)
},

removeRepo (repo) {
let index = this.repos.indexOf(repo)

if (~index) {
this.repos.splice(index, 1)
}
},

invoke (method, payload) {
let repos = this.repos

for (var i = 0; i < repos.length; i++) {
console.assert(repos[i], `Missing repo! Was it removed before it could run repo.${method}?`)
repos[i][method](payload)
}
},

append (command, status) {
let action = new Action(command, status)

Expand All @@ -159,12 +137,10 @@ inherit(History, Emitter, {
this.head = action
this.size += 1

this.invoke('createInitialSnapshot', action)
this._emit('append', action)

action.on('change', this.reconcile, this)

this._emit('append', action)

return this.head
},

Expand Down Expand Up @@ -204,7 +180,8 @@ inherit(History, Emitter, {
*/
clean (action) {
this.size -= 1
this.invoke('removeSnapshot', action)

this._emit('remove', action)

action.remove()
},
Expand All @@ -216,7 +193,7 @@ inherit(History, Emitter, {
let focus = action

while (focus) {
this.invoke('reconcile', focus)
this._emit('update', focus)

if (focus === this.head) {
break
Expand All @@ -227,7 +204,9 @@ inherit(History, Emitter, {

this.archive()

this.invoke('release', action)
this._emit('reconcile', action)

this._emit('release')
},

archive () {
Expand All @@ -236,7 +215,7 @@ inherit(History, Emitter, {

while (size > this.limit && root.complete) {
size -= 1
this.invoke('removeSnapshot', root.parent)
this._emit('remove', root.parent)
root = root.next
}

Expand Down
58 changes: 37 additions & 21 deletions src/microcosm.js
Expand Up @@ -31,16 +31,33 @@ function Microcosm (preOptions, state, deserialize) {
this.parent = options.parent

this.history = this.parent ? this.parent.history : new History(options.maxHistory)
this.history.addRepo(this)

this.archive = new Archive()
this.domains = new DomainEngine(this)
this.effects = new EffectEngine(this)
this.changes = new CompareTree(this.state)
this.changes = new CompareTree()

this.initial = this.parent ? this.parent.initial : {}
this.state = this.parent ? this.parent.state : this.initial

// History moves through a set lifecycle. As that lifecycle occurs,
// save snapshots of new state:

// When an action is first created
this.history.on('append', this.createSnapshot, this)

// When an action snapshot needs updating
this.history.on('update', this.updateSnapshot, this)

// When an action snapshot should be removed
this.history.on('remove', this.removeSnapshot, this)

// When an action changes, it causes a reconcilation
this.history.on('reconcile', this.dispatchEffect, this)

// A history is done reconciling and is ready for a release
this.history.on('release', this.release, this)

// Microcosm is now ready. Call the setup lifecycle method
this.setup(options)

Expand Down Expand Up @@ -83,27 +100,15 @@ inherit(Microcosm, Emitter, {
* that, when rolling back to this action, it always has a state value.
* @param {Action} action - The action to generate a snapshot for
*/
createInitialSnapshot (action) {
createSnapshot (action) {
this.archive.create(action)
},

/**
* Update the state snapshot for a given action
* @param {Action} action - The action to update the snapshot for
*/
updateSnapshot (action, state) {
this.archive.set(action, state)
},

/**
* Remove the snapshot for a given action
* @param {Action} action - The action to remove the snapshot for
*/
removeSnapshot (action) {
this.archive.remove(action)
},

reconcile (action) {
updateSnapshot (action) {
let next = this.recall(action.parent, this.initial)

if (this.parent) {
Expand All @@ -114,16 +119,27 @@ inherit(Microcosm, Emitter, {
next = this.domains.dispatch(next, action)
}

this.updateSnapshot(action, next)
this.archive.set(action, next)

this.state = next
},

release (action) {
this.changes.update(this.state)
/**
* Remove the snapshot for a given action
* @param {Action} action - The action to remove the snapshot for
*/
removeSnapshot (action) {
this.archive.remove(action)
},

dispatchEffect (action) {
this.effects.dispatch(action)
},

release () {
this.changes.update(this.state)
},

on (type, callback, scope) {
let [event, meta=''] = type.split(':', 2)

Expand Down Expand Up @@ -253,8 +269,8 @@ inherit(Microcosm, Emitter, {
// Trigger a teardown event before completely shutting down
this._emit('teardown', this)

// Remove this repo from history
this.history.removeRepo(this)
// Stop tracking history
this.history._removeScope(this)

// Remove all listeners
this.removeAllListeners()
Expand Down
23 changes: 0 additions & 23 deletions test/unit/history/invoke.test.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/unit/history/removeRepo.test.js

This file was deleted.