@pexip/signal
Version:
an observer pattern while avoiding boilerplate code. https://en.wikipedia.org/wiki/Signals_and_slots
38 lines (27 loc) • 714 B
text/mdx
```ts
type Detach = () => void;
```
A detach function to remove its observer from the Signal construct when it is
called.
`void`
```typescript
interface Message {
type: string;
payload: any;
}
const messageSignal = createSignal<Message>();
const messageHandler = (message: Message) => {
logger.info({message});
};
const detachMessageSignal = connectedSignal.add(messageHandler);
// Wire the signal to WebSocket onMessage
ws.onmessage((message) => {
messageSignal.emit(message);
});
// Later there is no need to listen the message event, or the context is
about to be abandoned
// Call the detach function to remove the listener from messageSignal
detachMessageSignal();
```