@mackin.com/styleguide 9.2.0 → 9.2.1

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/index.d.ts +0 -1
  2. package/index.js +33 -11
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1097,7 +1097,6 @@ interface AutoCompleteConfig {
1097
1097
  debounceMs?: number;
1098
1098
  }
1099
1099
 
1100
- /** Extracted logic around autocomplete functionality for Autocomplete.tsx. */
1101
1100
  declare class AutocompleteController {
1102
1101
  constructor(getOptions: (value: string | undefined) => Promise<string[]>, config?: AutoCompleteConfig);
1103
1102
  get value(): string | undefined;
package/index.js CHANGED
@@ -4656,7 +4656,6 @@ const TabContainer = (p) => {
4656
4656
  };
4657
4657
 
4658
4658
  const defaultMinChars = 3;
4659
- /** Extracted logic around autocomplete functionality for Autocomplete.tsx. */
4660
4659
  class AutocompleteController {
4661
4660
  constructor(getOptions, config) {
4662
4661
  var _a;
@@ -4664,10 +4663,7 @@ class AutocompleteController {
4664
4663
  this._options = [];
4665
4664
  this._minChars = (_a = config === null || config === void 0 ? void 0 : config.minChars) !== null && _a !== void 0 ? _a : defaultMinChars;
4666
4665
  if (config === null || config === void 0 ? void 0 : config.debounceMs) {
4667
- this.getOptions = lodash.debounce(getOptions, config.debounceMs, {
4668
- leading: false,
4669
- trailing: true
4670
- });
4666
+ this.getOptions = createDebouncedPromise(getOptions, config === null || config === void 0 ? void 0 : config.debounceMs);
4671
4667
  }
4672
4668
  else {
4673
4669
  this.getOptions = getOptions;
@@ -4680,7 +4676,6 @@ class AutocompleteController {
4680
4676
  return this._options;
4681
4677
  }
4682
4678
  async onChange(newValue) {
4683
- var _a;
4684
4679
  // don't make getOptions calls if the value hasn't changed.
4685
4680
  if (newValue === this.value) {
4686
4681
  return;
@@ -4699,19 +4694,46 @@ class AutocompleteController {
4699
4694
  }
4700
4695
  try {
4701
4696
  this._value = newValue;
4702
- // debounce (if used) will return undefined until the actual function is executed.
4703
- // after that it will return the result of the last execution.
4704
- this._options = (_a = (await this.getOptions(newValue))) !== null && _a !== void 0 ? _a : [];
4697
+ this._options = (await this.getOptions(newValue));
4705
4698
  }
4706
4699
  catch (err) {
4707
- // the getOptions method needs to handle it's own errors
4700
+ // this method will throw errors on debounce rejections. that is to be expected.
4701
+ // for actual getOptions exceptions, the owner of that function will need to handle errors.
4708
4702
  }
4709
4703
  }
4710
4704
  onPick(newValue) {
4711
4705
  this._value = newValue;
4712
4706
  this._options = [];
4713
4707
  }
4714
- }
4708
+ }
4709
+ const createDebouncedPromise = (originalFunction, trailingTimeoutMs) => {
4710
+ let timer;
4711
+ let onCancel;
4712
+ return (value) => {
4713
+ if (timer) {
4714
+ clearTimeout(timer);
4715
+ }
4716
+ if (onCancel) {
4717
+ onCancel('Promise cancelled due to in-progress debounce call.');
4718
+ onCancel = undefined;
4719
+ }
4720
+ return new Promise((res, rej) => {
4721
+ onCancel = rej;
4722
+ timer = setTimeout(() => {
4723
+ originalFunction(value)
4724
+ .then(values => {
4725
+ res(values);
4726
+ })
4727
+ .catch(err => {
4728
+ rej(err);
4729
+ })
4730
+ .finally(() => {
4731
+ clearTimeout(timer);
4732
+ });
4733
+ }, trailingTimeoutMs);
4734
+ });
4735
+ };
4736
+ };
4715
4737
 
4716
4738
  /** Extracted logic around autocomplete functionality for Autocomplete.tsx that supports Entity (id/name) mapping. */
4717
4739
  class AutocompleteEntityController {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mackin.com/styleguide",
3
- "version": "9.2.0",
3
+ "version": "9.2.1",
4
4
  "description": "",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",