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 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
4×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
194×
1×
194×
194×
1×
679×
1×
679×
7×
7×
672×
672×
1×
1×
672×
585×
585×
585×
672×
7×
7×
672×
194×
194×
1×
194×
1×
194×
1×
4140×
1×
4140×
1×
1×
4140×
4140×
1×
1× | 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _events = require('events');
var _inherits = require('inherits');
var _inherits2 = _interopRequireDefault(_inherits);
var _isChromeApp = require('./deps/env/isChromeApp');
var _isChromeApp2 = _interopRequireDefault(_isChromeApp);
var _hasLocalStorage = require('./deps/env/hasLocalStorage');
var _hasLocalStorage2 = _interopRequireDefault(_hasLocalStorage);
var _pick = require('./deps/pick');
var _pick2 = _interopRequireDefault(_pick);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _inherits2.default)(Changes, _events.EventEmitter);
/* istanbul ignore next */
function attachBrowserEvents(self) {
Iif ((0, _isChromeApp2.default)()) {
chrome.storage.onChanged.addListener(function (e) {
// make sure it's event addressed to us
if (e.db_name != null) {
//object only has oldValue, newValue members
self.emit(e.dbName.newValue);
}
});
} else Iif ((0, _hasLocalStorage2.default)()) {
if (typeof addEventListener !== 'undefined') {
addEventListener("storage", function (e) {
self.emit(e.key);
});
} else {
// old IE
window.attachEvent("storage", function (e) {
self.emit(e.key);
});
}
}
}
function Changes() {
_events.EventEmitter.call(this);
this._listeners = {};
attachBrowserEvents(this);
}
Changes.prototype.addListener = function (dbName, id, db, opts) {
/* istanbul ignore if */
Iif (this._listeners[id]) {
return;
}
var self = this;
var inprogress = false;
function eventFunction() {
/* istanbul ignore if */
Iif (!self._listeners[id]) {
return;
}
if (inprogress) {
inprogress = 'waiting';
return;
}
inprogress = true;
var changesOpts = (0, _pick2.default)(opts, ['style', 'include_docs', 'attachments', 'conflicts', 'filter', 'doc_ids', 'view', 'since', 'query_params', 'binary']);
/* istanbul ignore next */
function onError() {
inprogress = false;
}
db.changes(changesOpts).on('change', function (c) {
Eif (c.seq > opts.since && !opts.cancelled) {
opts.since = c.seq;
opts.onChange(c);
}
}).on('complete', function () {
if (inprogress === 'waiting') {
setTimeout(function () {
eventFunction();
}, 0);
}
inprogress = false;
}).on('error', onError);
}
this._listeners[id] = eventFunction;
this.on(dbName, eventFunction);
};
Changes.prototype.removeListener = function (dbName, id) {
/* istanbul ignore if */
Iif (!(id in this._listeners)) {
return;
}
_events.EventEmitter.prototype.removeListener.call(this, dbName, this._listeners[id]);
};
/* istanbul ignore next */
Changes.prototype.notifyLocalWindows = function (dbName) {
//do a useless change on a storage thing
//in order to get other windows's listeners to activate
Iif ((0, _isChromeApp2.default)()) {
chrome.storage.local.set({ dbName: dbName });
} else Iif ((0, _hasLocalStorage2.default)()) {
localStorage[dbName] = localStorage[dbName] === "a" ? "b" : "a";
}
};
Changes.prototype.notify = function (dbName) {
this.emit(dbName);
this.notifyLocalWindows(dbName);
};
exports.default = Changes;
module.exports = exports['default']; |