baraqex 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -465,8 +465,107 @@ async function render(element, container) {
465
465
  }
466
466
  });
467
467
  }
468
+ function* walkElements(node) {
469
+ for (const child of node.childNodes) {
470
+ if (child.nodeType === 1) {
471
+ yield child;
472
+ yield* walkElements(child);
473
+ }
474
+ }
475
+ }
476
+ async function hydrateElement(vnode, nextElement) {
477
+ if (vnode == null || typeof vnode === "boolean") {
478
+ return true;
479
+ }
480
+ if (typeof vnode === "number" || typeof vnode === "string") {
481
+ return true;
482
+ }
483
+ if (Array.isArray(vnode)) {
484
+ for (const child of vnode) {
485
+ if (!await hydrateElement(child, nextElement))
486
+ return false;
487
+ }
488
+ return true;
489
+ }
490
+ if (typeof vnode === "object" && "type" in vnode && vnode.props !== void 0) {
491
+ const { type, props } = vnode;
492
+ if (typeof type === "function") {
493
+ if (type === Fragment) {
494
+ const children = props?.children;
495
+ if (children == null)
496
+ return true;
497
+ return hydrateElement(children, nextElement);
498
+ }
499
+ const root = getCurrentRoot() ?? void 0;
500
+ prepareRender(root);
501
+ try {
502
+ const result = type(props || {});
503
+ return await hydrateElement(result, nextElement);
504
+ } finally {
505
+ finishRender();
506
+ }
507
+ }
508
+ if (typeof type === "string") {
509
+ const el = nextElement();
510
+ if (!el)
511
+ return false;
512
+ if (el.tagName.toLowerCase() !== type.toLowerCase())
513
+ return false;
514
+ applyProps(el, props || {});
515
+ const children = props?.children;
516
+ if (children != null) {
517
+ const childArray = Array.isArray(children) ? children : [children];
518
+ for (const child of childArray) {
519
+ if (!await hydrateElement(child, nextElement))
520
+ return false;
521
+ }
522
+ }
523
+ return true;
524
+ }
525
+ return false;
526
+ }
527
+ return true;
528
+ }
468
529
  async function hydrate(element, container) {
469
- await render(element, container);
530
+ if (!isBrowser2) {
531
+ throw new Error("[baraqex] hydrate() can only be called in a browser.");
532
+ }
533
+ if (!container) {
534
+ throw new Error("[baraqex] hydrate() requires a container element");
535
+ }
536
+ if (!container.hasChildNodes()) {
537
+ return render(element, container);
538
+ }
539
+ let root = rootMap.get(container);
540
+ if (!root) {
541
+ root = createRootState();
542
+ rootMap.set(container, root);
543
+ }
544
+ await batchUpdates(async () => {
545
+ prepareRender(root);
546
+ try {
547
+ setRenderCallback(render, element, container);
548
+ const generator = walkElements(container);
549
+ const nextElement = () => {
550
+ const { value, done } = generator.next();
551
+ return done ? null : value;
552
+ };
553
+ const matched = await hydrateElement(element, nextElement);
554
+ if (!matched) {
555
+ const freshRoot = createRootState();
556
+ rootMap.set(container, freshRoot);
557
+ prepareRender(freshRoot);
558
+ setRenderCallback(render, element, container);
559
+ const domNode = await createElement(element);
560
+ container.textContent = "";
561
+ container.appendChild(domNode);
562
+ finishRender();
563
+ return;
564
+ }
565
+ } finally {
566
+ finishRender();
567
+ }
568
+ });
470
569
  }
471
570
  function createRoot(container) {
472
571
  const root = createRootState();
@@ -2284,10 +2383,22 @@ function withErrorHandling(fn, errorHandler2) {
2284
2383
  }
2285
2384
 
2286
2385
  // src/wasm.ts
2287
- async function loadGoWasm(wasmUrl, options = {}) {
2386
+ var wasmCache = /* @__PURE__ */ new Map();
2387
+ function loadGoWasm(wasmUrl, options = {}) {
2288
2388
  if (typeof window === "undefined") {
2289
2389
  throw new Error("loadGoWasm() is for browser use only. Use loadGoWasmFromFile() in Node.js.");
2290
2390
  }
2391
+ const cached = wasmCache.get(wasmUrl);
2392
+ if (cached)
2393
+ return cached;
2394
+ const request = loadGoWasmInternal(wasmUrl, options).catch((error) => {
2395
+ wasmCache.delete(wasmUrl);
2396
+ throw error;
2397
+ });
2398
+ wasmCache.set(wasmUrl, request);
2399
+ return request;
2400
+ }
2401
+ async function loadGoWasmInternal(wasmUrl, options = {}) {
2291
2402
  try {
2292
2403
  if (!window.Go) {
2293
2404
  throw new Error("Go WASM runtime not available. Make sure wasm_exec.js is loaded.");