-
Notifications
You must be signed in to change notification settings - Fork 17
/
browser-couch.js
962 lines (817 loc) · 26.2 KB
/
browser-couch.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ubiquity.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Atul Varma <[email protected]>
* Peter Braden <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// = BrowserCouch =
//
// BrowserCouch is a client side map-reduce data store, inspired by CouchDB. It
// utilizes the browser's local storage where possible, and syncs to a CouchDB
// server.
//
var BrowserCouch = function(opts){
var bc = {};
// == Utility Functions ==
// === {{{isArray()}}} ===
//
// A helper function to determine whether an object is an Array or
// not. Taken from jQuery
var isArray = function(value) {
return Object.prototype.toString.call(value) === "[object Array]";
}
// === {{{ModuleLoader}}} ===
//
// A really basic module loader that allows dependencies to be
// "lazy-loaded" when their functionality is needed.
bc.ModuleLoader = {
LIBS: {JSON: "js/ext/json2.js",
UUID: "js/ext/uuid.js"},
require: function ML_require(libs, cb) {
var self = this,
i = 0,
lastLib = "";
if (!isArray(libs)){
libs = [libs];
}
function loadNextLib() {
if (lastLib && !window[lastLib]){
throw new Error("Failed to load library: " + lastLib);
}
if (i == libs.length){
cb();
}
else {
var libName = libs[i];
i += 1;
if (window[libName]){
loadNextLib();
}
else {
var libUrl = self.LIBS[libName];
if (!libUrl){
throw new Error("Unknown lib: " + libName);
}
lastLib = libName;
self._loadScript(libUrl, window, loadNextLib);
}
}
}
loadNextLib();
},
_loadScript: function ML__loadScript(url, window, cb) {
var doc = window.document;
var script = doc.createElement("script");
script.setAttribute("src", url);
script.addEventListener(
"load",
function onLoad() {
script.removeEventListener("load", onLoad, false);
cb();
},
false
);
doc.body.appendChild(script);
}
};
// == MapReducer Implementations ==
//
// //MapReducer// is a generic interface for any map-reduce
// implementation. Any object implementing this interface will need
// to be able to work asynchronously, passing back control to the
// client at a given interval, so that the client has the ability to
// pause/cancel or report progress on the calculation if needed.
// === {{{WebWorkerMapReducer}}} ===
//
// A MapReducer that uses
// [[https://developer.mozilla.org/En/Using_DOM_workers|Web Workers]]
// for its implementation, allowing the client to take advantage of
// multiple processor cores and potentially decouple the map-reduce
// calculation from the user interface.
//
// The script run by spawned Web Workers is
// [[#js/worker-map-reducer.js|worker-map-reducer.js]].
bc.WebWorkerMapReducer = function WebWorkerMapReducer(numWorkers, Worker) {
if (!Worker){
Worker = window.Worker;
}
var pool = [];
function MapWorker(id) {
var worker = new Worker('js/worker-map-reducer.js');
var onDone;
worker.onmessage = function(event) {
onDone(event.data);
};
this.id = id;
this.map = function MW_map(map, dict, cb) {
onDone = cb;
worker.postMessage({map: map.toString(), dict: dict});
};
}
for (var i = 0; i < numWorkers; i++){
pool.push(new MapWorker(i));
}
this.map = function WWMR_map(map, dict, progress, chunkSize, finished) {
var keys = dict.getKeys();
var size = keys.length;
var workersDone = 0;
var mapDict = {};
function getNextChunk() {
if (keys.length) {
var chunkKeys = keys.slice(0, chunkSize);
keys = keys.slice(chunkSize);
var chunk = {};
for (var i = 0; i < chunkKeys.length; i++){
chunk[chunkKeys[i]] = dict.get(chunkKeys[i]);
}
return chunk;
} else {
return null;
}
}
function nextJob(mapWorker) {
var chunk = getNextChunk();
if (chunk) {
mapWorker.map(
map,
chunk,
function jobDone(aMapDict) {
for (var name in aMapDict){
if (name in mapDict) {
var item = mapDict[name];
item.keys = item.keys.concat(aMapDict[name].keys);
item.values = item.values.concat(aMapDict[name].values);
} else{
mapDict[name] = aMapDict[name];
}
}
if (keys.length){
progress("map",
(size - keys.length) / size,
function() { nextJob(mapWorker); });
}else{
workerDone();
}
});
} else{
workerDone();
}
}
function workerDone() {
workersDone += 1;
if (workersDone == numWorkers){
allWorkersDone();
}
}
function allWorkersDone() {
var mapKeys = [];
for (var name in mapDict){
mapKeys.push(name);
}
mapKeys.sort();
finished({dict: mapDict, keys: mapKeys});
}
for (var i = 0; i < numWorkers; i++){
nextJob(pool[i]);
}
};
// TODO: Actually implement our own reduce() method here instead
// of delegating to the single-threaded version.
this.reduce = bc.SingleThreadedMapReducer.reduce;
};
// === {{{SingleThreadedMapReducer}}} ===
//
// A MapReducer that works on the current thread.
bc.SingleThreadedMapReducer = {
map: function STMR_map(map, dict, progress,
chunkSize, finished) {
var mapDict = {};
var keys = dict.getKeys();
var currDoc;
function emit(key, value) {
// TODO: This assumes that the key will always be
// an indexable value. We may have to hash the value,
// though, if it's e.g. an Object.
var item = mapDict[key];
if (!item){
item = mapDict[key] = {keys: [], values: []};
}
item.keys.push(currDoc._id);
item.values.push(value);
}
var i = 0;
function continueMap() {
var iAtStart = i;
do {
currDoc = dict.get(keys[i]);
map(currDoc, emit);
i++;
} while (i - iAtStart < chunkSize &&
i < keys.length);
if (i >= keys.length) {
var mapKeys = [];
for (name in mapDict)
mapKeys.push(name);
mapKeys.sort();
finished({dict: mapDict, keys: mapKeys});
} else
progress("map", i / keys.length, continueMap);
}
continueMap();
},
reduce: function STMR_reduce(reduce, mapResult, progress,
chunkSize, finished) {
var rows = [];
var mapDict = mapResult.dict;
var mapKeys = mapResult.keys;
var i = 0;
function continueReduce() {
var iAtStart = i;
do {
var key = mapKeys[i];
var item = mapDict[key];
var keys = [];
for (var j = 0; j < keys.length; j++)
newKeys.push([key, item.keys[j]]);
rows.push({key: key,
value: reduce(keys, item.values)});
i++;
} while (i - iAtStart < chunkSize &&
i < mapKeys.length)
if (i == mapKeys.length)
finished(rows);
else
progress("reduce", i / mapKeys.length, continueReduce);
}
continueReduce();
}
};
// == View ==
bc._View = function BC__View(rows) {
this.rows = rows;
function findRow(key, rows) {
if (rows.length > 1) {
var midpoint = Math.floor(rows.length / 2);
var row = rows[midpoint];
if (key < row.key)
return findRow(key, rows.slice(0, midpoint));
if (key > row.key)
return midpoint + findRow(key, rows.slice(midpoint));
return midpoint;
} else
return 0;
}
this.findRow = function V_findRow(key) {
return findRow(key, rows);
};
},
// == MapView ==
bc._MapView = function BC__MapView(mapResult) {
var rows = [];
var keyRows = [];
var mapKeys = mapResult.keys;
var mapDict = mapResult.dict;
for (var i = 0; i < mapKeys.length; i++) {
var key = mapKeys[i];
var item = mapDict[key];
keyRows.push({key: key, pos: rows.length});
var newRows = [];
for (var j = 0; j < item.keys.length; j++) {
var id = item.keys[j];
var value = item.values[j];
newRows.push({_id: id,
key: key,
value: value});
}
newRows.sort(function(a, b) {
if (a._id < b._id)
return -1;
if (a._id > b._id)
return 1;
return 0;
});
rows = rows.concat(newRows);
}
function findRow(key, keyRows) {
if (keyRows.length > 1) {
var midpoint = Math.floor(keyRows.length / 2);
var keyRow = keyRows[midpoint];
if (key < keyRow.key)
return findRow(key, keyRows.slice(0, midpoint));
if (key > keyRow.key)
return findRow(key, keyRows.slice(midpoint));
return keyRow.pos;
} else
return keyRows[0].pos;
}
this.rows = rows;
this.findRow = function MV_findRow(key) {
return findRow(key, keyRows);
};
}
// == Storage Implementations ==
//
// //Storage// is a generic interface for a persistent storage
// implementation capable of storing JSON-able objects.
// === {{{FakeStorage}}} ===
//
// This Storage implementation isn't actually persistent; it's just
// a placeholder that can be used for testing purposes, or when no
// persistent storage mechanisms are available.
bc.FakeStorage = function FakeStorage() {
var db = {};
function deepCopy(obj) {
if (typeof(obj) == "object") {
var copy;
if (isArray(obj))
copy = new Array();
else
copy = new Object();
for (name in obj) {
if (obj.hasOwnProperty(name)) {
var property = obj[name];
if (typeof(property) == "object")
copy[name] = deepCopy(property);
else
copy[name] = property;
}
}
return copy;
} else
return obj;
}
this.get = function FS_get(name, cb) {
if (!(name in db))
cb(null);
else
cb(db[name]);
};
this.put = function FS_put(name, obj, cb) {
db[name] = deepCopy(obj);
cb();
};
};
// === {{{LocalStorage}}} ===
//
// This Storage implementation uses the browser's HTML5 support for
// {{{localStorage}}} or {{{globalStorage}}} for object persistence.
//
// Each database is stored in a key, as a JSON encoded string. In
// future we may want to rethink this as it's horribly innefficient
bc.LocalStorage = function LocalStorage() {
var storage;
if (window.globalStorage)
storage = window.globalStorage[location.hostname];
else {
if (window.localStorage)
storage = window.localStorage;
else
throw new Error("globalStorage/localStorage not available.");
}
this.get = function LS_get(name, cb) {
if (name in storage && storage[name])//.value)
bc.ModuleLoader.require('JSON',
function() {
var obj = JSON.parse(storage[name])//.value);
cb(obj);
});
else
cb(null);
};
this.put = function LS_put(name, obj, cb) {
bc.ModuleLoader.require('JSON',
function() {
storage[name] = JSON.stringify(obj);
cb();
});
};
}
bc.LocalStorage.isAvailable = (this.location &&
this.location.protocol != "file:" &&
(this.globalStorage || this.localStorage));
// === {{{Dictionary}}} ===
//
// A wrapper for a map-like data structure.
//
bc._Dictionary = function BC__Dictionary() {
var dict = {};
var keys = [];
function regenerateKeys() {
keys = [];
for (key in dict)
keys.push(key);
}
this.has = function Dictionary_has(key) {
return (key in dict);
};
this.getKeys = function Dictionary_getKeys() {
return keys;
};
this.get = function Dictionary_get(key) {
return dict[key];
};
this.values = function(){
var res = [];
for (var d in dict){
res.push(dict[d]);
}
return res;
}
this.set = function Dictionary_set(key, value) {
if (!(key in dict))
keys.push(key);
dict[key] = value;
};
this.remove = function Dictionary_delete(key) {
delete dict[key];
// TODO: If we're in JS 1.6 and have Array.indexOf(), we
// shouldn't have to rebuild the key index like this.
regenerateKeys();
};
this.clear = function Dictionary_clear() {
dict = {};
keys = [];
};
this.pickle = function Dictionary_pickle() {
return dict;
};
this.unpickle = function Dictionary_unpickle(obj) {
dict = obj;
regenerateKeys();
};
}
// == Database Wrapper Interface ==
//
// A basic database interface. Implementing objects
// should support methods that emulate the basic REST commands
// that CouchDB uses.
//
// === Local Storage Database ===
// TODO, rename this
bc.BrowserDatabase = function(name, storage, cb, options) {
var self = {},
dbName = 'BrowserCouch_DB_' + name,
dict = new bc._Dictionary(),
syncManager,
commitToStorage = function (cb) {
storage.put(dbName, dict.pickle(), cb || function(){});
};
self.chgs = []; //TODO - this is until I get seq working.
self.wipe = function DB_wipe(cb) {
dict.clear();
commitToStorage(cb);
};
self.get = function DB_get(id, cb) {
cb = cb || function(){}
if (dict.has(id))
cb(dict.get(id));
else
cb(null);
};
// === {{{PUT}}} ===
//
// This method is vaguely isomorphic to a
// [[http://wiki.apache.org/couchdb/HTTP_Document_API#PUT|HTTP PUT]] to a
// url with the specified {{{id}}}.
//
// It creates or updates a document
self.put = function DB_put(document, cb, options) {
options = options || {};
var putObj = function(obj){
if (!obj._rev){
obj._rev = "1-" + (Math.random()*Math.pow(10,20));
// We're using the naive random versioning, rather
// than the md5 deterministic hash.
}else{
var iter = parseInt(obj._rev.split("-")[0]);
obj._rev = "" + (iter+1) +
obj._rev.slice(obj._rev.indexOf("-"));
}
if(options && (!options.noSync))
self.chgs.push(obj)
dict.set(obj._id, obj);
//If new object
self.seq +=1;
}
if (isArray(document)) {
for (var i = 0; i < document.length; i++){
putObj(document[i]);
}
} else{
putObj(document);
}
commitToStorage(cb);
};
// === {{{POST}}} ===
//
// Roughly isomorphic to the two POST options
// available in the REST interface. If an ID is present,
// then the functionality is the same as a PUT operation,
// however if there is no ID, then one will be created.
//
self.post =function(data, cb, options){
var _t = this
if (!data._id)
bc.ModuleLoader.require('UUID', function(){
data._id = new UUID().createUUID();
_t.put(data, function(){cb(data._id)}, options);
});
else{
_t.put(data, function(){cb(data._id)}, options)
}
}
// === {{{DELETE}}} ===
//
// Delete the document.
self.del = function(doc, cb){
this.put({_id : doc._id, _rev : doc._rev, _deleted : true}, cb);
}
//
self.getLength = function DB_getLength() {
return dict.getKeys().length;
};
// === View ===
//
// Perform a query on the data. Queries are in the form of
// map-reduce functions.
//
// takes object of options:
//
// * {{{options.map}}} : The map function to be applied to each document
// (REQUIRED)
//
// * {{{options.finished}}} : A callback for the result.
// (REQUIRED)
//
// * {{{options.chunkSize}}}
// * {{{options.progress}}} : A callback to indicate progress of a query
// * {{{options.mapReducer}}} : A Map-Reduce engine, by default uses a
// single thread
// * {{{options.reduce}}} : The reduce function
self.view = function DB_view(options) {
if (!options.map)
throw new Error('map function not provided');
if (!options.finished)
throw new Error('finished callback not provided');
// Maximum number of items to process before giving the UI a chance
// to breathe.
var DEFAULT_CHUNK_SIZE = 1000;
// If no progress callback is given, we'll automatically give the
// UI a chance to breathe for this many milliseconds before continuing
// processing.
var DEFAULT_UI_BREATHE_TIME = 50;
var chunkSize = options.chunkSize;
if (!chunkSize)
chunkSize = DEFAULT_CHUNK_SIZE;
var progress = options.progress;
if (!progress)
progress = function defaultProgress(phase, percent, resume) {
window.setTimeout(resume, DEFAULT_UI_BREATHE_TIME);
};
var mapReducer = options.mapReducer;
if (!mapReducer)
mapReducer = bc.SingleThreadedMapReducer;
mapReducer.map(
options.map,
dict,
progress,
chunkSize,
function(mapResult) {
if (options.reduce)
mapReducer.reduce(
options.reduce,
mapResult,
progress,
chunkSize,
function(rows) {
options.finished(new BrowserCouch._View(rows));
});
else
options.finished(new BrowserCouch._MapView(mapResult));
});
};
self.getChanges = function(cb){
cb({results: dict.values()});
}
storage.get(
dbName,
function(obj) {
if (obj)
dict.unpickle(obj);
cb(self);
});
}
// === Remote Database ===
// A constructor for a database wrapper for the REST interface
// for a remote CouchDB server. Mainly for use in the syncing.
//
//
bc.SameDomainDB = function (url, cb, options){
var rs = {
url : url,
seq : 0,
get : function(id, cb){
$.getJSON(this.url + "/" + id, {}, cb || function(){});
},
put : function(doc, cb, options){
$.ajax({
url : this.url + "/" + doc._id,
data : JSON.stringify(doc),
type : 'PUT',
processData : false,
contentType : 'application/json',
complete: function(data){
if (cb){
cb();
}
}
});
},
// ==== Get Changes ====
// We poll the {{{_changes}}} endpoint to get the most
// recent documents. At the moment, we're not storing the
// sequence numbers for each server, however this is on
// the TODO list.
getChanges : function(cb){
var url = this.url + "/_changes";
$.getJSON(url, {since : rs.seq}, function(data){
cb(data);
});
}
};
if (cb){
cb(rs);
}
}
// === Function to sync between a source, and target database ===
bc.sync = function(source, target, options){
var options = options || {};
var _sync = function(){
// ==== Get Changes ====
target.getChanges(function(data){
if (data && data.results){
// ==== Merge new data back in ====
// TODO, screw it, for now we'll assume the servers right.
// - In future we need to store the conflicts in the doc
var i = 0;
for (var d in data.results){
target.get(data.results[d]._id, function(doc){
if (doc){
source.put(doc, function(){
i ++;
if (i >= data.results.length){
if (options.update){
options.update();
}
}
});
}
})
}
}
});
// ==== Send Changes ====
// We'll ultimately use the bulk update methods, but for
// now, just iterate through the queue with a req for each
source.getChanges(function(x){
$.each(x, function(){
target.put(this);
});
});
}
_sync();
if (options.continuous){
var interval = setInterval(_sync, options.timeout || 3000);
}
}
// === //List All Databases// ===
//
// Similar to {{{/_all_dbs}}} as there is no way to see what
// keys are stored in localStorage, we have to store a metadata
// database
//
bc.allDbs = function(cb){
bc.ensureMeta(function(){
bc._meta.get('databases', cb);
});
}
// === Load Metadata Database ===
// We store metadata, such as a list of databases etc,
// in a metadatabase. This will create a browsercouch
// wrapper for this database unless it's already loaded
bc.ensureMeta = function(cb, storage, options){
if (bc._meta || options.meta){
cb();
}else{
bc._meta = cons('_BrowserCouchMeta', {meta:true, storage:storage})
bc._meta.onload(cb);
}
}
// == {{{BrowserCouch}}} Core Constructor ==
//
// {{{BrowserCouch}}} is the main object that clients will use. It's
// intended to be somewhat analogous to CouchDB's RESTful API.
//
// Returns a wrapper to the database that emulates the HTTP methods
// available to /<database>/
//
var cons = function(name, options){
var options = options || {};
var self = {
// 'private' variables - perhaps we should move these into the closure
loaded : false,
loadcbs : [],
// ==== Sync the database ====
// Emulates the CouchDB replication functionality
// At the moment only couch's on the same domain
// will work beause of XSS restrictions.
sync : function(target, syncOpts){
self.onload(function(){
var cb = function(rdb){
bc.sync(self, rdb, syncOpts);
};
if (target.indexOf(":")>-1 && target.split(":")[0] === "BrowserCouch"){
bc.BrowserDatabase(target.split(":")[1],
options.storage || new bc.LocalStorage(), cb, options);
}else{
bc.SameDomainDB(target, cb, options.storage, options)
}
});
},
// ==== Add an onload function ====
// Seeing as we're completely callback driven, and you're
// frequently going to want to do a bunch of things once
// the database is loaded, being able to add an arbitrary
// number of onload functions is useful.
//
// Onload functions are called with no arguments, but the
// database object from the constructor is now ready.
// (TODO - change this?)
//
onload : function(func){
if (self.loaded){
func(self);
} else{
self.loadcbs.push(func);
}
}
};
bc.ensureMeta(function(){
// Create a database wrapper.
bc.BrowserDatabase(name,
options.storage || new bc.LocalStorage(), // TODO - check local storage is available
function(db){
// == TODO ==
// We're copying the resultant methods back onto
// the self object. Could do this better.
for (var k in db){
self[k] = db[k];
}
// Fire the onload callbacks
self.loaded = true;
for (var cbi in self.loadcbs){
self.loadcbs[cbi](self);
}
},
options);
}, options.storage, options);
return self;
}
// == TODO ==
// We're copying the bc methods onto the Database object.
// Need to do this better, should research the jquery object.
for (var k in bc){
cons[k] = bc[k];
}
return cons
}();