@kaspernj/api-maker 1.0.368 → 1.0.370

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaspernj/api-maker",
3
- "version": "1.0.368",
3
+ "version": "1.0.370",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
@@ -208,8 +208,24 @@ export default class ApiMakerCommandsPool {
208
208
  return false
209
209
  }
210
210
 
211
+ // Try to batch calls to backend while waiting for the event-queue-call to clear any other jobs before the request and reset on every flush call
212
+ // If only waiting a single time, then other event-queue-jobs might be before us and queue other jobs that might queue calls to the backend
211
213
  setFlushTimeout() {
214
+ this.flushTriggerCount = 0
212
215
  this.clearTimeout()
213
- this.flushTimeout = setTimeout(() => this.flush(), 0)
216
+ this.flushTrigger()
217
+ }
218
+
219
+ flushTrigger = () => {
220
+ if (this.flushTriggerCount >= 10) {
221
+ this.flush()
222
+ } else {
223
+ this.flushTriggerCount++
224
+ this.flushTriggerQueue()
225
+ }
226
+ }
227
+
228
+ flushTriggerQueue() {
229
+ this.flushTimeout = setTimeout(this.flushTrigger, 0)
214
230
  }
215
231
  }
@@ -1,11 +1,12 @@
1
+ import {memo} from "react"
1
2
  import useCreatedEvent from "./use-created-event.mjs"
2
3
 
3
- const ApiMakerEventCreated = (props) => {
4
+ const ApiMakerEventCreated = memo((props) => {
4
5
  const {modelClass, onCreated, ...restProps} = props
5
6
 
6
7
  useCreatedEvent(modelClass, onCreated, restProps)
7
8
 
8
9
  return null
9
- }
10
+ })
10
11
 
11
12
  export default ApiMakerEventCreated
@@ -1,27 +1,25 @@
1
- import ModelEvents from "./model-events.mjs"
1
+ import {memo} from "react"
2
2
  import PropTypes from "prop-types"
3
3
  import propTypesExact from "prop-types-exact"
4
- import React from "react"
4
+ import {shapeComponent, ShapeComponent} from "set-state-compare/src/shape-component.js"
5
+ import useDestroyedEvent from "./use-destroyed-event.mjs"
5
6
 
