Skip to content

Commit

Permalink
use standard node.js EventEmitter class
Browse files Browse the repository at this point in the history
  • Loading branch information
nateps committed May 4, 2015
1 parent f2efd28 commit 9e34de2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 219 deletions.
6 changes: 4 additions & 2 deletions lib/client/connection.js
@@ -1,6 +1,6 @@
var Doc = require('./doc').Doc;
var Query = require('./query').Query;
var MicroEvent = require('./microevent');
var emitter = require('./emitter');


/**
Expand All @@ -19,6 +19,8 @@ var MicroEvent = require('./microevent');
* @param socket @see bindToSocket
*/
var Connection = exports.Connection = function (socket) {
emitter.EventEmitter.call(this);

// Map of collection -> docName -> doc object for created documents.
// (created documents MUST BE UNIQUE)
this.collections = {};
Expand Down Expand Up @@ -59,7 +61,7 @@ var Connection = exports.Connection = function (socket) {

this.bindToSocket(socket);
}
MicroEvent.mixin(Connection);
emitter.mixin(Connection);


/**
Expand Down
7 changes: 4 additions & 3 deletions lib/client/doc.js
@@ -1,5 +1,5 @@
var types = require('../types').ottypes;
var MicroEvent = require('./microevent');
var emitter = require('./emitter');

/**
* A Doc is a client's view on a sharejs document.
Expand Down Expand Up @@ -74,6 +74,8 @@ var MicroEvent = require('./microevent');
* TODO rename `op` to `after partial op`
*/
var Doc = exports.Doc = function(connection, collection, name) {
emitter.EventEmitter.call(this);

this.connection = connection;

this.collection = collection;
Expand Down Expand Up @@ -139,8 +141,7 @@ var Doc = exports.Doc = function(connection, collection, name) {
// The document also responds to the api provided by the type
this.type = null
};

MicroEvent.mixin(Doc);
emitter.mixin(Doc);

/**
* Unsubscribe and remove all editing contexts
Expand Down
10 changes: 10 additions & 0 deletions lib/client/emitter.js
@@ -0,0 +1,10 @@
var EventEmitter = require('events').EventEmitter;

exports.EventEmitter = EventEmitter;
exports.mixin = mixin;

function mixin(Constructor) {
for (var key in EventEmitter.prototype) {
Constructor.prototype[key] = EventEmitter.prototype[key];
}
}
78 changes: 0 additions & 78 deletions lib/client/microevent.js

This file was deleted.

11 changes: 5 additions & 6 deletions lib/client/query.js
@@ -1,11 +1,12 @@
var Doc = require('./doc').Doc;
var MicroEvent = require('./microevent');
var emitter = require('./emitter');

// Queries are live requests to the database for particular sets of fields.
//
// The server actively tells the client when there's new data that matches
// a set of conditions.
var Query = exports.Query = function(type, connection, id, collection, query, options, callback) {
emitter.EventEmitter.call(this);

// 'fetch' or 'sub'
this.type = type;

Expand Down Expand Up @@ -47,6 +48,8 @@ var Query = exports.Query = function(type, connection, id, collection, query, op

this.callback = callback;
};
emitter.mixin(Query);

Query.prototype.action = 'qsub';

// Helper for subscribe & fetch, since they share the same message format.
Expand Down Expand Up @@ -216,7 +219,3 @@ Query.prototype.setQuery = function(q) {
this._execute();
}
};


MicroEvent.mixin(Query);

4 changes: 2 additions & 2 deletions test/server/json-api.coffee
@@ -1,7 +1,7 @@
assert = require("assert")
json = require('ot-json0').type
require("../../lib/types/json-api")
MicroEvent = require("../../lib/client/microevent")
emitter = require('../../lib/client/emitter');

# in the future, it would be less brittle to use the real Doc object instead of this fake one
Doc = (data) ->
Expand Down Expand Up @@ -52,7 +52,7 @@ Doc = (data) ->

this

MicroEvent.mixin Doc
emitter.mixin Doc

apply = (cxt,op) ->
cxt._beforeOp? op
Expand Down
128 changes: 0 additions & 128 deletions test/server/microevent.coffee

This file was deleted.

0 comments on commit 9e34de2

Please sign in to comment.