@ngneat/react-rxjs
Version:
React goodies for applications that uses RxJS
170 lines (146 loc) • 5.68 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs'), require('react'), require('rxjs/operators')) :
typeof define === 'function' && define.amd ? define(['exports', 'rxjs', 'react', 'rxjs/operators'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactRxjs = {}, global.rxjs, global.React, global.operators));
})(this, (function (exports, rxjs, react, operators) { 'use strict';
/* eslint-disable @typescript-eslint/no-non-null-assertion */
function useObservable(source$, _a) {
var _b = _a === void 0 ? {} : _a,
_c = _b.deps,
deps = _c === void 0 ? [] : _c,
initialValue = _b.initialValue;
var sourceRef = react.useMemo(function () {
return source$;
}, deps);
var subscription = react.useRef(new rxjs.Subscription());
var nextValue = react.useRef(initialValue);
var _d = react.useState(),
error = _d[0],
setError = _d[1];
var _e = react.useState(false),
completed = _e[0],
setCompleted = _e[1];
var emitsInitialSyncValue = initialValue === undefined;
var _f = react.useReducer(function (x) {
return x + 1;
}, 0);
_f[0];
var forceUpdate = _f[1];
react.useMemo(function () {
if (emitsInitialSyncValue) {
var subscription_1 = sourceRef.subscribe(function (v) {
nextValue.current = v;
});
subscription_1.unsubscribe();
subscription_1 = null;
}
}, deps);
react.useEffect(function () {
var firstEmission = true;
subscription.current = sourceRef.subscribe({
next: function (value) {
if (emitsInitialSyncValue && firstEmission) {
firstEmission = false;
} else {
nextValue.current = value;
forceUpdate();
}
},
error: setError,
complete: setCompleted.bind(null, true)
});
return function () {
subscription === null || subscription === void 0 ? void 0 : subscription.current.unsubscribe();
};
}, deps);
return [nextValue.current, {
error: error,
completed: completed,
subscription: subscription.current
}];
}
function useUntilDestroyed() {
var subject = react.useMemo(function () {
return new rxjs.Subject();
}, []);
var data = react.useMemo(function () {
return {
untilDestroyed: function () {
return operators.takeUntil(subject.asObservable());
},
destroyed: subject.asObservable()
};
}, [subject]);
react.useEffect(function () {
return function () {
return subject.next(true);
};
}, [subject]);
return data;
}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __spreadArray(to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
}
function useEffect$(sourceFactory$, deps) {
if (deps === void 0) {
deps = [];
} // eslint-disable-next-line react-hooks/exhaustive-deps
var $ = react.useMemo(function () {
return sourceFactory$();
}, []);
var sub = react.useRef();
react.useEffect(function () {
sub.current = $.subscribe(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return function () {
return sub.current.unsubscribe();
}; // eslint-disable-next-line react-hooks/exhaustive-deps
}, __spreadArray([$], deps));
return sub.current;
}
function useFromEvent(eventName, action$, _a) {
var _b = _a === void 0 ? {
deps: []
} : _a,
deps = _b.deps;
var action = react.useRef(action$);
var eleRef = react.useRef(null);
react.useEffect(function () {
var subscription = new rxjs.Subscription();
var subject = null;
if (eleRef.current) {
subject = new rxjs.Subject();
subscription.add(action.current(subject.asObservable()).subscribe());
subscription.add(rxjs.fromEvent(eleRef.current, eventName).subscribe(function (v) {
subject === null || subject === void 0 ? void 0 : subject.next(v);
}));
}
return function () {
subscription === null || subscription === void 0 ? void 0 : subscription.unsubscribe();
subscription = null;
subject = null;
}; // eslint-disable-next-line react-hooks/exhaustive-deps
}, __spreadArray(__spreadArray([eleRef.current], deps), [eventName]));
return {
ref: eleRef
};
}
exports.useEffect$ = useEffect$;
exports.useFromEvent = useFromEvent;
exports.useObservable = useObservable;
exports.useUntilDestroyed = useUntilDestroyed;
Object.defineProperty(exports, '__esModule', { value: true });
}));