Skip to content

Commit

Permalink
Fix mobx: deleting a node should be done through destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdjohnson committed Apr 4, 2024
1 parent a1902f2 commit ae9693b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
6 changes: 2 additions & 4 deletions src/models/ChatModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import { types, Instance, detach, flow, cast } from 'mobx-state-tree'
import { types, Instance, flow, cast, destroy } from 'mobx-state-tree'
import moment from 'moment'

import { IMessageModel, MessageModel } from '~/models/MessageModel'
Expand Down Expand Up @@ -82,9 +82,7 @@ export const ChatModel = types
},

deleteMessage(message: IMessageModel) {
detach(message)

_.remove(self.messages, message)
destroy(message)
},

findAndRegenerateResponse() {
Expand Down
14 changes: 7 additions & 7 deletions src/models/ChatStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { detach, types, SnapshotIn } from 'mobx-state-tree'
import { types, SnapshotIn, destroy } from 'mobx-state-tree'
import persist from 'mst-persist'
import _ from 'lodash'

Expand All @@ -24,15 +24,15 @@ export const ChatStore = types
},

deleteChat(chat: IChatModel) {
detach(chat)

_.remove(self.chats, { id: chat.id })

if (!_.isEmpty(self.chats)) {
if (self.selectedChat?.id === chat.id) {
self.selectedChat = self.chats[0]
} else {
}

if (!self.selectedChat) {
this.createChat()
}

destroy(chat)
},

selectChat(chat: IChatModel) {
Expand Down
10 changes: 2 additions & 8 deletions src/models/PersonaStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'
import { Instance, cast, detach, types } from 'mobx-state-tree'
import { Instance, cast, destroy, types } from 'mobx-state-tree'
import { persist } from 'mst-persist'

export const PersonaModel = types.model({
Expand Down Expand Up @@ -29,13 +29,7 @@ const PersonaStore = types
},

deletePersona(persona: IPersonaModel) {
detach(persona)

_.remove(self.personas, persona)

if (persona === self.selectedPersona) {
self.selectedPersona = undefined
}
destroy(persona)
},

duplicatePersona(persona: IPersonaModel) {
Expand Down
8 changes: 3 additions & 5 deletions src/models/ToastStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types, cast, Instance, detach } from 'mobx-state-tree'
import { types, cast, Instance, destroy } from 'mobx-state-tree'
import _ from 'lodash'

const ToastModel = types.model({
Expand All @@ -25,10 +25,8 @@ const ToastStore = types
self.toasts = cast([...toasts, { id: _.uniqueId('toast_'), message: message, type }])
},

removeToast(toast: { id: string }) {
detach(toast)

_.remove(self.toasts, toast)
removeToast(toast: IToastModel) {
destroy(toast)
},

clearToasts() {
Expand Down

0 comments on commit ae9693b

Please sign in to comment.