diff --git a/bibimbap.js b/bibimbap.js index 5b76502..b37d1ae 100644 --- a/bibimbap.js +++ b/bibimbap.js @@ -1,6 +1,5 @@ 'use strict'; -var emitter = require('component-emitter'); var assign = require('object-assign'); var flatten = require('array-flatten'); var isPlainObject = require('is-plain-object'); @@ -12,10 +11,37 @@ module.exports = Bibimbap; */ function Bibimbap(tree) { if (!(this instanceof Bibimbap)) return new Bibimbap(tree); - emitter(this); this.tree = tree; + this.listeners = []; } +/** + * Subscribe to state changes + */ +Bibimbap.prototype.subscribe = function(listener) { + this.listeners.push(listener); + var isSubscribed = true; + + return function unsubscribe() { + if (!isSubscribed) { + return; + } + + isSubscribed = false; + var index = this.listeners.indexOf(listener); + this.listeners.splice(index, 1); + } +}; + +/** + * Publish a state change + */ +Bibimbap.prototype.publishCommit = function(tree, prev, cursor) { + this.listeners.slice().forEach(function(listener) { + listener(tree, prev, cursor); + }); +}; + /** * Commit a cursor */ @@ -29,7 +55,7 @@ Bibimbap.prototype.commit = function(cursor) { // applied to the tree cursor.operations = []; - this.emit('commit', this.tree, prev, cursor); + this.publishCommit(this.tree, prev, cursor); }; function runOperations(tree, operations) { diff --git a/package.json b/package.json index 77e6dd7..370bc16 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "author": "", "license": "MIT", "dependencies": { - "component-emitter": "^1.2.0", "array-flatten": "^2.0.0", "object-assign": "^4.0.1", "is-plain-object": "^2.0.1" diff --git a/test/bibimbap.js b/test/bibimbap.js index e9449f8..b262fe0 100644 --- a/test/bibimbap.js +++ b/test/bibimbap.js @@ -143,7 +143,7 @@ describe('Bibimbap', function() { var state = new Bibimbap({ test: 'original' }); - state.on('commit', function(tree, prev, cursor) { + state.subscribe(function(tree, prev, cursor) { assert.equal('hello', tree.test); assert.equal('hello', cursor.get()); assert.equal('original', prev.test); @@ -156,7 +156,7 @@ describe('Bibimbap', function() { var state = new Bibimbap({ test: 'original' }); - state.on('commit', function(tree, prev, cursor) { + state.subscribe(function(tree, prev, cursor) { assert.equal('second', tree.test); assert.equal('second', cursor.get()); assert.equal('original', prev.test);