/
index.coffee
81 lines (65 loc) · 2.34 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# This file implements the sharejs client, as defined here:
# https://github.com/josephg/ShareJS/wiki/Client-API
#
# It works from both a node.js context and a web context (though in the latter case,
# it needs to be compiled to work.)
#
# It should become a little nicer once I start using more of the new RPC features added
# in socket.io 0.7.
#
# Note that anything declared in the global scope here is shared with other files
# built by closure. Be careful what you put in this namespace.
if WEB?
hasBCSocket = window.BCSocket isnt undefined
hasSockJS = window.SockJS isnt undefined
throw new Error 'Must load socks or browserchannel before this library' unless hasBCSocket or hasSockJS
useSockJS = hasSockJS and !hasBCSocket
else
Connection = require('./connection').Connection
# Open a document with the given name. The connection is created implicitly and reused.
#
# This function uses a local (private) set of connections to support .open().
#
# Open returns the connection its using to access the document.
exports.open = do ->
# This is a private connection pool for implicitly created connections.
connections = {}
getConnection = (origin) ->
if WEB?
location = window.location
# default to browserchannel
path = if useSockJS then 'sockjs' else 'channel'
origin ?= "#{location.protocol}//#{location.host}/#{path}"
unless connections[origin]
c = new Connection origin
del = -> delete connections[origin]
c.on 'disconnecting', del
c.on 'connect failed', del
connections[origin] = c
connections[origin]
# If you're using the bare API, connections are cleaned up as soon as there's no
# documents using them.
maybeClose = (c) ->
numDocs = 0
for name, doc of c.docs
numDocs++ if doc.state isnt 'closed' || doc.autoOpen
if numDocs == 0
c.disconnect()
(docName, type, origin, callback) ->
if typeof origin == 'function'
callback = origin
origin = null
c = getConnection origin
c.numDocs++
c.open docName, type, (error, doc) ->
if error
callback error
maybeClose c
else
doc.on 'closed', -> maybeClose c
callback null, doc
c.on 'connect failed'
return c
unless WEB?
exports.Doc = require('./doc').Doc
exports.Connection = require('./connection').Connection