-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
129 lines (129 loc) · 6.92 KB
/
index.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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var _reqUnsubs, _uuid, _database, _unmountNotMounted;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Requests = void 0;
const events_1 = require("events");
class Requests extends events_1.EventEmitter {
constructor() {
super();
_reqUnsubs.set(this, {});
_uuid.set(this, null);
_database.set(this, null);
this.mount = (database = __classPrivateFieldGet(this, _database), uuid = __classPrivateFieldGet(this, _uuid)) => {
__classPrivateFieldSet(this, _database, database);
__classPrivateFieldSet(this, _uuid, uuid);
const reqToKeysRef = database.ref(`/requests/to/${__classPrivateFieldGet(this, _uuid)}`);
const reqFromKeysRef = database.ref(`/requests/from/${__classPrivateFieldGet(this, _uuid)}`);
const onReqKeyAdded = (keySnap) => {
const reqKey = keySnap.key;
const reqRef = database.ref(`/requests/${reqKey}`);
const settle = () => reqRef.update({ settled: true });
const onReq = (reqSnap) => {
var _a, _b;
const reqVal = reqSnap.val();
if (reqVal) {
if (reqVal.settled) {
if (reqVal.from === __classPrivateFieldGet(this, _uuid)) {
reqFromKeysRef.child(reqKey).set(null);
reqRef.set(null);
this.emit(`settled`, reqSnap);
this.emit(`settled#${reqKey}`, reqSnap);
}
if (reqVal.to === __classPrivateFieldGet(this, _uuid)) {
reqToKeysRef.child(reqKey).set(null);
}
}
if (reqVal.from === __classPrivateFieldGet(this, _uuid)) {
this.emit(`request:from#${reqKey}`, reqSnap);
this.emit(`request:from`, reqSnap);
}
if (reqVal.to === __classPrivateFieldGet(this, _uuid)) {
this.emit(`request:to#${reqKey}`, reqSnap, settle);
this.emit(`request:to`, reqSnap, settle);
this.emit(`request#${reqKey}`, reqSnap, settle);
this.emit(`request`, reqSnap, settle);
}
this.emit(reqKey, reqVal);
}
else {
(_b = (_a = __classPrivateFieldGet(this, _reqUnsubs))[reqKey]) === null || _b === void 0 ? void 0 : _b.call(_a);
delete __classPrivateFieldGet(this, _reqUnsubs)[reqKey];
}
};
if (!__classPrivateFieldGet(this, _reqUnsubs)[reqKey]) {
__classPrivateFieldGet(this, _reqUnsubs)[reqKey] = () => reqRef.off('value', onReq);
reqRef.on('value', onReq);
}
};
reqToKeysRef.on('child_added', onReqKeyAdded);
reqFromKeysRef.on('child_added', onReqKeyAdded);
this.unmount = () => {
var _a, _b;
reqToKeysRef.off('child_added', onReqKeyAdded);
reqFromKeysRef.off('child_added', onReqKeyAdded);
for (const reqKey in __classPrivateFieldGet(this, _reqUnsubs)) {
(_b = (_a = __classPrivateFieldGet(this, _reqUnsubs))[reqKey]) === null || _b === void 0 ? void 0 : _b.call(_a);
this.removeAllListeners('request:from:' + reqKey);
this.removeAllListeners('request:to:' + reqKey);
this.removeAllListeners('request:' + reqKey);
this.removeAllListeners('setteled:' + reqKey);
}
this.unmount = __classPrivateFieldGet(this, _unmountNotMounted);
};
return this.unmount;
};
this.update = (reqKey, req) => __awaiter(this, void 0, void 0, function* () {
yield __classPrivateFieldGet(this, _database).ref(`/requests/${reqKey}`).update(req);
});
_unmountNotMounted.set(this, () => {
throw new Error('Can not unmount Requests, not mounted');
});
this.unmount = __classPrivateFieldGet(this, _unmountNotMounted);
this.send = this.send.bind(this);
}
send(to, req, onValue) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.mounted)
throw new Error('Can not send firebase request, Requests is not mounted');
const from = __classPrivateFieldGet(this, _uuid);
const reqRef = yield __classPrivateFieldGet(this, _database).ref(`/requests`)
.push(Object.assign(Object.assign({}, req), { from, to }));
const reqKey = reqRef.key;
if (onValue)
this.on(`request:from#${reqKey}`, onValue);
yield __classPrivateFieldGet(this, _database).ref(`/requests/from/${from}/${reqKey}`).set(true);
yield __classPrivateFieldGet(this, _database).ref(`/requests/to/${to}/${reqKey}`).set(true);
if (onValue)
return () => this.off(`request:from#${reqKey}`, onValue);
else
return reqKey;
});
}
get mounted() {
return this.unmount !== __classPrivateFieldGet(this, _unmountNotMounted);
}
}
exports.Requests = Requests;
_reqUnsubs = new WeakMap(), _uuid = new WeakMap(), _database = new WeakMap(), _unmountNotMounted = new WeakMap();