@kaspernj/api-maker 1.0.324 → 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 +1 -1
- package/src/event-created.jsx +7 -61
- package/src/event-emitter-listener.jsx +7 -22
- package/src/use-collection.mjs +36 -26
- package/src/use-created-event.mjs +55 -0
- package/src/use-event-emitter.mjs +25 -0
package/package.json
CHANGED
package/src/event-created.jsx
CHANGED
|
@@ -1,65 +1,11 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
active: true
|
|
10
|
-
}
|
|
3
|
+
const ApiMakerEventCreated = (props) => {
|
|
4
|
+
const {modelClass, onCreated, ...restProps} = props
|
|
11
5
|
|
|
12
|
-
|
|
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
|
-
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import React from "react"
|
|
1
|
+
import {memo} from "react"
|
|
2
|
+
import useEventEmitter from "./use-event-emitter.mjs"
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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)
|
package/src/use-collection.mjs
CHANGED
|
@@ -46,21 +46,23 @@ const useCollection = (
|
|
|
46
46
|
|
|
47
47
|
if (!queryName) queryName = digg(modelClass.modelClassData(), "collectionKey")
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
s.useStates({
|
|
50
|
+
models: undefined,
|
|
51
|
+
overallCount: undefined,
|
|
52
|
+
query: undefined,
|
|
53
|
+
queryName,
|
|
54
|
+
queryPerKey: `${queryName}_per`,
|
|
55
|
+
queryQName: `${queryName}_q`,
|
|
56
|
+
querySName: `${queryName}_s`,
|
|
57
|
+
queryPageName: `${queryName}_page`,
|
|
58
|
+
qParams: undefined,
|
|
59
|
+
result: undefined,
|
|
60
|
+
searchParams: undefined,
|
|
61
|
+
showNoRecordsAvailableContent: false,
|
|
62
|
+
showNoRecordsFoundContent: false
|
|
63
|
+
})
|
|
64
|
+
|
|
62
65
|
const queryParams = useQueryParams()
|
|
63
|
-
const modelIds = s.s.models?.map((model) => model.id())
|
|
64
66
|
|
|
65
67
|
let modelIdsCacheString
|
|
66
68
|
|
|
@@ -69,7 +71,7 @@ const useCollection = (
|
|
|
69
71
|
} else if (s.s.models.length === 0) {
|
|
70
72
|
modelIdsCacheString = "no-models"
|
|
71
73
|
} else {
|
|
72
|
-
modelIdsCacheString =
|
|
74
|
+
modelIdsCacheString = s.s.models.map((model) => model.cacheKey())?.join("---")
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
s.updateMeta({queryParams})
|
|
@@ -78,9 +80,11 @@ const useCollection = (
|
|
|
78
80
|
const baseQuery = s.p.collection || s.p.modelClass.all()
|
|
79
81
|
const overallCount = await baseQuery.count()
|
|
80
82
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
s.set({
|
|
84
|
+
overallCount,
|
|
85
|
+
showNoRecordsAvailableContent: showNoRecordsAvailableContent({overallCount}),
|
|
86
|
+
showNoRecordsFoundContent: showNoRecordsFoundContent({overallCount})
|
|
87
|
+
})
|
|
84
88
|
}, [])
|
|
85
89
|
|
|
86
90
|
const hasQParams = useCallback(() => {
|
|
@@ -107,8 +111,10 @@ const useCollection = (
|
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
s.set({
|
|
115
|
+
qParams: qParamsToSet,
|
|
116
|
+
searchParams
|
|
117
|
+
})
|
|
112
118
|
}, [])
|
|
113
119
|
|
|
114
120
|
const loadModels = useCallback(async () => {
|
|
@@ -160,16 +166,20 @@ const useCollection = (
|
|
|
160
166
|
})
|
|
161
167
|
}
|
|
162
168
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
169
|
+
s.set({
|
|
170
|
+
models: result.models(),
|
|
171
|
+
query,
|
|
172
|
+
result,
|
|
173
|
+
showNoRecordsAvailableContent: showNoRecordsAvailableContent({models}),
|
|
174
|
+
showNoRecordsFoundContent: showNoRecordsFoundContent({models})
|
|
175
|
+
})
|
|
168
176
|
}, [])
|
|
169
177
|
|
|
170
178
|
const loadModelsDebounce = useCallback(debounce(loadModels), [])
|
|
171
179
|
const onModelDestroyed = useCallback((args) => {
|
|
172
|
-
|
|
180
|
+
s.set({
|
|
181
|
+
models: s.s.models.filter((model) => model.id() != args.model.id())
|
|
182
|
+
})
|
|
173
183
|
}, [])
|
|
174
184
|
|
|
175
185
|
const onModelUpdated = useCallback((args) => {
|
|
@@ -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
|