forked from pouchdb/pouchdb
/
setup.js
143 lines (118 loc) · 3.93 KB
/
setup.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
"use strict";
import PouchDB from "./constructor";
import utils from './utils';
import inherits from 'inherits';
import collections from 'pouchdb-collections';
import events from 'events';
var EE = events.EventEmitter;
import hasLocalStorage from './deps/env/hasLocalStorage';
PouchDB.adapters = {};
PouchDB.preferredAdapters = [];
PouchDB.prefix = '_pouch_';
var eventEmitter = new EE();
function setUpEventEmitter(Pouch) {
Object.keys(EE.prototype).forEach(function (key) {
if (typeof EE.prototype[key] === 'function') {
Pouch[key] = eventEmitter[key].bind(eventEmitter);
}
});
// these are created in constructor.js, and allow us to notify each DB with
// the same name that it was destroyed, via the constructor object
var destructListeners = Pouch._destructionListeners = new collections.Map();
Pouch.on('destroyed', function onConstructorDestroyed(name) {
if (!destructListeners.has(name)) {
return;
}
destructListeners.get(name).forEach(function (callback) {
callback();
});
destructListeners.delete(name);
});
}
setUpEventEmitter(PouchDB);
PouchDB.parseAdapter = function (name, opts) {
var match = name.match(/([a-z\-]*):\/\/(.*)/);
var adapter, adapterName;
if (match) {
// the http adapter expects the fully qualified name
name = /http(s?)/.test(match[1]) ? match[1] + '://' + match[2] : match[2];
adapter = match[1];
/* istanbul ignore if */
if (!PouchDB.adapters[adapter].valid()) {
throw 'Invalid adapter';
}
return {name: name, adapter: match[1]};
}
// check for browsers that have been upgraded from websql-only to websql+idb
var skipIdb = 'idb' in PouchDB.adapters && 'websql' in PouchDB.adapters &&
hasLocalStorage() &&
localStorage['_pouch__websqldb_' + PouchDB.prefix + name];
if (opts.adapter) {
adapterName = opts.adapter;
} else if (typeof opts !== 'undefined' && opts.db) {
adapterName = 'leveldb';
} else { // automatically determine adapter
for (var i = 0; i < PouchDB.preferredAdapters.length; ++i) {
adapterName = PouchDB.preferredAdapters[i];
if (adapterName in PouchDB.adapters) {
/* istanbul ignore if */
if (skipIdb && adapterName === 'idb') {
// log it, because this can be confusing during development
console.log('PouchDB is downgrading "' + name + '" to WebSQL to' +
' avoid data loss, because it was already opened with WebSQL.');
continue; // keep using websql to avoid user data loss
}
break;
}
}
}
adapter = PouchDB.adapters[adapterName];
// if adapter is invalid, then an error will be thrown later
var usePrefix = (adapter && 'use_prefix' in adapter) ?
adapter.use_prefix : true;
return {
name: usePrefix ? (PouchDB.prefix + name) : name,
adapter: adapterName
};
};
PouchDB.adapter = function (id, obj, addToPreferredAdapters) {
if (obj.valid()) {
PouchDB.adapters[id] = obj;
if (addToPreferredAdapters) {
PouchDB.preferredAdapters.push(id);
}
}
};
PouchDB.plugin = function (obj) {
Object.keys(obj).forEach(function (id) {
PouchDB.prototype[id] = obj[id];
});
return PouchDB;
};
PouchDB.defaults = function (defaultOpts) {
function PouchAlt(name, opts, callback) {
if (!(this instanceof PouchAlt)) {
return new PouchAlt(name, opts, callback);
}
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (name && typeof name === 'object') {
opts = name;
name = undefined;
}
opts = utils.extend({}, defaultOpts, opts);
PouchDB.call(this, name, opts, callback);
}
inherits(PouchAlt, PouchDB);
setUpEventEmitter(PouchAlt);
PouchAlt.preferredAdapters = PouchDB.preferredAdapters.slice();
Object.keys(PouchDB).forEach(function (key) {
if (!(key in PouchAlt)) {
PouchAlt[key] = PouchDB[key];
}
});
return PouchAlt;
};
export default PouchDB;