cheap-di-react
Advanced tools
Comparing version 3.2.3 to 3.3.0
@@ -1,2 +0,8 @@ | ||
export { stateful, isStateful, statefulSymbol } from './stateful'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.statefulSymbol = exports.isStateful = exports.stateful = void 0; | ||
var stateful_1 = require("./stateful"); | ||
Object.defineProperty(exports, "stateful", { enumerable: true, get: function () { return stateful_1.stateful; } }); | ||
Object.defineProperty(exports, "isStateful", { enumerable: true, get: function () { return stateful_1.isStateful; } }); | ||
Object.defineProperty(exports, "statefulSymbol", { enumerable: true, get: function () { return stateful_1.statefulSymbol; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -1,11 +0,14 @@ | ||
import { inheritancePreserveSymbol } from 'cheap-di'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InheritancePreserver = void 0; | ||
const cheap_di_1 = require("cheap-di"); | ||
class InheritancePreserver { | ||
static constructorModified(constructor) { | ||
constructor[inheritancePreserveSymbol] = constructor; | ||
constructor[cheap_di_1.inheritancePreserveSymbol] = constructor; | ||
} | ||
static getModifiedConstructor(constructor) { | ||
return constructor[inheritancePreserveSymbol]; | ||
return constructor[cheap_di_1.inheritancePreserveSymbol]; | ||
} | ||
} | ||
export { InheritancePreserver }; | ||
exports.InheritancePreserver = InheritancePreserver; | ||
//# sourceMappingURL=InheritancePreserver.js.map |
@@ -1,10 +0,15 @@ | ||
import { di } from 'cheap-di'; | ||
import { InheritancePreserver } from './InheritancePreserver'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.removeStateful = exports.statefulSymbol = exports.isStateful = exports.stateful = void 0; | ||
const cheap_di_1 = require("cheap-di"); | ||
const InheritancePreserver_1 = require("./InheritancePreserver"); | ||
const statefulSymbol = Symbol('cheap-di-react stateful'); | ||
exports.statefulSymbol = statefulSymbol; | ||
function stateful(constructor) { | ||
constructor[statefulSymbol] = true; | ||
di(constructor); | ||
InheritancePreserver.constructorModified(constructor); | ||
(0, cheap_di_1.di)(constructor); | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(constructor); | ||
return constructor; | ||
} | ||
exports.stateful = stateful; | ||
function removeStateful(constructor) { | ||
@@ -15,4 +20,5 @@ if (isStateful(constructor)) { | ||
} | ||
exports.removeStateful = removeStateful; | ||
function isStateful(constructor) { | ||
const modifiedConstructor = InheritancePreserver.getModifiedConstructor(constructor); | ||
const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); | ||
return !!modifiedConstructor | ||
@@ -22,3 +28,3 @@ && modifiedConstructor === constructor | ||
} | ||
export { stateful, isStateful, statefulSymbol, removeStateful }; | ||
exports.isStateful = isStateful; | ||
//# sourceMappingURL=stateful.js.map |
@@ -1,3 +0,6 @@ | ||
import { createContext } from 'react'; | ||
export const DiContext = createContext({ container: undefined }); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DiContext = void 0; | ||
const react_1 = require("react"); | ||
exports.DiContext = (0, react_1.createContext)({ container: undefined }); | ||
//# sourceMappingURL=DiContext.js.map |
@@ -1,3 +0,6 @@ | ||
import { reconfigureObject } from '../utils'; | ||
import { configuredSymbol, updateContextSymbol, } from '../StatefulImplementation'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isConfigured = exports.configureStateful = void 0; | ||
const utils_1 = require("../utils"); | ||
const StatefulImplementation_1 = require("../StatefulImplementation"); | ||
function configureStateful(instance, rerender, logger) { | ||
@@ -8,11 +11,12 @@ const configurationInstance = instance; | ||
} | ||
configurationInstance[updateContextSymbol] = rerender; | ||
reconfigureObject(instance, logger); | ||
configurationInstance[configuredSymbol] = true; | ||
configurationInstance[StatefulImplementation_1.updateContextSymbol] = rerender; | ||
(0, utils_1.reconfigureObject)(instance, logger); | ||
configurationInstance[StatefulImplementation_1.configuredSymbol] = true; | ||
logger.log('instance', instance, 'configured'); | ||
} | ||
exports.configureStateful = configureStateful; | ||
function isConfigured(constructor) { | ||
return constructor[configuredSymbol] === true; | ||
return constructor[StatefulImplementation_1.configuredSymbol] === true; | ||
} | ||
export { configureStateful, isConfigured }; | ||
exports.isConfigured = isConfigured; | ||
//# sourceMappingURL=configureStateful.js.map |
@@ -1,3 +0,6 @@ | ||
export * from './useContainer'; | ||
export * from './use'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./useContainer"), exports); | ||
tslib_1.__exportStar(require("./use"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,8 @@ | ||
import { useContext } from 'react'; | ||
import { DiContext } from '../DiContext'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.use = void 0; | ||
const react_1 = require("react"); | ||
const DiContext_1 = require("../DiContext"); | ||
function use(type, ...args) { | ||
const context = useContext(DiContext); | ||
const context = (0, react_1.useContext)(DiContext_1.DiContext); | ||
if (!context || !context.container) { | ||
@@ -14,3 +17,3 @@ throw new Error('Container not found. You should use Provider as one of parent nodes of this component'); | ||
} | ||
export { use }; | ||
exports.use = use; | ||
//# sourceMappingURL=use.js.map |
@@ -1,16 +0,19 @@ | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { DiContext } from '../DiContext'; | ||
import { ReactContainer } from '../ReactContainer'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useContainer = void 0; | ||
const react_1 = require("react"); | ||
const DiContext_1 = require("../DiContext"); | ||
const ReactContainer_1 = require("../ReactContainer"); | ||
function useContainer(logger) { | ||
const parentContainer = useContext(DiContext).container; | ||
const [contextValue, setContextValue] = useState({ container: undefined }); | ||
useEffect(() => { | ||
const parentContainer = (0, react_1.useContext)(DiContext_1.DiContext).container; | ||
const [contextValue, setContextValue] = (0, react_1.useState)({ container: undefined }); | ||
(0, react_1.useEffect)(() => { | ||
if (!contextValue.container) { | ||
if (parentContainer) { | ||
logger.log('create container'); | ||
contextValue.container = new ReactContainer(parentContainer); | ||
contextValue.container = new ReactContainer_1.ReactContainer(parentContainer); | ||
} | ||
else { | ||
logger.log('create root container'); | ||
contextValue.container = new ReactContainer(); | ||
contextValue.container = new ReactContainer_1.ReactContainer(); | ||
contextValue.container.rerender = () => setContextValue(Object.assign({}, contextValue)); | ||
@@ -23,6 +26,6 @@ } | ||
} | ||
// exceptional case: if by some of reasons parent container will be changed | ||
// exceptional case: if by some reasons parent container will be changed | ||
// possible, unreachable case | ||
logger.log('RECREATE container'); | ||
setContextValue({ container: new ReactContainer(parentContainer) }); | ||
setContextValue({ container: new ReactContainer_1.ReactContainer(parentContainer) }); | ||
}, [parentContainer]); | ||
@@ -37,3 +40,3 @@ const rerender = () => { | ||
} | ||
export { useContainer }; | ||
exports.useContainer = useContainer; | ||
//# sourceMappingURL=useContainer.js.map |
@@ -1,5 +0,8 @@ | ||
export * from './DiContext'; | ||
export * from './decorators'; | ||
export * from './Providers'; | ||
export * from './hooks/use'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./DiContext"), exports); | ||
tslib_1.__exportStar(require("./decorators"), exports); | ||
tslib_1.__exportStar(require("./Providers"), exports); | ||
tslib_1.__exportStar(require("./hooks/use"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,4 +0,2 @@ | ||
export * from './OneTimeProvider'; | ||
export * from './Provider'; | ||
export * from './SelfOneTimeProvider'; | ||
export * from './SelfProvider'; | ||
export * from './DIOneTimeProvider'; | ||
export * from './DIProvider'; |
@@ -1,5 +0,6 @@ | ||
export * from './OneTimeProvider'; | ||
export * from './Provider'; | ||
export * from './SelfOneTimeProvider'; | ||
export * from './SelfProvider'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./DIOneTimeProvider"), exports); | ||
tslib_1.__exportStar(require("./DIProvider"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,11 +0,14 @@ | ||
import { __rest } from "tslib"; | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useMemo } from 'react'; | ||
import { Provider } from './Provider'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OneTimeProvider = void 0; | ||
const tslib_1 = require("tslib"); | ||
const jsx_runtime_1 = require("react/jsx-runtime"); | ||
const react_1 = require("react"); | ||
const Provider_1 = require("./Provider"); | ||
const OneTimeProvider = props => { | ||
const { dependencies, children } = props, rest = __rest(props, ["dependencies", "children"]); | ||
const registrations = useMemo(() => dependencies, []); | ||
return (_jsx(Provider, Object.assign({ dependencies: registrations }, rest, { children: children }))); | ||
const { dependencies, children } = props, rest = tslib_1.__rest(props, ["dependencies", "children"]); | ||
const registrations = (0, react_1.useMemo)(() => dependencies, []); | ||
return ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: registrations }, rest, { children: children }))); | ||
}; | ||
export { OneTimeProvider }; | ||
exports.OneTimeProvider = OneTimeProvider; | ||
//# sourceMappingURL=OneTimeProvider.js.map |
@@ -1,17 +0,20 @@ | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { isSingleton, } from 'cheap-di'; | ||
import { Fragment, memo, useEffect, useRef, useState } from 'react'; | ||
import { isStateful } from '../decorators'; | ||
import { DiContext } from '../DiContext'; | ||
import { useContainer } from '../hooks'; | ||
import { configureStateful } from '../hooks/configureStateful'; | ||
import { InternalLogger } from '../utils'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Provider = void 0; | ||
const jsx_runtime_1 = require("react/jsx-runtime"); | ||
const cheap_di_1 = require("cheap-di"); | ||
const react_1 = require("react"); | ||
const decorators_1 = require("../decorators"); | ||
const DiContext_1 = require("../DiContext"); | ||
const hooks_1 = require("../hooks"); | ||
const configureStateful_1 = require("../hooks/configureStateful"); | ||
const utils_1 = require("../utils"); | ||
const Provider = props => { | ||
const { dependencies, debugName, children, } = props; | ||
const [logger] = useState(() => new InternalLogger(debugName)); | ||
const [initialized, setInitialized] = useState(false); | ||
const timerRef = useRef(null); | ||
const [contextValue, rerender] = useContainer(logger); | ||
const [logger] = (0, react_1.useState)(() => new utils_1.InternalLogger(debugName)); | ||
const [initialized, setInitialized] = (0, react_1.useState)(false); | ||
const timerRef = (0, react_1.useRef)(null); | ||
const [contextValue, rerender] = (0, hooks_1.useContainer)(logger); | ||
const container = contextValue.container; | ||
useEffect(() => { | ||
(0, react_1.useEffect)(() => { | ||
var _a; | ||
@@ -31,8 +34,8 @@ if (!container || !dependencies) { | ||
} | ||
if (isSingleton(constructor)) { | ||
if ((0, cheap_di_1.isSingleton)(constructor)) { | ||
logger.log('singleton', constructor, 'founded'); | ||
const instance = container.resolve(type); | ||
configureStateful(instance, container.rootContainer.rerender, logger); | ||
(0, configureStateful_1.configureStateful)(instance, container.rootContainer.rerender, logger); | ||
} | ||
if (isStateful(constructor)) { | ||
if ((0, decorators_1.isStateful)(constructor)) { | ||
logger.log('stateful', constructor, 'founded'); | ||
@@ -42,3 +45,3 @@ container.skipParentInstanceResolvingOnce(); // target instance should be instantiated from zero | ||
container.registerInstance(instance).as(type); | ||
configureStateful(instance, rerender, logger); | ||
(0, configureStateful_1.configureStateful)(instance, rerender, logger); | ||
} | ||
@@ -54,3 +57,3 @@ } | ||
}, [container, dependencies]); | ||
useEffect(() => { | ||
(0, react_1.useEffect)(() => { | ||
return () => { | ||
@@ -66,7 +69,7 @@ if (timerRef.current) { | ||
} | ||
return (_jsx(DiContext.Provider, Object.assign({ value: contextValue }, { children: _jsx(MemoizedChildren, { children: children }) }))); | ||
return ((0, jsx_runtime_1.jsx)(DiContext_1.DiContext.Provider, Object.assign({ value: contextValue }, { children: (0, jsx_runtime_1.jsx)(MemoizedChildren, { children: children }) }))); | ||
}; | ||
const MemoizedChildren = memo(({ children }) => _jsx(Fragment, { children: children })); | ||
const memoizedComponent = memo(Provider); | ||
export { memoizedComponent as Provider }; | ||
const MemoizedChildren = (0, react_1.memo)(({ children }) => (0, jsx_runtime_1.jsx)(react_1.Fragment, { children: children })); | ||
const memoizedComponent = (0, react_1.memo)(Provider); | ||
exports.Provider = memoizedComponent; | ||
//# sourceMappingURL=Provider.js.map |
@@ -1,10 +0,12 @@ | ||
import { __awaiter, __decorate } from "tslib"; | ||
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; | ||
import { render, fireEvent, act } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { singleton } from 'cheap-di'; | ||
import { stateful } from '../decorators'; | ||
import { Provider } from './Provider'; | ||
import { SelfOneTimeProvider } from './SelfOneTimeProvider'; | ||
import { use } from '../hooks'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const jsx_runtime_1 = require("react/jsx-runtime"); | ||
const react_1 = require("@testing-library/react"); | ||
require("@testing-library/jest-dom"); | ||
const cheap_di_1 = require("cheap-di"); | ||
const decorators_1 = require("../decorators"); | ||
const Provider_1 = require("./Provider"); | ||
const SelfOneTimeProvider_1 = require("./SelfOneTimeProvider"); | ||
const hooks_1 = require("../hooks"); | ||
test('use jsdom in this test file', () => { | ||
@@ -36,9 +38,9 @@ const element = document.createElement('div'); | ||
const Component = () => { | ||
const logger = use(SomeConsoleLogger); | ||
return _jsx("p", { children: logger.debug('another layout') }); | ||
const logger = (0, hooks_1.use)(SomeConsoleLogger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('another layout') }); | ||
}; | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(SomeConsoleLogger), | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryByText('another layout')).toBeInTheDocument(); | ||
@@ -48,9 +50,9 @@ }); | ||
const Component = () => { | ||
const logger = use(Logger); | ||
return _jsx("p", { children: logger.debug('my layout') }); | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }); | ||
}; | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(ConsoleLogger).as(Logger), | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryByText('my layout')).toBeInTheDocument(); | ||
@@ -60,9 +62,9 @@ }); | ||
const Component = () => { | ||
const logger = use(Logger); | ||
return _jsx("p", { children: logger.debug('my layout') }); | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }); | ||
}; | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(ConsoleLogger).as(Logger).with('my message'), | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryByText('my message: my layout')).toBeInTheDocument(); | ||
@@ -78,9 +80,9 @@ }); | ||
const Component = () => { | ||
const database = use(Database); | ||
return (_jsx("p", { children: database.entities.map(e => (_jsx("span", { children: e }, e))) })); | ||
const database = (0, hooks_1.use)(Database); | ||
return ((0, jsx_runtime_1.jsx)("p", { children: database.entities.map(e => ((0, jsx_runtime_1.jsx)("span", { children: e }, e))) })); | ||
}; | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerInstance(new Database(entities)), | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryByText(entities[0])).toBeInTheDocument(); | ||
@@ -91,11 +93,11 @@ expect(queryByText(entities[1])).toBeInTheDocument(); | ||
const Component = () => { | ||
const logger = use(Logger); | ||
return _jsx("p", { children: logger.debug('my layout') }); | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }); | ||
}; | ||
const RootComponent = () => (_jsxs(Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => ((0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(ConsoleLogger).as(Logger).with('my message'), | ||
] }, { children: [_jsx(Component, {}), _jsx(Provider, Object.assign({ dependencies: [ | ||
] }, { children: [(0, jsx_runtime_1.jsx)(Component, {}), (0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(SomeConsoleLogger).as(Logger), | ||
] }, { children: _jsx(Component, {}) }))] }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))] }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryByText('my message: my layout')).toBeInTheDocument(); | ||
@@ -106,3 +108,3 @@ expect(queryByText('my layout')).toBeInTheDocument(); | ||
describe('singleton and stateful', () => { | ||
test('singleton', () => __awaiter(void 0, void 0, void 0, function* () { | ||
test('singleton', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
let MySingleton = class MySingleton { | ||
@@ -113,3 +115,3 @@ constructor() { | ||
loadData() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this.data = yield Promise.resolve('loaded data'); | ||
@@ -119,4 +121,4 @@ }); | ||
}; | ||
MySingleton = __decorate([ | ||
singleton | ||
MySingleton = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
], MySingleton); | ||
@@ -127,23 +129,23 @@ const dependencies = [ | ||
const RootComponent = () => { | ||
return (_jsxs(Provider, Object.assign({ dependencies: dependencies }, { children: [_jsx(LoadComponent, {}), _jsx(Provider, Object.assign({ dependencies: dependencies }, { children: _jsx(ReadComponent, {}) }))] }))); | ||
return ((0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [(0, jsx_runtime_1.jsx)(LoadComponent, {}), (0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: (0, jsx_runtime_1.jsx)(ReadComponent, {}) }))] }))); | ||
}; | ||
const buttonId = 'my-button'; | ||
const LoadComponent = () => { | ||
const mySingleton = use(MySingleton); | ||
return (_jsxs("div", { children: [_jsx("button", { "data-testid": buttonId, onClick: () => { | ||
const mySingleton = (0, hooks_1.use)(MySingleton); | ||
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("button", { "data-testid": buttonId, onClick: () => { | ||
mySingleton.loadData(); | ||
} }), _jsx("span", { children: mySingleton.data })] })); | ||
} }), (0, jsx_runtime_1.jsx)("span", { children: mySingleton.data })] })); | ||
}; | ||
const ReadComponent = () => (_jsx("span", { children: use(MySingleton).data })); | ||
const { queryAllByText, getByTestId, rerender } = render(_jsx(RootComponent, {})); | ||
const ReadComponent = () => ((0, jsx_runtime_1.jsx)("span", { children: (0, hooks_1.use)(MySingleton).data })); | ||
const { queryAllByText, getByTestId, rerender } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(2); | ||
expect(queryAllByText('loaded data').length).toBe(0); | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(buttonId)); | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(buttonId)); | ||
})); | ||
rerender(_jsx(RootComponent, {})); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(0); | ||
expect(queryAllByText('loaded data').length).toBe(2); | ||
})); | ||
test('stateful', () => __awaiter(void 0, void 0, void 0, function* () { | ||
test('stateful', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
let MyStateful = class MyStateful { | ||
@@ -154,4 +156,4 @@ constructor() { | ||
}; | ||
MyStateful = __decorate([ | ||
stateful | ||
MyStateful = tslib_1.__decorate([ | ||
decorators_1.stateful | ||
], MyStateful); | ||
@@ -166,7 +168,7 @@ const dependencies = [ | ||
const RootComponent = () => { | ||
return (_jsxs(Provider, Object.assign({ dependencies: dependencies }, { children: [_jsx(LoadComponent, { message: firstMessage, buttonId: button1 }), _jsx(ReadComponent, {}), _jsxs(Provider, Object.assign({ dependencies: dependencies }, { children: [_jsx(LoadComponent, { message: secondMessage, buttonId: button2 }), _jsx(ReadComponent, {})] }))] }))); | ||
return ((0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [(0, jsx_runtime_1.jsx)(LoadComponent, { message: firstMessage, buttonId: button1 }), (0, jsx_runtime_1.jsx)(ReadComponent, {}), (0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [(0, jsx_runtime_1.jsx)(LoadComponent, { message: secondMessage, buttonId: button2 }), (0, jsx_runtime_1.jsx)(ReadComponent, {})] }))] }))); | ||
}; | ||
const LoadComponent = ({ message, buttonId }) => { | ||
const myStateful = use(MyStateful); | ||
return (_jsx("div", { children: _jsx("button", { "data-testid": buttonId, onClick: () => { | ||
const myStateful = (0, hooks_1.use)(MyStateful); | ||
return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)("button", { "data-testid": buttonId, onClick: () => { | ||
myStateful.message = message; | ||
@@ -176,6 +178,6 @@ } }) })); | ||
const ReadComponent = () => { | ||
const myStateful = use(MyStateful); | ||
return (_jsx("span", { children: myStateful.message })); | ||
const myStateful = (0, hooks_1.use)(MyStateful); | ||
return ((0, jsx_runtime_1.jsx)("span", { children: myStateful.message })); | ||
}; | ||
const { queryAllByText, getByTestId, rerender } = render(_jsx(RootComponent, {})); | ||
const { queryAllByText, getByTestId, rerender } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(2); | ||
@@ -185,6 +187,6 @@ expect(queryAllByText(firstMessage).length).toBe(0); | ||
// first state | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(button1)); | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(button1)); | ||
})); | ||
rerender(_jsx(RootComponent, {})); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(1); | ||
@@ -194,6 +196,6 @@ expect(queryAllByText(firstMessage).length).toBe(1); | ||
// second state | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(button2)); | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(button2)); | ||
})); | ||
rerender(_jsx(RootComponent, {})); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(0); | ||
@@ -203,3 +205,3 @@ expect(queryAllByText(firstMessage).length).toBe(1); | ||
})); | ||
test('nested singletons', () => __awaiter(void 0, void 0, void 0, function* () { | ||
test('nested singletons', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
let Service1 = class Service1 { | ||
@@ -210,4 +212,4 @@ constructor() { | ||
}; | ||
Service1 = __decorate([ | ||
singleton | ||
Service1 = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
], Service1); | ||
@@ -219,4 +221,4 @@ let Service2 = class Service2 { | ||
}; | ||
Service2 = __decorate([ | ||
singleton | ||
Service2 = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
], Service2); | ||
@@ -228,16 +230,16 @@ let Service3 = class Service3 { | ||
}; | ||
Service3 = __decorate([ | ||
singleton | ||
Service3 = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
], Service3); | ||
const buttonId = 'my-button-3'; | ||
const TestComponent = () => { | ||
const s1 = use(Service1); | ||
const s2 = use(Service2); | ||
const s3 = use(Service3); | ||
return (_jsxs(_Fragment, { children: [_jsx("button", Object.assign({ "data-testid": buttonId, onClick: () => { | ||
const s1 = (0, hooks_1.use)(Service1); | ||
const s2 = (0, hooks_1.use)(Service2); | ||
const s3 = (0, hooks_1.use)(Service3); | ||
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("button", Object.assign({ "data-testid": buttonId, onClick: () => { | ||
s3.state = '000'; | ||
} }, { children: "update s3 state" })), _jsx("p", { children: s1.state }), _jsx("p", { children: s2.state }), _jsx("p", { children: s3.state })] })); | ||
} }, { children: "update s3 state" })), (0, jsx_runtime_1.jsx)("p", { children: s1.state }), (0, jsx_runtime_1.jsx)("p", { children: s2.state }), (0, jsx_runtime_1.jsx)("p", { children: s3.state })] })); | ||
}; | ||
const Component = () => (_jsx(SelfOneTimeProvider, Object.assign({ dependencies: [Service1] }, { children: _jsx(SelfOneTimeProvider, Object.assign({ dependencies: [Service2] }, { children: _jsx(SelfOneTimeProvider, Object.assign({ dependencies: [Service3] }, { children: _jsx(TestComponent, {}) })) })) }))); | ||
const { rerender, getByTestId, queryByText } = render(_jsx(Component, {})); | ||
const Component = () => ((0, jsx_runtime_1.jsx)(SelfOneTimeProvider_1.SelfOneTimeProvider, Object.assign({ dependencies: [Service1] }, { children: (0, jsx_runtime_1.jsx)(SelfOneTimeProvider_1.SelfOneTimeProvider, Object.assign({ dependencies: [Service2] }, { children: (0, jsx_runtime_1.jsx)(SelfOneTimeProvider_1.SelfOneTimeProvider, Object.assign({ dependencies: [Service3] }, { children: (0, jsx_runtime_1.jsx)(TestComponent, {}) })) })) }))); | ||
const { rerender, getByTestId, queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(Component, {})); | ||
expect(queryByText('123')).toBeInTheDocument(); | ||
@@ -247,6 +249,6 @@ expect(queryByText('456')).toBeInTheDocument(); | ||
expect(queryByText('000')).toBeNull(); | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(buttonId)); | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(buttonId)); | ||
})); | ||
rerender(_jsx(Component, {})); | ||
rerender((0, jsx_runtime_1.jsx)(Component, {})); | ||
expect(queryByText('123')).toBeInTheDocument(); | ||
@@ -253,0 +255,0 @@ expect(queryByText('456')).toBeInTheDocument(); |
@@ -1,11 +0,14 @@ | ||
import { __rest } from "tslib"; | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useMemo } from 'react'; | ||
import { SelfProvider } from './SelfProvider'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SelfOneTimeProvider = void 0; | ||
const tslib_1 = require("tslib"); | ||
const jsx_runtime_1 = require("react/jsx-runtime"); | ||
const react_1 = require("react"); | ||
const SelfProvider_1 = require("./SelfProvider"); | ||
const SelfOneTimeProvider = props => { | ||
const { dependencies, children } = props, rest = __rest(props, ["dependencies", "children"]); | ||
const registrations = useMemo(() => dependencies, []); | ||
return (_jsx(SelfProvider, Object.assign({ dependencies: registrations }, rest, { children: children }))); | ||
const { dependencies, children } = props, rest = tslib_1.__rest(props, ["dependencies", "children"]); | ||
const registrations = (0, react_1.useMemo)(() => dependencies, []); | ||
return ((0, jsx_runtime_1.jsx)(SelfProvider_1.SelfProvider, Object.assign({ dependencies: registrations }, rest, { children: children }))); | ||
}; | ||
export { SelfOneTimeProvider }; | ||
exports.SelfOneTimeProvider = SelfOneTimeProvider; | ||
//# sourceMappingURL=SelfOneTimeProvider.js.map |
@@ -1,16 +0,19 @@ | ||
import { __rest } from "tslib"; | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useEffect, useState } from 'react'; | ||
import { Provider } from './Provider'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SelfProvider = void 0; | ||
const tslib_1 = require("tslib"); | ||
const jsx_runtime_1 = require("react/jsx-runtime"); | ||
const react_1 = require("react"); | ||
const Provider_1 = require("./Provider"); | ||
const mapper = (type) => (container) => container.registerType(type); | ||
const SelfProvider = props => { | ||
const { dependencies, children } = props, rest = __rest(props, ["dependencies", "children"]); | ||
const [registrationFunctions, setRegistrationFunctions] = useState(() => dependencies.map(mapper)); | ||
useEffect(() => { | ||
const { dependencies, children } = props, rest = tslib_1.__rest(props, ["dependencies", "children"]); | ||
const [registrationFunctions, setRegistrationFunctions] = (0, react_1.useState)(() => dependencies.map(mapper)); | ||
(0, react_1.useEffect)(() => { | ||
const mappedDependencies = dependencies.map(mapper); | ||
setRegistrationFunctions(mappedDependencies); | ||
}, [dependencies]); | ||
return (_jsx(Provider, Object.assign({ dependencies: registrationFunctions }, rest, { children: children }))); | ||
return ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: registrationFunctions }, rest, { children: children }))); | ||
}; | ||
export { SelfProvider }; | ||
exports.SelfProvider = SelfProvider; | ||
//# sourceMappingURL=SelfProvider.js.map |
@@ -1,5 +0,8 @@ | ||
import { ContainerImpl, } from 'cheap-di'; | ||
import { isStateful, stateful } from './decorators'; | ||
import { removeStateful } from './decorators/stateful'; | ||
export class ReactContainer extends ContainerImpl { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ReactContainer = void 0; | ||
const cheap_di_1 = require("cheap-di"); | ||
const decorators_1 = require("./decorators"); | ||
const stateful_1 = require("./decorators/stateful"); | ||
class ReactContainer extends cheap_di_1.ContainerImpl { | ||
constructor(parentContainer) { | ||
@@ -13,4 +16,4 @@ super(); | ||
registerType(implementationType) { | ||
if (!isStateful(implementationType)) { | ||
stateful(implementationType); | ||
if (!(0, decorators_1.isStateful)(implementationType)) { | ||
(0, decorators_1.stateful)(implementationType); | ||
} | ||
@@ -20,3 +23,3 @@ const registration = super.registerType(implementationType); | ||
registration.asSingleton = (type) => { | ||
removeStateful(implementationType); | ||
(0, stateful_1.removeStateful)(implementationType); | ||
return superAsSingleton(type); | ||
@@ -77,2 +80,3 @@ }; | ||
} | ||
exports.ReactContainer = ReactContainer; | ||
//# sourceMappingURL=ReactContainer.js.map |
@@ -0,4 +1,8 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.configuredSymbol = exports.updateContextSymbol = void 0; | ||
const updateContextSymbol = Symbol('cheap-di-react update context'); | ||
exports.updateContextSymbol = updateContextSymbol; | ||
const configuredSymbol = Symbol('cheap-di-react configured'); | ||
export { updateContextSymbol, configuredSymbol, }; | ||
exports.configuredSymbol = configuredSymbol; | ||
//# sourceMappingURL=StatefulImplementation.js.map |
@@ -1,3 +0,6 @@ | ||
export * from './InternalLogger'; | ||
export * from './reconfigureObject'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./InternalLogger"), exports); | ||
tslib_1.__exportStar(require("./reconfigureObject"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,5 @@ | ||
export class InternalLogger { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InternalLogger = void 0; | ||
class InternalLogger { | ||
constructor(debugName) { | ||
@@ -11,2 +14,3 @@ this.debugName = debugName; | ||
} | ||
exports.InternalLogger = InternalLogger; | ||
//# sourceMappingURL=InternalLogger.js.map |
@@ -1,2 +0,5 @@ | ||
import { updateContextSymbol } from '../StatefulImplementation'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.reconfigureObject = void 0; | ||
const StatefulImplementation_1 = require("../StatefulImplementation"); | ||
function reconfigureObject(instance, logger) { | ||
@@ -19,4 +22,4 @@ const newConfiguration = Object.keys(instance).reduce((config, property) => { | ||
const statefulInstance = instance; | ||
if (statefulInstance[updateContextSymbol]) { | ||
statefulInstance[updateContextSymbol](); | ||
if (statefulInstance[StatefulImplementation_1.updateContextSymbol]) { | ||
statefulInstance[StatefulImplementation_1.updateContextSymbol](); | ||
if (logger) { | ||
@@ -32,3 +35,3 @@ logger.log('CHANGE:', 'instance', instance, 'property', property); | ||
} | ||
export { reconfigureObject }; | ||
exports.reconfigureObject = reconfigureObject; | ||
//# sourceMappingURL=reconfigureObject.js.map |
{ | ||
"name": "cheap-di-react", | ||
"version": "3.2.3", | ||
"version": "3.3.0", | ||
"description": "", | ||
@@ -14,13 +14,13 @@ "scripts": { | ||
"@types/react": "18.0.12", | ||
"cheap-di": "3.2.5", | ||
"jest": "28.1.1", | ||
"jest-environment-jsdom": "28.1.1", | ||
"ts-jest": "28.0.4", | ||
"react": "18.1.0", | ||
"react-dom": "18.1.0", | ||
"ts-jest": "28.0.5", | ||
"tslib": "2.4.0", | ||
"typescript": "4.7.3", | ||
"cheap-di": "3.2.1", | ||
"react": "18.1.0", | ||
"react-dom": "18.1.0" | ||
"typescript": "4.7.3" | ||
}, | ||
"peerDependencies": { | ||
"cheap-di": ">= 3.2.1", | ||
"cheap-di": ">= 3.2.5", | ||
"react": ">= 16.14.0", | ||
@@ -48,3 +48,4 @@ "react-dom": ">= 16.14.0" | ||
"instance type" | ||
] | ||
], | ||
"packageManager": "yarn@3.2.1" | ||
} |
@@ -37,4 +37,2 @@ # cheap-di-react | ||
OneTimeProvider, | ||
SelfProvider, | ||
SelfOneTimeProvider, | ||
} from 'cheap-di-react'; | ||
@@ -51,2 +49,4 @@ import { Logger, ConsoleLogger } from './logger'; | ||
]} | ||
// shortcut for dependencies={[ dr => dr.registerType(AnotherConsoleLogger) ]} | ||
self={[AnotherConsoleLogger]} | ||
> | ||
@@ -61,20 +61,7 @@ <ComponentA/> | ||
]} | ||
// will use initial self dependecies (it uses useMemo under hood) | ||
self={[AnotherConsoleLogger]} | ||
> | ||
<ComponentA/> | ||
</OneTimeProvider> | ||
{/* shortcut for <Provider dependencies={[ dr => dr.registerType(ConsoleLogger) ]}> ... </Provider> */} | ||
<SelfProvider | ||
// will update dependencies on each render | ||
dependencies={[AnotherConsoleLogger]} | ||
> | ||
<ComponentB/> | ||
</SelfProvider> | ||
<SelfOneTimeProvider | ||
// will use initial dependecies (it uses useMemo under hood) | ||
dependencies={[AnotherConsoleLogger]} | ||
> | ||
<ComponentB/> | ||
</SelfOneTimeProvider> | ||
</> | ||
@@ -108,3 +95,3 @@ ); | ||
```tsx | ||
import { SelfOneTimeProvider, use, stateful } from 'cheap-di-react'; | ||
import { OneTimeProvider, use, stateful } from 'cheap-di-react'; | ||
@@ -118,11 +105,11 @@ @stateful | ||
return ( | ||
<SelfOneTimeProvider dependencies={[MyStateful]}> | ||
<OneTimeProvider self={[MyStateful]}> | ||
<LoadComponent message="level 1"/> | ||
<ReadComponent/> | ||
<SelfOneTimeProvider dependencies={[MyStateful]}> | ||
<OneTimeProvider self={[MyStateful]}> | ||
<LoadComponent message="level 2"/> | ||
<ReadComponent/> | ||
</SelfOneTimeProvider> | ||
</SelfOneTimeProvider> | ||
</OneTimeProvider> | ||
</OneTimeProvider> | ||
); | ||
@@ -151,3 +138,3 @@ }; | ||
import { singleton } from 'cheap-di'; | ||
import { SelfOneTimeProvider, use } from 'cheap-di-react'; | ||
import { OneTimeProvider, use } from 'cheap-di-react'; | ||
@@ -165,5 +152,5 @@ @singleton | ||
return ( | ||
<SelfOneTimeProvider dependencies={[MySingleton]}> | ||
<OneTimeProvider self={[MySingleton]}> | ||
<Component/> | ||
</SelfOneTimeProvider> | ||
</OneTimeProvider> | ||
); | ||
@@ -170,0 +157,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
139933
72
1111
223