Comparing version 0.1.19 to 0.1.20
@@ -22,4 +22,3 @@ import AxiomObject from "./AxiomObject"; | ||
dataset: Message | null; | ||
datasets: number; | ||
onLoad: (() => void)[] | null; | ||
onLoad: ((sms: SignedMessage[]) => void)[] | null; | ||
constructor(name: string, channel: Channel, node?: Node, prefix?: string); | ||
@@ -35,3 +34,3 @@ wrap<T>(p: Promise<T>): Promise<any>; | ||
handleDatabaseWrite(sm: SignedMessage): Promise<void>; | ||
handleDataset(sm: SignedMessage): Promise<void>; | ||
handleDataset(peer: Peer, sm: SignedMessage): Promise<void>; | ||
handleSignedMessage(peer: Peer, sm: SignedMessage): Promise<void>; | ||
@@ -55,4 +54,4 @@ signedMessageToDocument(sm: SignedMessage): any; | ||
load(): void; | ||
waitForLoad(): Promise<void>; | ||
waitForLoad(query: Query): Promise<AxiomObject[]>; | ||
} | ||
export {}; |
@@ -89,3 +89,2 @@ "use strict"; | ||
this.filterer = null; | ||
this.datasets = 0; | ||
this.onLoad = []; | ||
@@ -287,55 +286,57 @@ this.dataset = null; | ||
}; | ||
Database.prototype.handleDataset = function (sm) { | ||
Database.prototype.handleDataset = function (peer, sm) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _i, _a, serialized, nested, _b, copy, _c, copy_1, callback; | ||
var messages, _i, _a, serialized, nested, copy, _b, copy_1, callback, _c, messages_1, sm_1; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
_i = 0, _a = sm.message.messages; | ||
_d.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) return [3 /*break*/, 6]; | ||
serialized = _a[_i]; | ||
nested = void 0; | ||
try { | ||
nested = SignedMessage_1.default.fromSerialized(serialized, true); | ||
messages = []; | ||
for (_i = 0, _a = sm.message.messages; _i < _a.length; _i++) { | ||
serialized = _a[_i]; | ||
nested = void 0; | ||
try { | ||
nested = SignedMessage_1.default.fromSerialized(serialized, true); | ||
} | ||
catch (e) { | ||
console.log("bad dataset:", e); | ||
return [2 /*return*/]; | ||
} | ||
if (nested.message.channel != this.channel.name || | ||
nested.message.database != this.name) { | ||
console.log("inconsistent dataset"); | ||
return [2 /*return*/]; | ||
} | ||
switch (nested.message.type) { | ||
case "Create": | ||
case "Update": | ||
case "Delete": | ||
messages.push(nested); | ||
break; | ||
default: | ||
console.log("weird dataset message type:", nested.message.type); | ||
return [2 /*return*/]; | ||
} | ||
} | ||
catch (e) { | ||
console.log("bad dataset:", e); | ||
return [2 /*return*/]; | ||
} | ||
if (nested.message.channel != this.channel.name || | ||
nested.message.database != this.name) { | ||
console.log("inconsistent dataset"); | ||
return [2 /*return*/]; | ||
} | ||
_b = nested.message.type; | ||
switch (_b) { | ||
case "Create": return [3 /*break*/, 2]; | ||
case "Update": return [3 /*break*/, 2]; | ||
case "Delete": return [3 /*break*/, 2]; | ||
} | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, this.handleDatabaseWrite(nested)]; | ||
case 3: | ||
_d.sent(); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
console.log("weird dataset message type:", nested.message.type); | ||
return [2 /*return*/]; | ||
case 5: | ||
_i++; | ||
return [3 /*break*/, 1]; | ||
case 6: | ||
this.datasets++; | ||
if (this.onLoad && this.datasets >= 1) { | ||
this.log(this.name + " db loaded"); | ||
if (this.onLoad) { | ||
this.log(this.name + " db loaded from " + peer.peerPublicKey.slice(0, 6)); | ||
copy = this.onLoad; | ||
this.onLoad = null; | ||
for (_c = 0, copy_1 = copy; _c < copy_1.length; _c++) { | ||
callback = copy_1[_c]; | ||
callback(); | ||
for (_b = 0, copy_1 = copy; _b < copy_1.length; _b++) { | ||
callback = copy_1[_b]; | ||
callback(messages); | ||
} | ||
} | ||
return [2 /*return*/]; | ||
_c = 0, messages_1 = messages; | ||
_d.label = 1; | ||
case 1: | ||
if (!(_c < messages_1.length)) return [3 /*break*/, 4]; | ||
sm_1 = messages_1[_c]; | ||
return [4 /*yield*/, this.handleDatabaseWrite(sm_1)]; | ||
case 2: | ||
_d.sent(); | ||
_d.label = 3; | ||
case 3: | ||
_c++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
@@ -348,3 +349,3 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
var _a, response; | ||
return __generator(this, function (_b) { | ||
@@ -366,4 +367,6 @@ switch (_b.label) { | ||
case 4: return [2 /*return*/, _b.sent()]; | ||
case 5: return [4 /*yield*/, this.handleDataset(sm)]; | ||
case 6: return [2 /*return*/, _b.sent()]; | ||
case 5: return [4 /*yield*/, this.handleDataset(peer, sm)]; | ||
case 6: | ||
response = _b.sent(); | ||
return [2 /*return*/, response]; | ||
case 7: throw new Error("Database cannot handleSignedMessage of type " + sm.message.type); | ||
@@ -479,3 +482,3 @@ } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fakeQuery, _a; | ||
var start, cached, fakeQuery, _a, elapsed; | ||
return __generator(this, function (_b) { | ||
@@ -489,3 +492,6 @@ switch (_b.label) { | ||
} | ||
start = new Date(); | ||
cached = true; | ||
if (!!this.dataset) return [3 /*break*/, 2]; | ||
cached = false; | ||
fakeQuery = new Message_1.default("Query", { | ||
@@ -503,2 +509,4 @@ selector: {} | ||
} | ||
elapsed = (new Date().getTime() - start.getTime()) / 1000; | ||
this.log("sent" + (cached ? " cached" : ""), this.name, "dataset to", peer.peerPublicKey.slice(0, 6), "in " + elapsed.toFixed(3) + "s"); | ||
return [2 /*return*/]; | ||
@@ -742,3 +750,3 @@ } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var start, response, answer, _i, _a, doc, ms, s; | ||
var start, response, answer, _i, _a, doc, ms, s, q; | ||
return __generator(this, function (_b) { | ||
@@ -760,4 +768,7 @@ switch (_b.label) { | ||
ms = new Date().getTime() - start.getTime(); | ||
s = (ms / 1000).toFixed(3); | ||
this.log(this.name + " handled query " + JSON.stringify(query.selector) + " in " + s + "s"); | ||
s = ms / 1000; | ||
if (s > 1) { | ||
q = JSON.stringify(query.selector); | ||
this.log(this.name + " handled query " + q + " in " + s.toFixed(3) + "s"); | ||
} | ||
return [2 /*return*/, answer]; | ||
@@ -806,12 +817,32 @@ } | ||
}; | ||
Database.prototype.waitForLoad = function () { | ||
// TODO: ensure this doesn't return objects that don't match the query | ||
Database.prototype.waitForLoad = function (query) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var sms, answer, _i, sms_2, sm, obj; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (!this.onLoad) { | ||
return [2 /*return*/]; | ||
switch (_a.label) { | ||
case 0: | ||
if (!!this.onLoad) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.find(query)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 2: return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
_this.onLoad.push(resolve); | ||
})]; | ||
case 3: | ||
sms = _a.sent(); | ||
answer = []; | ||
for (_i = 0, sms_2 = sms; _i < sms_2.length; _i++) { | ||
sm = sms_2[_i]; | ||
if (sm.message.type === "Delete") { | ||
continue; | ||
} | ||
obj = this.signedMessageToObject(sm); | ||
if (this.filterer && !this.filterer(obj)) { | ||
continue; | ||
} | ||
answer.push(obj); | ||
} | ||
return [2 /*return*/, answer]; | ||
} | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.onLoad.push(resolve); | ||
})]; | ||
}); | ||
@@ -818,0 +849,0 @@ }); |
@@ -534,3 +534,3 @@ "use strict"; | ||
} | ||
this.log("joining channel " + channel); | ||
this.log("joining channel " + channel + " as " + this.keyPair.getPublicKey().slice(0, 6)); | ||
this.joined[channel] = new Date(); | ||
@@ -537,0 +537,0 @@ var message = new Message_1.default("Join", { channel: channel }); |
{ | ||
"name": "axiom-api", | ||
"version": "0.1.19", | ||
"version": "0.1.20", | ||
"description": "API for interacting with the Axiom.org platform", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -49,9 +49,5 @@ import PouchDB from "pouchdb"; | ||
// How many Dataset messages this database has received. | ||
// Useful as a heuristic to guess whether we are done loading, for the UI. | ||
datasets: number; | ||
// A list of callbacks for when we are done with the initial load. | ||
// Null if we already loaded. | ||
onLoad: (() => void)[] | null; | ||
onLoad: ((sms: SignedMessage[]) => void)[] | null; | ||
@@ -74,3 +70,2 @@ constructor(name: string, channel: Channel, node?: Node, prefix?: string) { | ||
this.filterer = null; | ||
this.datasets = 0; | ||
this.onLoad = []; | ||
@@ -202,3 +197,4 @@ this.dataset = null; | ||
async handleDataset(sm: SignedMessage): Promise<void> { | ||
async handleDataset(peer: Peer, sm: SignedMessage): Promise<void> { | ||
let messages = []; | ||
for (let serialized of sm.message.messages) { | ||
@@ -223,3 +219,3 @@ let nested; | ||
case "Delete": | ||
await this.handleDatabaseWrite(nested); | ||
messages.push(nested); | ||
break; | ||
@@ -231,11 +227,15 @@ default: | ||
} | ||
this.datasets++; | ||
if (this.onLoad && this.datasets >= 1) { | ||
this.log(`${this.name} db loaded`); | ||
if (this.onLoad) { | ||
this.log(`${this.name} db loaded from ${peer.peerPublicKey.slice(0, 6)}`); | ||
let copy = this.onLoad; | ||
this.onLoad = null; | ||
for (let callback of copy) { | ||
callback(); | ||
callback(messages); | ||
} | ||
} | ||
for (let sm of messages) { | ||
await this.handleDatabaseWrite(sm); | ||
} | ||
} | ||
@@ -253,3 +253,4 @@ | ||
case "Dataset": | ||
return await this.handleDataset(sm); | ||
let response = await this.handleDataset(peer, sm); | ||
return response; | ||
default: | ||
@@ -386,3 +387,6 @@ throw new Error( | ||
} | ||
let start = new Date(); | ||
let cached = true; | ||
if (!this.dataset) { | ||
cached = false; | ||
let fakeQuery = new Message("Query", { | ||
@@ -397,2 +401,10 @@ selector: {} | ||
} | ||
let elapsed = (new Date().getTime() - start.getTime()) / 1000; | ||
this.log( | ||
`sent${cached ? " cached" : ""}`, | ||
this.name, | ||
"dataset to", | ||
peer.peerPublicKey.slice(0, 6), | ||
`in ${elapsed.toFixed(3)}s` | ||
); | ||
} | ||
@@ -539,6 +551,7 @@ | ||
let ms = new Date().getTime() - start.getTime(); | ||
let s = (ms / 1000).toFixed(3); | ||
this.log( | ||
`${this.name} handled query ${JSON.stringify(query.selector)} in ${s}s` | ||
); | ||
let s = ms / 1000; | ||
if (s > 1) { | ||
let q = JSON.stringify(query.selector); | ||
this.log(`${this.name} handled query ${q} in ${s.toFixed(3)}s`); | ||
} | ||
return answer; | ||
@@ -576,11 +589,24 @@ } | ||
async waitForLoad(): Promise<void> { | ||
// TODO: ensure this doesn't return objects that don't match the query | ||
async waitForLoad(query: Query): Promise<AxiomObject[]> { | ||
if (!this.onLoad) { | ||
return; | ||
return await this.find(query); | ||
} | ||
return new Promise((resolve, reject) => { | ||
let sms: SignedMessage[] = await new Promise((resolve, reject) => { | ||
this.onLoad.push(resolve); | ||
}); | ||
let answer = []; | ||
for (let sm of sms) { | ||
if (sm.message.type === "Delete") { | ||
continue; | ||
} | ||
let obj = this.signedMessageToObject(sm); | ||
if (this.filterer && !this.filterer(obj)) { | ||
continue; | ||
} | ||
answer.push(obj); | ||
} | ||
return answer; | ||
} | ||
} |
@@ -527,3 +527,5 @@ import Channel from "./Channel"; | ||
} | ||
this.log(`joining channel ${channel}`); | ||
this.log( | ||
`joining channel ${channel} as ${this.keyPair.getPublicKey().slice(0, 6)}` | ||
); | ||
this.joined[channel] = new Date(); | ||
@@ -530,0 +532,0 @@ let message = new Message("Join", { channel: channel }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
370780
7230