forked from josephg/ShareJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharefile.coffee
36 lines (28 loc) · 936 Bytes
/
sharefile.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
# This script watches for changes in a document and constantly resaves a file
# with the document's contents.
#client = require('share').client
client = require('../src').client
fs = require('fs')
argv = require('optimist')
.usage('Usage: $0 -d docname [--url URL] [-f filename]')
.default('d', 'hello')
.default('url', 'http://localhost:8000/channel')
.argv
filename = argv.f || argv.d
console.log "Opening '#{argv.d}' at #{argv.url}. Saving to '#{filename}'"
timeout = null
doc = null
# Writes the snapshot data to the file not more than once per second.
write = ->
if (timeout == null)
timeout = setTimeout ->
console.log "Saved version " + doc.version
fs.writeFile filename, doc.snapshot
timeout = null
, 1000
client.open argv.d, 'text', argv.url, (d, error) ->
doc = d
console.log('Document ' + argv.d + ' open at version ' + doc.version)
write()
doc.on 'change', (op) ->
write()