text-stream-search
Version:
Searches for occurrences of a given search term in Node.js text streams
37 lines (36 loc) • 1.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const debug = debug_1.default("text-stream-search");
const delay_1 = __importDefault(require("delay"));
// calls the given handler exactly one time
// when text matches the given string
class BaseSearch {
constructor(accumulator, resolve, reject, timeout) {
this.accumulator = accumulator;
this.resolve = resolve;
this.reject = reject;
if (timeout != null) {
setTimeout(this.onTimeout.bind(this), timeout);
}
}
getDisplayName() {
throw new Error("implement in subclass");
}
// called when a match is found
async foundMatch() {
debug(`found match for ${this.getDisplayName()}`);
await delay_1.default(1);
this.resolve();
}
// called after a given timeout
onTimeout() {
const errorMessage = `Expected '${this.accumulator.toString()}' to include ${this.getDisplayName()}`;
debug(`Timeout: rejecting with error message '${errorMessage}'`);
this.reject(new Error(errorMessage));
}
}
exports.default = BaseSearch;