@pro6pp/infer-react 0.1.0-beta.20 → 0.1.0-beta.22

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/index.cjs CHANGED
@@ -180,6 +180,7 @@ var InferCore = class {
180
180
  __publicField(this, "state");
181
181
  __publicField(this, "abortController", null);
182
182
  __publicField(this, "debouncedFetch");
183
+ __publicField(this, "isDestroyed", false);
183
184
  this.country = config.country;
184
185
  this.authKey = config.authKey;
185
186
  this.explicitApiUrl = config.apiUrl;
@@ -204,6 +205,7 @@ var InferCore = class {
204
205
  * @param value The raw string from the input field.
205
206
  */
206
207
  handleInput(value) {
208
+ if (this.isDestroyed) return;
207
209
  this.currentLimit = this.baseLimit;
208
210
  const isEditingFinal = this.state.stage === "final" && value !== this.state.query;
209
211
  this.updateState({
@@ -224,6 +226,7 @@ var InferCore = class {
224
226
  * Increases the current limit and re-fetches the query to show more results.
225
227
  */
226
228
  loadMore() {
229
+ if (this.isDestroyed) return;
227
230
  if (this.state.isLoading) return;
228
231
  this.currentLimit += this.baseLimit;
229
232
  this.updateState({ isLoading: true });
@@ -238,6 +241,7 @@ var InferCore = class {
238
241
  * @param event The keyboard event from the input element.
239
242
  */
240
243
  handleKeyDown(event) {
244
+ if (this.isDestroyed) return;
241
245
  const target = event.target;
242
246
  if (!target) return;
243
247
  const totalItems = this.state.cities.length + this.state.streets.length + this.state.suggestions.length;
@@ -285,6 +289,7 @@ var InferCore = class {
285
289
  * @returns boolean True if the selection is a final address.
286
290
  */
287
291
  selectItem(item) {
292
+ if (this.isDestroyed) return false;
288
293
  this.debouncedFetch.cancel();
289
294
  if (this.abortController) {
290
295
  this.abortController.abort();
@@ -313,6 +318,16 @@ var InferCore = class {
313
318
  this.processSelection(logicValue, subtitle);
314
319
  return false;
315
320
  }
321
+ /**
322
+ * Disposes the engine and prevents pending async work from updating state.
323
+ */
324
+ destroy() {
325
+ if (this.isDestroyed) return;
326
+ this.isDestroyed = true;
327
+ this.debouncedFetch.cancel();
328
+ this.abortController?.abort();
329
+ this.abortController = null;
330
+ }
316
331
  shouldAutoInsertComma(currentVal) {
317
332
  const isStartOfSegmentAndNumeric = !currentVal.includes(",") && PATTERNS.DIGITS_1_3.test(currentVal.trim());
318
333
  if (isStartOfSegmentAndNumeric) return true;
@@ -381,6 +396,7 @@ var InferCore = class {
381
396
  this.updateQueryAndFetch(nextQuery);
382
397
  }
383
398
  executeFetch(val, attempt = 0) {
399
+ if (this.isDestroyed) return;
384
400
  const text = (val || "").toString();
385
401
  if (!text.trim()) {
386
402
  this.abortController?.abort();
@@ -418,8 +434,10 @@ var InferCore = class {
418
434
  }
419
435
  return res.json();
420
436
  }).then((data) => {
437
+ if (this.isDestroyed || currentSignal?.aborted) return;
421
438
  if (data) this.mapResponseToState(data);
422
439
  }).catch((e) => {
440
+ if (this.isDestroyed) return;
423
441
  if (e.name === "AbortError") return;
424
442
  if (attempt < this.maxRetries) {
425
443
  return this.retry(val, attempt, currentSignal);
@@ -428,10 +446,11 @@ var InferCore = class {
428
446
  });
429
447
  }
430
448
  retry(val, attempt, signal) {
449
+ if (this.isDestroyed) return;
431
450
  if (signal?.aborted) return;
432
451
  const delay = Math.pow(2, attempt) * 200;
433
452
  setTimeout(() => {
434
- if (!signal?.aborted) {
453
+ if (!this.isDestroyed && !signal?.aborted) {
435
454
  this.executeFetch(val, attempt + 1);
436
455
  }
437
456
  }, delay);
@@ -521,6 +540,7 @@ var InferCore = class {
521
540
  this.updateState({ ...INITIAL_STATE, query: this.state.query });
522
541
  }
523
542
  updateState(updates) {
543
+ if (this.isDestroyed) return;
524
544
  this.state = { ...this.state, ...updates };
525
545
  this.onStateChange(this.state);
526
546
  }
@@ -851,6 +871,10 @@ function useInfer(config) {
851
871
  config.initialValue,
852
872
  config.language
853
873
  ]);
874
+ (0, import_react.useEffect)(() => {
875
+ setState({ ...core.state });
876
+ return () => core.destroy();
877
+ }, [core]);
854
878
  const setValue = (address) => {
855
879
  if (!address) return;
856
880
  const suffix = address.addition ? ` ${address.addition}` : "";
package/dist/index.js CHANGED
@@ -154,6 +154,7 @@ var InferCore = class {
154
154
  __publicField(this, "state");
155
155
  __publicField(this, "abortController", null);
156
156
  __publicField(this, "debouncedFetch");
157
+ __publicField(this, "isDestroyed", false);
157
158
  this.country = config.country;
158
159
  this.authKey = config.authKey;
159
160
  this.explicitApiUrl = config.apiUrl;
@@ -178,6 +179,7 @@ var InferCore = class {
178
179
  * @param value The raw string from the input field.
179
180
  */
180
181
  handleInput(value) {
182
+ if (this.isDestroyed) return;
181
183
  this.currentLimit = this.baseLimit;
182
184
  const isEditingFinal = this.state.stage === "final" && value !== this.state.query;
183
185
  this.updateState({
@@ -198,6 +200,7 @@ var InferCore = class {
198
200
  * Increases the current limit and re-fetches the query to show more results.
199
201
  */
200
202
  loadMore() {
203
+ if (this.isDestroyed) return;
201
204
  if (this.state.isLoading) return;
202
205
  this.currentLimit += this.baseLimit;
203
206
  this.updateState({ isLoading: true });
@@ -212,6 +215,7 @@ var InferCore = class {
212
215
  * @param event The keyboard event from the input element.
213
216
  */
214
217
  handleKeyDown(event) {
218
+ if (this.isDestroyed) return;
215
219
  const target = event.target;
216
220
  if (!target) return;
217
221
  const totalItems = this.state.cities.length + this.state.streets.length + this.state.suggestions.length;
@@ -259,6 +263,7 @@ var InferCore = class {
259
263
  * @returns boolean True if the selection is a final address.
260
264
  */
261
265
  selectItem(item) {
266
+ if (this.isDestroyed) return false;
262
267
  this.debouncedFetch.cancel();
263
268
  if (this.abortController) {
264
269
  this.abortController.abort();
@@ -287,6 +292,16 @@ var InferCore = class {
287
292
  this.processSelection(logicValue, subtitle);
288
293
  return false;
289
294
  }
295
+ /**
296
+ * Disposes the engine and prevents pending async work from updating state.
297
+ */
298
+ destroy() {
299
+ if (this.isDestroyed) return;
300
+ this.isDestroyed = true;
301
+ this.debouncedFetch.cancel();
302
+ this.abortController?.abort();
303
+ this.abortController = null;
304
+ }
290
305
  shouldAutoInsertComma(currentVal) {
291
306
  const isStartOfSegmentAndNumeric = !currentVal.includes(",") && PATTERNS.DIGITS_1_3.test(currentVal.trim());
292
307
  if (isStartOfSegmentAndNumeric) return true;
@@ -355,6 +370,7 @@ var InferCore = class {
355
370
  this.updateQueryAndFetch(nextQuery);
356
371
  }
357
372
  executeFetch(val, attempt = 0) {
373
+ if (this.isDestroyed) return;
358
374
  const text = (val || "").toString();
359
375
  if (!text.trim()) {
360
376
  this.abortController?.abort();
@@ -392,8 +408,10 @@ var InferCore = class {
392
408
  }
393
409
  return res.json();
394
410
  }).then((data) => {
411
+ if (this.isDestroyed || currentSignal?.aborted) return;
395
412
  if (data) this.mapResponseToState(data);
396
413
  }).catch((e) => {
414
+ if (this.isDestroyed) return;
397
415
  if (e.name === "AbortError") return;
398
416
  if (attempt < this.maxRetries) {
399
417
  return this.retry(val, attempt, currentSignal);
@@ -402,10 +420,11 @@ var InferCore = class {
402
420
  });
403
421
  }
404
422
  retry(val, attempt, signal) {
423
+ if (this.isDestroyed) return;
405
424
  if (signal?.aborted) return;
406
425
  const delay = Math.pow(2, attempt) * 200;
407
426
  setTimeout(() => {
408
- if (!signal?.aborted) {
427
+ if (!this.isDestroyed && !signal?.aborted) {
409
428
  this.executeFetch(val, attempt + 1);
410
429
  }
411
430
  }, delay);
@@ -495,6 +514,7 @@ var InferCore = class {
495
514
  this.updateState({ ...INITIAL_STATE, query: this.state.query });
496
515
  }
497
516
  updateState(updates) {
517
+ if (this.isDestroyed) return;
498
518
  this.state = { ...this.state, ...updates };
499
519
  this.onStateChange(this.state);
500
520
  }
@@ -825,6 +845,10 @@ function useInfer(config) {
825
845
  config.initialValue,
826
846
  config.language
827
847
  ]);
848
+ useEffect(() => {
849
+ setState({ ...core.state });
850
+ return () => core.destroy();
851
+ }, [core]);
828
852
  const setValue = (address) => {
829
853
  if (!address) return;
830
854
  const suffix = address.addition ? ` ${address.addition}` : "";
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "sideEffects": [
23
23
  "*.css"
24
24
  ],
25
- "version": "0.1.0-beta.20",
25
+ "version": "0.1.0-beta.22",
26
26
  "main": "./dist/index.cjs",
27
27
  "module": "./dist/index.js",
28
28
  "types": "./dist/index.d.ts",
@@ -49,7 +49,7 @@
49
49
  "react": ">=16"
50
50
  },
51
51
  "dependencies": {
52
- "@pro6pp/infer-core": "0.1.0-beta.18"
52
+ "@pro6pp/infer-core": "0.1.0-beta.20"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@testing-library/dom": "^10.4.1",