UNPKG

@td-design/rn-hooks

Version:

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

57 lines (52 loc) 1.69 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _tslib = require('../_virtual/_tslib.js'); var react = require('react'); var EventEmitter = /** @class */ (function () { function EventEmitter() { this.subscriptions = new Set(); } EventEmitter.prototype.emit = function (val) { var e_1, _a; try { for (var _b = _tslib.__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 = react.useRef(); callbackRef.current = cb; react.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 = react.useRef(); if (!ref.current) { ref.current = new EventEmitter(); } return ref.current; } exports.EventEmitter = EventEmitter; exports.default = useEventEmitter;