 # There is a corresponding implementation run by clients.
 #---------------------------------------------------------------------
 
+###
+x={};require('bup_server').global_client(cb:(e,c)->x.c=c; x.s = require('syncstring'); x.s.init_syncstring_db(x.c.database); x.ss=x.s.syncdb(string_id:'4bd8cb98-506c-45a5-8042-5c6bd8fddff1', cb:(e,t)->x.t=t))
+###
 _syncdb_cache = {}
 exports.syncdb = (opts) ->
     opts = defaults opts,
         string_id      : required
+        max_length     : MAX_STRING_LENGTH
         cb             : required
     d = _syncdb_cache[opts.string_id]
     if d?
         opts.cb(undefined, d)
         return
-    get_syncstring opts.string_id, (err, S) =>
-        if err
-            opts.cb(err)
-        else
-            doc = new diffsync.SynchronizedDB_DiffSyncWrapper(S.in_memory_client)
-            S.db_client.on 'changed', () =>
-                doc.emit("sync")
-            d = _syncdb_cache[opts.string_id] = new diffsync.SynchronizedDB(doc)
-            d.string_id = opts.string_id
-            opts.cb(undefined, d)
-
-
+    get_syncstring
+        string_id  : opts.string_id
+        max_length : opts.max_length
+        cb         : (err, S) =>
+            if err
+                opts.cb(err)
+            else
+                doc = new diffsync.SynchronizedDB_DiffSyncWrapper(S.in_memory_client)
+                S.db_client.on 'changed', () =>
+                    doc.emit("sync")
+                d = _syncdb_cache[opts.string_id] = new diffsync.SynchronizedDB(doc)
+                d.string_id = opts.string_id
+                opts.cb(undefined, d)
+
+

            else
                cb()
        async.map v, f, () =>
            if misc.len(successful_live) > 1 # if not stable (with ones with no err), do again.
                #winston.debug("syncing again")
                @_sync(cb)
            else
                #winston.debug("not syncing again since successful_live='#{misc.to_json(successful_live)}'")
                cb?()

sync_strings = {}

exports.get_syncstring = get_syncstring = (string_id, cb) ->
    S = sync_strings[string_id]
    if S?
        cb(undefined, S); return

    S = new SynchronizedString(string_id)
    async.series([
        (cb) =>
            S.new_database_client

        @push_edits_to_browser(undefined, cb)

# A string that is synchronized amongst all connected clients.
# IMPORTANT: anything in the string after @max_size
# will be automatically truncated -- i.e., if any operation results
# in the string getting longer than that, it is truncated at the end.  It is
# the responsibility of the client code to properly deal with this, e.g.,
# by putting a sentinel character at the end of the string and checking for
# it to see if the string was truncated as a result of a sync.

# x={};require('bup_server').global_client(cb:(e,c)->x.c=c; x.s = require('syncstring'); x.s.init_syncstring_db(x.c.database); x.ss=x.s.get_syncstring_db(); x.s.get_syncstring(string_id:'4bd8cb98-506c-45a5-8042-5c6bd8fddff0',max_length:50,cb:(e,t)->x.t=t))
class SynchronizedString
    constructor: (@string_id, @max_length) ->
        misc.call_lock(obj:@)
        @clients = {}
        @head = ''
        if not @max_length?
            @max_length = MAX_STRING_LENGTH
        else
            # no matter what, we never allow syncstrings to exceed the MAX_STRING_SIZE
            @max_length = Math.min(@max_length, MAX_STRING_LENGTH)

    new_browser_client: (opts) =>
        opts = defaults opts,
