kitchen-simulator 3.12.0-test.2 → 3.12.0-test.3

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.
Files changed (3) hide show
  1. package/es/index.js +97 -24
  2. package/lib/index.js +97 -24
  3. package/package.json +1 -1
package/es/index.js CHANGED
@@ -249,10 +249,10 @@ export function renderKitchenSimulator(container) {
249
249
 
250
250
  // ✅ Reuse existing API for same container
251
251
  if (container[API_KEY]) {
252
- var _container$API_KEY$__, _container$API_KEY;
253
- // update render with latest props (safe)
254
- (_container$API_KEY$__ = (_container$API_KEY = container[API_KEY]).__render) === null || _container$API_KEY$__ === void 0 || _container$API_KEY$__.call(_container$API_KEY, props);
255
- return container[API_KEY];
252
+ var _api$__render;
253
+ var _api = container[API_KEY];
254
+ (_api$__render = _api.__render) === null || _api$__render === void 0 || _api$__render.call(_api, props); // will merge via lastProps inside api (after patch)
255
+ return _api;
256
256
  }
257
257
 
258
258
  // ✅ Reuse root for same container
@@ -447,32 +447,80 @@ export function renderKitchenSimulator(container) {
447
447
  }
448
448
  }]);
449
449
  }(React.Component);
450
+ var ro = null;
451
+ var roEl = null;
452
+ var roRaf = 0;
453
+ function installContentBoxObserver() {
454
+ var _lastProps$width2, _lastProps$height2;
455
+ if (typeof window === 'undefined') return;
456
+ if (typeof ResizeObserver === 'undefined') return;
457
+ var el = document.getElementById('content-box');
458
+ if (!el) return; // tool won't crash if element isn't there yet
459
+
460
+ roEl = el;
461
+ var syncFromEl = function syncFromEl(entry) {
462
+ var _lastProps$width, _lastProps$height;
463
+ // Prefer contentBoxSize when available; fallback to getBoundingClientRect.
464
+ var w = 0,
465
+ h = 0;
466
+ var cbs = entry === null || entry === void 0 ? void 0 : entry.contentBoxSize;
467
+ if (cbs) {
468
+ var _box$inlineSize, _box$blockSize;
469
+ // contentBoxSize can be array (Chrome) or single object (Firefox)
470
+ var box = Array.isArray(cbs) ? cbs[0] : cbs;
471
+ w = (_box$inlineSize = box === null || box === void 0 ? void 0 : box.inlineSize) !== null && _box$inlineSize !== void 0 ? _box$inlineSize : 0;
472
+ h = (_box$blockSize = box === null || box === void 0 ? void 0 : box.blockSize) !== null && _box$blockSize !== void 0 ? _box$blockSize : 0;
473
+ }
474
+ if (!w || !h) {
475
+ var _rect = roEl.getBoundingClientRect();
476
+ w = _rect.width;
477
+ h = _rect.height;
478
+ }
479
+ w = normalizeSize(w, (_lastProps$width = lastProps.width) !== null && _lastProps$width !== void 0 ? _lastProps$width : 1);
480
+ h = normalizeSize(h, (_lastProps$height = lastProps.height) !== null && _lastProps$height !== void 0 ? _lastProps$height : 1);
481
+
482
+ // avoid thrash: coalesce multiple observer callbacks into 1 RAF
483
+ if (roRaf) cancelAnimationFrame(roRaf);
484
+ roRaf = requestAnimationFrame(function () {
485
+ roRaf = 0;
486
+ if (destroyed) return;
487
+ // only update if changed (prevents infinite loops)
488
+ if (lastProps.width === w && lastProps.height === h) return;
489
+ // update props via existing render path
490
+ api.__render({
491
+ width: w,
492
+ height: h
493
+ });
494
+ });
495
+ };
496
+ ro = new ResizeObserver(function (entries) {
497
+ var entry = entries && entries[0];
498
+ if (!entry) return;
499
+ syncFromEl(entry);
500
+ });
501
+ ro.observe(el, {
502
+ box: 'content-box'
503
+ });
504
+
505
+ // initial sync (in case observer doesn't fire immediately)
506
+ var rect = el.getBoundingClientRect();
507
+ api.__render({
508
+ width: normalizeSize(rect.width, (_lastProps$width2 = lastProps.width) !== null && _lastProps$width2 !== void 0 ? _lastProps$width2 : 1),
509
+ height: normalizeSize(rect.height, (_lastProps$height2 = lastProps.height) !== null && _lastProps$height2 !== void 0 ? _lastProps$height2 : 1)
510
+ });
511
+ }
450
512
  var lastProps = _objectSpread({}, props);
