Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Made my own version of microevent in coffee. doc.subscribe -> doc.on …
…like in node.
  • Loading branch information
josephg committed Apr 12, 2011
1 parent 982bc64 commit 6eebcbe
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 350 deletions.
12 changes: 5 additions & 7 deletions Cakefile
Expand Up @@ -3,34 +3,32 @@
task 'test', 'Run all tests', ->
require './tests'

#option '-w', '--watch', 'Watch'

task 'build', 'Build the .js files', (options) ->
# console.log options
# options.watch ||= no
# exec "coffee --compile #{if options.watch then '--watch' else ''} --output lib/ src/", (err, stdout, stderr) ->
exec "coffee --compile --bare --output lib/ src/", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr

lib = [
'thirdparty/microevent.js/microevent.js'
]

client = [
'client/microevent'
'types/text'
'client/opstream'
'client/client'
]

# Backticks
e = (str, callback) ->
exec str, (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
out = stdout + stderr
console.log out if out != ''
callback() if callback?

task 'webclient', 'Assemble the web client into one file', ->
clientfiles = ("src/#{c}.coffee" for c in client).join ' '
# I would really rather do this in pure JS.
e "coffee -cj #{clientfiles}", ->
e "cat #{lib.join ' '} concatenation.js >share.js", ->
e 'rm concatenation.js'
2 changes: 1 addition & 1 deletion examples/_wiki/wiki.html.mu
Expand Up @@ -46,7 +46,7 @@
window.doc = doc;

render();
doc.subscribe('change', render);
doc.on('change', render);
});
};
</script>
Expand Down
4 changes: 2 additions & 2 deletions examples/lib/ace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/readonly/html.html
Expand Up @@ -23,7 +23,8 @@
}

update();
doc.subscribe('change', update);
window.doc = doc;
doc.on('change', update);


if (doc.created) {
Expand Down
2 changes: 1 addition & 1 deletion examples/readonly/markdown.html
Expand Up @@ -30,7 +30,7 @@
}

update();
doc.subscribe('change', update);
doc.on('change', update);

