'use strict'; var solidJs = require('solid-js'); var web = require('solid-js/web'); // src/repeat.ts // src/common.ts var { abs, sign, min, ceil, floor } = Math; var accessor = (a) => typeof a() === "function" ? a() : a.bind(void 0); var toFunction = (a) => (...args) => { const v = a(); return typeof v === "function" ? v(...args) : v; }; // src/repeat.ts function repeat(times, mapFn, options = {}) { let disposers = [], items = [], prevLen = 0; solidJs.onCleanup(() => disposers.forEach((f) => f())); const mapLength = (len) => { if (len === 0) { disposers.forEach((f) => f()); if (options.fallback) return solidJs.createRoot((dispose) => { disposers = [dispose]; return items = [options.fallback()]; }); disposers = []; return items = []; } if (prevLen === 0) { if (disposers[0]) disposers[0](); for (let i = 0; i < len; i++) items[i] = solidJs.createRoot(mapper.bind(void 0, i)); return items; } { const diff = prevLen - len; if (diff > 0) { for (let i = prevLen - 1; i >= len; i--) disposers[i](); items.splice(len, diff); disposers.splice(len, diff); return items; } } for (let i = prevLen; i < len; i++) items[i] = solidJs.createRoot(mapper.bind(void 0, i)); return items; }; const mapper = (index, dispose) => { disposers[index] = dispose; return mapFn(index); }; const memoLen = solidJs.createMemo(() => Math.floor(Math.max(times(), 0))); return () => { const len = memoLen(); return solidJs.untrack(() => { const newItems = mapLength(len); prevLen = len; return newItems; }); }; } function Repeat(props) { return solidJs.createMemo( repeat( () => props.times, toFunction(() => props.children), "fallback" in props ? { fallback: () => props.fallback } : void 0 ) ); } function mapRange(getStart, getTo, getStep, mapFn, options = {}) { let disposers = [], items = [], prevStart = 0, prevTo = 0, prevStep = getStep(), fallback = false; solidJs.onCleanup(() => disposers.forEach((f) => f())); const mapper = (i, n, itemsList, disposersList) => solidJs.createRoot((dispose) => { disposersList[i] = dispose; itemsList[i] = mapFn(n); }); const mapNewRange = (start, to, step) => { if (start === to) { disposers.forEach((f) => f()); if (options.fallback) { fallback = true; return solidJs.createRoot((dispose) => { disposers = [dispose]; return items = [options.fallback()]; }); } disposers = []; return items = []; } let n = start, i = 0; if (fallback) { fallback = false; disposers[0](); items = []; } if (!items.length) { for (; n < to; i++, n += step) mapper(i, n, items, disposers); return items; } const newLength = ceil((to - start) / step); const newItems = new Array(newLength); const newDisposers = new Array(newLength); const oldDisposers = disposers; let end; if (start < prevStart) { end = min(ceil((prevStart - start) / step), newLength); for (n = start; i < end; i++, n += step) mapper(i, n, newItems, newDisposers); } end = ceil((min(prevTo, to) - start) / step); for (; i < end; n += step, i++) { let index = (n - prevStart) / prevStep; if (Number.isInteger(index) || index < 1 && index + Number.EPSILON > 1) { index = ceil(index); newItems[i] = items[index]; newDisposers[i] = disposers[index]; oldDisposers[index] = void 0; } else { mapper(i, n, newItems, newDisposers); } } if (to > prevTo) { for (; i < newLength; i++, n += step) mapper(i, n, newItems, newDisposers); } oldDisposers.forEach((f) => f?.()); disposers = newDisposers; return items = newItems; }; return () => { let _step = getStep(); if (_step === 0) { if (!web.isServer && solidJs.DEV) console.warn("Range cannot have a step of 0"); return items; } let _start = getStart(); let _to = getTo(); _step = abs(_step); const positive = _start <= _to; if (!positive) { const temp = _start; const x = (_start - _to) / _step; _start = _start - (Number.isInteger(x) ? x - 1 : floor(x)) * _step; _to = temp + _step; } const list = solidJs.untrack(mapNewRange.bind(void 0, _start, _to, _step)); prevStart = _start; prevTo = _to; prevStep = _step; return positive ? list : [...list].reverse(); }; } function Range(props) { let start, to, step; if ("to" in props) { start = () => props.start ?? 0; to = () => props.to; step = () => props.step ?? 1; } else { start = accessor(() => props[0]); to = accessor(() => props[1]); step = accessor(() => props[2] ?? 1); } return solidJs.createMemo( mapRange( start, to, step, toFunction(() => props.children), "fallback" in props ? { fallback: () => props.fallback } : void 0 ) ); } function indexRange(getStart, getTo, getStep, mapFn, options = {}) { let disposers = [], items = [], setters = [], fallback = false; solidJs.onCleanup(() => disposers.forEach((f) => f())); const mapper = (i, n) => solidJs.createRoot((dispose) => { const [number, setNumber] = solidJs.createSignal(n); disposers[i] = dispose; items[i] = mapFn(number); setters[i] = setNumber; }); const mapNewRange = (start, to, step) => { const newLength = abs(ceil((to - start) / step)); if (newLength === 0) { disposers.forEach((f) => f()); if (options.fallback) { fallback = true; return solidJs.createRoot((dispose) => { disposers = [dispose]; return items = [options.fallback()]; }); } disposers = []; setters = []; return items = []; } let n = start, i = 0; if (fallback) { fallback = false; disposers[0](); items = []; } const oldLength = items.length; if (!oldLength) { for (; i < newLength; i++, n += step) mapper(i, n); return items; } const bodyEnd = min(newLength, oldLength); for (; i < bodyEnd; i++, n += step) setters[i](n); for (; i < newLength; i++, n += step) mapper(i, n); if (newLength < oldLength) { items.splice(i); setters.splice(i); disposers.splice(i).forEach((f) => f()); } return items; }; return () => { let _step = getStep(); if (_step === 0) { if (!web.isServer && solidJs.DEV) console.warn("Range cannot have a step of 0"); return items; } const _start = getStart(); const _to = getTo(); _step = abs(_step) * sign(_to - _start || 1); return solidJs.untrack(mapNewRange.bind(void 0, _start, _to, _step)); }; } function IndexRange(props) { let start, to, step; if ("to" in props) { start = () => props.start ?? 0; to = () => props.to; step = () => props.step ?? 1; } else { start = accessor(() => props[0]); to = accessor(() => props[1]); step = accessor(() => props[2] ?? 1); } return solidJs.createMemo( indexRange( start, to, step, toFunction(() => props.children), "fallback" in props ? { fallback: () => props.fallback } : void 0 ) ); } exports.IndexRange = IndexRange; exports.Range = Range; exports.Repeat = Repeat; exports.indexRange = indexRange; exports.mapRange = mapRange; exports.repeat = repeat;