Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add offListener method #4

Merged
merged 3 commits into from
Jul 29, 2024
Merged

feat: add offListener method #4

merged 3 commits into from
Jul 29, 2024

Conversation

sanyuan0704
Copy link
Contributor

Movitation

Now we have onMessage to bind the listener for events from other process. But sometimes we want to remove the listener , especially in React component:

useEffect(() => {
  const hander = () => {
    // xxx
  }

  parentPort.onMessage('syn', handler);

  return () => {
    parentPort.offListener('syn', handler)
  }
}, [])

API Change

Add a new method offListener for MessagePort:

interface Port<T extends MessageDefinition, D extends Direction<T>> {
  // eslint-disable-next-line no-use-before-define
  postMessage<U extends keyof T[D]>(t: U, p?: Payload<T, D, U>, extra?: Pick<ChannelMessage, 'd' | 'c' | 'e'>): void;
  onMessage<U extends keyof T[ReverseDirection<T, D>]>(
    t: U,
    handler: Callback<[Payload<T, ReverseDirection<T, D>, U>]>,
  ): void;
+  offListener<U extends keyof T[ReverseDirection<T, D>]>(
+    t: U,
+    handler?: Callback<[Payload<T, ReverseDirection<T, D>, U>]>,
+  ): void;
}

If the handler param is specified, we will remove the handler. If not, we will remove all the handlers for the event.

@sanyuan0704 sanyuan0704 force-pushed the feat/add-off-listener branch from 1635019 to c808d10 Compare July 29, 2024 03:49
@sanyuan0704 sanyuan0704 force-pushed the feat/add-off-listener branch from c808d10 to 91ee549 Compare July 29, 2024 03:51
@sanyuan0704 sanyuan0704 requested a review from ulivz July 29, 2024 03:51
@sanyuan0704 sanyuan0704 merged commit 32c707d into main Jul 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant