solid.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. let taskIdCounter = 1,
  2. isCallbackScheduled = false,
  3. isPerformingWork = false,
  4. taskQueue = [],
  5. currentTask = null,
  6. shouldYieldToHost = null,
  7. yieldInterval = 5,
  8. deadline = 0,
  9. maxYieldInterval = 300,
  10. scheduleCallback = null,
  11. scheduledCallback = null;
  12. const maxSigned31BitInt = 1073741823;
  13. function setupScheduler() {
  14. const channel = new MessageChannel(),
  15. port = channel.port2;
  16. scheduleCallback = () => port.postMessage(null);
  17. channel.port1.onmessage = () => {
  18. if (scheduledCallback !== null) {
  19. const currentTime = performance.now();
  20. deadline = currentTime + yieldInterval;
  21. const hasTimeRemaining = true;
  22. try {
  23. const hasMoreWork = scheduledCallback(hasTimeRemaining, currentTime);
  24. if (!hasMoreWork) {
  25. scheduledCallback = null;
  26. } else port.postMessage(null);
  27. } catch (error) {
  28. port.postMessage(null);
  29. throw error;
  30. }
  31. }
  32. };
  33. if (navigator && navigator.scheduling && navigator.scheduling.isInputPending) {
  34. const scheduling = navigator.scheduling;
  35. shouldYieldToHost = () => {
  36. const currentTime = performance.now();
  37. if (currentTime >= deadline) {
  38. if (scheduling.isInputPending()) {
  39. return true;
  40. }
  41. return currentTime >= maxYieldInterval;
  42. } else {
  43. return false;
  44. }
  45. };
  46. } else {
  47. shouldYieldToHost = () => performance.now() >= deadline;
  48. }
  49. }
  50. function enqueue(taskQueue, task) {
  51. function findIndex() {
  52. let m = 0;
  53. let n = taskQueue.length - 1;
  54. while (m <= n) {
  55. let k = n + m >> 1;
  56. let cmp = task.expirationTime - taskQueue[k].expirationTime;
  57. if (cmp > 0) m = k + 1;else if (cmp < 0) n = k - 1;else return k;
  58. }
  59. return m;
  60. }
  61. taskQueue.splice(findIndex(), 0, task);
  62. }
  63. function requestCallback(fn, options) {
  64. if (!scheduleCallback) setupScheduler();
  65. let startTime = performance.now(),
  66. timeout = maxSigned31BitInt;
  67. if (options && options.timeout) timeout = options.timeout;
  68. const newTask = {
  69. id: taskIdCounter++,
  70. fn,
  71. startTime,
  72. expirationTime: startTime + timeout
  73. };
  74. enqueue(taskQueue, newTask);
  75. if (!isCallbackScheduled && !isPerformingWork) {
  76. isCallbackScheduled = true;
  77. scheduledCallback = flushWork;
  78. scheduleCallback();
  79. }
  80. return newTask;
  81. }
  82. function cancelCallback(task) {
  83. task.fn = null;
  84. }
  85. function flushWork(hasTimeRemaining, initialTime) {
  86. isCallbackScheduled = false;
  87. isPerformingWork = true;
  88. try {
  89. return workLoop(hasTimeRemaining, initialTime);
  90. } finally {
  91. currentTask = null;
  92. isPerformingWork = false;
  93. }
  94. }
  95. function workLoop(hasTimeRemaining, initialTime) {
  96. let currentTime = initialTime;
  97. currentTask = taskQueue[0] || null;
  98. while (currentTask !== null) {
  99. if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost())) {
  100. break;
  101. }
  102. const callback = currentTask.fn;
  103. if (callback !== null) {
  104. currentTask.fn = null;
  105. const didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
  106. callback(didUserCallbackTimeout);
  107. currentTime = performance.now();
  108. if (currentTask === taskQueue[0]) {
  109. taskQueue.shift();
  110. }
  111. } else taskQueue.shift();
  112. currentTask = taskQueue[0] || null;
  113. }
  114. return currentTask !== null;
  115. }
  116. const sharedConfig = {};
  117. function setHydrateContext(context) {
  118. sharedConfig.context = context;
  119. }
  120. function nextHydrateContext() {
  121. return { ...sharedConfig.context,
  122. id: `${sharedConfig.context.id}${sharedConfig.context.count++}.`,
  123. count: 0
  124. };
  125. }
  126. const equalFn = (a, b) => a === b;
  127. const signalOptions = {
  128. equals: equalFn
  129. };
  130. let ERROR = null;
  131. let runEffects = runQueue;
  132. const NOTPENDING = {};
  133. const STALE = 1;
  134. const PENDING = 2;
  135. const UNOWNED = {
  136. owned: null,
  137. cleanups: null,
  138. context: null,
  139. owner: null
  140. };
  141. const [transPending, setTransPending] = /*@__PURE__*/createSignal(false);
  142. var Owner = null;
  143. var Listener = null;
  144. let Pending = null;
  145. let Updates = null;
  146. let Effects = null;
  147. let Transition = null;
  148. let ExecCount = 0;
  149. function createRoot(fn, detachedOwner) {
  150. detachedOwner && (Owner = detachedOwner);
  151. const listener = Listener,
  152. owner = Owner,
  153. root = fn.length === 0 && !false ? UNOWNED : {
  154. owned: null,
  155. cleanups: null,
  156. context: null,
  157. owner,
  158. attached: !!detachedOwner
  159. };
  160. Owner = root;
  161. Listener = null;
  162. let result;
  163. try {
  164. runUpdates(() => result = fn(() => cleanNode(root)), true);
  165. } finally {
  166. Listener = listener;
  167. Owner = owner;
  168. }
  169. return result;
  170. }
  171. function createSignal(value, options) {
  172. options = options ? Object.assign({}, signalOptions, options) : signalOptions;
  173. const s = {
  174. value,
  175. observers: null,
  176. observerSlots: null,
  177. pending: NOTPENDING,
  178. comparator: options.equals || undefined
  179. };
  180. return [readSignal.bind(s), writeSignal.bind(s)];
  181. }
  182. function createComputed(fn, value, options) {
  183. updateComputation(createComputation(fn, value, true));
  184. }
  185. function createRenderEffect(fn, value, options) {
  186. updateComputation(createComputation(fn, value, false));
  187. }
  188. function createEffect(fn, value, options) {
  189. runEffects = runUserEffects;
  190. const c = createComputation(fn, value, false),
  191. s = SuspenseContext && lookup(Owner, SuspenseContext.id);
  192. if (s) c.suspense = s;
  193. c.user = true;
  194. Effects && Effects.push(c);
  195. }
  196. function createMemo(fn, value, options) {
  197. options = options ? Object.assign({}, signalOptions, options) : signalOptions;
  198. const c = createComputation(fn, value, true);
  199. c.pending = NOTPENDING;
  200. c.observers = null;
  201. c.observerSlots = null;
  202. c.state = 0;
  203. c.comparator = options.equals || undefined;
  204. updateComputation(c);
  205. return readSignal.bind(c);
  206. }
  207. function createResource(source, fetcher, options = {}) {
  208. if (arguments.length === 2) {
  209. if (typeof fetcher === "object") {
  210. options = fetcher;
  211. fetcher = source;
  212. source = true;
  213. }
  214. } else if (arguments.length === 1) {
  215. fetcher = source;
  216. source = true;
  217. }
  218. const contexts = new Set(),
  219. [s, set] = createSignal(options.initialValue),
  220. [track, trigger] = createSignal(undefined, {
  221. equals: false
  222. }),
  223. [loading, setLoading] = createSignal(false),
  224. [error, setError] = createSignal();
  225. let err = undefined,
  226. pr = null,
  227. initP = null,
  228. id = null,
  229. loadedUnderTransition = false,
  230. dynamic = typeof source === "function";
  231. if (sharedConfig.context) {
  232. id = `${sharedConfig.context.id}${sharedConfig.context.count++}`;
  233. if (sharedConfig.context.loadResource) {
  234. initP = sharedConfig.context.loadResource(id);
  235. } else if (sharedConfig.resources && id && id in sharedConfig.resources) {
  236. initP = sharedConfig.resources[id];
  237. delete sharedConfig.resources[id];
  238. }
  239. }
  240. function loadEnd(p, v, e) {
  241. if (pr === p) {
  242. setError(err = e);
  243. pr = null;
  244. if (Transition && p && loadedUnderTransition) {
  245. Transition.promises.delete(p);
  246. loadedUnderTransition = false;
  247. runUpdates(() => {
  248. Transition.running = true;
  249. if (!Transition.promises.size) {
  250. Effects.push.apply(Effects, Transition.effects);
  251. Transition.effects = [];
  252. }
  253. completeLoad(v);
  254. }, false);
  255. } else completeLoad(v);
  256. }
  257. return v;
  258. }
  259. function completeLoad(v) {
  260. batch(() => {
  261. set(v);
  262. setLoading(false);
  263. for (let c of contexts.keys()) c.decrement();
  264. contexts.clear();
  265. });
  266. }
  267. function read() {
  268. const c = SuspenseContext && lookup(Owner, SuspenseContext.id),
  269. v = s();
  270. if (err) throw err;
  271. if (Listener && !Listener.user && c) {
  272. createComputed(() => {
  273. track();
  274. if (pr) {
  275. if (c.resolved && Transition) Transition.promises.add(pr);else if (!contexts.has(c)) {
  276. c.increment();
  277. contexts.add(c);
  278. }
  279. }
  280. });
  281. }
  282. return v;
  283. }
  284. function load() {
  285. setError(err = undefined);
  286. let lookup = dynamic ? source() : source;
  287. loadedUnderTransition = Transition && Transition.running;
  288. if (lookup == null || lookup === false) {
  289. loadEnd(pr, untrack(s));
  290. return;
  291. }
  292. if (Transition && pr) Transition.promises.delete(pr);
  293. const p = initP || fetcher(lookup, s);
  294. initP = null;
  295. if (typeof p !== "object" || !("then" in p)) {
  296. loadEnd(pr, p);
  297. return;
  298. }
  299. pr = p;
  300. batch(() => {
  301. setLoading(true);
  302. trigger();
  303. });
  304. p.then(v => loadEnd(p, v), e => loadEnd(p, e, e));
  305. }
  306. Object.defineProperties(read, {
  307. loading: {
  308. get() {
  309. return loading();
  310. }
  311. },
  312. error: {
  313. get() {
  314. return error();
  315. }
  316. }
  317. });
  318. if (dynamic) createComputed(load);else load();
  319. return [read, {
  320. refetch: load,
  321. mutate: set
  322. }];
  323. }
  324. function createDeferred(source, options) {
  325. let t,
  326. timeout = options ? options.timeoutMs : undefined;
  327. const node = createComputation(() => {
  328. if (!t || !t.fn) t = requestCallback(() => setDeferred(node.value), timeout !== undefined ? {
  329. timeout
  330. } : undefined);
  331. return source();
  332. }, undefined, true);
  333. const [deferred, setDeferred] = createSignal(node.value, options);
  334. updateComputation(node);
  335. setDeferred(node.value);
  336. return deferred;
  337. }
  338. function createSelector(source, fn = equalFn, options) {
  339. let subs = new Map();
  340. const node = createComputation(p => {
  341. const v = source();
  342. for (const key of subs.keys()) if (fn(key, v) || p !== undefined && fn(key, p)) {
  343. const l = subs.get(key);
  344. for (const c of l.values()) {
  345. c.state = STALE;
  346. if (c.pure) Updates.push(c);else Effects.push(c);
  347. }
  348. }
  349. return v;
  350. }, undefined, true);
  351. updateComputation(node);
  352. return key => {
  353. let listener;
  354. if (listener = Listener) {
  355. let l;
  356. if (l = subs.get(key)) l.add(listener);else subs.set(key, l = new Set([listener]));
  357. onCleanup(() => {
  358. l.size > 1 ? l.delete(listener) : subs.delete(key);
  359. });
  360. }
  361. return fn(key, node.value);
  362. };
  363. }
  364. function batch(fn) {
  365. if (Pending) return fn();
  366. let result;
  367. const q = Pending = [];
  368. try {
  369. result = fn();
  370. } finally {
  371. Pending = null;
  372. }
  373. runUpdates(() => {
  374. for (let i = 0; i < q.length; i += 1) {
  375. const data = q[i];
  376. if (data.pending !== NOTPENDING) {
  377. const pending = data.pending;
  378. data.pending = NOTPENDING;
  379. writeSignal.call(data, pending);
  380. }
  381. }
  382. }, false);
  383. return result;
  384. }
  385. function untrack(fn) {
  386. let result,
  387. listener = Listener;
  388. Listener = null;
  389. result = fn();
  390. Listener = listener;
  391. return result;
  392. }
  393. function on(deps, fn, options) {
  394. let isArray = Array.isArray(deps);
  395. let prev;
  396. let defer = options && options.defer;
  397. return prevResult => {
  398. let value;
  399. if (isArray) {
  400. value = [];
  401. for (let i = 0; i < deps.length; i++) value.push(deps[i]());
  402. } else value = deps();
  403. if (defer) {
  404. defer = false;
  405. return undefined;
  406. }
  407. const result = untrack(() => fn(value, prev, prevResult));
  408. prev = value;
  409. return result;
  410. };
  411. }
  412. function onMount(fn) {
  413. createEffect(() => untrack(fn));
  414. }
  415. function onCleanup(fn) {
  416. if (Owner === null) ;else if (Owner.cleanups === null) Owner.cleanups = [fn];else Owner.cleanups.push(fn);
  417. return fn;
  418. }
  419. function onError(fn) {
  420. ERROR || (ERROR = Symbol("error"));
  421. if (Owner === null) ;else if (Owner.context === null) Owner.context = {
  422. [ERROR]: [fn]
  423. };else if (!Owner.context[ERROR]) Owner.context[ERROR] = [fn];else Owner.context[ERROR].push(fn);
  424. }
  425. function getListener() {
  426. return Listener;
  427. }
  428. function getOwner() {
  429. return Owner;
  430. }
  431. function runWithOwner(o, fn) {
  432. const prev = Owner;
  433. Owner = o;
  434. try {
  435. return fn();
  436. } finally {
  437. Owner = prev;
  438. }
  439. }
  440. function useTransition() {
  441. return [transPending, (fn, cb) => {
  442. if (SuspenseContext) {
  443. Transition || (Transition = {
  444. sources: new Set(),
  445. effects: [],
  446. promises: new Set(),
  447. disposed: new Set(),
  448. running: true,
  449. cb: []
  450. });
  451. cb && Transition.cb.push(cb);
  452. Transition.running = true;
  453. }
  454. batch(fn);
  455. if (!SuspenseContext && cb) cb();
  456. }];
  457. }
  458. function resumeEffects(e) {
  459. Transition && (Transition.running = true);
  460. Effects.push.apply(Effects, e);
  461. e.length = 0;
  462. }
  463. function createContext(defaultValue) {
  464. const id = Symbol("context");
  465. return {
  466. id,
  467. Provider: createProvider(id),
  468. defaultValue
  469. };
  470. }
  471. function useContext(context) {
  472. return lookup(Owner, context.id) || context.defaultValue;
  473. }
  474. function children(fn) {
  475. const children = createMemo(fn);
  476. return createMemo(() => resolveChildren(children()));
  477. }
  478. let SuspenseContext;
  479. function getSuspenseContext() {
  480. return SuspenseContext || (SuspenseContext = createContext({}));
  481. }
  482. function readSignal() {
  483. if (this.state && this.sources) {
  484. const updates = Updates;
  485. Updates = null;
  486. this.state === STALE ? updateComputation(this) : lookDownstream(this);
  487. Updates = updates;
  488. }
  489. if (Listener) {
  490. const sSlot = this.observers ? this.observers.length : 0;
  491. if (!Listener.sources) {
  492. Listener.sources = [this];
  493. Listener.sourceSlots = [sSlot];
  494. } else {
  495. Listener.sources.push(this);
  496. Listener.sourceSlots.push(sSlot);
  497. }
  498. if (!this.observers) {
  499. this.observers = [Listener];
  500. this.observerSlots = [Listener.sources.length - 1];
  501. } else {
  502. this.observers.push(Listener);
  503. this.observerSlots.push(Listener.sources.length - 1);
  504. }
  505. }
  506. if (Transition && Transition.running && Transition.sources.has(this)) return this.tValue;
  507. return this.value;
  508. }
  509. function writeSignal(value, isComp) {
  510. if (this.comparator) {
  511. if (Transition && Transition.running && Transition.sources.has(this)) {
  512. if (this.comparator(this.tValue, value)) return value;
  513. } else if (this.comparator(this.value, value)) return value;
  514. }
  515. if (Pending) {
  516. if (this.pending === NOTPENDING) Pending.push(this);
  517. this.pending = value;
  518. return value;
  519. }
  520. if (Transition) {
  521. if (Transition.running || !isComp && Transition.sources.has(this)) {
  522. Transition.sources.add(this);
  523. this.tValue = value;
  524. }
  525. if (!Transition.running) this.value = value;
  526. } else this.value = value;
  527. if (this.observers && (!Updates || this.observers.length)) {
  528. runUpdates(() => {
  529. for (let i = 0; i < this.observers.length; i += 1) {
  530. const o = this.observers[i];
  531. if (Transition && Transition.running && Transition.disposed.has(o)) continue;
  532. if (o.observers && o.state !== PENDING) markUpstream(o);
  533. o.state = STALE;
  534. if (o.pure) Updates.push(o);else Effects.push(o);
  535. }
  536. if (Updates.length > 10e5) {
  537. Updates = [];
  538. if (false) ;
  539. throw new Error();
  540. }
  541. }, false);
  542. }
  543. return value;
  544. }
  545. function updateComputation(node) {
  546. if (!node.fn) return;
  547. cleanNode(node);
  548. const owner = Owner,
  549. listener = Listener,
  550. time = ExecCount;
  551. Listener = Owner = node;
  552. runComputation(node, node.value, time);
  553. if (Transition && !Transition.running && Transition.sources.has(node)) {
  554. Transition.running = true;
  555. runComputation(node, node.tValue, time);
  556. Transition.running = false;
  557. }
  558. Listener = listener;
  559. Owner = owner;
  560. }
  561. function runComputation(node, value, time) {
  562. let nextValue;
  563. try {
  564. nextValue = node.fn(value);
  565. } catch (err) {
  566. handleError(err);
  567. }
  568. if (!node.updatedAt || node.updatedAt <= time) {
  569. if (node.observers && node.observers.length) {
  570. writeSignal.call(node, nextValue, true);
  571. } else if (Transition && Transition.running && node.pure) {
  572. Transition.sources.add(node);
  573. node.tValue = nextValue;
  574. } else node.value = nextValue;
  575. node.updatedAt = time;
  576. }
  577. }
  578. function createComputation(fn, init, pure, options) {
  579. const c = {
  580. fn,
  581. state: STALE,
  582. updatedAt: null,
  583. owned: null,
  584. sources: null,
  585. sourceSlots: null,
  586. cleanups: null,
  587. value: init,
  588. owner: Owner,
  589. context: null,
  590. pure
  591. };
  592. if (Owner === null) ;else if (Owner !== UNOWNED) {
  593. if (Transition && Transition.running && Owner.pure) {
  594. if (!Owner.tOwned) Owner.tOwned = [c];else Owner.tOwned.push(c);
  595. } else {
  596. if (!Owner.owned) Owner.owned = [c];else Owner.owned.push(c);
  597. }
  598. }
  599. return c;
  600. }
  601. function runTop(node) {
  602. let top = node.state === STALE && node,
  603. pending;
  604. if (node.suspense && untrack(node.suspense.inFallback)) return node.suspense.effects.push(node);
  605. const runningTransition = Transition && Transition.running;
  606. while ((node.fn || runningTransition && node.attached) && (node = node.owner)) {
  607. if (runningTransition && Transition.disposed.has(node)) return;
  608. if (node.state === PENDING) pending = node;else if (node.state === STALE) {
  609. top = node;
  610. pending = undefined;
  611. }
  612. }
  613. if (pending) {
  614. const updates = Updates;
  615. Updates = null;
  616. lookDownstream(pending);
  617. Updates = updates;
  618. if (!top || top.state !== STALE) return;
  619. if (runningTransition) {
  620. node = top;
  621. while ((node.fn || node.attached) && (node = node.owner)) {
  622. if (Transition.disposed.has(node)) return;
  623. }
  624. }
  625. }
  626. top && updateComputation(top);
  627. }
  628. function runUpdates(fn, init) {
  629. if (Updates) return fn();
  630. let wait = false;
  631. if (!init) Updates = [];
  632. if (Effects) wait = true;else Effects = [];
  633. ExecCount++;
  634. try {
  635. fn();
  636. } catch (err) {
  637. handleError(err);
  638. } finally {
  639. completeUpdates(wait);
  640. }
  641. }
  642. function completeUpdates(wait) {
  643. if (Updates) {
  644. runQueue(Updates);
  645. Updates = null;
  646. }
  647. if (wait) return;
  648. let cbs;
  649. if (Transition && Transition.running) {
  650. if (Transition.promises.size) {
  651. Transition.running = false;
  652. Transition.effects.push.apply(Transition.effects, Effects);
  653. Effects = null;
  654. setTransPending(true);
  655. return;
  656. }
  657. const sources = Transition.sources;
  658. cbs = Transition.cb;
  659. Transition = null;
  660. batch(() => {
  661. sources.forEach(v => {
  662. v.value = v.tValue;
  663. if (v.owned) {
  664. for (let i = 0, len = v.owned.length; i < len; i++) cleanNode(v.owned[i]);
  665. }
  666. if (v.tOwned) v.owned = v.tOwned;
  667. delete v.tValue;
  668. delete v.tOwned;
  669. });
  670. setTransPending(false);
  671. });
  672. }
  673. if (Effects.length) batch(() => {
  674. runEffects(Effects);
  675. Effects = null;
  676. });else {
  677. Effects = null;
  678. }
  679. if (cbs) cbs.forEach(cb => cb());
  680. }
  681. function runQueue(queue) {
  682. for (let i = 0; i < queue.length; i++) runTop(queue[i]);
  683. }
  684. function runUserEffects(queue) {
  685. let i,
  686. userLength = 0;
  687. for (i = 0; i < queue.length; i++) {
  688. const e = queue[i];
  689. if (!e.user) runTop(e);else queue[userLength++] = e;
  690. }
  691. const resume = queue.length;
  692. for (i = 0; i < userLength; i++) runTop(queue[i]);
  693. for (i = resume; i < queue.length; i++) runTop(queue[i]);
  694. }
  695. function lookDownstream(node) {
  696. node.state = 0;
  697. for (let i = 0; i < node.sources.length; i += 1) {
  698. const source = node.sources[i];
  699. if (source.sources) {
  700. if (source.state === STALE) runTop(source);else if (source.state === PENDING) lookDownstream(source);
  701. }
  702. }
  703. }
  704. function markUpstream(node) {
  705. for (let i = 0; i < node.observers.length; i += 1) {
  706. const o = node.observers[i];
  707. if (!o.state) {
  708. o.state = PENDING;
  709. o.observers && markUpstream(o);
  710. }
  711. }
  712. }
  713. function cleanNode(node) {
  714. let i;
  715. if (node.sources) {
  716. while (node.sources.length) {
  717. const source = node.sources.pop(),
  718. index = node.sourceSlots.pop(),
  719. obs = source.observers;
  720. if (obs && obs.length) {
  721. const n = obs.pop(),
  722. s = source.observerSlots.pop();
  723. if (index < obs.length) {
  724. n.sourceSlots[s] = index;
  725. obs[index] = n;
  726. source.observerSlots[index] = s;
  727. }
  728. }
  729. }
  730. }
  731. if (Transition && Transition.running && node.pure) {
  732. if (node.tOwned) {
  733. for (i = 0; i < node.tOwned.length; i++) cleanNode(node.tOwned[i]);
  734. delete node.tOwned;
  735. }
  736. reset(node, true);
  737. } else if (node.owned) {
  738. for (i = 0; i < node.owned.length; i++) cleanNode(node.owned[i]);
  739. node.owned = null;
  740. }
  741. if (node.cleanups) {
  742. for (i = 0; i < node.cleanups.length; i++) node.cleanups[i]();
  743. node.cleanups = null;
  744. }
  745. node.state = 0;
  746. node.context = null;
  747. }
  748. function reset(node, top) {
  749. if (!top) {
  750. node.state = 0;
  751. Transition.disposed.add(node);
  752. }
  753. if (node.owned) {
  754. for (let i = 0; i < node.owned.length; i++) reset(node.owned[i]);
  755. }
  756. }
  757. function handleError(err) {
  758. const fns = ERROR && lookup(Owner, ERROR);
  759. if (!fns) throw err;
  760. fns.forEach(f => f(err));
  761. }
  762. function lookup(owner, key) {
  763. return owner && (owner.context && owner.context[key] || owner.owner && lookup(owner.owner, key));
  764. }
  765. function resolveChildren(children) {
  766. if (typeof children === "function" && !children.length) return resolveChildren(children());
  767. if (Array.isArray(children)) {
  768. const results = [];
  769. for (let i = 0; i < children.length; i++) {
  770. let result = resolveChildren(children[i]);
  771. Array.isArray(result) ? results.push.apply(results, result) : results.push(result);
  772. }
  773. return results;
  774. }
  775. return children;
  776. }
  777. function createProvider(id) {
  778. return function provider(props) {
  779. return createMemo(() => {
  780. Owner.context = {
  781. [id]: props.value
  782. };
  783. return children(() => props.children);
  784. });
  785. };
  786. }
  787. const $RAW = Symbol("state-raw"),
  788. $NODE = Symbol("state-node"),
  789. $PROXY = Symbol("state-proxy"),
  790. $NAME = Symbol("state-name");
  791. function wrap$1(value, name) {
  792. let p = value[$PROXY];
  793. if (!p) {
  794. Object.defineProperty(value, $PROXY, {
  795. value: p = new Proxy(value, proxyTraps$1)
  796. });
  797. let keys = Object.keys(value),
  798. desc = Object.getOwnPropertyDescriptors(value);
  799. for (let i = 0, l = keys.length; i < l; i++) {
  800. const prop = keys[i];
  801. if (desc[prop].get) {
  802. const get = desc[prop].get.bind(p);
  803. Object.defineProperty(value, prop, {
  804. get
  805. });
  806. }
  807. }
  808. }
  809. return p;
  810. }
  811. function isWrappable(obj) {
  812. return obj != null && typeof obj === "object" && (!obj.__proto__ || obj.__proto__ === Object.prototype || Array.isArray(obj));
  813. }
  814. function unwrap(item, set = new Set()) {
  815. let result, unwrapped, v, prop;
  816. if (result = item != null && item[$RAW]) return result;
  817. if (!isWrappable(item) || set.has(item)) return item;
  818. if (Array.isArray(item)) {
  819. if (Object.isFrozen(item)) item = item.slice(0);else set.add(item);
  820. for (let i = 0, l = item.length; i < l; i++) {
  821. v = item[i];
  822. if ((unwrapped = unwrap(v, set)) !== v) item[i] = unwrapped;
  823. }
  824. } else {
  825. if (Object.isFrozen(item)) item = Object.assign({}, item);else set.add(item);
  826. let keys = Object.keys(item),
  827. desc = Object.getOwnPropertyDescriptors(item);
  828. for (let i = 0, l = keys.length; i < l; i++) {
  829. prop = keys[i];
  830. if (desc[prop].get) continue;
  831. v = item[prop];
  832. if ((unwrapped = unwrap(v, set)) !== v) item[prop] = unwrapped;
  833. }
  834. }
  835. return item;
  836. }
  837. function getDataNodes(target) {
  838. let nodes = target[$NODE];
  839. if (!nodes) Object.defineProperty(target, $NODE, {
  840. value: nodes = {}
  841. });
  842. return nodes;
  843. }
  844. function proxyDescriptor(target, property) {
  845. const desc = Reflect.getOwnPropertyDescriptor(target, property);
  846. if (!desc || desc.get || property === $PROXY || property === $NODE || property === $NAME) return desc;
  847. delete desc.value;
  848. delete desc.writable;
  849. desc.get = () => target[$PROXY][property];
  850. return desc;
  851. }
  852. function createDataNode() {
  853. const [s, set] = createSignal(undefined, {
  854. equals: false
  855. });
  856. s.set = set;
  857. return s;
  858. }
  859. const proxyTraps$1 = {
  860. get(target, property, receiver) {
  861. if (property === $RAW) return target;
  862. if (property === $PROXY) return receiver;
  863. const value = target[property];
  864. if (property === $NODE || property === "__proto__") return value;
  865. const wrappable = isWrappable(value);
  866. if (Listener && (typeof value !== "function" || target.hasOwnProperty(property))) {
  867. let nodes, node;
  868. if (wrappable && (nodes = getDataNodes(value))) {
  869. node = nodes._ || (nodes._ = createDataNode());
  870. node();
  871. }
  872. nodes = getDataNodes(target);
  873. node = nodes[property] || (nodes[property] = createDataNode());
  874. node();
  875. }
  876. return wrappable ? wrap$1(value) : value;
  877. },
  878. set() {
  879. return true;
  880. },
  881. deleteProperty() {
  882. return true;
  883. },
  884. getOwnPropertyDescriptor: proxyDescriptor
  885. };
  886. function setProperty(state, property, value) {
  887. if (state[property] === value) return;
  888. const array = Array.isArray(state);
  889. const len = state.length;
  890. const notify = array || !(property in state);
  891. if (value === undefined) {
  892. delete state[property];
  893. } else state[property] = value;
  894. let nodes = getDataNodes(state),
  895. node;
  896. (node = nodes[property]) && node.set();
  897. if (array && state.length !== len) (node = nodes.length) && node.set();
  898. notify && (node = nodes._) && node.set();
  899. }
  900. function mergeState(state, value) {
  901. const keys = Object.keys(value);
  902. for (let i = 0; i < keys.length; i += 1) {
  903. const key = keys[i];
  904. setProperty(state, key, value[key]);
  905. }
  906. }
  907. function updatePath(current, path, traversed = []) {
  908. let part,
  909. prev = current;
  910. if (path.length > 1) {
  911. part = path.shift();
  912. const partType = typeof part,
  913. isArray = Array.isArray(current);
  914. if (Array.isArray(part)) {
  915. for (let i = 0; i < part.length; i++) {
  916. updatePath(current, [part[i]].concat(path), [part[i]].concat(traversed));
  917. }
  918. return;
  919. } else if (isArray && partType === "function") {
  920. for (let i = 0; i < current.length; i++) {
  921. if (part(current[i], i)) updatePath(current, [i].concat(path), [i].concat(traversed));
  922. }
  923. return;
  924. } else if (isArray && partType === "object") {
  925. const {
  926. from = 0,
  927. to = current.length - 1,
  928. by = 1
  929. } = part;
  930. for (let i = from; i <= to; i += by) {
  931. updatePath(current, [i].concat(path), [i].concat(traversed));
  932. }
  933. return;
  934. } else if (path.length > 1) {
  935. updatePath(current[part], path, [part].concat(traversed));
  936. return;
  937. }
  938. prev = current[part];
  939. traversed = [part].concat(traversed);
  940. }
  941. let value = path[0];
  942. if (typeof value === "function") {
  943. value = value(prev, traversed);
  944. if (value === prev) return;
  945. }
  946. if (part === undefined && value == undefined) return;
  947. value = unwrap(value);
  948. if (part === undefined || isWrappable(prev) && isWrappable(value) && !Array.isArray(value)) {
  949. mergeState(prev, value);
  950. } else setProperty(current, part, value);
  951. }
  952. function createState(state, options) {
  953. const unwrappedState = unwrap(state || {});
  954. const wrappedState = wrap$1(unwrappedState);
  955. function setState(...args) {
  956. batch(() => updatePath(unwrappedState, args));
  957. }
  958. return [wrappedState, setState];
  959. }
  960. const proxyTraps = {
  961. get(target, property, receiver) {
  962. if (property === $RAW) return target;
  963. if (property === $PROXY) return receiver;
  964. const value = target[property];
  965. if (property === $NODE || property === "__proto__") return value;
  966. const wrappable = isWrappable(value);
  967. if (Listener && (typeof value !== "function" || target.hasOwnProperty(property))) {
  968. let nodes, node;
  969. if (wrappable && (nodes = getDataNodes(value))) {
  970. node = nodes._ || (nodes._ = createDataNode());
  971. node();
  972. }
  973. nodes = getDataNodes(target);
  974. node = nodes[property] || (nodes[property] = createDataNode());
  975. node();
  976. }
  977. return wrappable ? wrap(value) : value;
  978. },
  979. set(target, property, value) {
  980. setProperty(target, property, unwrap(value));
  981. return true;
  982. },
  983. deleteProperty(target, property) {
  984. setProperty(target, property, undefined);
  985. return true;
  986. },
  987. getOwnPropertyDescriptor: proxyDescriptor
  988. };
  989. function wrap(value, name) {
  990. let p = value[$PROXY];
  991. if (!p) {
  992. Object.defineProperty(value, $PROXY, {
  993. value: p = new Proxy(value, proxyTraps)
  994. });
  995. let keys = Object.keys(value),
  996. desc = Object.getOwnPropertyDescriptors(value);
  997. for (let i = 0, l = keys.length; i < l; i++) {
  998. const prop = keys[i];
  999. if (desc[prop].get) {
  1000. const get = desc[prop].get.bind(p);
  1001. Object.defineProperty(value, prop, {
  1002. get
  1003. });
  1004. }
  1005. if (desc[prop].set) {
  1006. const og = desc[prop].set,
  1007. set = v => batch(() => og.call(p, v));
  1008. Object.defineProperty(value, prop, {
  1009. set
  1010. });
  1011. }
  1012. }
  1013. }
  1014. return p;
  1015. }
  1016. function createMutable(state, options) {
  1017. const unwrappedState = unwrap(state || {});
  1018. const wrappedState = wrap(unwrappedState);
  1019. return wrappedState;
  1020. }
  1021. function getSymbol() {
  1022. const SymbolCopy = Symbol;
  1023. return SymbolCopy.observable || '@@observable';
  1024. }
  1025. function observable(input) {
  1026. const $$observable = getSymbol();
  1027. return {
  1028. subscribe(observer) {
  1029. if (!(observer instanceof Object) || observer == null) {
  1030. throw new TypeError("Expected the observer to be an object.");
  1031. }
  1032. const handler = "next" in observer ? observer.next : observer;
  1033. let complete = false;
  1034. createComputed(() => {
  1035. if (complete) return;
  1036. const v = input();
  1037. untrack(() => handler(v));
  1038. });
  1039. return {
  1040. unsubscribe() {
  1041. complete = true;
  1042. }
  1043. };
  1044. },
  1045. [$$observable]() {
  1046. return this;
  1047. }
  1048. };
  1049. }
  1050. function applyState(target, parent, property, merge, key) {
  1051. let previous = parent[property];
  1052. if (target === previous) return;
  1053. if (!isWrappable(target) || !isWrappable(previous) || key && target[key] !== previous[key]) {
  1054. target !== previous && setProperty(parent, property, target);
  1055. return;
  1056. }
  1057. if (Array.isArray(target)) {
  1058. if (target.length && previous.length && (!merge || key && target[0][key] != null)) {
  1059. let i, j, start, end, newEnd, item, newIndicesNext, keyVal;
  1060. for (start = 0, end = Math.min(previous.length, target.length); start < end && (previous[start] === target[start] || key && previous[start][key] === target[start][key]); start++) {
  1061. applyState(target[start], previous, start, merge, key);
  1062. }
  1063. const temp = new Array(target.length),
  1064. newIndices = new Map();
  1065. for (end = previous.length - 1, newEnd = target.length - 1; end >= start && newEnd >= start && (previous[end] === target[newEnd] || key && previous[end][key] === target[newEnd][key]); end--, newEnd--) {
  1066. temp[newEnd] = previous[end];
  1067. }
  1068. if (start > newEnd || start > end) {
  1069. for (j = start; j <= newEnd; j++) setProperty(previous, j, target[j]);
  1070. for (; j < target.length; j++) {
  1071. setProperty(previous, j, temp[j]);
  1072. applyState(target[j], previous, j, merge, key);
  1073. }
  1074. if (previous.length > target.length) setProperty(previous, "length", target.length);
  1075. return;
  1076. }
  1077. newIndicesNext = new Array(newEnd + 1);
  1078. for (j = newEnd; j >= start; j--) {
  1079. item = target[j];
  1080. keyVal = key ? item[key] : item;
  1081. i = newIndices.get(keyVal);
  1082. newIndicesNext[j] = i === undefined ? -1 : i;
  1083. newIndices.set(keyVal, j);
  1084. }
  1085. for (i = start; i <= end; i++) {
  1086. item = previous[i];
  1087. keyVal = key ? item[key] : item;
  1088. j = newIndices.get(keyVal);
  1089. if (j !== undefined && j !== -1) {
  1090. temp[j] = previous[i];
  1091. j = newIndicesNext[j];
  1092. newIndices.set(keyVal, j);
  1093. }
  1094. }
  1095. for (j = start; j < target.length; j++) {
  1096. if (j in temp) {
  1097. setProperty(previous, j, temp[j]);
  1098. applyState(target[j], previous, j, merge, key);
  1099. } else setProperty(previous, j, target[j]);
  1100. }
  1101. } else {
  1102. for (let i = 0, len = target.length; i < len; i++) {
  1103. applyState(target[i], previous, i, merge, key);
  1104. }
  1105. }
  1106. if (previous.length > target.length) setProperty(previous, "length", target.length);
  1107. return;
  1108. }
  1109. const targetKeys = Object.keys(target);
  1110. for (let i = 0, len = targetKeys.length; i < len; i++) {
  1111. applyState(target[targetKeys[i]], previous, targetKeys[i], merge, key);
  1112. }
  1113. const previousKeys = Object.keys(previous);
  1114. for (let i = 0, len = previousKeys.length; i < len; i++) {
  1115. if (target[previousKeys[i]] === undefined) setProperty(previous, previousKeys[i], undefined);
  1116. }
  1117. }
  1118. function reconcile(value, options = {}) {
  1119. const {
  1120. merge,
  1121. key = "id"
  1122. } = options,
  1123. v = unwrap(value);
  1124. return state => {
  1125. if (!isWrappable(state) || !isWrappable(v)) return v;
  1126. applyState(v, {
  1127. state
  1128. }, "state", merge, key);
  1129. return state;
  1130. };
  1131. }
  1132. const setterTraps = {
  1133. get(target, property) {
  1134. if (property === $RAW) return target;
  1135. const value = target[property];
  1136. return isWrappable(value) ? new Proxy(value, setterTraps) : value;
  1137. },
  1138. set(target, property, value) {
  1139. setProperty(target, property, unwrap(value));
  1140. return true;
  1141. },
  1142. deleteProperty(target, property) {
  1143. setProperty(target, property, undefined);
  1144. return true;
  1145. }
  1146. };
  1147. function produce(fn) {
  1148. return state => {
  1149. if (isWrappable(state)) fn(new Proxy(state, setterTraps));
  1150. return state;
  1151. };
  1152. }
  1153. const FALLBACK = Symbol("fallback");
  1154. function mapArray(list, mapFn, options = {}) {
  1155. let items = [],
  1156. mapped = [],
  1157. disposers = [],
  1158. len = 0,
  1159. indexes = mapFn.length > 1 ? [] : null,
  1160. ctx = Owner;
  1161. onCleanup(() => {
  1162. for (let i = 0, length = disposers.length; i < length; i++) disposers[i]();
  1163. });
  1164. return () => {
  1165. let newItems = list() || [],
  1166. i,
  1167. j;
  1168. return untrack(() => {
  1169. let newLen = newItems.length,
  1170. newIndices,
  1171. newIndicesNext,
  1172. temp,
  1173. tempdisposers,
  1174. tempIndexes,
  1175. start,
  1176. end,
  1177. newEnd,
  1178. item;
  1179. if (newLen === 0) {
  1180. if (len !== 0) {
  1181. for (i = 0; i < len; i++) disposers[i]();
  1182. disposers = [];
  1183. items = [];
  1184. mapped = [];
  1185. len = 0;
  1186. indexes && (indexes = []);
  1187. }
  1188. if (options.fallback) {
  1189. items = [FALLBACK];
  1190. mapped[0] = createRoot(disposer => {
  1191. disposers[0] = disposer;
  1192. return options.fallback();
  1193. }, ctx);
  1194. len = 1;
  1195. }
  1196. }
  1197. else if (len === 0) {
  1198. for (j = 0; j < newLen; j++) {
  1199. items[j] = newItems[j];
  1200. mapped[j] = createRoot(mapper, ctx);
  1201. }
  1202. len = newLen;
  1203. } else {
  1204. temp = new Array(newLen);
  1205. tempdisposers = new Array(newLen);
  1206. indexes && (tempIndexes = new Array(newLen));
  1207. for (start = 0, end = Math.min(len, newLen); start < end && items[start] === newItems[start]; start++);
  1208. for (end = len - 1, newEnd = newLen - 1; end >= start && newEnd >= start && items[end] === newItems[newEnd]; end--, newEnd--) {
  1209. temp[newEnd] = mapped[end];
  1210. tempdisposers[newEnd] = disposers[end];
  1211. indexes && (tempIndexes[newEnd] = indexes[end]);
  1212. }
  1213. newIndices = new Map();
  1214. newIndicesNext = new Array(newEnd + 1);
  1215. for (j = newEnd; j >= start; j--) {
  1216. item = newItems[j];
  1217. i = newIndices.get(item);
  1218. newIndicesNext[j] = i === undefined ? -1 : i;
  1219. newIndices.set(item, j);
  1220. }
  1221. for (i = start; i <= end; i++) {
  1222. item = items[i];
  1223. j = newIndices.get(item);
  1224. if (j !== undefined && j !== -1) {
  1225. temp[j] = mapped[i];
  1226. tempdisposers[j] = disposers[i];
  1227. indexes && (tempIndexes[j] = indexes[i]);
  1228. j = newIndicesNext[j];
  1229. newIndices.set(item, j);
  1230. } else disposers[i]();
  1231. }
  1232. for (j = start; j < newLen; j++) {
  1233. if (j in temp) {
  1234. mapped[j] = temp[j];
  1235. disposers[j] = tempdisposers[j];
  1236. if (indexes) {
  1237. indexes[j] = tempIndexes[j];
  1238. indexes[j](j);
  1239. }
  1240. } else mapped[j] = createRoot(mapper, ctx);
  1241. }
  1242. len = mapped.length = newLen;
  1243. items = newItems.slice(0);
  1244. }
  1245. return mapped;
  1246. });
  1247. function mapper(disposer) {
  1248. disposers[j] = disposer;
  1249. if (indexes) {
  1250. const [s, set] = createSignal(j);
  1251. indexes[j] = set;
  1252. return mapFn(newItems[j], s);
  1253. }
  1254. return mapFn(newItems[j]);
  1255. }
  1256. };
  1257. }
  1258. function indexArray(list, mapFn, options = {}) {
  1259. let items = [],
  1260. mapped = [],
  1261. disposers = [],
  1262. signals = [],
  1263. len = 0,
  1264. i,
  1265. ctx = Owner;
  1266. onCleanup(() => {
  1267. for (let i = 0, length = disposers.length; i < length; i++) disposers[i]();
  1268. });
  1269. return () => {
  1270. const newItems = list() || [];
  1271. return untrack(() => {
  1272. if (newItems.length === 0) {
  1273. if (len !== 0) {
  1274. for (i = 0; i < len; i++) disposers[i]();
  1275. disposers = [];
  1276. items = [];
  1277. mapped = [];
  1278. len = 0;
  1279. signals = [];
  1280. }
  1281. if (options.fallback) {
  1282. items = [FALLBACK];
  1283. mapped[0] = createRoot(disposer => {
  1284. disposers[0] = disposer;
  1285. return options.fallback();
  1286. }, ctx);
  1287. len = 1;
  1288. }
  1289. return mapped;
  1290. }
  1291. if (items[0] === FALLBACK) {
  1292. disposers[0]();
  1293. disposers = [];
  1294. items = [];
  1295. mapped = [];
  1296. len = 0;
  1297. }
  1298. for (i = 0; i < newItems.length; i++) {
  1299. if (i < items.length && items[i] !== newItems[i]) {
  1300. signals[i](newItems[i]);
  1301. } else if (i >= items.length) {
  1302. mapped[i] = createRoot(mapper, ctx);
  1303. }
  1304. }
  1305. for (; i < items.length; i++) {
  1306. disposers[i]();
  1307. }
  1308. len = mapped.length = signals.length = disposers.length = newItems.length;
  1309. items = newItems.slice(0);
  1310. return mapped;
  1311. });
  1312. function mapper(disposer) {
  1313. disposers[i] = disposer;
  1314. const [s, set] = createSignal(newItems[i]);
  1315. signals[i] = set;
  1316. return mapFn(s, i);
  1317. }
  1318. };
  1319. }
  1320. function createComponent(Comp, props) {
  1321. if (sharedConfig.context) {
  1322. const c = sharedConfig.context;
  1323. setHydrateContext(nextHydrateContext());
  1324. const r = untrack(() => Comp(props));
  1325. setHydrateContext(c);
  1326. return r;
  1327. }
  1328. return untrack(() => Comp(props));
  1329. }
  1330. function trueFn() {
  1331. return true;
  1332. }
  1333. const propTraps = {
  1334. get(_, property, receiver) {
  1335. if (property === $PROXY) return receiver;
  1336. return _.get(property);
  1337. },
  1338. has(_, property) {
  1339. return _.has(property);
  1340. },
  1341. set: trueFn,
  1342. deleteProperty: trueFn,
  1343. getOwnPropertyDescriptor(_, property) {
  1344. return {
  1345. configurable: true,
  1346. enumerable: true,
  1347. get() {
  1348. return _.get(property);
  1349. },
  1350. set: trueFn,
  1351. deleteProperty: trueFn
  1352. };
  1353. },
  1354. ownKeys(_) {
  1355. return _.keys();
  1356. }
  1357. };
  1358. function mergeProps(...sources) {
  1359. return new Proxy({
  1360. get(property) {
  1361. for (let i = sources.length - 1; i >= 0; i--) {
  1362. const v = sources[i][property];
  1363. if (v !== undefined) return v;
  1364. }
  1365. },
  1366. has(property) {
  1367. for (let i = sources.length - 1; i >= 0; i--) {
  1368. if (property in sources[i]) return true;
  1369. }
  1370. return false;
  1371. },
  1372. keys() {
  1373. const keys = [];
  1374. for (let i = 0; i < sources.length; i++) keys.push(...Object.keys(sources[i]));
  1375. return [...new Set(keys)];
  1376. }
  1377. }, propTraps);
  1378. }
  1379. function splitProps(props, ...keys) {
  1380. const blocked = new Set(keys.flat());
  1381. const descriptors = Object.getOwnPropertyDescriptors(props);
  1382. const res = keys.map(k => {
  1383. const clone = {};
  1384. for (let i = 0; i < k.length; i++) {
  1385. const key = k[i];
  1386. Object.defineProperty(clone, key, descriptors[key] ? descriptors[key] : {
  1387. get() {
  1388. return props[key];
  1389. }
  1390. });
  1391. }
  1392. return clone;
  1393. });
  1394. res.push(new Proxy({
  1395. get(property) {
  1396. return blocked.has(property) ? undefined : props[property];
  1397. },
  1398. has(property) {
  1399. return blocked.has(property) ? false : property in props;
  1400. },
  1401. keys() {
  1402. return Object.keys(props).filter(k => !blocked.has(k));
  1403. }
  1404. }, propTraps));
  1405. return res;
  1406. }
  1407. function lazy(fn) {
  1408. let p;
  1409. const wrap = props => {
  1410. const ctx = sharedConfig.context;
  1411. let comp;
  1412. if (ctx && sharedConfig.resources) {
  1413. ctx.count++;
  1414. const [s, set] = createSignal();
  1415. (p || (p = fn())).then(mod => {
  1416. setHydrateContext(ctx);
  1417. set(mod.default);
  1418. setHydrateContext(undefined);
  1419. });
  1420. comp = s;
  1421. } else {
  1422. const [s] = createResource(() => (p || (p = fn())).then(mod => mod.default));
  1423. comp = s;
  1424. }
  1425. let Comp;
  1426. return createMemo(() => (Comp = comp()) && untrack(() => {
  1427. if (!ctx) return Comp(props);
  1428. const c = sharedConfig.context;
  1429. setHydrateContext(ctx);
  1430. const r = Comp(props);
  1431. setHydrateContext(c);
  1432. return r;
  1433. }));
  1434. };
  1435. wrap.preload = () => (p || (p = fn())).then(mod => mod.default);
  1436. return wrap;
  1437. }
  1438. function For(props) {
  1439. const fallback = "fallback" in props && {
  1440. fallback: () => props.fallback
  1441. };
  1442. return createMemo(mapArray(() => props.each, props.children, fallback ? fallback : undefined), undefined, {
  1443. equals: false
  1444. });
  1445. }
  1446. function Index(props) {
  1447. const fallback = "fallback" in props && {
  1448. fallback: () => props.fallback
  1449. };
  1450. return createMemo(indexArray(() => props.each, props.children, fallback ? fallback : undefined), undefined, {
  1451. equals: false
  1452. });
  1453. }
  1454. function Show(props) {
  1455. let strictEqual = false;
  1456. const condition = createMemo(() => props.when, undefined, {
  1457. equals: (a, b) => strictEqual ? a === b : !a === !b
  1458. });
  1459. return createMemo(() => {
  1460. const c = condition();
  1461. if (c) {
  1462. const child = props.children;
  1463. return (strictEqual = typeof child === "function" && child.length > 0) ? untrack(() => child(c)) : child;
  1464. }
  1465. return props.fallback;
  1466. });
  1467. }
  1468. function Switch(props) {
  1469. let strictEqual = false;
  1470. const conditions = children(() => props.children),
  1471. evalConditions = createMemo(() => {
  1472. let conds = conditions();
  1473. if (!Array.isArray(conds)) conds = [conds];
  1474. for (let i = 0; i < conds.length; i++) {
  1475. const c = conds[i].when;
  1476. if (c) return [i, c, conds[i]];
  1477. }
  1478. return [-1];
  1479. }, undefined, {
  1480. equals: (a, b) => a && a[0] === b[0] && (strictEqual ? a[1] === b[1] : !a[1] === !b[1]) && a[2] === b[2]
  1481. });
  1482. return createMemo(() => {
  1483. const [index, when, cond] = evalConditions();
  1484. if (index < 0) return props.fallback;
  1485. const c = cond.children;
  1486. return (strictEqual = typeof c === "function" && c.length > 0) ? untrack(() => c(when)) : c;
  1487. });
  1488. }
  1489. function Match(props) {
  1490. return props;
  1491. }
  1492. function ErrorBoundary(props) {
  1493. const [errored, setErrored] = createSignal();
  1494. onError(setErrored);
  1495. let e;
  1496. return createMemo(() => {
  1497. if ((e = errored()) != null) {
  1498. const f = props.fallback;
  1499. return typeof f === "function" && f.length ? untrack(() => f(e, () => setErrored(null))) : f;
  1500. }
  1501. return props.children;
  1502. });
  1503. }
  1504. const SuspenseListContext = createContext();
  1505. function SuspenseList(props) {
  1506. let index = 0,
  1507. suspenseSetter,
  1508. showContent,
  1509. showFallback;
  1510. const listContext = useContext(SuspenseListContext);
  1511. if (listContext) {
  1512. const [inFallback, setFallback] = createSignal(false);
  1513. suspenseSetter = setFallback;
  1514. [showContent, showFallback] = listContext.register(inFallback);
  1515. }
  1516. const registry = [],
  1517. comp = createComponent(SuspenseListContext.Provider, {
  1518. value: {
  1519. register: inFallback => {
  1520. const [showingContent, showContent] = createSignal(false),
  1521. [showingFallback, showFallback] = createSignal(false);
  1522. registry[index++] = {
  1523. inFallback,
  1524. showContent,
  1525. showFallback
  1526. };
  1527. return [showingContent, showingFallback];
  1528. }
  1529. },
  1530. get children() {
  1531. return props.children;
  1532. }
  1533. });
  1534. createComputed(() => {
  1535. const reveal = props.revealOrder,
  1536. tail = props.tail,
  1537. visibleContent = showContent ? showContent() : true,
  1538. visibleFallback = showFallback ? showFallback() : true,
  1539. reverse = reveal === "backwards";
  1540. if (reveal === "together") {
  1541. const all = registry.every(i => !i.inFallback());
  1542. suspenseSetter && suspenseSetter(!all);
  1543. registry.forEach(i => {
  1544. i.showContent(all && visibleContent);
  1545. i.showFallback(visibleFallback);
  1546. });
  1547. return;
  1548. }
  1549. let stop = false;
  1550. for (let i = 0, len = registry.length; i < len; i++) {
  1551. const n = reverse ? len - i - 1 : i,
  1552. s = registry[n].inFallback();
  1553. if (!stop && !s) {
  1554. registry[n].showContent(visibleContent);
  1555. registry[n].showFallback(visibleFallback);
  1556. } else {
  1557. const next = !stop;
  1558. if (next && suspenseSetter) suspenseSetter(true);
  1559. if (!tail || next && tail === "collapsed") {
  1560. registry[n].showFallback(visibleFallback);
  1561. } else registry[n].showFallback(false);
  1562. stop = true;
  1563. registry[n].showContent(next);
  1564. }
  1565. }
  1566. if (!stop && suspenseSetter) suspenseSetter(false);
  1567. });
  1568. return comp;
  1569. }
  1570. function Suspense(props) {
  1571. let counter = 0,
  1572. showContent,
  1573. showFallback;
  1574. const [inFallback, setFallback] = createSignal(false),
  1575. SuspenseContext = getSuspenseContext(),
  1576. store = {
  1577. increment: () => {
  1578. if (++counter === 1) setFallback(true);
  1579. },
  1580. decrement: () => {
  1581. if (--counter === 0) setFallback(false);
  1582. },
  1583. inFallback,
  1584. effects: [],
  1585. resolved: false
  1586. };
  1587. const listContext = useContext(SuspenseListContext);
  1588. if (listContext) [showContent, showFallback] = listContext.register(store.inFallback);
  1589. return createComponent(SuspenseContext.Provider, {
  1590. value: store,
  1591. get children() {
  1592. const rendered = untrack(() => props.children);
  1593. return createMemo(() => {
  1594. const inFallback = store.inFallback(),
  1595. visibleContent = showContent ? showContent() : true,
  1596. visibleFallback = showFallback ? showFallback() : true;
  1597. if (!inFallback && visibleContent) {
  1598. store.resolved = true;
  1599. resumeEffects(store.effects);
  1600. return rendered;
  1601. }
  1602. if (!visibleFallback) return;
  1603. return props.fallback;
  1604. });
  1605. }
  1606. });
  1607. }
  1608. function awaitSuspense() {}
  1609. let DEV;
  1610. export { $RAW, DEV, ErrorBoundary, For, Index, Match, Show, Suspense, SuspenseList, Switch, awaitSuspense, batch, cancelCallback, children, createComponent, createComputed, createContext, createDeferred, createEffect, createMemo, createMutable, createRenderEffect, createResource, createRoot, createSelector, createSignal, createState, equalFn, getListener, getOwner, indexArray, lazy, mapArray, mergeProps, observable, on, onCleanup, onError, onMount, produce, reconcile, requestCallback, runWithOwner, sharedConfig, splitProps, untrack, unwrap, useContext, useTransition };