UNPKG

@td-design/rn-hooks

Version:

从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率

52 lines (49 loc) 1.58 kB
import { __values } from '../_virtual/_tslib.js'; import { useRef, useEffect } from 'react'; var EventEmitter = /** @class */ (function () { function EventEmitter() { this.subscriptions = new Set(); } EventEmitter.prototype.emit = function (val) { var e_1, _a; try { for (var _b = __values(this.subscriptions), _c = _b.next(); !_c.done; _c = _b.next()) { var subscription = _c.value; subscription(val); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; EventEmitter.prototype.useSubscription = function (cb) { var _this = this; var callbackRef = useRef(); callbackRef.current = cb; useEffect(function () { function subscription(val) { if (callbackRef.current) { callbackRef.current(val); } } _this.subscriptions.add(subscription); return function () { _this.subscriptions.delete(subscription); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); }; return EventEmitter; }()); function useEventEmitter() { var ref = useRef(); if (!ref.current) { ref.current = new EventEmitter(); } return ref.current; } export { EventEmitter, useEventEmitter as default };