-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathsetup.js
More file actions
127 lines (106 loc) · 3.37 KB
/
Copy pathsetup.js
File metadata and controls
127 lines (106 loc) · 3.37 KB
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
'use strict';
import PouchDB from './constructor';
import EE from 'pouchdb-events';
import { fetch } from 'pouchdb-fetch';
import ActiveTasks from './active-tasks';
import { createClass } from './utils';
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 Map();
Pouch.on('ref', function onConstructorRef(db) {
if (!destructListeners.has(db.name)) {
destructListeners.set(db.name, []);
}
destructListeners.get(db.name).push(db);
});
Pouch.on('unref', function onConstructorUnref(db) {
if (!destructListeners.has(db.name)) {
return;
}
var dbList = destructListeners.get(db.name);
var pos = dbList.indexOf(db);
if (pos < 0) {
/* istanbul ignore next */
return;
}
dbList.splice(pos, 1);
if (dbList.length > 1) {
/* istanbul ignore next */
destructListeners.set(db.name, dbList);
} else {
destructListeners.delete(db.name);
}
});
Pouch.on('destroyed', function onConstructorDestroyed(name) {
if (!destructListeners.has(name)) {
return;
}
var dbList = destructListeners.get(name);
destructListeners.delete(name);
dbList.forEach(function (db) {
db.emit('destroyed',true);
});
});
}
setUpEventEmitter(PouchDB);
PouchDB.adapter = function (id, obj, addToPreferredAdapters) {
/* istanbul ignore else */
if (obj.valid()) {
PouchDB.adapters[id] = obj;
if (addToPreferredAdapters) {
PouchDB.preferredAdapters.push(id);
}
}
};
PouchDB.plugin = function (obj) {
if (typeof obj === 'function') { // function style for plugins
obj(PouchDB);
} else if (typeof obj !== 'object' || Object.keys(obj).length === 0) {
throw new Error('Invalid plugin: got "' + obj + '", expected an object or a function');
} else {
Object.keys(obj).forEach(function (id) { // object style for plugins
PouchDB.prototype[id] = obj[id];
});
}
if (this.__defaults) {
PouchDB.__defaults = Object.assign({}, this.__defaults);
}
return PouchDB;
};
PouchDB.defaults = function (defaultOpts) {
let PouchWithDefaults = createClass(PouchDB, function (name, opts) {
opts = opts || {};
if (name && typeof name === 'object') {
opts = name;
name = opts.name;
delete opts.name;
}
opts = Object.assign({}, PouchWithDefaults.__defaults, opts);
PouchDB.call(this, name, opts);
});
PouchWithDefaults.preferredAdapters = PouchDB.preferredAdapters.slice();
Object.keys(PouchDB).forEach(function (key) {
if (!(key in PouchWithDefaults)) {
PouchWithDefaults[key] = PouchDB[key];
}
});
// make default options transitive
// https://github.com/pouchdb/pouchdb/issues/5922
PouchWithDefaults.__defaults = Object.assign({}, this.__defaults, defaultOpts);
return PouchWithDefaults;
};
PouchDB.fetch = function (url, opts) {
return fetch(url, opts);
};
PouchDB.prototype.activeTasks = PouchDB.activeTasks = new ActiveTasks();
export default PouchDB;