513
+ function normalizeSize(n) {
514
+ var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
515
+ var v = typeof n === 'string' ? parseFloat(n) : n;
516
+ return Number.isFinite(v) && v > 0 ? v : fallback;
517
+ }
451
518
  var api = {
452
519
  // internal: rerender wrapper with latest props if host calls renderKitchenSimulator again
453
520
  __render: function __render(nextProps) {
454
- lastProps = _objectSpread(_objectSpread({}, lastProps), nextProps);
455
- console.log({
456
- lastProps: lastProps,
457
- nextProps: nextProps
458
- });
521
+ lastProps = _objectSpread(_objectSpread({}, lastProps), nextProps || {});
459
522
  root.render(/*#__PURE__*/React.createElement(Wrapper, lastProps));
460
523
  },
461
- updateContainerDimensions: function updateContainerDimensions(width, height) {
462
- var extraProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
463
- if (destroyed) return false;
464
-
465
- // optional: accept numbers or strings, up to you
466
- console.log({
467
- width: width,
468
- height: height
469
- });
470
- api.__render(_objectSpread(_objectSpread(_objectSpread({}, lastProps), extraProps), {}, {
471
- width: width,
472
- height: height
473
- }));
474
- return true;
475
- },
476
524
  /**
477
525
  * Send one or many events (in order).
478
526
  * Resolves when this batch has been delivered + settled.
@@ -555,6 +603,17 @@ export function renderKitchenSimulator(container) {
555
603
  container[API_KEY] = null;
556
604
  }
557
605
  };
606
+ if (ro) {
607
+ try {
608
+ ro.disconnect();
609
+ } catch (_unused2) {}
610
+ ro = null;
611
+ }
612
+ roEl = null;
613
+ if (roRaf) {
614
+ cancelAnimationFrame(roRaf);
615
+ roRaf = 0;
616
+ }
558
617
 
559
618
  // Prefer microtask when available, otherwise macrotask
560
619
  if (typeof queueMicrotask === 'function') queueMicrotask(doUnmount);else setTimeout(doUnmount, 0);
@@ -564,6 +623,20 @@ export function renderKitchenSimulator(container) {
564
623
  // first render
565
624
  api.__render(props);
566
625
 
626
+ // Install observer after initial mount.
627
+ // If #content-box is created later, retry a few frames.
628
+ (function tryInstall() {
629
+ var attempt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
630
+ if (destroyed) return;
631
+ installContentBoxObserver();
632
+ if (!ro && attempt < 30) {
633
+ // element not found yet; retry next frame for up to ~30 frames
634
+ requestAnimationFrame(function () {
635
+ return tryInstall(attempt + 1);
636
+ });
637
+ }
638
+ })();
639
+
567
640
  // store api on container so repeated calls reuse it
568
641
  container[API_KEY] = api;
569
642
  return api;
package/lib/index.js CHANGED
@@ -257,10 +257,10 @@ function renderKitchenSimulator(container) {
257
257
 
258
258
  // ✅ Reuse existing API for same container
259
259
  if (container[API_KEY]) {
260
- var _container$API_KEY$__, _container$API_KEY;
261
- // update render with latest props (safe)
262
- (_container$API_KEY$__ = (_container$API_KEY = container[API_KEY]).__render) === null || _container$API_KEY$__ === void 0 || _container$API_KEY$__.call(_container$API_KEY, props);
263
- return container[API_KEY];
260
+ var _api$__render;
261
+ var _api = container[API_KEY];
262
+ (_api$__render = _api.__render) === null || _api$__render === void 0 || _api$__render.call(_api, props); // will merge via lastProps inside api (after patch)
263
+ return _api;
264
264
  }
265
265
 
266
266
  // ✅ Reuse root for same container
@@ -455,32 +455,80 @@ function renderKitchenSimulator(container) {
455
455
  }
456
456
  }]);
457
457
  }(_react["default"].Component);
458
+ var ro = null;
459
+ var roEl = null;
460
+ var roRaf = 0;
461
+ function installContentBoxObserver() {
462
+ var _lastProps$width2, _lastProps$height2;
463
+ if (typeof window === 'undefined') return;
464
+ if (typeof ResizeObserver === 'undefined') return;
465
+ var el = document.getElementById('content-box');
466
+ if (!el) return; // tool won't crash if element isn't there yet
467
+
468
+ roEl = el;
469
+ var syncFromEl = function syncFromEl(entry) {
470
+ var _lastProps$width, _lastProps$height;
471
+ // Prefer contentBoxSize when available; fallback to getBoundingClientRect.
472
+ var w = 0,
473
+ h = 0;
474
+ var cbs = entry === null || entry === void 0 ? void 0 : entry.contentBoxSize;
475
+ if (cbs) {
476
+ var _box$inlineSize, _box$blockSize;
477
+ // contentBoxSize can be array (Chrome) or single object (Firefox)
478
+ var box = Array.isArray(cbs) ? cbs[0] : cbs;
479
+ w = (_box$inlineSize = box === null || box === void 0 ? void 0 : box.inlineSize) !== null && _box$inlineSize !== void 0 ? _box$inlineSize : 0;
480
+ h = (_box$blockSize = box === null || box === void 0 ? void 0 : box.blockSize) !== null && _box$blockSize !== void 0 ? _box$blockSize : 0;
481
+ }
482
+ if (!w || !h) {
483
+ var _rect = roEl.getBoundingClientRect();
484
+ w = _rect.width;
485
+ h = _rect.height;
486
+ }
487
+ w = normalizeSize(w, (_lastProps$width = lastProps.width) !== null && _lastProps$width !== void 0 ? _lastProps$width : 1);
488
+ h = normalizeSize(h, (_lastProps$height = lastProps.height) !== null && _lastProps$height !== void 0 ? _lastProps$height : 1);
489
+
490
+ // avoid thrash: coalesce multiple observer callbacks into 1 RAF
491
+ if (roRaf) cancelAnimationFrame(roRaf);
492
+ roRaf = requestAnimationFrame(function () {
493
+ roRaf = 0;
494
+ if (destroyed) return;
495
+ // only update if changed (prevents infinite loops)
496
+ if (lastProps.width === w && lastProps.height === h) return;
497
+ // update props via existing render path
498
+ api.__render({
499
+ width: w,
500
+ height: h
501
+ });
502
+ });
503
+ };
504
+ ro = new ResizeObserver(function (entries) {
505
+ var entry = entries && entries[0];
506
+ if (!entry) return;
507
+ syncFromEl(entry);
508
+ });
509
+ ro.observe(el, {
510
+ box: 'content-box'
511
+ });
512
+
513
+ // initial sync (in case observer doesn't fire immediately)
514
+ var rect = el.getBoundingClientRect();
515
+ api.__render({
516
+ width: normalizeSize(rect.width, (_lastProps$width2 = lastProps.width) !== null && _lastProps$width2 !== void 0 ? _lastProps$width2 : 1),
517
+ height: normalizeSize(rect.height, (_lastProps$height2 = lastProps.height) !== null && _lastProps$height2 !== void 0 ? _lastProps$height2 : 1)
518
+ });
519
+ }
458
520
  var lastProps = _objectSpread({}, props);
521
+ function normalizeSize(n) {
522
+ var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
523
+ var v = typeof n === 'string' ? parseFloat(n) : n;
524
+ return Number.isFinite(v) && v > 0 ? v : fallback;
525
+ }
459
526
  var api = {
460
527
  // internal: rerender wrapper with latest props if host calls renderKitchenSimulator again
461
528
  __render: function __render(nextProps) {
462
- lastProps = _objectSpread(_objectSpread({}, lastProps), nextProps);
463
- console.log({
464
- lastProps: lastProps,
465
- nextProps: nextProps
466
- });
529
+ lastProps = _objectSpread(_objectSpread({}, lastProps), nextProps || {});
467
530
  root.render(/*#__PURE__*/_react["default"].createElement(Wrapper, lastProps));
