Skip to content

Commit

Permalink
fix: fixed dialog destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed May 21, 2024
1 parent 3f4f70b commit ec32ac7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/web/src/common/dialogs/createDialog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, noop, share } from "rxjs"
import { Observable, share, tap } from "rxjs"
import { CancelError } from "../errors/errors"
import { DialogType, dialogSignal } from "./state"

Expand All @@ -11,15 +11,19 @@ export const createDialog = <Result = undefined>(
const id = generatedId.toString()

const $ = new Observable<Result>((observer) => {
let isClosed = false

const wrappedDialog: DialogType<Result> = {
...dialog,
id,
onCancel: () => {
isClosed = true
dialog.onCancel?.()
observer.error(new CancelError())
observer.complete()
},
onConfirm: () => {
isClosed = true
const data = dialog.onConfirm?.()
observer.next(data)
observer.complete()
Expand All @@ -29,9 +33,9 @@ export const createDialog = <Result = undefined>(
actions: dialog.actions?.map((action) => ({
...action,
onConfirm: () => {
isClosed = true
const data = action.onConfirm?.()

console.log("data", data)
observer.next(data)
observer.complete()

Expand All @@ -41,9 +45,17 @@ export const createDialog = <Result = undefined>(
}

dialogSignal.setValue((old) => [...old, wrappedDialog])
}).pipe(share())

$.subscribe({ error: noop })
return () => {
/**
* Make sure to close the dialog if there are no more subscribers
* and if the dialog is cancellable
*/
if (!isClosed && dialog.cancellable !== false) {
dialog.onCancel?.()
}
}
}).pipe(share())

return { id, $ }
}

0 comments on commit ec32ac7

Please sign in to comment.