forked from maxfortun/sharedb-promises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.js
242 lines (200 loc) · 5.09 KB
/
doc.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
'use strict';
const Debug = require('debug');
class Doc {
constructor(doc) {
this.doc = doc;
this.debug = new Debug('sharedb-promises:doc:' + doc.collection + ':' + doc.id);
return this;
}
async create() {
const { doc } = this;
const [ data ] = arguments;
return new Promise((resolve, reject) => {
this.debug("create >", doc.collection, doc.id);
try {
doc.create(data, (err) => {
if (err) {
this.debug("create !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
this.debug("create <", doc.collection, doc.id, doc.data);
return resolve(doc);
});
} catch(err) {
this.debug("create !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
});
}
async del() {
const { doc } = this;
const [ options ] = arguments;
return new Promise((resolve, reject) => {
this.debug("del >", doc.collection, doc.id);
try {
doc.del(options, (err) => {
if (err) {
this.debug("del !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
this.debug("del <", doc.collection, doc.id, doc.data);
return resolve(doc);
});
} catch(err) {
this.debug("del !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
});
}
async fetch() {
const { doc } = this;
return new Promise((resolve, reject) => {
this.debug("fetch >", doc.collection, doc.id);
try {
doc.fetch((err) => {
if (err) {
this.debug("fetch !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
this.debug("fetch <", doc.collection, doc.id, doc.data);
return resolve(doc);
});
} catch(err) {
this.debug("fetch !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
});
}
async submitOp() {
const { doc } = this;
const [ op, options ] = arguments;
return new Promise((resolve, reject) => {
this.debug("submitOp >", doc.collection, doc.id, op, options);
try {
doc.submitOp(op, options, (err) => {
if (err) {
this.debug("submitOp !", doc.collection, doc.id, op, options, err, err.stack);
return reject(err);
}
this.debug("submitOp <", doc.collection, doc.id, op, options);
return resolve(doc);
});
} catch(err) {
this.debug("submitOp !", doc.collection, doc.id, op, options, err, err.stack);
return reject(err);
}
});
}
async subscribe() {
const { doc } = this;
return new Promise((resolve, reject) => {
this.debug("subscribe >", doc.collection, doc.id);
try {
doc.subscribe((err) => {
if (err) {
this.debug("subscribe !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
this.debug("subscribe <", doc.collection, doc.id);
return resolve(doc);
});
} catch(err) {
this.debug("subscribe !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
});
}
async unsubscribe() {
const { doc } = this;
return new Promise((resolve, reject) => {
this.debug("unsubscribe >", doc.collection, doc.id);
try {
doc.subscribe((err) => {
if (err) {
this.debug("unsubscribe !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
this.debug("unsubscribe <", doc.collection, doc.id);
return resolve(doc);
});
} catch(err) {
this.debug("unsubscribe !", doc.collection, doc.id, err, err.stack);
return reject(err);
}
});
}
async fetchOrCreate() {
const { doc } = this;
const [ data ] = arguments;
await this.fetch();
if (doc.type !== null) {
return doc;
}
return this.create(data);
}
async fetchOrCreateAndSubscribe() {
const [ data ] = arguments;
await this.fetchOrCreate(data);
return this.subscribe();
}
getPath(data) {
let path = [];
while(data && typeof data === "object") {
let keys = Object.keys(data);
if(keys.length == 0) {
break;
}
if(keys.length != 1) {
if(path.length > 0) {
path.pop();
}
return path;
}
let key = keys[0];
path.push(key);
data = data[key];
}
return path;
}
createDataOp() {
const { doc } = this;
const [ data ] = arguments;
let path = this.getPath(data);
// this.debug("creating data", doc, path, data);
let insertPath = [];
let parentData = null;
let docData = doc.data;
let _data = data; // modifiable
for(let i = 0; i < path.length && docData != undefined; i++) {
let key = path[i];
insertPath.push(key);
_data = _data[key];
parentData = docData;
docData = docData[key];
}
if(null != docData) {
// this.debug("createDataOp already exists", path, doc);
return {path};
}
let action = "oi";
if(Array.isArray(parentData)) {
action = "li";
} else if (typeof parentData === "string") {
action = "si";
}
let op = { p: insertPath };
op[action] = _data;
// this.debug("createDataOp", op, doc);
return {path, op};
}
async createData() {
const [ data ] = arguments;
let opInfo = this.createDataOp(data);
if(!opInfo.op) {
return opInfo.path;
}
return this.submitOp([opInfo.op])
.then(() => opInfo.path);
}
}
module.exports = Doc;