@parity/light.js
Version:
A high-level reactive JS library optimized for light clients
198 lines (197 loc) • 9.02 kB
JavaScript
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var createRpc_1 = require("./createRpc");
var mockApi_1 = require("../../utils/testHelpers/mockApi");
var rxjs_1 = require("rxjs");
var api_1 = require("../../api");
var sleep_1 = require("../../utils/testHelpers/sleep");
describe('should manage source observable sparingly', function () {
var unsubscribe = 0;
// Returns an observable
function rpc(options) {
return createRpc_1.default({
frequency: [
function () {
return new rxjs_1.Observable(function (subscriber) {
var i = 0;
subscriber.next(i++);
var itl = setInterval(function () {
subscriber.next(i++);
}, 1000);
return function () {
clearInterval(itl);
unsubscribe++;
};
});
}
],
pipes: function () { return []; }
})(options)();
}
var obs;
var subscription;
it('should defer subscription to the source observable', function (done) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
obs = rpc();
return [4 /*yield*/, sleep_1.default(500)];
case 1:
_a.sent();
api_1.setApi(mockApi_1.resolveApi());
subscription = obs.subscribe(function (n) {
expect(n).toBe(0);
done();
});
return [2 /*return*/];
}
});
}); });
it('should delay unsubscription to the source observable', function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, sleep_1.default(30)];
case 1:
_a.sent(); // t=30
subscription.unsubscribe();
return [4 /*yield*/, sleep_1.default(createRpc_1.UNSUB_DELAY - 5)];
case 2:
_a.sent(); // t=30+(2000-5)=2025
expect(unsubscribe).toEqual(0);
// unsubscription to source observable at t = 30+2000 = 2030
// source observable had emitted [0,1,2]
return [4 /*yield*/, sleep_1.default(10)];
case 3:
// unsubscription to source observable at t = 30+2000 = 2030
// source observable had emitted [0,1,2]
_a.sent();
expect(unsubscribe).toEqual(1);
return [2 /*return*/];
}
});
}); });
it('should re-emit previous value on observable subscription', function () { return __awaiter(_this, void 0, void 0, function () {
var values;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
values = [];
subscription = obs.subscribe(function (x) {
values.push(x);
});
return [4 /*yield*/, sleep_1.default(5)];
case 1:
_a.sent();
expect(values).toEqual([2, 0]);
return [2 /*return*/];
}
});
}); });
it('should use the same source observable when used twice', function () { return __awaiter(_this, void 0, void 0, function () {
var obs2, values;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
obs2 = rpc();
values = [];
return [4 /*yield*/, sleep_1.default(1000)];
case 1:
_a.sent();
obs2.subscribe(function (x) { return values.push(x); });
expect(values).toEqual([1]);
return [2 /*return*/];
}
});
}); });
it('should not use the same source observable if we change api', function () { return __awaiter(_this, void 0, void 0, function () {
var obs2, values;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
api_1.setApi(mockApi_1.resolveApi('foo'));
obs2 = rpc();
values = [];
obs2.subscribe(function (x) { return values.push(x); });
return [4 /*yield*/, sleep_1.default(5)];
case 1:
_a.sent();
expect(values).toEqual([0]);
return [2 /*return*/];
}
});
}); });
var obs3;
var provider = new mockApi_1.MockProvider();
it('should not use the same source observable if options are passed', function () { return __awaiter(_this, void 0, void 0, function () {
var values;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
obs3 = rpc({ provider: provider });
values = [];
obs3.subscribe(function (x) { return values.push(x); });
return [4 /*yield*/, sleep_1.default(5)];
case 1:
_a.sent();
expect(values).toEqual([0]);
return [2 /*return*/];
}
});
}); });
it('should use the same source observable if the same options are passed', function () { return __awaiter(_this, void 0, void 0, function () {
var obs4, values;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
obs4 = rpc({ provider: provider });
return [4 /*yield*/, sleep_1.default(1000)];
case 1:
_a.sent();
values = [];
obs4.subscribe(function (x) { return values.push(x); });
expect(values).toEqual([1]);
return [2 /*return*/];
}
});
}); });
});
;