468
531
  },
469
- updateContainerDimensions: function updateContainerDimensions(width, height) {
470
- var extraProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
471
- if (destroyed) return false;
472
-
473
- // optional: accept numbers or strings, up to you
474
- console.log({
475
- width: width,
476
- height: height
477
- });
478
- api.__render(_objectSpread(_objectSpread(_objectSpread({}, lastProps), extraProps), {}, {
479
- width: width,
480
- height: height
481
- }));
482
- return true;
483
- },
484
532
  /**
485
533
  * Send one or many events (in order).
486
534
  * Resolves when this batch has been delivered + settled.
@@ -563,6 +611,17 @@ function renderKitchenSimulator(container) {
563
611
  container[API_KEY] = null;
564
612
  }
565
613
  };
614
+ if (ro) {
615
+ try {
616
+ ro.disconnect();
617
+ } catch (_unused2) {}
618
+ ro = null;
619
+ }
620
+ roEl = null;
621
+ if (roRaf) {
622
+ cancelAnimationFrame(roRaf);
623
+ roRaf = 0;
624
+ }
566
625
 
567
626
  // Prefer microtask when available, otherwise macrotask
568
627
  if (typeof queueMicrotask === 'function') queueMicrotask(doUnmount);else setTimeout(doUnmount, 0);
@@ -572,6 +631,20 @@ function renderKitchenSimulator(container) {
572
631
  // first render
573
632
  api.__render(props);
574
633
 
634
+ // Install observer after initial mount.
635
+ // If #content-box is created later, retry a few frames.
636
+ (function tryInstall() {
637
+ var attempt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
638
+ if (destroyed) return;
639
+ installContentBoxObserver();
640
+ if (!ro && attempt < 30) {
641
+ // element not found yet; retry next frame for up to ~30 frames
642
+ requestAnimationFrame(function () {
643
+ return tryInstall(attempt + 1);
644
+ });
645
+ }
646
+ })();
647
+
575
648
  // store api on container so repeated calls reuse it
576
649
  container[API_KEY] = api;
577
650
  return api;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitchen-simulator",
3
- "version": "3.12.0-test.2",
3
+ "version": "3.12.0-test.3",
4
4
  "description": "It is a kitchen simulator (self-contained micro-frontend).",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",