@budibase/frontend-core 2.0.30-alpha.7 → 2.0.30-alpha.8

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 (2) hide show
  1. package/package.json +3 -3
  2. package/src/utils/utils.js +34 -0
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@budibase/frontend-core",
3
- "version": "2.0.30-alpha.7",
3
+ "version": "2.0.30-alpha.8",
4
4
  "description": "Budibase frontend core libraries used in builder and client",
5
5
  "author": "Budibase",
6
6
  "license": "MPL-2.0",
7
7
  "svelte": "src/index.js",
8
8
  "dependencies": {
9
- "@budibase/bbui": "2.0.30-alpha.7",
9
+ "@budibase/bbui": "2.0.30-alpha.8",
10
10
  "lodash": "^4.17.21",
11
11
  "svelte": "^3.46.2"
12
12
  },
13
- "gitHead": "74511ef9396141daf6d0154eec2b7abc4e884470"
13
+ "gitHead": "a0da70381f85e509e5f9f9ee21471f146632182c"
14
14
  }
@@ -40,3 +40,37 @@ export const debounce = (callback, minDelay = 1000) => {
40
40
  })
41
41
  }
42
42
  }
43
+
44
+ /**
45
+ * Utility to throttle invocations of a synchronous function. This is better
46
+ * than a simple debounce invocation for a number of reasons. Features include:
47
+ * - First invocation is immediate (no initial delay)
48
+ * - Every invocation has the latest params (no stale params)
49
+ * - There will always be a final invocation with the last params (no missing
50
+ * final update)
51
+ * @param callback
52
+ * @param minDelay
53
+ * @returns {Function} a throttled version function
54
+ */
55
+ export const throttle = (callback, minDelay = 1000) => {
56
+ let lastParams
57
+ let stalled = false
58
+ let pending = false
59
+ const invoke = (...params) => {
60
+ lastParams = params
61
+ if (stalled) {
62
+ pending = true
63
+ return
64
+ }
65
+ callback(...lastParams)
66
+ stalled = true
67
+ setTimeout(() => {
68
+ stalled = false
69
+ if (pending) {
70
+ pending = false
71
+ invoke(...lastParams)
72
+ }
73
+ }, minDelay)
74
+ }
75
+ return invoke
76
+ }