UNPKG

opendash

Version:

open.DASH data visualization framework

48 lines 2.29 kB
var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; import { useState, useEffect, useRef } from "react"; import { useOpenDashServices, } from ".."; export function useDataItemHistory(item, options) { var _a, _b, _c, _d, _e, _f, _g; var DataService = useOpenDashServices().DataService; var loadingRef = useRef(false); var bufferRef = useRef([]); var _h = useState([]), result = _h[0], setResult = _h[1]; useEffect(function () { var _a; if (item && ((_a = options) === null || _a === void 0 ? void 0 : _a.live)) { return DataService.subscribeValue(item, function () { var value = DataService._getValueOrThrowSync(item); if (loadingRef.current) { bufferRef.current.push(value); } else { setResult(function (current) { return __spreadArrays(current, [value]); }); } }); } }, [(_a = item) === null || _a === void 0 ? void 0 : _a.id, (_b = item) === null || _b === void 0 ? void 0 : _b.source, (_c = options) === null || _c === void 0 ? void 0 : _c.live]); useEffect(function () { if (item) { loadingRef.current = true; DataService.fetchValues(item, options).then(function (result) { loadingRef.current = false; setResult(__spreadArrays(result, bufferRef.current)); }, function (error) { loadingRef.current = false; console.error(error); setResult([]); }); } else { setResult([]); } }, [(_d = item) === null || _d === void 0 ? void 0 : _d.id, (_e = item) === null || _e === void 0 ? void 0 : _e.source, (_f = options) === null || _f === void 0 ? void 0 : _f.start, (_g = options) === null || _g === void 0 ? void 0 : _g.end]); return result; } //# sourceMappingURL=useDataItemHistory.js.map