@kaspernj/api-maker 1.0.368 → 1.0.369
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 +1 -1
- package/src/event-created.jsx +3 -2
- package/src/event-destroyed.jsx +16 -18
- package/src/event-updated.jsx +18 -15
- package/src/use-destroyed-event.mjs +60 -0
- package/src/use-updated-event.mjs +2 -2
package/package.json
CHANGED
package/src/event-created.jsx
CHANGED
|
@@ -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
|
package/src/event-destroyed.jsx
CHANGED
|
@@ -1,27 +1,25 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {memo} from "react"
|
|
2
2
|
import PropTypes from "prop-types"
|
|
3
3
|
import propTypesExact from "prop-types-exact"
|
|
4
|
-
import
|
|
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
|
|
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
|
-
|
|
15
|
+
onConnected: PropTypes.func,
|
|
16
|
+
onUpdated: PropTypes.func.isRequired
|
|
10
17
|
})
|
|
11
18
|
|
|
12
|
-
|
|
13
|
-
this.
|
|
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
|
-
|
|
23
|
-
this.connectDestroyed = ModelEvents.connectDestroyed(this.props.model, this.props.onDestroyed)
|
|
23
|
+
return null
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
render = () => null
|
|
27
|
-
}
|
|
25
|
+
}))
|
package/src/event-updated.jsx
CHANGED
|
@@ -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
|
-
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
}
|
|
19
|
+
render() {
|
|
20
|
+
const {model, onUpdated, ...restProps} = this.props
|
|
11
21
|
|
|
12
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
58
|
+
export default apiMakerUseUpdatedEvent
|