UNPKG

@brimdata/zealot

Version:

The Javascript Client for Zed Lakes

136 lines (135 loc) 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Channel", { enumerable: true, get: ()=>Channel }); const _events = /*#__PURE__*/ _interopRequireDefault(require("events")); const _zed = /*#__PURE__*/ _interopRequireWildcard(require("../zed")); const _decodeStream = require("../zed/decode-stream"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } class Channel extends _events.default { get shapes() { return Object.values(this.shapesMap); } addRow(row) { this.rows.push(row); this.emit("row", row); } addShape(id, type) { this.shapesMap[id] = type; this.emit("shape", type); } hasShape(id) { return id in this.shapesMap; } done() { this.emit("end"); } consume(json) { const value = this.stream.decode(json); if ("id" in json.type && !this.hasShape(json.type.id)) { this.addShape(json.type.id, value.type); } else if (json.type.kind === "primitive") { this.addShape(json.type.name, value.type); } this.addRow(value); } collect(collector) { /** * The goal here is to get the first batch of results out * to the collector as soon as possible. Then, only give * updates every timeThres. This allows the UI to avoid * frequent, expensive updates. */ let first = true; let count = 0; let countThresh = 2000; let timeThresh = 2000; let timeId = 0; const flush = ()=>{ collector({ rows: this.rows, shapesMap: this.shapesMap }); first = false; count = 0; clearTimeout(timeId); }; const startTimer = ()=>{ timeId = setTimeout(()=>{ if (count > 0) flush(); startTimer(); }, timeThresh); }; this.on("row", ()=>{ count += 1; if (first && count >= countThresh) flush(); }); this.on("end", ()=>flush()); startTimer(); } constructor(...args){ super(...args); _defineProperty(this, "rows", []); _defineProperty(this, "shapesMap", {}); _defineProperty(this, "stream", new _decodeStream.DecodeStream(_zed.DefaultContext)); } }