Skip to content

Commit

Permalink
[desk-tool] Make sure to dispose remote mutations subscription upon o…
Browse files Browse the repository at this point in the history
…bservableController disposal
  • Loading branch information
bjoerge authored and rexxars committed Oct 6, 2020
1 parent a529b7b commit 5f64066
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,13 @@ function DocumentPane(props: Props) {
})
)

const historyState = useObservable(
const historyController = useObservable(
createObservableController({
timeline,
documentId,
client
}),
{error: new Error('should not happen')}
)

if (historyState.error) throw historyState.error
const historyController = historyState.controller
})
)! // note: this emits sync so can never be null

// TODO: Fetch only when open
React.useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,25 @@ export class Controller {
}
}

export type ControllerState = {controller: Controller; error?: undefined} | {error: Error}

export function createObservableController(
options: Omit<Options, 'handler'>
): Observable<ControllerState> {
return fromEventPattern(
handler => {
const controller = new Controller({handler, ...options})

// TODO: How to handle unsubscription?
remoteMutations({
publishedId: options.documentId,
draftId: `drafts.${options.documentId}`
}).subscribe(ev => {
controller.handleRemoteMutation(ev)
})
},
undefined,
(error, controller) => (error ? {error} : {controller})
)
): Observable<Controller> {
return new Observable(observer => {
const controller = new Controller({
...options,
handler: (err, innerController) => {
if (err) {
observer.error(err)
} else {
observer.next(innerController)
}
}
})
return remoteMutations({
publishedId: options.documentId,
draftId: `drafts.${options.documentId}`
}).subscribe(ev => {
controller.handleRemoteMutation(ev)
})
})
}

0 comments on commit 5f64066

Please sign in to comment.