| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- var __assign = (this && this.__assign) || function () {
- __assign = Object.assign || function(t) {
- for (var s, i = 1, n = arguments.length; i < n; i++) {
- s = arguments[i];
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
- t[p] = s[p];
- }
- return t;
- };
- return __assign.apply(this, arguments);
- };
- import * as React from 'react';
- import { Injector } from '@wendellhu/redi';
- import { RediProvider, RediConsumer } from './reactContext';
- function RediInjector(props) {
- var children = props.children, dependencies = props.dependencies;
- var childInjectorRef = React.useRef(null);
- // dispose the injector when the container Injector unmounts
- React.useEffect(function () { return function () { var _a; return (_a = childInjectorRef.current) === null || _a === void 0 ? void 0 : _a.dispose(); }; }, []);
- return (React.createElement(RediConsumer, null, function (context) {
- var childInjector;
- if (childInjectorRef.current) {
- childInjector = childInjectorRef.current;
- }
- else {
- childInjector = context.injector
- ? context.injector.createChild(dependencies)
- : new Injector(dependencies);
- childInjectorRef.current = childInjector;
- }
- return (React.createElement(RediProvider, { value: { injector: childInjector } }, children));
- }));
- }
- /**
- * @param Comp
- * @param injector
- * @returns
- */
- export function connectInjector(Comp, injector) {
- return function ComponentWithInjector(props) {
- return (React.createElement(RediProvider, { value: { injector: injector } },
- React.createElement(Comp, __assign({}, props))));
- };
- }
- export function connectDependencies(Comp, dependencies) {
- return function ComponentWithInjector(props) {
- return (React.createElement(RediInjector, { dependencies: dependencies },
- React.createElement(Comp, __assign({}, props))));
- };
- }
- //# sourceMappingURL=reactComponent.js.map
|