@neo-one/utils
Version:
NEO•ONE shared utils.
302 lines (247 loc) • 7.49 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var _ = _interopDefault(require('lodash'));
var rxjs = require('rxjs');
var OuterSubscriber = require('rxjs/internal/OuterSubscriber');
var subscribeToResult = require('rxjs/internal/util/subscribeToResult');
// tslint:disable
// @ts-ignore
function inherits(subClass, superClass) {
if (typeof superClass !== 'function' && superClass != null) {
throw new TypeError('Super expression must either be null or a function');
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) {
Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
}
let CustomError = function (_super) {
inherits(CustomError, _super);
function CustomError() {
// @ts-ignore
var _newTarget = this.constructor; // @ts-ignore
var _this = _super.apply(this, arguments); // 'Error' breaks prototype chain here
_this.__proto__ = _newTarget.prototype; // restore prototype chain
return _this;
}
return CustomError;
}(Error);
const nowSeconds = () => Math.round(Date.now() / 1000);
function nullthrows(value) {
if (value == undefined) {
throw new Error('Unexpected null');
}
return value;
}
function assertNever(_value) {} // do nothing
// tslint:disable-next-line no-any
function isPromise(value) {
return value != undefined && value.then != undefined && typeof value.then === 'function';
}
function notNull(value) {
return value != undefined;
}
function zip(...arrays) {
// tslint:disable-next-line no-any
return _.zip(...arrays);
} // tslint:enable readonly-array
const utils = {
nowSeconds,
nullthrows,
assertNever,
notNull,
isPromise,
zip
};
let currentID = 0;
const getID = () => {
const result = currentID;
currentID += 1;
return result;
};
function finalize(func) {
return source$ => rxjs.Observable.create(observer => {
let lastValue;
const subscription = source$.subscribe({
next: value => {
lastValue = value;
observer.next(value);
},
error: error => observer.error(error),
complete: () => observer.complete()
});
subscription.add(() => {
const result = func(lastValue);
if (utils.isPromise(result)) {
const id = getID();
let deleted = false;
const promise = result.then(() => {
deleted = true;
finalize.shutdownPromises.delete(id);
});
if (!deleted) {
finalize.shutdownPromises.set(id, promise);
}
}
});
return subscription;
});
}
(function (finalize) {
finalize.shutdownPromises = new Map();
finalize.wait = async () => {
const promises = [...finalize.shutdownPromises.values()];
if (promises.length === 0) {
return;
}
await Promise.all(promises);
await finalize.wait();
};
})(finalize || (finalize = {}));
(function (labels) {
labels["PLUGIN_NAME"] = "plugin.name";
labels["RESOURCETYPE_NAME"] = "resource_type.name";
labels["NODE_NAME"] = "node.name";
labels["OP_CODE"] = "neo.op.code";
labels["NEO_ADDRESS"] = "neo.address";
labels["NODE_OPTIONSPATH"] = "node.options_path";
labels["NEO_TRANSACTION_HASH"] = "neo.transaction.hash";
labels["NEO_TRANSACTION_TYPE"] = "neo.transaction.type";
labels["NEO_TRANSACTION_FOUND"] = "neo.transaction.found";
labels["NEO_CONSENSUS_HASH"] = "neo.consensus.hash";
labels["NEO_BLOCK_INDEX"] = "neo.block.index";
labels["CALL_METHOD"] = "call.method";
labels["INVOKE_METHOD"] = "invoke.method";
labels["INVOKE_RAW_METHOD"] = "invoke_raw.method";
labels["JSONRPC_TYPE"] = "jsonrpc.type";
labels["COMMAND_NAME"] = "command.name";
})(exports.labels || (exports.labels = {}));
const EMPTY_LAST_VALUE = {
type: 'empty'
};
class MergeScanSubscriber extends OuterSubscriber.OuterSubscriber {
constructor(destination, accumulator, acc) {
super(destination);
this.hasValue = false;
this.hasCompleted = false;
this.lastValue = EMPTY_LAST_VALUE;
this.active = false;
this.index = 0;
this.accumulator = accumulator;
this.acc = acc;
}
notifyNext(_outerValue, innerValue) {
const {
destination
} = this;
this.acc = innerValue;
this.hasValue = true;
if (destination.next !== undefined) {
destination.next(innerValue);
}
}
notifyComplete(innerSub) {
const {
lastValue
} = this;
this.remove(innerSub);
this.active = false;
if (lastValue.type === 'value') {
this.lastValue = EMPTY_LAST_VALUE;
this.next(lastValue.value);
} else if (!this.active && this.hasCompleted) {
if (!this.hasValue && this.destination.next !== undefined) {
this.destination.next(this.acc);
}
if (this.destination.complete !== undefined) {
this.destination.complete();
}
}
}
_next(value) {
if (!this.active) {
const {
index,
destination
} = this;
this.index += 1;
try {
const result = this.accumulator(this.acc, value);
this.active = true;
this._innerSub(result, value, index);
} catch (error) {
if (destination.error !== undefined) {
destination.error(error);
}
}
} else {
this.lastValue = {
type: 'value',
value
};
}
}
_complete() {
this.hasCompleted = true;
if (!this.active && this.lastValue.type === 'empty') {
if (!this.hasValue && this.destination.next !== undefined) {
this.destination.next(this.acc);
}
if (this.destination.complete !== undefined) {
this.destination.complete();
}
}
}
_innerSub(ish, value, index) {
this.add(subscribeToResult.subscribeToResult(this, ish, value, index));
}
}
class MergeScanOperator {
constructor(accumulator, seed) {
this.accumulator = accumulator;
this.seed = seed;
} // tslint:disable-next-line
call(subscriber, source) {
// tslint:disable-next-line
return source.subscribe(new MergeScanSubscriber(subscriber, this.accumulator, this.seed));
}
}
function mergeScanLatest(accumulator, seed) {
return source$ => source$.lift(new MergeScanOperator(accumulator, seed));
}
function neverComplete() {
return source$ => rxjs.Observable.create(observer => source$.subscribe({
next: value => observer.next(value),
error: error => observer.error(error),
complete: () => {// do nothing
}
}));
}
function onComplete(func) {
return source$ => rxjs.Observable.create(observer => source$.subscribe({
next: value => observer.next(value),
error: error => observer.error(error),
complete: () => {
// tslint:disable-next-line no-expression-statement
func(); // tslint:disable-next-line no-expression-statement
observer.complete();
}
}));
}
exports.CustomError = CustomError;
exports.finalize = finalize;
exports.MergeScanSubscriber = MergeScanSubscriber;
exports.MergeScanOperator = MergeScanOperator;
exports.mergeScanLatest = mergeScanLatest;
exports.neverComplete = neverComplete;
exports.onComplete = onComplete;
exports.utils = utils;
//# sourceMappingURL=index.js.map