if (doc.created) {
doc.submitOp([{i:"A heading\n=========\n\nLets talk about code:\n\n codey(codey).code;\n", p:0}]);
Expand Down
117 changes: 57 additions & 60 deletions share.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/client/ace.coffee
Expand Up @@ -104,10 +104,10 @@ window.sharejs.Document::attach_ace = (editor) ->

check()

doc.subscribe 'remoteop', docListener
doc.on 'remoteop', docListener

doc.detach_ace = ->
doc.unsubscribe 'remoteop', docListener
doc.removeListener 'remoteop', docListener
editorDoc.removeListener 'change', editorListener
delete doc.detach_ace

Expand Down
17 changes: 10 additions & 7 deletions src/client/client.coffee
@@ -1,8 +1,11 @@
# Abstraction over raw net stream, for use by a client.

OpStream = window?.sharejs.OpStream || require('./opstream').OpStream
types = window?.sharejs.types || require('../types')
MicroEvent = window?.MicroEvent || require '../../thirdparty/microevent.js/microevent'
if window?
types ||= window.sharejs.types
else
OpStream = require('./opstream').OpStream
types = require('../types')
MicroEvent = require './microevent'

exports ||= {}

Expand All @@ -11,7 +14,7 @@ i = -> #require('util').inspect

# An open document.
#
# Documents are event emitters - use doc.subscribe(eventname, fn) to subscribe.
# Documents are event emitters - use doc.on(eventname, fn) to subscribe.
#
# Events:
# - remoteop (op)
Expand Down Expand Up @@ -118,8 +121,8 @@ class Document
@snapshot = @type.apply @snapshot, docOp
@version++

@publish 'remoteop', docOp
@publish 'change', docOp
@emit 'remoteop', docOp
@emit 'change', docOp

# Submit an op to the server. The op maybe held for a little while before being sent, as only one
# op can be inflight at any time.
Expand All @@ -146,7 +149,7 @@ class Document

@pendingCallbacks.push callback if callback?

@publish 'change', op
@emit 'change', op

# A timeout is used so if the user sends multiple ops at the same time, they'll be composed
# together and sent together.
Expand Down
32 changes: 32 additions & 0 deletions src/client/microevent.coffee
@@ -0,0 +1,32 @@
# This is a simple port of microevent.js to Coffeescript. I've changed the
# function names to be consistent with node.js EventEmitter.
#
# microevent.js is copyright Jerome Etienne, and licensed under the MIT license:
# https://github.com/jeromeetienne/microevent.js

class MicroEvent
on: (event, fct) ->
@_events ||= {}
@_events[event] ||= []
@_events[event].push(fct)
this

removeListener: (event, fct) ->
@_events ||= {}
idx = @_events[event]?.indexOf fct
@_events[event].splice(idx, 1) if idx? and idx >= 0
this

emit: (event, args...) ->
return this unless @_events?[event]
fn.apply this, args for fn in @_events[event]
this

# mixin will delegate all MicroEvent.js function in the destination object
MicroEvent.mixin = (obj) ->
proto = obj.prototype || obj
proto[fname] = MicroEvent.prototype[fname] for fname in ['on', 'removeListener', 'emit']
obj

module.exports = MicroEvent if module?.exports

14 changes: 7 additions & 7 deletions test/client.coffee
Expand Up @@ -135,7 +135,7 @@ module.exports = testCase {
test.ifError error
test.strictEqual doc.name, @name

doc.subscribe 'remoteop', (op) ->
doc.on 'remoteop', (op) ->
test.deepEqual op, [{i:'hi', p:0}]

test.expect 4
Expand Down Expand Up @@ -184,7 +184,7 @@ module.exports = testCase {
test.done()

doc.submitOp clientOp, onOpApplied
doc.subscribe 'remoteop', (op) ->
doc.on 'remoteop', (op) ->
test.deepEqual op, serverTransformed
onOpApplied()

Expand All @@ -195,10 +195,10 @@ module.exports = testCase {
passPart = makePassPart test, 2
@c.open @name, 'text', (doc, error) =>
sentOp = [{i:'asdf', p:0}]
doc.subscribe 'change', (op) ->
doc.on 'change', (op) ->
test.deepEqual op, sentOp
passPart()
doc.subscribe 'remoteop', (op) ->
doc.on 'remoteop', (op) ->
test.deepEqual op, sentOp
passPart()

Expand All @@ -209,18 +209,18 @@ module.exports = testCase {
passPart = makePassPart test, 2
@c.open @name, 'text', (doc, error) ->
sentOp = [{i:'asdf', p:0}]
doc.subscribe 'change', (op) ->
doc.on 'change', (op) ->
test.deepEqual op, sentOp
passPart()
doc.subscribe 'remoteop', (op) ->
doc.on 'remoteop', (op) ->
throw new Error 'Should not have received remoteOp event'

doc.submitOp sentOp, (error, v) ->
passPart()

'doc does not receive ops after unfollow called': (test) ->
@c.open @name, 'text', (doc, error) =>
doc.subscribe 'change', (op) ->
doc.on 'change', (op) ->
throw new Error 'Should not have received op when the doc was unfollowed'

doc.unfollow =>
Expand Down
6 changes: 3 additions & 3 deletions test/integration.coffee
Expand Up @@ -44,7 +44,7 @@ module.exports = testCase {
[submittedOp, result] = @doc1.type.generateRandomOp @doc1.snapshot
@doc1.submitOp submittedOp

@doc2.subscribe 'remoteop', (op) =>
@doc2.on 'remoteop', (op) =>
test.deepEqual op, submittedOp
test.strictEqual @doc2.snapshot, result
test.strictEqual @doc2.version, 2
Expand Down Expand Up @@ -84,10 +84,10 @@ module.exports = testCase {
inflight--
checkSync()

@doc1.subscribe 'remoteop', (op) =>
@doc1.on 'remoteop', (op) =>
maxV = Math.max(maxV, @doc1.version)
checkSync()
@doc2.subscribe 'remoteop', (op) =>
@doc2.on 'remoteop', (op) =>
maxV = Math.max(maxV, @doc2.version)
checkSync()

Expand Down

0 comments on commit 6eebcbe

Please sign in to comment.