@kaspernj/api-maker 1.0.327 → 1.0.328

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.327",
19
+ "version": "1.0.328",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -49,7 +49,7 @@
49
49
  "js-money": ">= 0.6.3",
50
50
  "numberable": ">= 1.0.0",
51
51
  "object-to-formdata": ">= 4.1.0",
52
- "on-location-changed": ">= 1.0.9",
52
+ "on-location-changed": ">= 1.0.10",
53
53
  "qs": ">= 6.9.3",
54
54
  "replaceall": ">= 0.1.6",
55
55
  "set-state-compare": ">= 1.0.28",
@@ -552,15 +552,24 @@ export default class BaseModel {
552
552
 
553
553
  setNewModel (model) {
554
554
  this.setNewModelData(model)
555
- this.relationships = digg(model, "relationships")
556
- this.relationshipsCache = digg(model, "relationshipsCache")
555
+
556
+ for(const relationshipName in model.relationships) {
557
+ this.relationships[relationshipName] = model.relationships[relationshipName]
558
+ }
559
+
560
+ for(const relationshipCacheName in model.relationshipsCache) {
561
+ this.relationshipsCache[relationshipCacheName] = model.relationshipsCache[name]
562
+ }
557
563
  }
558
564
 
559
565
  setNewModelData (model) {
560
566
  if (!("modelData" in model)) throw new Error(`No modelData in model: ${JSON.stringify(model)}`)
561
567
 
562
- this.previousModelData = digg(this, "modelData")
563
- this.modelData = digg(model, "modelData")
568
+ this.previousModelData = Object.assign({}, digg(this, "modelData"))
569
+
570
+ for(const attributeName in model.modelData) {
571
+ this.modelData[attributeName] = model.modelData[attributeName]
572
+ }
564
573
  }
565
574
 
566
575
  _isDateChanged (oldValue, newValue) {
package/src/devise.mjs CHANGED
@@ -6,44 +6,38 @@ import * as inflection from "inflection"
6
6
  import modelClassRequire from "./model-class-require.mjs"
7
7
  import Services from "./services.mjs"
8
8
 
9
+ const events = new EventEmitter()
10
+ const shared = {}
11
+
12
+ events.setMaxListeners(1000)
13
+
14
+ export {events}
15
+
9
16
  export default class ApiMakerDevise {
10
17
  static callSignOutEvent (args) {
11
- ApiMakerDevise.events().emit("onDeviseSignOut", {args})
18
+ events.emit("onDeviseSignOut", {args})
12
19
  }
13
20
 
14
21
  static current () {
15
- if (!globalThis.currentApiMakerDevise) {
16
- globalThis.currentApiMakerDevise = new ApiMakerDevise()
22
+ if (!shared.currentApiMakerDevise) {
23
+ shared.currentApiMakerDevise = new ApiMakerDevise()
17
24
  }
18
25
 
19
- return globalThis.currentApiMakerDevise
26
+ return shared.currentApiMakerDevise
20
27
  }
21
28
 
22
29
  static events () {
23
- if (!globalThis.apiMakerDeviseEvents) {
24
- globalThis.apiMakerDeviseEvents = new EventEmitter()
25
- globalThis.apiMakerDeviseEvents.setMaxListeners(1000)
26
- }
27
-
28
- return globalThis.apiMakerDeviseEvents
30
+ return events
29
31
  }
30
32
 
31
33
  static addUserScope (scope) {
32
34
  const currentMethodName = `current${inflection.camelize(scope)}`
33
35
 
34
- ApiMakerDevise[currentMethodName] = function () {
35
- return ApiMakerDevise.current().getCurrentScope(scope)
36
- }
36
+ ApiMakerDevise[currentMethodName] = () => ApiMakerDevise.current().getCurrentScope(scope)
37
37
 
38
38
  const isSignedInMethodName = `is${inflection.camelize(scope)}SignedIn`
39
39
 
40
- ApiMakerDevise[isSignedInMethodName] = function () {
41
- if (ApiMakerDevise.current().getCurrentScope(scope)) {
42
- return true
43
- }
44
-
45
- return false
46
- }
40
+ ApiMakerDevise[isSignedInMethodName] = () => Boolean(ApiMakerDevise.current().getCurrentScope(scope))
47
41
  }
48
42
 
49
43
  static async signIn (username, password, args = {}) {
@@ -59,7 +53,7 @@ export default class ApiMakerDevise {
59
53
  await CanCan.current().resetAbilities()
60
54
 
61
55
  ApiMakerDevise.updateSession(model)
62
- ApiMakerDevise.events().emit("onDeviseSignIn", Object.assign({username}, args))
56
+ events.emit("onDeviseSignIn", Object.assign({username}, args))
63
57
 
64
58
  return {model, response}
65
59
  }
@@ -88,8 +82,8 @@ export default class ApiMakerDevise {
88
82
  await CanCan.current().resetAbilities()
89
83
 
90
84
  // Cannot use the class because they would both import each other
91
- if (globalThis.apiMakerSessionStatusUpdater) {
92
- globalThis.apiMakerSessionStatusUpdater.updateSessionStatus()
85
+ if (shared.apiMakerSessionStatusUpdater) {
86
+ shared.apiMakerSessionStatusUpdater.updateSessionStatus()
93
87
  }
94
88
 
95
89
  ApiMakerDevise.setSignedOut(args)
@@ -103,8 +97,9 @@ export default class ApiMakerDevise {
103
97
  }
104
98
 
105
99
  getCurrentScope (scope) {
106
- if (!(scope in this.currents))
100
+ if (!(scope in this.currents)) {
107
101
  this.currents[scope] = this.loadCurrentScope(scope)
102
+ }
108
103
 
109
104
  return this.currents[scope]
110
105
  }
@@ -5,7 +5,7 @@ const useCurrentUser = () => {
5
5
  const [currentUser, setCurrentUser] = useState(Devise.currentUser())
6
6
  const updateCurrentUser = useCallback(() => {
7
7
  setCurrentUser(Devise.currentUser())
8
- })
8
+ }, [])
9
9
 
10
10
  useEffect(() => {
11
11
  Devise.events().addListener("onDeviseSignIn", updateCurrentUser)
@@ -1,30 +1,9 @@
1
- import Devise from "./devise"
2
- import {digs} from "diggerize"
3
- import EventEmitterListener from "./event-emitter-listener"
4
- import PureComponent from "set-state-compare/src/pure-component"
5
- import React from "react"
1
+ import useCurrentUser from "./use-current-user.mjs"
6
2
 
7
- export default (WrappedComponent) => class WithCurrentUser extends PureComponent {
8
- state = {
9
- currentUser: Devise.currentUser()
10
- }
3
+ export default (WrappedComponent) => (props) => {
4
+ const currentUser = useCurrentUser()
11
5
 
12
- render() {
13
- const {onDeviseSigned} = digs(this, "onDeviseSigned")
14
- const {currentUser} = digs(this.state, "currentUser")
15
-
16
- return (
17
- <>
18
- <EventEmitterListener events={Devise.events()} event="onDeviseSignIn" onCalled={onDeviseSigned} />
19
- <EventEmitterListener events={Devise.events()} event="onDeviseSignOut" onCalled={onDeviseSigned} />
20
- <WrappedComponent {...this.props} currentUser={currentUser} />
21
- </>
22
- )
23
- }
24
-
25
- onDeviseSigned = () => {
26
- const currentUser = Devise.currentUser()
27
-
28
- if (this.state.currentUser !== currentUser) this.setState({currentUser})
29
- }
6
+ return (
7
+ <WrappedComponent {...props} currentUser={currentUser} />
8
+ )
30
9
  }