6
- export default class ApiMakerEventDestroyed extends React.PureComponent {
7
+ export default memo(shapeComponent(class ApiMakerEventDestroyed extends ShapeComponent {
7
8
  static propTypes = propTypesExact({
9
+ active: PropTypes.bool,
10
+ debounce: PropTypes.oneOfType([
11
+ PropTypes.bool,
12
+ PropTypes.number
13
+ ]),
8
14
  model: PropTypes.object.isRequired,
9
- onDestroyed: PropTypes.func.isRequired
15
+ onConnected: PropTypes.func,
16
+ onUpdated: PropTypes.func.isRequired
10
17
  })
11
18
 
12
- componentDidMount () {
13
- this.connect()
14
- }
15
-
16
- componentWillUnmount () {
17
- if (this.connectDestroyed) {
18
- this.connectDestroyed.unsubscribe()
19
- }
20
- }
19
+ render() {
20
+ const {model, onDestroyed, ...restProps} = this.props
21
+ useDestroyedEvent(model, onDestroyed, restProps)
21
22
 
22
- connect () {
23
- this.connectDestroyed = ModelEvents.connectDestroyed(this.props.model, this.props.onDestroyed)
23
+ return null
24
24
  }
25
-
26
- render = () => null
27
- }
25
+ }))
@@ -1,23 +1,26 @@
1
1
  import PropTypes from "prop-types"
2
2
  import propTypesExact from "prop-types-exact"
3
3
  import {memo} from "react"
4
+ import {shapeComponent, ShapeComponent} from "set-state-compare/src/shape-component.js"
4
5
  import useUpdatedEvent from "./use-updated-event.mjs"
5
6
 
6
- const ApiMakerEventUpdated = ({model, onUpdated, ...restProps}) => {
7
- useUpdatedEvent(model, onUpdated, restProps)
7
+ export default memo(shapeComponent(class ApiMakerEventUpdated extends ShapeComponent {
8
+ static propTypes = propTypesExact({
9
+ active: PropTypes.bool,
10
+ debounce: PropTypes.oneOfType([
11
+ PropTypes.bool,
12
+ PropTypes.number
13
+ ]),
14
+ model: PropTypes.object.isRequired,
15
+ onConnected: PropTypes.func,
16
+ onUpdated: PropTypes.func.isRequired
17
+ })
8
18
 
9
- return null
10
- }
19
+ render() {
20
+ const {model, onUpdated, ...restProps} = this.props
11
21
 
12
- ApiMakerEventUpdated.propTypes = propTypesExact({
13
- active: PropTypes.bool,
14
- debounce: PropTypes.oneOfType([
15
- PropTypes.bool,
16
- PropTypes.number
17
- ]),
18
- model: PropTypes.object.isRequired,
19
- onConnected: PropTypes.func,
20
- onUpdated: PropTypes.func.isRequired
21
- })
22
+ useUpdatedEvent(model, onUpdated, restProps)
22
23
 
23
- export default memo(ApiMakerEventUpdated)
24
+ return null
25
+ }
26
+ }))
@@ -0,0 +1,60 @@
1
+ import {useCallback, useEffect, useMemo} from "react"
2
+ import debounceFunction from "debounce"
3
+ import ModelEvents from "./model-events.mjs"
4
+ import useShape from "set-state-compare/src/use-shape.js"
5
+
6
+ const apiMakerUseDestroyedEvent = (model, onDestroyed, props) => {
7
+ const {active = true, debounce, onConnected, ...restProps} = props || {}
8
+
9
+ if (Object.keys(restProps).length > 0) {
10
+ throw new Error(`Unknown props given to useDestroyedEvent: ${Object.keys(restProps).join(", ")}`)
11
+ }
12
+
13
+ const s = useShape({active, debounce, model, onDestroyed})
14
+
15
+ const debounceCallback = useMemo(() => {
16
+ if (typeof debounce == "number") {
17
+ return debounceFunction(s.p.onDestroyed, debounce)
18
+ } else {
19
+ return debounceFunction(s.p.onDestroyed)
20
+ }
21
+ }, [debounce])
22
+
23
+ s.updateMeta({debounceCallback})
24
+
25
+ const onDestroyedCallback = useCallback((...args) => {
26
+ if (!s.p.active) {
27
+ return
28
+ }
29
+
30
+ if (s.p.debounce) {
31
+ s.m.debounceCallback(...args)
32
+ } else {
33
+ s.p.onDestroyed(...args)
34
+ }
35
+ }, [])
36
+
37
+ useEffect(() => {
38
+ let connectDestroyed, onConnectedListener
39
+
40
+ if (model) {
41
+ connectDestroyed = ModelEvents.connectDestroyed(model, onDestroyedCallback)
42
+
43
+ if (onConnected) {
44
+ onConnectedListener = connectDestroyed.events.addListener("connected", onConnected)
45
+ }
46
+ }
47
+
48
+ return () => {
49
+ if (onConnectedListener) {
50
+ connectDestroyed.events.removeListener("connected", onConnected)
51
+ }
52
+
53
+ if (connectDestroyed) {
54
+ connectDestroyed.unsubscribe()
55
+ }
56
+ }
57
+ }, [model?.id()])
58
+ }
59
+
60
+ export default apiMakerUseDestroyedEvent
@@ -3,7 +3,7 @@ import debounceFunction from "debounce"
3
3
  import ModelEvents from "./model-events.mjs"
4
4
  import useShape from "set-state-compare/src/use-shape.js"
5
5
 
6
- const ApiMakerUseUpdatedEvent = (model, onUpdated, {active = true, debounce, onConnected, ...restProps}) => {
6
+ const apiMakerUseUpdatedEvent = (model, onUpdated, {active = true, debounce, onConnected, ...restProps}) => {
7
7
  if (Object.keys(restProps).length > 0) {
8
8
  throw new Error(`Unknown props given to useUpdatedEvent: ${Object.keys(restProps).join(", ")}`)
9
9
  }
@@ -55,4 +55,4 @@ const ApiMakerUseUpdatedEvent = (model, onUpdated, {active = true, debounce, onC
55
55
  }, [model?.id()])
56
56
  }
57
57
 
58
- export default ApiMakerUseUpdatedEvent
58
+ export default apiMakerUseUpdatedEvent