cheap-di-react
Advanced tools
Comparing version 3.2.2 to 3.2.3
@@ -1,8 +0,2 @@ | ||
"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; } }); | ||
export { stateful, isStateful, statefulSymbol } from './stateful'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,14 +0,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InheritancePreserver = void 0; | ||
const cheap_di_1 = require("cheap-di"); | ||
import { inheritancePreserveSymbol } from 'cheap-di'; | ||
class InheritancePreserver { | ||
static constructorModified(constructor) { | ||
constructor[cheap_di_1.inheritancePreserveSymbol] = constructor; | ||
constructor[inheritancePreserveSymbol] = constructor; | ||
} | ||
static getModifiedConstructor(constructor) { | ||
return constructor[cheap_di_1.inheritancePreserveSymbol]; | ||
return constructor[inheritancePreserveSymbol]; | ||
} | ||
} | ||
exports.InheritancePreserver = InheritancePreserver; | ||
export { InheritancePreserver }; | ||
//# sourceMappingURL=InheritancePreserver.js.map |
@@ -1,15 +0,10 @@ | ||
"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"); | ||
import { di } from 'cheap-di'; | ||
import { InheritancePreserver } from './InheritancePreserver'; | ||
const statefulSymbol = Symbol('cheap-di-react stateful'); | ||
exports.statefulSymbol = statefulSymbol; | ||
function stateful(constructor) { | ||
constructor[statefulSymbol] = true; | ||
(0, cheap_di_1.di)(constructor); | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(constructor); | ||
di(constructor); | ||
InheritancePreserver.constructorModified(constructor); | ||
return constructor; | ||
} | ||
exports.stateful = stateful; | ||
function removeStateful(constructor) { | ||
@@ -20,5 +15,4 @@ if (isStateful(constructor)) { | ||
} | ||
exports.removeStateful = removeStateful; | ||
function isStateful(constructor) { | ||
const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); | ||
const modifiedConstructor = InheritancePreserver.getModifiedConstructor(constructor); | ||
return !!modifiedConstructor | ||
@@ -28,3 +22,3 @@ && modifiedConstructor === constructor | ||
} | ||
exports.isStateful = isStateful; | ||
export { stateful, isStateful, statefulSymbol, removeStateful }; | ||
//# sourceMappingURL=stateful.js.map |
@@ -1,6 +0,3 @@ | ||
"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 }); | ||
import { createContext } from 'react'; | ||
export const DiContext = createContext({ container: undefined }); | ||
//# sourceMappingURL=DiContext.js.map |
@@ -1,6 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isConfigured = exports.configureStateful = void 0; | ||
const utils_1 = require("../utils"); | ||
const StatefulImplementation_1 = require("../StatefulImplementation"); | ||
import { reconfigureObject } from '../utils'; | ||
import { configuredSymbol, updateContextSymbol, } from '../StatefulImplementation'; | ||
function configureStateful(instance, rerender, logger) { | ||
@@ -11,12 +8,11 @@ const configurationInstance = instance; | ||
} | ||
configurationInstance[StatefulImplementation_1.updateContextSymbol] = rerender; | ||
(0, utils_1.reconfigureObject)(instance, logger); | ||
configurationInstance[StatefulImplementation_1.configuredSymbol] = true; | ||
configurationInstance[updateContextSymbol] = rerender; | ||
reconfigureObject(instance, logger); | ||
configurationInstance[configuredSymbol] = true; | ||
logger.log('instance', instance, 'configured'); | ||
} | ||
exports.configureStateful = configureStateful; | ||
function isConfigured(constructor) { | ||
return constructor[StatefulImplementation_1.configuredSymbol] === true; | ||
return constructor[configuredSymbol] === true; | ||
} | ||
exports.isConfigured = isConfigured; | ||
export { configureStateful, isConfigured }; | ||
//# sourceMappingURL=configureStateful.js.map |
@@ -1,6 +0,3 @@ | ||
"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); | ||
export * from './useContainer'; | ||
export * from './use'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,8 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.use = void 0; | ||
const react_1 = require("react"); | ||
const DiContext_1 = require("../DiContext"); | ||
import { useContext } from 'react'; | ||
import { DiContext } from '../DiContext'; | ||
function use(type, ...args) { | ||
const context = (0, react_1.useContext)(DiContext_1.DiContext); | ||
const context = useContext(DiContext); | ||
if (!context || !context.container) { | ||
@@ -17,3 +14,3 @@ throw new Error('Container not found. You should use Provider as one of parent nodes of this component'); | ||
} | ||
exports.use = use; | ||
export { use }; | ||
//# sourceMappingURL=use.js.map |
@@ -1,19 +0,16 @@ | ||
"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"); | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { DiContext } from '../DiContext'; | ||
import { ReactContainer } from '../ReactContainer'; | ||
function useContainer(logger) { | ||
const parentContainer = (0, react_1.useContext)(DiContext_1.DiContext).container; | ||
const [contextValue, setContextValue] = (0, react_1.useState)({ container: undefined }); | ||
(0, react_1.useEffect)(() => { | ||
const parentContainer = useContext(DiContext).container; | ||
const [contextValue, setContextValue] = useState({ container: undefined }); | ||
useEffect(() => { | ||
if (!contextValue.container) { | ||
if (parentContainer) { | ||
logger.log('create container'); | ||
contextValue.container = new ReactContainer_1.ReactContainer(parentContainer); | ||
contextValue.container = new ReactContainer(parentContainer); | ||
} | ||
else { | ||
logger.log('create root container'); | ||
contextValue.container = new ReactContainer_1.ReactContainer(); | ||
contextValue.container = new ReactContainer(); | ||
contextValue.container.rerender = () => setContextValue(Object.assign({}, contextValue)); | ||
@@ -29,3 +26,3 @@ } | ||
logger.log('RECREATE container'); | ||
setContextValue({ container: new ReactContainer_1.ReactContainer(parentContainer) }); | ||
setContextValue({ container: new ReactContainer(parentContainer) }); | ||
}, [parentContainer]); | ||
@@ -40,3 +37,3 @@ const rerender = () => { | ||
} | ||
exports.useContainer = useContainer; | ||
export { useContainer }; | ||
//# sourceMappingURL=useContainer.js.map |
@@ -1,8 +0,5 @@ | ||
"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); | ||
export * from './DiContext'; | ||
export * from './decorators'; | ||
export * from './Providers'; | ||
export * from './hooks/use'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,8 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./OneTimeProvider"), exports); | ||
tslib_1.__exportStar(require("./Provider"), exports); | ||
tslib_1.__exportStar(require("./SelfOneTimeProvider"), exports); | ||
tslib_1.__exportStar(require("./SelfProvider"), exports); | ||
export * from './OneTimeProvider'; | ||
export * from './Provider'; | ||
export * from './SelfOneTimeProvider'; | ||
export * from './SelfProvider'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,14 +0,11 @@ | ||
"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"); | ||
import { __rest } from "tslib"; | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useMemo } from 'react'; | ||
import { Provider } from './Provider'; | ||
const OneTimeProvider = props => { | ||
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 }))); | ||
const { dependencies, children } = props, rest = __rest(props, ["dependencies", "children"]); | ||
const registrations = useMemo(() => dependencies, []); | ||
return (_jsx(Provider, Object.assign({ dependencies: registrations }, rest, { children: children }))); | ||
}; | ||
exports.OneTimeProvider = OneTimeProvider; | ||
export { OneTimeProvider }; | ||
//# sourceMappingURL=OneTimeProvider.js.map |
@@ -1,20 +0,17 @@ | ||
"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"); | ||
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'; | ||
const Provider = props => { | ||
const { dependencies, debugName, children, } = props; | ||
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 [logger] = useState(() => new InternalLogger(debugName)); | ||
const [initialized, setInitialized] = useState(false); | ||
const timerRef = useRef(null); | ||
const [contextValue, rerender] = useContainer(logger); | ||
const container = contextValue.container; | ||
(0, react_1.useEffect)(() => { | ||
useEffect(() => { | ||
var _a; | ||
@@ -34,8 +31,8 @@ if (!container || !dependencies) { | ||
} | ||
if ((0, cheap_di_1.isSingleton)(constructor)) { | ||
if (isSingleton(constructor)) { | ||
logger.log('singleton', constructor, 'founded'); | ||
const instance = container.resolve(type); | ||
(0, configureStateful_1.configureStateful)(instance, container.rootContainer.rerender, logger); | ||
configureStateful(instance, container.rootContainer.rerender, logger); | ||
} | ||
if ((0, decorators_1.isStateful)(constructor)) { | ||
if (isStateful(constructor)) { | ||
logger.log('stateful', constructor, 'founded'); | ||
@@ -45,3 +42,3 @@ container.skipParentInstanceResolvingOnce(); // target instance should be instantiated from zero | ||
container.registerInstance(instance).as(type); | ||
(0, configureStateful_1.configureStateful)(instance, rerender, logger); | ||
configureStateful(instance, rerender, logger); | ||
} | ||
@@ -57,3 +54,3 @@ } | ||
}, [container, dependencies]); | ||
(0, react_1.useEffect)(() => { | ||
useEffect(() => { | ||
return () => { | ||
@@ -69,7 +66,7 @@ if (timerRef.current) { | ||
} | ||
return ((0, jsx_runtime_1.jsx)(DiContext_1.DiContext.Provider, Object.assign({ value: contextValue }, { children: (0, jsx_runtime_1.jsx)(MemoizedChildren, { children: children }) }))); | ||
return (_jsx(DiContext.Provider, Object.assign({ value: contextValue }, { children: _jsx(MemoizedChildren, { children: children }) }))); | ||
}; | ||
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; | ||
const MemoizedChildren = memo(({ children }) => _jsx(Fragment, { children: children })); | ||
const memoizedComponent = memo(Provider); | ||
export { memoizedComponent as Provider }; | ||
//# sourceMappingURL=Provider.js.map |
@@ -1,12 +0,10 @@ | ||
"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"); | ||
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'; | ||
test('use jsdom in this test file', () => { | ||
@@ -38,9 +36,9 @@ const element = document.createElement('div'); | ||
const Component = () => { | ||
const logger = (0, hooks_1.use)(SomeConsoleLogger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('another layout') }); | ||
const logger = use(SomeConsoleLogger); | ||
return _jsx("p", { children: logger.debug('another layout') }); | ||
}; | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(SomeConsoleLogger), | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
expect(queryByText('another layout')).toBeInTheDocument(); | ||
@@ -50,9 +48,9 @@ }); | ||
const Component = () => { | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }); | ||
const logger = use(Logger); | ||
return _jsx("p", { children: logger.debug('my layout') }); | ||
}; | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(ConsoleLogger).as(Logger), | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
expect(queryByText('my layout')).toBeInTheDocument(); | ||
@@ -62,9 +60,9 @@ }); | ||
const Component = () => { | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }); | ||
const logger = use(Logger); | ||
return _jsx("p", { children: logger.debug('my layout') }); | ||
}; | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(ConsoleLogger).as(Logger).with('my message'), | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
expect(queryByText('my message: my layout')).toBeInTheDocument(); | ||
@@ -80,9 +78,9 @@ }); | ||
const Component = () => { | ||
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 database = use(Database); | ||
return (_jsx("p", { children: database.entities.map(e => (_jsx("span", { children: e }, e))) })); | ||
}; | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => (_jsx(Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerInstance(new Database(entities)), | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
] }, { children: _jsx(Component, {}) }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
expect(queryByText(entities[0])).toBeInTheDocument(); | ||
@@ -93,11 +91,11 @@ expect(queryByText(entities[1])).toBeInTheDocument(); | ||
const Component = () => { | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }); | ||
const logger = use(Logger); | ||
return _jsx("p", { children: logger.debug('my layout') }); | ||
}; | ||
const RootComponent = () => ((0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => (_jsxs(Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(ConsoleLogger).as(Logger).with('my message'), | ||
] }, { children: [(0, jsx_runtime_1.jsx)(Component, {}), (0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
] }, { children: [_jsx(Component, {}), _jsx(Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(SomeConsoleLogger).as(Logger), | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}) }))] }))); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
] }, { children: _jsx(Component, {}) }))] }))); | ||
const { queryByText } = render(_jsx(RootComponent, {})); | ||
expect(queryByText('my message: my layout')).toBeInTheDocument(); | ||
@@ -108,3 +106,3 @@ expect(queryByText('my layout')).toBeInTheDocument(); | ||
describe('singleton and stateful', () => { | ||
test('singleton', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
test('singleton', () => __awaiter(void 0, void 0, void 0, function* () { | ||
let MySingleton = class MySingleton { | ||
@@ -115,3 +113,3 @@ constructor() { | ||
loadData() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.data = yield Promise.resolve('loaded data'); | ||
@@ -121,4 +119,4 @@ }); | ||
}; | ||
MySingleton = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
MySingleton = __decorate([ | ||
singleton | ||
], MySingleton); | ||
@@ -129,23 +127,23 @@ const dependencies = [ | ||
const RootComponent = () => { | ||
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, {}) }))] }))); | ||
return (_jsxs(Provider, Object.assign({ dependencies: dependencies }, { children: [_jsx(LoadComponent, {}), _jsx(Provider, Object.assign({ dependencies: dependencies }, { children: _jsx(ReadComponent, {}) }))] }))); | ||
}; | ||
const buttonId = 'my-button'; | ||
const LoadComponent = () => { | ||
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: () => { | ||
const mySingleton = use(MySingleton); | ||
return (_jsxs("div", { children: [_jsx("button", { "data-testid": buttonId, onClick: () => { | ||
mySingleton.loadData(); | ||
} }), (0, jsx_runtime_1.jsx)("span", { children: mySingleton.data })] })); | ||
} }), _jsx("span", { children: mySingleton.data })] })); | ||
}; | ||
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, {})); | ||
const ReadComponent = () => (_jsx("span", { children: use(MySingleton).data })); | ||
const { queryAllByText, getByTestId, rerender } = render(_jsx(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(2); | ||
expect(queryAllByText('loaded data').length).toBe(0); | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(buttonId)); | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(buttonId)); | ||
})); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
rerender(_jsx(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(0); | ||
expect(queryAllByText('loaded data').length).toBe(2); | ||
})); | ||
test('stateful', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
test('stateful', () => __awaiter(void 0, void 0, void 0, function* () { | ||
let MyStateful = class MyStateful { | ||
@@ -156,4 +154,4 @@ constructor() { | ||
}; | ||
MyStateful = tslib_1.__decorate([ | ||
decorators_1.stateful | ||
MyStateful = __decorate([ | ||
stateful | ||
], MyStateful); | ||
@@ -168,7 +166,7 @@ const dependencies = [ | ||
const RootComponent = () => { | ||
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, {})] }))] }))); | ||
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, {})] }))] }))); | ||
}; | ||
const LoadComponent = ({ message, buttonId }) => { | ||
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: () => { | ||
const myStateful = use(MyStateful); | ||
return (_jsx("div", { children: _jsx("button", { "data-testid": buttonId, onClick: () => { | ||
myStateful.message = message; | ||
@@ -178,6 +176,6 @@ } }) })); | ||
const ReadComponent = () => { | ||
const myStateful = (0, hooks_1.use)(MyStateful); | ||
return ((0, jsx_runtime_1.jsx)("span", { children: myStateful.message })); | ||
const myStateful = use(MyStateful); | ||
return (_jsx("span", { children: myStateful.message })); | ||
}; | ||
const { queryAllByText, getByTestId, rerender } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
const { queryAllByText, getByTestId, rerender } = render(_jsx(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(2); | ||
@@ -187,6 +185,6 @@ expect(queryAllByText(firstMessage).length).toBe(0); | ||
// first state | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(button1)); | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(button1)); | ||
})); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
rerender(_jsx(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(1); | ||
@@ -196,6 +194,6 @@ expect(queryAllByText(firstMessage).length).toBe(1); | ||
// second state | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(button2)); | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(button2)); | ||
})); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {})); | ||
rerender(_jsx(RootComponent, {})); | ||
expect(queryAllByText('initial').length).toBe(0); | ||
@@ -205,3 +203,3 @@ expect(queryAllByText(firstMessage).length).toBe(1); | ||
})); | ||
test('nested singletons', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
test('nested singletons', () => __awaiter(void 0, void 0, void 0, function* () { | ||
let Service1 = class Service1 { | ||
@@ -212,4 +210,4 @@ constructor() { | ||
}; | ||
Service1 = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
Service1 = __decorate([ | ||
singleton | ||
], Service1); | ||
@@ -221,4 +219,4 @@ let Service2 = class Service2 { | ||
}; | ||
Service2 = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
Service2 = __decorate([ | ||
singleton | ||
], Service2); | ||
@@ -230,16 +228,16 @@ let Service3 = class Service3 { | ||
}; | ||
Service3 = tslib_1.__decorate([ | ||
cheap_di_1.singleton | ||
Service3 = __decorate([ | ||
singleton | ||
], Service3); | ||
const buttonId = 'my-button-3'; | ||
const TestComponent = () => { | ||
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: () => { | ||
const s1 = use(Service1); | ||
const s2 = use(Service2); | ||
const s3 = use(Service3); | ||
return (_jsxs(_Fragment, { children: [_jsx("button", Object.assign({ "data-testid": buttonId, onClick: () => { | ||
s3.state = '000'; | ||
} }, { 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 })] })); | ||
} }, { children: "update s3 state" })), _jsx("p", { children: s1.state }), _jsx("p", { children: s2.state }), _jsx("p", { children: s3.state })] })); | ||
}; | ||
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, {})); | ||
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, {})); | ||
expect(queryByText('123')).toBeInTheDocument(); | ||
@@ -249,6 +247,6 @@ expect(queryByText('456')).toBeInTheDocument(); | ||
expect(queryByText('000')).toBeNull(); | ||
yield (0, react_1.act)(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(buttonId)); | ||
yield act(() => __awaiter(void 0, void 0, void 0, function* () { | ||
fireEvent.click(getByTestId(buttonId)); | ||
})); | ||
rerender((0, jsx_runtime_1.jsx)(Component, {})); | ||
rerender(_jsx(Component, {})); | ||
expect(queryByText('123')).toBeInTheDocument(); | ||
@@ -255,0 +253,0 @@ expect(queryByText('456')).toBeInTheDocument(); |
@@ -1,14 +0,11 @@ | ||
"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"); | ||
import { __rest } from "tslib"; | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useMemo } from 'react'; | ||
import { SelfProvider } from './SelfProvider'; | ||
const SelfOneTimeProvider = props => { | ||
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 }))); | ||
const { dependencies, children } = props, rest = __rest(props, ["dependencies", "children"]); | ||
const registrations = useMemo(() => dependencies, []); | ||
return (_jsx(SelfProvider, Object.assign({ dependencies: registrations }, rest, { children: children }))); | ||
}; | ||
exports.SelfOneTimeProvider = SelfOneTimeProvider; | ||
export { SelfOneTimeProvider }; | ||
//# sourceMappingURL=SelfOneTimeProvider.js.map |
@@ -1,19 +0,16 @@ | ||
"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"); | ||
import { __rest } from "tslib"; | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useEffect, useState } from 'react'; | ||
import { Provider } from './Provider'; | ||
const mapper = (type) => (container) => container.registerType(type); | ||
const SelfProvider = props => { | ||
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 { dependencies, children } = props, rest = __rest(props, ["dependencies", "children"]); | ||
const [registrationFunctions, setRegistrationFunctions] = useState(() => dependencies.map(mapper)); | ||
useEffect(() => { | ||
const mappedDependencies = dependencies.map(mapper); | ||
setRegistrationFunctions(mappedDependencies); | ||
}, [dependencies]); | ||
return ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: registrationFunctions }, rest, { children: children }))); | ||
return (_jsx(Provider, Object.assign({ dependencies: registrationFunctions }, rest, { children: children }))); | ||
}; | ||
exports.SelfProvider = SelfProvider; | ||
export { SelfProvider }; | ||
//# sourceMappingURL=SelfProvider.js.map |
@@ -1,8 +0,5 @@ | ||
"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 { | ||
import { ContainerImpl, } from 'cheap-di'; | ||
import { isStateful, stateful } from './decorators'; | ||
import { removeStateful } from './decorators/stateful'; | ||
export class ReactContainer extends ContainerImpl { | ||
constructor(parentContainer) { | ||
@@ -16,4 +13,4 @@ super(); | ||
registerType(implementationType) { | ||
if (!(0, decorators_1.isStateful)(implementationType)) { | ||
(0, decorators_1.stateful)(implementationType); | ||
if (!isStateful(implementationType)) { | ||
stateful(implementationType); | ||
} | ||
@@ -23,3 +20,3 @@ const registration = super.registerType(implementationType); | ||
registration.asSingleton = (type) => { | ||
(0, stateful_1.removeStateful)(implementationType); | ||
removeStateful(implementationType); | ||
return superAsSingleton(type); | ||
@@ -80,3 +77,2 @@ }; | ||
} | ||
exports.ReactContainer = ReactContainer; | ||
//# sourceMappingURL=ReactContainer.js.map |
@@ -1,8 +0,4 @@ | ||
"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'); | ||
exports.configuredSymbol = configuredSymbol; | ||
export { updateContextSymbol, configuredSymbol, }; | ||
//# sourceMappingURL=StatefulImplementation.js.map |
@@ -1,6 +0,3 @@ | ||
"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); | ||
export * from './InternalLogger'; | ||
export * from './reconfigureObject'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InternalLogger = void 0; | ||
class InternalLogger { | ||
export class InternalLogger { | ||
constructor(debugName) { | ||
@@ -14,3 +11,2 @@ this.debugName = debugName; | ||
} | ||
exports.InternalLogger = InternalLogger; | ||
//# sourceMappingURL=InternalLogger.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.reconfigureObject = void 0; | ||
const StatefulImplementation_1 = require("../StatefulImplementation"); | ||
import { updateContextSymbol } from '../StatefulImplementation'; | ||
function reconfigureObject(instance, logger) { | ||
@@ -22,4 +19,4 @@ const newConfiguration = Object.keys(instance).reduce((config, property) => { | ||
const statefulInstance = instance; | ||
if (statefulInstance[StatefulImplementation_1.updateContextSymbol]) { | ||
statefulInstance[StatefulImplementation_1.updateContextSymbol](); | ||
if (statefulInstance[updateContextSymbol]) { | ||
statefulInstance[updateContextSymbol](); | ||
if (logger) { | ||
@@ -35,3 +32,3 @@ logger.log('CHANGE:', 'instance', instance, 'property', property); | ||
} | ||
exports.reconfigureObject = reconfigureObject; | ||
export { reconfigureObject }; | ||
//# sourceMappingURL=reconfigureObject.js.map |
{ | ||
"name": "cheap-di-react", | ||
"version": "3.2.2", | ||
"version": "3.2.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
89152
697