Skip to content

Mechanism for removing connection onClose callback #229

Open
@viglucci

Description

@viglucci

We can register a callback with an RSocket using the onClose method. We do not currently have any way to deregister a previously registered callback.

Motivation

I'm sure there are other use cases, however, this would be useful when paired with the useEffect hook in React, which expect to be able to "undo" registrations and side-effects.

useEffect(() => {
  const id = rsocket.onClose((e) => {
    console.log(e);
  });
  return () => {
    rsocket.removeCloseListener(id);
  };
}, [rsocket]);

Desired solution

RSocket, likely through Closeable, could be extended to allow deregistration of callbacks. This might be best with having Closeable more closely resemble event emitter APIs.

// add a listener
const id = rsocket.onClose.addEventListener(() => {...});

// remove a listener
rsocket.onClose.removeEventListener(id);

Considered alternatives

Rsocket.onClose could implement/expose an observable that would natively support subscriptions. This could use existing types such as Cancellable and OnTerminalSubscriber.

Metadata

Metadata

Assignees

No one assigned

    Labels

    1.0Pullrequests & issues related to the Typescript rewrite and 1.0 releaseenhancementSuggests, requests, or implements a feature or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions