kitchen-simulator 7.0.3-react-18 → 7.0.4-react-18

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.
@@ -1,77 +1,49 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
- import React, { useEffect, useRef } from 'react';
4
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
4
5
  import { useResizeDetector } from 'react-resize-detector';
5
6
  import mockProps from "./mocks/mockProps.json";
6
7
  import projectItemsCatalog from "./mocks/projectItemsCatalog.json";
7
8
  import renderKitchenSimulator from "./index";
8
- import { EXTERNAL_EVENT_CENTERING_2D, EXTERNAL_EVENT_LOAD_PROJECT, INTERNAL_EVENT_ITEMS_CATALOG } from "./constants";
9
+ import { EXTERNAL_EVENT_CENTERING_2D, INTERNAL_EVENT_ITEMS_CATALOG } from "./constants";
9
10
  var options = {
10
11
  unit: 'in',
11
12
  enable3D: true
12
13
  };
13
- var onInternalEvent = /*#__PURE__*/function () {
14
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(evt, callback) {
15
- var _t, _t2;
16
- return _regeneratorRuntime.wrap(function (_context) {
17
- while (1) switch (_context.prev = _context.next) {
18
- case 0:
19
- console.log('INTERNAL:', evt.type);
20
- _context.prev = 1;
21
- if (!(evt.type === INTERNAL_EVENT_ITEMS_CATALOG)) {
22
- _context.next = 3;
23
- break;
24
- }
25
- _context.next = 2;
26
- return callback === null || callback === void 0 ? void 0 : callback(projectItemsCatalog);
27
- case 2:
28
- return _context.abrupt("return");
29
- case 3:
30
- _context.next = 4;
31
- return callback === null || callback === void 0 ? void 0 : callback();
32
- case 4:
33
- _context.next = 9;
34
- break;
35
- case 5:
36
- _context.prev = 5;
37
- _t = _context["catch"](1);
38
- console.error('onInternalEvent failed', _t);
39
- // still try to ack to avoid deadlocks
40
- _context.prev = 6;
41
- _context.next = 7;
42
- return callback === null || callback === void 0 ? void 0 : callback();
43
- case 7:
44
- _context.next = 9;
45
- break;
46
- case 8:
47
- _context.prev = 8;
48
- _t2 = _context["catch"](6);
49
- case 9:
50
- case "end":
51
- return _context.stop();
52
- }
53
- }, _callee, null, [[1, 5], [6, 8]]);
54
- }));
55
- return function onInternalEvent(_x, _x2) {
56
- return _ref.apply(this, arguments);
57
- };
58
- }();
59
- function WorkSpaceHostMimic() {
14
+ export default function WorkSpaceHostMimic() {
60
15
  var _useResizeDetector = useResizeDetector(),
61
16
  width = _useResizeDetector.width,
62
17
  height = _useResizeDetector.height,
63
18
  sizeRef = _useResizeDetector.ref;
64
-
65
- // ✅ pure JS refs
66
19
  var mountRef = useRef(null);
67
20
  var apiRef = useRef(null);
68
- console.log('render WorkSpaceHostMimic');
69
- useEffect(function () {
70
- if (!mountRef.current) {
71
- console.log('mountRef not ready');
72
- return;
21
+ var sentOnceRef = useRef(false);
22
+ var _useState = useState(0),
23
+ _useState2 = _slicedToArray(_useState, 2),
24
+ inFlight = _useState2[0],
25
+ setInFlight = _useState2[1];
26
+ var rafRef = useRef(0);
27
+ var onInternalEvent = useCallback(function (evt, callback) {
28
+ try {
29
+ if ((evt === null || evt === void 0 ? void 0 : evt.type) === INTERNAL_EVENT_ITEMS_CATALOG) {
30
+ // ✅ correct shape
31
+ callback === null || callback === void 0 || callback({
32
+ elements: projectItemsCatalog
33
+ });
34
+ } else {
35
+ callback === null || callback === void 0 || callback();
36
+ }
37
+ } catch (e) {
38
+ console.error('onInternalEvent failed', e);
39
+ try {
40
+ callback === null || callback === void 0 || callback();
41
+ } catch (_unused) {}
73
42
  }
74
- console.log('Initializing simulator');
43
+ }, []);
44
+ useEffect(function () {
45
+ if (!mountRef.current) return;
46
+ if (apiRef.current) return;
75
47
  var api = renderKitchenSimulator(mountRef.current, {
76
48
  width: width !== null && width !== void 0 ? width : window.innerWidth,
77
49
  height: height !== null && height !== void 0 ? height : window.innerHeight,
@@ -86,60 +58,77 @@ function WorkSpaceHostMimic() {
86
58
  }
87
59
  });
88
60
  apiRef.current = api;
89
- console.log('Sending immediate external events');
90
- _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
91
- return _regeneratorRuntime.wrap(function (_context2) {
92
- while (1) switch (_context2.prev = _context2.next) {
61
+ var unsub = typeof api.subscribeGltfInFlight === 'function' ? api.subscribeGltfInFlight(function (count) {
62
+ return setInFlight(count);
63
+ }) : null;
64
+ _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
65
+ var ok;
66
+ return _regeneratorRuntime.wrap(function (_context) {
67
+ while (1) switch (_context.prev = _context.next) {
93
68
  case 0:
94
- _context2.next = 1;
95
- return api.whenReady();
69
+ if (!sentOnceRef.current) {
70
+ _context.next = 1;
71
+ break;
72
+ }
73
+ return _context.abrupt("return");
96
74
  case 1:
97
- api.sendExternalEvents({
98
- type: EXTERNAL_EVENT_LOAD_PROJECT,
99
- // send request for sync scene data to 3DTool
100
- payload: JSON.parse(mockProps.projectElement[0].project_data),
101
- framesPerEvent: 2,
102
- waitForGltfIdleAfterEachEvent: true
103
- });
104
- api.sendExternalEvents({
75
+ sentOnceRef.current = true;
76
+ _context.next = 2;
77
+ return api.whenReady();
78
+ case 2:
79
+ _context.next = 3;
80
+ return api.sendExternalEvents([{
105
81
  type: EXTERNAL_EVENT_CENTERING_2D,
106
82
  payload: {},
107
- framesPerEvent: 1,
108
- waitForGltfIdleAfterEachEvent: true
83
+ framesPerEvent: 2,
84
+ waitForGltfIdleAfterEachEvent: false
85
+ }], {
86
+ timeoutMs: 15000
109
87
  });
110
- case 2:
88
+ case 3:
89
+ ok = _context.sent;
90
+ if (!ok) console.warn('Centering timed out (fail-open).');
91
+ case 4:
111
92
  case "end":
112
- return _context2.stop();
93
+ return _context.stop();
113
94
  }
114
- }, _callee2);
95
+ }, _callee);
115
96
  }))();
116
97
  return function () {
117
98
  var _apiRef$current, _apiRef$current$unmou;
118
- console.log('Unmounting simulator');
99
+ try {
100
+ unsub === null || unsub === void 0 || unsub();
101
+ } catch (_unused2) {}
119
102
  (_apiRef$current = apiRef.current) === null || _apiRef$current === void 0 || (_apiRef$current$unmou = _apiRef$current.unmount) === null || _apiRef$current$unmou === void 0 || _apiRef$current$unmou.call(_apiRef$current);
120
103
  apiRef.current = null;
104
+ sentOnceRef.current = false;
121
105
  };
106
+ // eslint-disable-next-line react-hooks/exhaustive-deps
122
107
  }, []);
123
-
124
- // Resize mimic
125
108
  useEffect(function () {
126
- var _api$__render;
127
109
  var api = apiRef.current;
128
110
  if (!api) return;
129
- (_api$__render = api.__render) === null || _api$__render === void 0 || _api$__render.call(api, {
130
- width: width !== null && width !== void 0 ? width : window.innerWidth,
131
- height: height !== null && height !== void 0 ? height : window.innerHeight,
132
- configData: mockProps.configData,
133
- options: options,
134
- logoImg: '/assets/logo.png',
135
- companyUrl: 'https://example.com',
136
- toolbarButtons: [],
137
- onInternalEvent: onInternalEvent,
138
- onError: function onError(payload) {
139
- return console.error('TOOL ERROR:', payload);
140
- }
111
+ cancelAnimationFrame(rafRef.current);
112
+ rafRef.current = requestAnimationFrame(function () {
113
+ var _api$__render;
114
+ (_api$__render = api.__render) === null || _api$__render === void 0 || _api$__render.call(api, {
115
+ width: width !== null && width !== void 0 ? width : window.innerWidth,
116
+ height: height !== null && height !== void 0 ? height : window.innerHeight,
117
+ configData: mockProps.configData,
118
+ options: options,
119
+ logoImg: '/assets/logo.png',
120
+ companyUrl: 'https://example.com',
121
+ toolbarButtons: [],
122
+ onInternalEvent: onInternalEvent,
123
+ onError: function onError(payload) {
124
+ return console.error('TOOL ERROR:', payload);
125
+ }
126
+ });
141
127
  });
142
- }, [width, height]);
128
+ return function () {
129
+ return cancelAnimationFrame(rafRef.current);
130
+ };
131
+ }, [width, height, onInternalEvent]);
143
132
  return /*#__PURE__*/React.createElement("div", {
144
133
  style: {
145
134
  width: '100vw',
@@ -151,7 +140,20 @@ function WorkSpaceHostMimic() {
151
140
  width: '100%',
152
141
  height: '100%'
153
142
  }
154
- }, /*#__PURE__*/React.createElement("div", {
143
+ }, inFlight > 0 ? /*#__PURE__*/React.createElement("div", {
144
+ style: {
145
+ position: 'absolute',
146
+ zIndex: 9999,
147
+ top: 12,
148
+ right: 12,
149
+ padding: '8px 10px',
150
+ borderRadius: 8,
151
+ background: 'rgba(0,0,0,0.6)',
152
+ color: 'white',
153
+ fontSize: 12,
154
+ pointerEvents: 'none'
155
+ }
156
+ }, "Loading 3D\u2026 (", inFlight, ")") : null, /*#__PURE__*/React.createElement("div", {
155
157
  ref: mountRef,
156
158
  id: "kitchen-simulator-container",
157
159
  style: {
@@ -159,5 +161,4 @@ function WorkSpaceHostMimic() {
159
161
  height: '100%'
160
162
  }
161
163
  })));
162
- }
163
- export default WorkSpaceHostMimic;
164
+ }
package/es/index.js CHANGED
@@ -374,10 +374,6 @@ export function renderKitchenSimulator(container) {
374
374
  }
375
375
  var setExternalEventFn = null;
376
376
  var destroyed = false;
377
- var resolveReady = null;
378
- var readyPromise = new Promise(function (res) {
379
- return resolveReady = res;
380
- });
381
377
  var queue = [];
382
378
  var pendingMarkers = new Set();
383
379
  var MARKER = Symbol('marker');
@@ -533,8 +529,7 @@ export function renderKitchenSimulator(container) {
533
529
  return _createClass(Wrapper, [{
534
530
  key: "componentDidMount",
535
531
  value: function componentDidMount() {
536
- var _this3 = this,
537
- _resolveReady;
532
+ var _this3 = this;
538
533
  this._mounted = true;
539
534
  setExternalEventFn = function setExternalEventFn(newEvent) {
540
535
  if (!_this3._mounted) return;
@@ -542,9 +537,6 @@ export function renderKitchenSimulator(container) {
542
537
  externalEvent: newEvent
543
538
  });
544
539
  };
545
- (_resolveReady = resolveReady) === null || _resolveReady === void 0 || _resolveReady();
546
- resolveReady = null;
547
- drain(); // now it can actually process queued events
548
540
  }
549
541
  }, {
550
542
  key: "componentWillUnmount",
@@ -565,9 +557,6 @@ export function renderKitchenSimulator(container) {
565
557
  __render: function __render(nextProps) {
566
558
  root.render(/*#__PURE__*/React.createElement(Wrapper, nextProps));
567
559
  },
568
- whenReady: function whenReady() {
569
- return readyPromise;
570
- },
571
560
  // ✅ host can query current inFlight
572
561
  getGltfInFlight: function getGltfInFlight() {
573
562
  return gltfTracker.getInFlight();
@@ -599,7 +588,7 @@ export function renderKitchenSimulator(container) {
599
588
  __marker: MARKER,
600
589
  token: token
601
590
  });
602
- if (setExternalEventFn) drain();
591
+ drain();
603
592
 
604
593
  // resolve when marker cleared
605
594
  return new Promise(function (resolve) {
@@ -657,12 +646,9 @@ export function renderKitchenSimulator(container) {
657
646
  pendingMarkers.clear();
658
647
  },
659
648
  unmount: function unmount() {
660
- var _resolveReady2;
661
649
  destroyed = true;
662
650
  api.clearQueue();
663
651
  gltfTracker.uninstall();
664
- (_resolveReady2 = resolveReady) === null || _resolveReady2 === void 0 || _resolveReady2();
665
- resolveReady = null;
666
652
  var doUnmount = function doUnmount() {
667
653
  try {
668
654
  root.unmount();
@@ -341,20 +341,12 @@ export var updateViwer2D = function updateViwer2D(value) {
341
341
  var _zoomValue = parseInt((value.a - 0.5) / ZOOM_VARIABLE);
342
342
  if (_zoomValue > MAX_ZOOM_IN_SCALE) return;
343
343
  if (_zoomValue < 0 || Number.isNaN(_zoomValue)) return;
344
-
345
- // modify e, f to fit to SVG
346
- while (!(value.e <= 10)) {
347
- value.e -= 0.1;
348
- }
349
- while (!(value.e + value.a * value.SVGWidth + 10 >= value.viewerWidth)) {
350
- value.e += 0.1;
351
- }
352
- while (!(value.f <= 80)) {
353
- value.f -= 0.1;
354
- }
355
- while (!(value.f + value.a * value.SVGHeight + 10 >= value.viewerHeight)) {
356
- value.f += 0.1;
357
- }
344
+ var maxE = 10;
345
+ var minE = value.viewerWidth - (value.a * value.SVGWidth + 10);
346
+ value.e = Math.min(maxE, Math.max(minE, value.e));
347
+ var maxF = 80;
348
+ var minF = value.viewerHeight - (value.a * value.SVGHeight + 10);
349
+ value.f = Math.min(maxF, Math.max(minF, value.f));
358
350
  if (viewer2DActions && compareSVGRect(value)) viewer2DActions.updateCameraView(value);
359
351
  };
360
352
  export function centering2D(state) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitchen-simulator",
3
- "version": "7.0.3-react-18",
3
+ "version": "7.0.4-react-18",
4
4
  "description": "It is a kitchen simulator (self-contained micro-frontend).",
5
5
  "license": "MIT",
6
6
  "module": "es/index.js",