textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
110 lines (109 loc) • 4.56 kB
JavaScript
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../core/api");
const handlers_1 = require("../helpers/handlers");
const ponyfill_1 = require("web-streams-polyfill/ponyfill");
/**
* Snapshots is an API module for managing thread snapshots
*
* @extends API
*/
class Snapshots extends api_1.API {
/**
* Snapshot all threads and push to registered cafes
*
* @returns Whether the snapshot process was successfull
*/
create() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPost('snapshots');
return response.status === 201;
});
}
/**
* Search the network for thread snapshots
*
* @param wait Stops searching after 'wait' seconds have elapsed (max 30 default 2)
* @returns A ReadableStream of QueryResult objects.
*/
search(wait) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPost('snapshots/search', undefined, { wait: wait || 2 });
if (!response.body) {
throw Error('Empty response stream');
}
return handlers_1.streamHandler(response.body);
});
}
/**
* Apply a single thread snapshot
* @param id The snapshot id (omit to find and apply all snapshots)
* @param wait Stops searching after 'wait' seconds have elapsed (max 30 default 2)
* @returns A ReadableStream of QueryResult objects.
*/
apply(id, wait) {
return __awaiter(this, void 0, void 0, function* () {
const stream = yield this.search(wait);
// For cancellation
let isReader;
let cancellationRequest = false;
const self = this;
return new ponyfill_1.ReadableStream({
start(controller) {
const reader = stream.getReader();
isReader = reader;
const processResult = (result) => {
if (result.done) {
if (cancellationRequest) {
return; // Immediately exit
}
controller.close();
return;
}
try {
if (id === undefined || result.value.id === id) {
self.applySnapshot(result.value).then((success) => {
if (success) {
controller.enqueue(result.value);
}
else {
throw new Error('Unable to apply snapshot');
}
});
}
}
catch (e) {
controller.error(e);
cancellationRequest = true;
reader.cancel(undefined);
return;
}
reader.read().then(processResult);
};
reader.read().then(processResult);
},
cancel(reason) {
cancellationRequest = true;
isReader.cancel(reason);
}
});
});
}
applySnapshot(snapshot) {
return __awaiter(this, void 0, void 0, function* () {
const snap = snapshot.value;
const response = yield this.sendPut(`threads/${snap.id}`, undefined, undefined, snap);
return response.status === 204;
});
}
}
exports.default = Snapshots;
;