@kaspernj/api-maker 1.0.325 → 1.0.326

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
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.325",
19
+ "version": "1.0.326",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -1,65 +1,11 @@
1
- import debounce from "debounce"
2
- import ModelEvents from "./model-events.mjs"
3
- import PropTypes from "prop-types"
4
- import propTypesExact from "prop-types-exact"
5
- import React from "react"
1
+ import useCreatedEvent from "./use-created-event.mjs"
6
2
 
7
- export default class ApiMakerEventCreated extends React.PureComponent {
8
- static defaultProps = {
9
- active: true
10
- }
3
+ const ApiMakerEventCreated = (props) => {
4
+ const {modelClass, onCreated, ...restProps} = props
11
5
 
12
- static propTypes = propTypesExact({
13
- active: PropTypes.bool.isRequired,
14
- debounce: PropTypes.oneOfType([
15
- PropTypes.bool,
16
- PropTypes.number
17
- ]),
18
- modelClass: PropTypes.func.isRequired,
19
- onCreated: PropTypes.func.isRequired
20
- })
6
+ useCreatedEvent(modelClass, onCreated, restProps)
21
7
 
22
- componentDidMount () {
23
- this.connect()
24
- }
25
-
26
- componentWillUnmount () {
27
- if (this.connectCreated) {
28
- this.connectCreated.unsubscribe()
29
- }
30
- }
31
-
32
- connect () {
33
- this.connectCreated = ModelEvents.connectCreated(this.props.modelClass, (...args) => this.onCreated(...args))
34
- }
35
-
36
- debounce () {
37
- if (!this.debounceInstance) {
38
- const {onCreated} = digs(this.props, "onCreated")
39
-
40
- if (typeof this.props.debounce == "number") {
41
- this.debounceInstance = debounce(onCreated, this.props.debounce)
42
- } else {
43
- this.debounceInstance = debounce(onCreated)
44
- }
45
- }
46
-
47
- return this.debounceInstance
48
- }
49
-
50
- onCreated (...args) {
51
- if (!this.props.active) {
52
- return
53
- }
54
-
55
- if (this.props.debounce) {
56
- this.debounce()(...args)
57
- } else {
58
- this.props.onCreated(...args)
59
- }
60
- }
61
-
62
- render () {
63
- return null
64
- }
8
+ return null
65
9
  }
10
+
11
+ export default ApiMakerEventCreated
@@ -1,25 +1,10 @@
1
- import EventEmitter from "events"
2
- import PropTypes from "prop-types"
3
- import React from "react"
1
+ import {memo} from "react"
2
+ import useEventEmitter from "./use-event-emitter.mjs"
4
3
 
5
- export default class ApiMakerEventEmitterListener extends React.PureComponent {
6
- static propTypes = {
7
- event: PropTypes.string.isRequired,
8
- events: PropTypes.instanceOf(EventEmitter).isRequired,
9
- onCalled: PropTypes.func.isRequired
10
- }
4
+ const ApiMakerEventEmitterListener = ({events, event, onCalled}) => {
5
+ useEventEmitter(events, event, onCalled)
11
6
 
12
- componentDidMount () {
13
- this.props.events.addListener(this.props.event, this.onCalled)
14
- }
15
-
16
- componentWillUnmount () {
17
- this.props.events.removeListener(this.props.event, this.onCalled)
18
- }
19
-
20
- onCalled = (...args) => {
21
- this.props.onCalled.apply(null, ...args)
22
- }
23
-
24
- render = () => null
7
+ return null
25
8
  }
9
+
10
+ export default memo(ApiMakerEventEmitterListener)
@@ -0,0 +1,55 @@
1
+ import debounceFunction from "debounce"
2
+ import ModelEvents from "./model-events.mjs"
3
+ import PropTypes from "prop-types"
4
+ import propTypesExact from "prop-types-exact"
5
+ import {useCallback, useEffect, memo} from "react"
6
+ import useShape from "set-state-compare/src/use-shape.js"
7
+
8
+ const ApiMakerUseCreatedEvent = (modelClass, onCreated, args = {}) => {
9
+ const {active = true, debounce} = args
10
+ const s = useShape({active, debounce, modelClass, onCreated})
11
+
12
+ const eventDebounce = useCallback(() => {
13
+ if (!s.meta.debounceInstance) {
14
+ if (typeof this.props.debounce == "number") {
15
+ s.meta.debounceInstance = debounceFunction(s.p.onCreated, s.p.debounce)
16
+ } else {
17
+ s.meta.debounceInstance = debounceFunction(s.p.onCreated)
18
+ }
19
+ }
20
+
21
+ return s.meta.debounceInstance
22
+ }, [])
23
+
24
+ const onCreatedCallback = useCallback((...args) => {
25
+ if (!s.p.active) {
26
+ return
27
+ }
28
+
29
+ if (s.p.debounce) {
30
+ eventDebounce()(...args)
31
+ } else {
32
+ s.p.onCreated(...args)
33
+ }
34
+ }, [])
35
+
36
+ useEffect(() => {
37
+ const connectCreated = ModelEvents.connectCreated(s.p.modelClass, (...args) => onCreatedCallback(...args))
38
+
39
+ return () => {
40
+ connectCreated.unsubscribe()
41
+ }
42
+ }, [])
43
+ }
44
+
45
+ ApiMakerUseCreatedEvent.propTypes = propTypesExact({
46
+ active: PropTypes.bool.isRequired,
47
+ debounce: PropTypes.oneOfType([
48
+ PropTypes.bool,
49
+ PropTypes.number
50
+ ]),
51
+ modelClass: PropTypes.func.isRequired,
52
+ onCreated: PropTypes.func.isRequired
53
+ })
54
+
55
+ export default ApiMakerUseCreatedEvent
@@ -0,0 +1,25 @@
1
+ import EventEmitter from "events"
2
+ import PropTypes from "prop-types"
3
+ import {useCallback, useEffect} from "react"
4
+
5
+ const ApiMakerUseEventEmitter = (events, event, onCalled) => {
6
+ const onCalledCallback = useCallback((...args) => {
7
+ onCalled.apply(null, ...args)
8
+ }, [events, event, onCalled])
9
+
10
+ useEffect(() => {
11
+ events.addListener(event, onCalledCallback)
12
+
13
+ return () => {
14
+ events.removeListener(event, onCalledCallback)
15
+ }
16
+ }, [events, event, onCalled])
17
+ }
18
+
19
+ ApiMakerUseEventEmitter.propTypes = {
20
+ event: PropTypes.string.isRequired,
21
+ events: PropTypes.instanceOf(EventEmitter).isRequired,
22
+ onCalled: PropTypes.func.isRequired
23
+ }
24
+
25
+ export default ApiMakerUseEventEmitter