mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
Improve async functions
This commit is contained in:
parent
73e18171e1
commit
4b754fc956
@ -67,17 +67,17 @@ class OrbitDB {
|
||||
|
||||
// Adds an event to the log
|
||||
add(channel, password, data) {
|
||||
return await(this._write(channel, password, DBOperation.Types.Add, null, data));
|
||||
return this._write(channel, password, DBOperation.Types.Add, null, data);
|
||||
}
|
||||
|
||||
// Sets a key-value pair
|
||||
put(channel, password, key, data) {
|
||||
return await(this._write(channel, password, DBOperation.Types.Put, key, data));
|
||||
return this._write(channel, password, DBOperation.Types.Put, key, data);
|
||||
}
|
||||
|
||||
// Deletes an event based on hash (of the operation) or 'key' of a key/val pair
|
||||
del(channel, password, key) {
|
||||
return await(this._write(channel, password, DBOperation.Types.Delete, key));
|
||||
return this._write(channel, password, DBOperation.Types.Delete, key);
|
||||
}
|
||||
|
||||
deleteChannel(channel, password) {
|
||||
@ -105,7 +105,7 @@ class OrbitDB {
|
||||
.map((f) => await(f.fetchPayload())) // IO - fetch the actual OP from ipfs. consider merging with LL.
|
||||
.skipWhile((f) => key && f.key !== key) // Drop elements until we have the first one requested
|
||||
.map(_createLWWSet) // Return items as LWW (ignore values after the first found)
|
||||
.filter((f) => f !== null) // Make sure we don't have empty ones
|
||||
.compact() // Remove nulls
|
||||
.drop(inclusive ? 0 : 1) // Drop the 'gt/lt' item, include 'gte/lte' item
|
||||
.take(amount)
|
||||
}
|
||||
@ -119,4 +119,27 @@ class OrbitDB {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: move to where this is needed
|
||||
// static fetchPayload(hash) {
|
||||
// return new Promise(async((resolve, reject) => {
|
||||
// if(hash) {
|
||||
// this._ipfs.object.get(hash)
|
||||
// .then((payload) => {
|
||||
// this.Payload = JSON.parse(payload.Data);
|
||||
// console.log(this.Payload, hash, this.hash)
|
||||
// let res = this.Payload;
|
||||
// Object.assign(res, { hash: hash });
|
||||
// if(this.Payload.key === null)
|
||||
// Object.assign(res, { key: hash });
|
||||
// console.log(this.Payload)
|
||||
// console.log(this)
|
||||
// resolve(res);
|
||||
// })
|
||||
// .catch(reject);
|
||||
// } else {
|
||||
// resolve(this.Payload);
|
||||
// }
|
||||
// }));
|
||||
// }
|
||||
|
||||
module.exports = OrbitDB;
|
||||
|
@ -8,30 +8,24 @@ const Post = require('../post/Post');
|
||||
|
||||
class Operation {
|
||||
static create(ipfs, log, user, operation, key, value, data) {
|
||||
var createAsync = async(() => {
|
||||
return new Promise(async((resolve, reject) => {
|
||||
const hash = await(Operation._createOperation(ipfs, user, operation, key, value));
|
||||
await(log.add(hash));
|
||||
resolve(hash);
|
||||
}));
|
||||
})
|
||||
return await(createAsync());
|
||||
return new Promise(async((resolve, reject) => {
|
||||
const hash = await(Operation._createOperation(ipfs, user, operation, key, value));
|
||||
await(log.add(hash));
|
||||
resolve(hash);
|
||||
}));
|
||||
}
|
||||
|
||||
static _createOperation(ipfs, user, operation, key, value) {
|
||||
var createOperationAsync = async(() => {
|
||||
return new Promise(async((resolve, reject) => {
|
||||
const data = {
|
||||
operation: operation,
|
||||
key: key,
|
||||
value: value,
|
||||
by: user.id || 'empty'
|
||||
};
|
||||
const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data));
|
||||
resolve(op.Hash);
|
||||
}));
|
||||
})
|
||||
return await(createOperationAsync());
|
||||
return new Promise(async((resolve, reject) => {
|
||||
const data = {
|
||||
operation: operation,
|
||||
key: key,
|
||||
value: value,
|
||||
by: user.id || 'empty'
|
||||
};
|
||||
const op = await(Post.create(ipfs, Post.Types.OrbitDBItem, data));
|
||||
resolve(op.Hash);
|
||||
}));
|
||||
}
|
||||
|
||||
static get Types() {
|
||||
|
@ -15,14 +15,19 @@ class OrbitNode {
|
||||
fetchPayload() {
|
||||
return new Promise(async((resolve, reject) => {
|
||||
if(!this.Payload) {
|
||||
const payload = await(this._ipfs.object.get(this.data));
|
||||
this.Payload = JSON.parse(payload.Data);
|
||||
this._ipfs.object.get(this.data)
|
||||
.then((payload) => {
|
||||
this.Payload = JSON.parse(payload.Data);
|
||||
let res = this.Payload;
|
||||
Object.assign(res, { hash: this.data });
|
||||
if(this.Payload.key === null)
|
||||
Object.assign(res, { key: this.data });
|
||||
resolve(res);
|
||||
})
|
||||
.catch(reject);
|
||||
} else {
|
||||
resolve(this.Payload);
|
||||
}
|
||||
let res = this.Payload;
|
||||
Object.assign(res, { hash: this.data });
|
||||
if(this.Payload.key === null)
|
||||
Object.assign(res, { key: this.data });
|
||||
resolve(res);
|
||||
}));
|
||||
}
|
||||
|
||||
@ -45,21 +50,20 @@ class OrbitNode {
|
||||
|
||||
get asJson() {
|
||||
let res = { id: this.id, data: this.data }
|
||||
let items = this.next.map((f) => f.ipfsHash);
|
||||
let items = this.next.map((f) => f.hash);
|
||||
Object.assign(res, { next: items });
|
||||
return res;
|
||||
}
|
||||
|
||||
static fromIpfsHash(ipfs, hash) {
|
||||
const createNode = async(() => {
|
||||
return new Promise(async((resolve, reject) => {
|
||||
const o = await(ipfs.object.get(hash));
|
||||
const f = JSON.parse(o.Data)
|
||||
const node = new OrbitNode(ipfs, f.id, f.data, f.next, hash)
|
||||
resolve(node);
|
||||
}));
|
||||
});
|
||||
return await(createNode());
|
||||
return new Promise(async((resolve, reject) => {
|
||||
ipfs.object.get(hash)
|
||||
.then((obj) => {
|
||||
const f = JSON.parse(obj.Data)
|
||||
const node = new OrbitNode(ipfs, f.id, f.data, f.next, hash)
|
||||
resolve(node);
|
||||
}).catch(reject);
|
||||
}));
|
||||
}
|
||||
|
||||
static equals(a, b) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user