r3bl-ts-utils
Version:
The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu
22 lines (21 loc) • 1.51 kB
TypeScript
import EventEmitter from "events";
import { IsActive, NodeJsListenerFn } from "./nodejs-types";
/**
* In React, emitters can't invoke callback functions that are part of a function component.
* Even though hooks look like normal functions, there are lots of restrictions on them. A big
* one is that only React function components can call hooks. This leads to a problem that shows
* up when you need to have an external (to React) event emitter generate an event that needs to
* be dispatched to a function component. This emitter can't directly call into React code!
*
* The solution is using this hook as an intermediary / proxy. Internally, this hook maintains a
* state that gets updated by the external (to React) emitter, and then an effect (which marks
* this as a dependency) that actually calls into your React component callback function. This
* article on developerlife.com explains this in much greater detail:
* http://developerlife.com/2021/10/19/react-hooks-redux-typescript-handbook/#custom-hooks
*
* Here are some more resources related to this:
* https://stackoverflow.com/questions/53898810/executing-async-code-on-update-of-state-with-react-hooks
* https://github.com/r3bl-org/r3bl-ts-utils/commit/a3248540ea325d3896ee56a84d003f15529169cd
* https://github.com/r3bl-org/r3bl-ts-utils/commit/1f3cbb2b4988f44c6ea48233db1730e10f18dc60
*/
export declare const useEventEmitter: (emitter: EventEmitter, eventName: string, eventHandler: NodeJsListenerFn, options: IsActive) => void;