UNPKG

php-wasm

Version:

Run PHP right in the browser or anywhere else JS can run

98 lines (95 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OutputBuffer = void 0; var _Event2 = require("./_Event.js"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Buffers output bytes until they should be emitted as an event. */ var OutputBuffer = exports.OutputBuffer = /*#__PURE__*/function () { /** * Creates a new output buffer for an event target. * @param {OutputTarget} target Event target that receives flushed events. * @param {string} eventType Event name to dispatch when the buffer flushes. * @param {number} maxLength Maximum buffered byte length before an automatic flush. */ function OutputBuffer(target, eventType, maxLength) { _classCallCheck(this, OutputBuffer); /** @type {OutputTarget} */ _defineProperty(this, "target", void 0); /** @type {number[]} */ _defineProperty(this, "buffer", void 0); /** @type {string} */ _defineProperty(this, "eventType", void 0); /** @type {number} */ _defineProperty(this, "maxLength", void 0); /** @type {TextDecoder} */ _defineProperty(this, "decoder", void 0); Object.defineProperty(this, 'target', { value: target }); Object.defineProperty(this, 'buffer', { value: [] }); Object.defineProperty(this, 'eventType', { value: eventType }); Object.defineProperty(this, 'maxLength', { value: maxLength }); Object.defineProperty(this, 'decoder', { value: new TextDecoder() }); } /** * Appends bytes to the internal output buffer. * @param {...number} items Bytes to append to the buffer. */ return _createClass(OutputBuffer, [{ key: "push", value: function push() { var _this$buffer; (_this$buffer = this.buffer).push.apply(_this$buffer, arguments); var end = this.buffer.length - 1; if (this.maxLength === -1 && this.buffer[end] === 10) { this.flush(); } if (this.maxLength >= 0 && this.buffer.length >= this.maxLength) { this.flush(); } } /** * Emits the buffered output as a single event payload. */ }, { key: "flush", value: function flush() { if (!this.buffer.length) { return; } var detail = [this.decoder.decode(new Uint8Array(this.buffer))]; var event = new _Event2._Event(this.eventType, { detail: detail }); var target = /** @type {EventTarget & {[key: string]: PhpEventHook|undefined}} */this.target; var handler = target['on' + this.eventType]; if (handler) { if (handler(event) === false) { return; } } if (!target.dispatchEvent(event)) { return; } this.buffer.splice(0); } }]); }();