@tamagui/react-native-web-lite
Version:
React Native for Web
160 lines (159 loc) • 8.24 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var FillRateHelper_exports = {};
__export(FillRateHelper_exports, {
default: () => FillRateHelper_default
});
module.exports = __toCommonJS(FillRateHelper_exports);
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor))
throw new TypeError("Cannot call a class as a function");
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor;
}
function _define_property(obj, key, value) {
return key in obj ? Object.defineProperty(obj, key, {
value,
enumerable: !0,
configurable: !0,
writable: !0
}) : obj[key] = value, obj;
}
var Info = function Info2() {
"use strict";
_class_call_check(this, Info2), _define_property(this, "any_blank_count", 0), _define_property(this, "any_blank_ms", 0), _define_property(this, "any_blank_speed_sum", 0), _define_property(this, "mostly_blank_count", 0), _define_property(this, "mostly_blank_ms", 0), _define_property(this, "pixels_blank", 0), _define_property(this, "pixels_sampled", 0), _define_property(this, "pixels_scrolled", 0), _define_property(this, "total_time_spent", 0), _define_property(this, "sample_count", 0);
};
var DEBUG = !1, _listeners = [], _minSampleCount = 10, _sampleRate = DEBUG ? 1 : null, FillRateHelper = /* @__PURE__ */ function() {
"use strict";
function FillRateHelper2(getFrameMetrics) {
_class_call_check(this, FillRateHelper2), _define_property(this, "_anyBlankStartTime", null), _define_property(this, "_enabled", !1), _define_property(this, "_getFrameMetrics", void 0), _define_property(this, "_info", new Info()), _define_property(this, "_mostlyBlankStartTime", null), _define_property(this, "_samplesStartTime", null), this._getFrameMetrics = getFrameMetrics, this._enabled = (_sampleRate || 0) > Math.random(), this._resetData();
}
return _create_class(FillRateHelper2, [
{
key: "activate",
value: function() {
this._enabled && this._samplesStartTime == null && (DEBUG && console.debug("FillRateHelper: activate"), this._samplesStartTime = global.performance.now());
}
},
{
key: "deactivateAndFlush",
value: function() {
if (this._enabled) {
var start = this._samplesStartTime;
if (start == null) {
DEBUG && console.debug("FillRateHelper: bail on deactivate with no start time");
return;
}
if (this._info.sample_count < _minSampleCount) {
this._resetData();
return;
}
var total_time_spent = global.performance.now() - start, info = {
...this._info,
total_time_spent
};
if (DEBUG) {
var derived = {
avg_blankness: this._info.pixels_blank / this._info.pixels_sampled,
avg_speed: this._info.pixels_scrolled / (total_time_spent / 1e3),
avg_speed_when_any_blank: this._info.any_blank_speed_sum / this._info.any_blank_count,
any_blank_per_min: this._info.any_blank_count / (total_time_spent / 1e3 / 60),
any_blank_time_frac: this._info.any_blank_ms / total_time_spent,
mostly_blank_per_min: this._info.mostly_blank_count / (total_time_spent / 1e3 / 60),
mostly_blank_time_frac: this._info.mostly_blank_ms / total_time_spent
};
for (var key in derived)
derived[key] = Math.round(1e3 * derived[key]) / 1e3;
console.debug("FillRateHelper deactivateAndFlush: ", {
derived,
info
});
}
_listeners.forEach(function(listener) {
return listener(info);
}), this._resetData();
}
}
},
{
key: "computeBlankness",
value: function(props, cellsAroundViewport, scrollMetrics) {
if (!this._enabled || props.getItemCount(props.data) === 0 || cellsAroundViewport.last < cellsAroundViewport.first || this._samplesStartTime == null)
return 0;
var { dOffset, offset, velocity, visibleLength } = scrollMetrics;
this._info.sample_count++, this._info.pixels_sampled += Math.round(visibleLength), this._info.pixels_scrolled += Math.round(Math.abs(dOffset));
var scrollSpeed = Math.round(Math.abs(velocity) * 1e3), now = global.performance.now();
this._anyBlankStartTime != null && (this._info.any_blank_ms += now - this._anyBlankStartTime), this._anyBlankStartTime = null, this._mostlyBlankStartTime != null && (this._info.mostly_blank_ms += now - this._mostlyBlankStartTime), this._mostlyBlankStartTime = null;
for (var blankTop = 0, first = cellsAroundViewport.first, firstFrame = this._getFrameMetrics(first, props); first <= cellsAroundViewport.last && (!firstFrame || !firstFrame.inLayout); )
firstFrame = this._getFrameMetrics(first, props), first++;
firstFrame && first > 0 && (blankTop = Math.min(visibleLength, Math.max(0, firstFrame.offset - offset)));
for (var blankBottom = 0, last = cellsAroundViewport.last, lastFrame = this._getFrameMetrics(last, props); last >= cellsAroundViewport.first && (!lastFrame || !lastFrame.inLayout); )
lastFrame = this._getFrameMetrics(last, props), last--;
if (lastFrame && last < props.getItemCount(props.data) - 1) {
var bottomEdge = lastFrame.offset + lastFrame.length;
blankBottom = Math.min(visibleLength, Math.max(0, offset + visibleLength - bottomEdge));
}
var pixels_blank = Math.round(blankTop + blankBottom), blankness = pixels_blank / visibleLength;
return blankness > 0 ? (this._anyBlankStartTime = now, this._info.any_blank_speed_sum += scrollSpeed, this._info.any_blank_count++, this._info.pixels_blank += pixels_blank, blankness > 0.5 && (this._mostlyBlankStartTime = now, this._info.mostly_blank_count++)) : (scrollSpeed < 0.01 || Math.abs(dOffset) < 1) && this.deactivateAndFlush(), blankness;
}
},
{
key: "enabled",
value: function() {
return this._enabled;
}
},
{
key: "_resetData",
value: function() {
this._anyBlankStartTime = null, this._info = new Info(), this._mostlyBlankStartTime = null, this._samplesStartTime = null;
}
}
], [
{
key: "addListener",
value: function(callback) {
return _sampleRate === null && console.warn("Call `FillRateHelper.setSampleRate` before `addListener`."), _listeners.push(callback), {
remove: function() {
_listeners = _listeners.filter(function(listener) {
return callback !== listener;
});
}
};
}
},
{
key: "setSampleRate",
value: function(sampleRate) {
_sampleRate = sampleRate;
}
},
{
key: "setMinSampleCount",
value: function(minSampleCount) {
_minSampleCount = minSampleCount;
}
}
]), FillRateHelper2;
}(), FillRateHelper_default = FillRateHelper;
//# sourceMappingURL=index.js.map