cheap-di-react
Advanced tools
Comparing version 3.0.0 to 3.2.1
@@ -1,1 +0,1 @@ | ||
export * from './stateful'; | ||
export { stateful, isStateful, statefulSymbol, StatefulImplementation } from './stateful'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./stateful"), exports); | ||
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 |
@@ -7,4 +7,5 @@ import { Constructor, ImplementationType } from 'cheap-di'; | ||
declare function stateful<TClass extends Constructor>(constructor: TClass): TClass; | ||
declare function removeStateful<TClass extends Constructor>(constructor: TClass): void; | ||
declare function isStateful<TClass extends Constructor>(constructor: TClass): boolean; | ||
export { stateful, isStateful, statefulSymbol }; | ||
export { stateful, isStateful, statefulSymbol, removeStateful }; | ||
export type { StatefulImplementation }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.statefulSymbol = exports.isStateful = exports.stateful = void 0; | ||
exports.removeStateful = exports.statefulSymbol = exports.isStateful = exports.stateful = void 0; | ||
const cheap_di_1 = require("cheap-di"); | ||
const InheritancePreserver_1 = require("./InheritancePreserver"); | ||
@@ -9,2 +10,3 @@ const statefulSymbol = Symbol('cheap-di-react stateful'); | ||
constructor[statefulSymbol] = true; | ||
(0, cheap_di_1.di)(constructor); | ||
InheritancePreserver_1.InheritancePreserver.constructorModified(constructor); | ||
@@ -14,2 +16,8 @@ return constructor; | ||
exports.stateful = stateful; | ||
function removeStateful(constructor) { | ||
if (isStateful(constructor)) { | ||
delete constructor[statefulSymbol]; | ||
} | ||
} | ||
exports.removeStateful = removeStateful; | ||
function isStateful(constructor) { | ||
@@ -16,0 +24,0 @@ const modifiedConstructor = InheritancePreserver_1.InheritancePreserver.getModifiedConstructor(constructor); |
@@ -5,3 +5,3 @@ "use strict"; | ||
const react_1 = require("react"); | ||
exports.DiContext = react_1.createContext({ container: undefined }); | ||
exports.DiContext = (0, react_1.createContext)({ container: undefined }); | ||
//# sourceMappingURL=DiContext.js.map |
@@ -12,3 +12,3 @@ "use strict"; | ||
configurationInstance[StatefulImplementation_1.updateContextSymbol] = rerender; | ||
utils_1.reconfigureObject(instance, logger); | ||
(0, utils_1.reconfigureObject)(instance, logger); | ||
configurationInstance[StatefulImplementation_1.configuredSymbol] = true; | ||
@@ -15,0 +15,0 @@ logger.log('instance', instance, 'configured'); |
"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); | ||
(0, tslib_1.__exportStar)(require("./useContainer"), exports); | ||
(0, tslib_1.__exportStar)(require("./use"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -7,3 +7,3 @@ "use strict"; | ||
function use(type, ...args) { | ||
const context = react_1.useContext(DiContext_1.DiContext); | ||
const context = (0, react_1.useContext)(DiContext_1.DiContext); | ||
if (!context || !context.container) { | ||
@@ -10,0 +10,0 @@ throw new Error('Container not found. You should use Provider as one of parent nodes of this component'); |
@@ -8,5 +8,5 @@ "use strict"; | ||
function useContainer(logger) { | ||
const parentContainer = react_1.useContext(DiContext_1.DiContext).container; | ||
const [contextValue, setContextValue] = react_1.useState({ container: undefined }); | ||
react_1.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) { | ||
@@ -13,0 +13,0 @@ if (parentContainer) { |
"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); | ||
(0, tslib_1.__exportStar)(require("./DiContext"), exports); | ||
(0, tslib_1.__exportStar)(require("./decorators"), exports); | ||
(0, tslib_1.__exportStar)(require("./Providers"), exports); | ||
(0, tslib_1.__exportStar)(require("./hooks/use"), exports); | ||
//# sourceMappingURL=index.js.map |
"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); | ||
(0, tslib_1.__exportStar)(require("./OneTimeProvider"), exports); | ||
(0, tslib_1.__exportStar)(require("./Provider"), exports); | ||
(0, tslib_1.__exportStar)(require("./SelfOneTimeProvider"), exports); | ||
(0, tslib_1.__exportStar)(require("./SelfProvider"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -9,7 +9,7 @@ "use strict"; | ||
const OneTimeProvider = props => { | ||
const { dependencies, children } = props, rest = tslib_1.__rest(props, ["dependencies", "children"]); | ||
const registrations = react_1.useMemo(() => dependencies, []); | ||
return (jsx_runtime_1.jsx(Provider_1.Provider, Object.assign({ dependencies: registrations }, rest, { children: children }), void 0)); | ||
const { dependencies, children } = props, rest = (0, 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 }), void 0)); | ||
}; | ||
exports.OneTimeProvider = OneTimeProvider; | ||
//# sourceMappingURL=OneTimeProvider.js.map |
@@ -14,7 +14,7 @@ "use strict"; | ||
const { dependencies, debugName, children, } = props; | ||
const [logger] = react_1.useState(() => new utils_1.InternalLogger(debugName)); | ||
const [initialized, setInitialized] = react_1.useState(false); | ||
const [contextValue, rerender] = hooks_1.useContainer(logger); | ||
const [logger] = (0, react_1.useState)(() => new utils_1.InternalLogger(debugName)); | ||
const [initialized, setInitialized] = (0, react_1.useState)(false); | ||
const [contextValue, rerender] = (0, hooks_1.useContainer)(logger); | ||
const container = contextValue.container; | ||
react_1.useEffect(() => { | ||
(0, react_1.useEffect)(() => { | ||
var _a; | ||
@@ -33,8 +33,8 @@ if (!container || !dependencies) { | ||
} | ||
if (cheap_di_1.isSingleton(constructor)) { | ||
if ((0, cheap_di_1.isSingleton)(constructor)) { | ||
logger.log('singleton', constructor, 'founded'); | ||
const instance = container.resolve(type); | ||
configureStateful_1.configureStateful(instance, container.rootContainer.rerender, logger); | ||
(0, configureStateful_1.configureStateful)(instance, container.rootContainer.rerender, logger); | ||
} | ||
if (decorators_1.isStateful(constructor)) { | ||
if ((0, decorators_1.isStateful)(constructor)) { | ||
logger.log('stateful', constructor, 'founded'); | ||
@@ -44,3 +44,3 @@ container.skipParentInstanceResolvingOnce(); // target instance should be instantiated from zero | ||
container.registerInstance(instance).as(type); | ||
configureStateful_1.configureStateful(instance, rerender, logger); | ||
(0, configureStateful_1.configureStateful)(instance, rerender, logger); | ||
} | ||
@@ -59,7 +59,7 @@ } | ||
} | ||
return (jsx_runtime_1.jsx(DiContext_1.DiContext.Provider, Object.assign({ value: contextValue }, { children: jsx_runtime_1.jsx(MemoizedChildren, { children: children }, void 0) }), void 0)); | ||
return ((0, jsx_runtime_1.jsx)(DiContext_1.DiContext.Provider, Object.assign({ value: contextValue }, { children: (0, jsx_runtime_1.jsx)(MemoizedChildren, { children: children }, void 0) }), void 0)); | ||
}; | ||
const MemoizedChildren = react_1.memo(({ children }) => jsx_runtime_1.jsx(react_1.Fragment, { children: children }, void 0)); | ||
const memoizedComponent = react_1.memo(Provider); | ||
const MemoizedChildren = (0, react_1.memo)(({ children }) => (0, jsx_runtime_1.jsx)(react_1.Fragment, { children: children }, void 0)); | ||
const memoizedComponent = (0, react_1.memo)(Provider); | ||
exports.Provider = memoizedComponent; | ||
//# sourceMappingURL=Provider.js.map |
@@ -34,9 +34,9 @@ "use strict"; | ||
const Component = () => { | ||
const logger = hooks_1.use(SomeConsoleLogger); | ||
return jsx_runtime_1.jsx("p", { children: logger.debug('another layout') }, void 0); | ||
const logger = (0, hooks_1.use)(SomeConsoleLogger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('another layout') }, void 0); | ||
}; | ||
const RootComponent = () => (jsx_runtime_1.jsx(Provider_1.Provider, Object.assign({ dependencies: [ | ||
const RootComponent = () => ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(SomeConsoleLogger), | ||
] }, { children: jsx_runtime_1.jsx(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryByText('another layout')).toBeInTheDocument(); | ||
@@ -46,9 +46,9 @@ }); | ||
const Component = () => { | ||
const logger = hooks_1.use(Logger); | ||
return jsx_runtime_1.jsx("p", { children: logger.debug('my layout') }, void 0); | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }, void 0); | ||
}; | ||
const RootComponent = () => (jsx_runtime_1.jsx(Provider_1.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_runtime_1.jsx(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryByText('my layout')).toBeInTheDocument(); | ||
@@ -58,9 +58,9 @@ }); | ||
const Component = () => { | ||
const logger = hooks_1.use(Logger); | ||
return jsx_runtime_1.jsx("p", { children: logger.debug('my layout') }, void 0); | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }, void 0); | ||
}; | ||
const RootComponent = () => (jsx_runtime_1.jsx(Provider_1.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_runtime_1.jsx(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryByText('my message: my layout')).toBeInTheDocument(); | ||
@@ -76,9 +76,9 @@ }); | ||
const Component = () => { | ||
const database = hooks_1.use(Database); | ||
return (jsx_runtime_1.jsx("p", { children: database.entities.map(e => (jsx_runtime_1.jsx("span", { children: e }, e))) }, void 0)); | ||
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))) }, void 0)); | ||
}; | ||
const RootComponent = () => (jsx_runtime_1.jsx(Provider_1.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_runtime_1.jsx(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryByText(entities[0])).toBeInTheDocument(); | ||
@@ -89,12 +89,11 @@ expect(queryByText(entities[1])).toBeInTheDocument(); | ||
const Component = () => { | ||
const logger = hooks_1.use(Logger); | ||
return jsx_runtime_1.jsx("p", { children: logger.debug('my layout') }, void 0); | ||
const logger = (0, hooks_1.use)(Logger); | ||
return (0, jsx_runtime_1.jsx)("p", { children: logger.debug('my layout') }, void 0); | ||
}; | ||
const RootComponent = () => (jsx_runtime_1.jsxs(Provider_1.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_runtime_1.jsx(Component, {}, void 0), | ||
jsx_runtime_1.jsx(Provider_1.Provider, Object.assign({ dependencies: [ | ||
] }, { children: [(0, jsx_runtime_1.jsx)(Component, {}, void 0), (0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: [ | ||
dr => dr.registerType(SomeConsoleLogger).as(Logger), | ||
] }, { children: jsx_runtime_1.jsx(Component, {}, void 0) }), void 0)] }), void 0)); | ||
const { queryByText } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
] }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)] }), void 0)); | ||
const { queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryByText('my message: my layout')).toBeInTheDocument(); | ||
@@ -105,3 +104,3 @@ expect(queryByText('my layout')).toBeInTheDocument(); | ||
describe('singleton and stateful', () => { | ||
test('singleton', (done) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
test('singleton', (done) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () { | ||
let MySingleton = class MySingleton { | ||
@@ -112,3 +111,3 @@ constructor() { | ||
loadData() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
this.data = yield Promise.resolve('loaded data'); | ||
@@ -118,3 +117,3 @@ }); | ||
}; | ||
MySingleton = tslib_1.__decorate([ | ||
MySingleton = (0, tslib_1.__decorate)([ | ||
cheap_di_1.singleton | ||
@@ -126,21 +125,19 @@ ], MySingleton); | ||
const RootComponent = () => { | ||
return (jsx_runtime_1.jsxs(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [jsx_runtime_1.jsx(LoadComponent, {}, void 0), | ||
jsx_runtime_1.jsx(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: jsx_runtime_1.jsx(ReadComponent, {}, void 0) }), void 0)] }), void 0)); | ||
return ((0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [(0, jsx_runtime_1.jsx)(LoadComponent, {}, void 0), (0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: (0, jsx_runtime_1.jsx)(ReadComponent, {}, void 0) }), void 0)] }), void 0)); | ||
}; | ||
const buttonId = 'my-button'; | ||
const LoadComponent = () => { | ||
const mySingleton = hooks_1.use(MySingleton); | ||
return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.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(); | ||
} }, void 0), | ||
jsx_runtime_1.jsx("span", { children: mySingleton.data }, void 0)] }, void 0)); | ||
} }, void 0), (0, jsx_runtime_1.jsx)("span", { children: mySingleton.data }, void 0)] }, void 0)); | ||
}; | ||
const ReadComponent = () => (jsx_runtime_1.jsx("span", { children: hooks_1.use(MySingleton).data }, void 0)); | ||
const { queryAllByText, getByTestId, rerender } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
const ReadComponent = () => ((0, jsx_runtime_1.jsx)("span", { children: (0, hooks_1.use)(MySingleton).data }, void 0)); | ||
const { queryAllByText, getByTestId, rerender } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryAllByText('initial').length).toBe(2); | ||
expect(queryAllByText('loaded data').length).toBe(0); | ||
yield react_1.act(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
yield (0, react_1.act)(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(buttonId)); | ||
})); | ||
rerender(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryAllByText('initial').length).toBe(0); | ||
@@ -150,3 +147,3 @@ expect(queryAllByText('loaded data').length).toBe(2); | ||
})); | ||
test('stateful', (done) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
test('stateful', (done) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () { | ||
let MyStateful = class MyStateful { | ||
@@ -157,3 +154,3 @@ constructor() { | ||
}; | ||
MyStateful = tslib_1.__decorate([ | ||
MyStateful = (0, tslib_1.__decorate)([ | ||
decorators_1.stateful | ||
@@ -169,10 +166,7 @@ ], MyStateful); | ||
const RootComponent = () => { | ||
return (jsx_runtime_1.jsxs(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [jsx_runtime_1.jsx(LoadComponent, { message: firstMessage, buttonId: button1 }, void 0), | ||
jsx_runtime_1.jsx(ReadComponent, {}, void 0), | ||
jsx_runtime_1.jsxs(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [jsx_runtime_1.jsx(LoadComponent, { message: secondMessage, buttonId: button2 }, void 0), | ||
jsx_runtime_1.jsx(ReadComponent, {}, void 0)] }), void 0)] }), void 0)); | ||
return ((0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [(0, jsx_runtime_1.jsx)(LoadComponent, { message: firstMessage, buttonId: button1 }, void 0), (0, jsx_runtime_1.jsx)(ReadComponent, {}, void 0), (0, jsx_runtime_1.jsxs)(Provider_1.Provider, Object.assign({ dependencies: dependencies }, { children: [(0, jsx_runtime_1.jsx)(LoadComponent, { message: secondMessage, buttonId: button2 }, void 0), (0, jsx_runtime_1.jsx)(ReadComponent, {}, void 0)] }), void 0)] }), void 0)); | ||
}; | ||
const LoadComponent = ({ message, buttonId }) => { | ||
const myStateful = hooks_1.use(MyStateful); | ||
return (jsx_runtime_1.jsx("div", { children: jsx_runtime_1.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; | ||
@@ -182,6 +176,6 @@ } }, void 0) }, void 0)); | ||
const ReadComponent = () => { | ||
const myStateful = hooks_1.use(MyStateful); | ||
return (jsx_runtime_1.jsx("span", { children: myStateful.message }, void 0)); | ||
const myStateful = (0, hooks_1.use)(MyStateful); | ||
return ((0, jsx_runtime_1.jsx)("span", { children: myStateful.message }, void 0)); | ||
}; | ||
const { queryAllByText, getByTestId, rerender } = react_1.render(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
const { queryAllByText, getByTestId, rerender } = (0, react_1.render)((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryAllByText('initial').length).toBe(2); | ||
@@ -191,6 +185,6 @@ expect(queryAllByText(firstMessage).length).toBe(0); | ||
// first state | ||
yield react_1.act(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
yield (0, react_1.act)(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(button1)); | ||
})); | ||
rerender(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryAllByText('initial').length).toBe(1); | ||
@@ -200,6 +194,6 @@ expect(queryAllByText(firstMessage).length).toBe(1); | ||
// second state | ||
yield react_1.act(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
yield (0, react_1.act)(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () { | ||
react_1.fireEvent.click(getByTestId(button2)); | ||
})); | ||
rerender(jsx_runtime_1.jsx(RootComponent, {}, void 0)); | ||
rerender((0, jsx_runtime_1.jsx)(RootComponent, {}, void 0)); | ||
expect(queryAllByText('initial').length).toBe(0); | ||
@@ -216,3 +210,3 @@ expect(queryAllByText(firstMessage).length).toBe(1); | ||
}; | ||
Service1 = tslib_1.__decorate([ | ||
Service1 = (0, tslib_1.__decorate)([ | ||
cheap_di_1.singleton | ||
@@ -225,3 +219,3 @@ ], Service1); | ||
}; | ||
Service2 = tslib_1.__decorate([ | ||
Service2 = (0, tslib_1.__decorate)([ | ||
cheap_di_1.singleton | ||
@@ -234,3 +228,3 @@ ], Service2); | ||
}; | ||
Service3 = tslib_1.__decorate([ | ||
Service3 = (0, tslib_1.__decorate)([ | ||
cheap_di_1.singleton | ||
@@ -240,14 +234,11 @@ ], Service3); | ||
const TestComponent = () => { | ||
const s1 = hooks_1.use(Service1); | ||
const s2 = hooks_1.use(Service2); | ||
const s3 = hooks_1.use(Service3); | ||
return (jsx_runtime_1.jsxs(jsx_runtime_1.Fragment, { children: [jsx_runtime_1.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" }), void 0), | ||
jsx_runtime_1.jsx("p", { children: s1.state }, void 0), | ||
jsx_runtime_1.jsx("p", { children: s2.state }, void 0), | ||
jsx_runtime_1.jsx("p", { children: s3.state }, void 0)] }, void 0)); | ||
} }, { children: "update s3 state" }), void 0), (0, jsx_runtime_1.jsx)("p", { children: s1.state }, void 0), (0, jsx_runtime_1.jsx)("p", { children: s2.state }, void 0), (0, jsx_runtime_1.jsx)("p", { children: s3.state }, void 0)] }, void 0)); | ||
}; | ||
const Component = () => (jsx_runtime_1.jsx(SelfOneTimeProvider_1.SelfOneTimeProvider, Object.assign({ dependencies: [Service1] }, { children: jsx_runtime_1.jsx(SelfOneTimeProvider_1.SelfOneTimeProvider, Object.assign({ dependencies: [Service2] }, { children: jsx_runtime_1.jsx(SelfOneTimeProvider_1.SelfOneTimeProvider, Object.assign({ dependencies: [Service3] }, { children: jsx_runtime_1.jsx(TestComponent, {}, void 0) }), void 0) }), void 0) }), void 0)); | ||
const { rerender, getByTestId, queryByText } = react_1.render(jsx_runtime_1.jsx(Component, {}, void 0)); | ||
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, {}, void 0) }), void 0) }), void 0) }), void 0)); | ||
const { rerender, getByTestId, queryByText } = (0, react_1.render)((0, jsx_runtime_1.jsx)(Component, {}, void 0)); | ||
expect(queryByText('123')).toBeInTheDocument(); | ||
@@ -258,3 +249,3 @@ expect(queryByText('456')).toBeInTheDocument(); | ||
react_1.fireEvent.click(getByTestId(buttonId)); | ||
rerender(jsx_runtime_1.jsx(Component, {}, void 0)); | ||
rerender((0, jsx_runtime_1.jsx)(Component, {}, void 0)); | ||
setTimeout(() => { | ||
@@ -261,0 +252,0 @@ expect(queryByText('123')).toBeInTheDocument(); |
@@ -9,7 +9,7 @@ "use strict"; | ||
const SelfOneTimeProvider = props => { | ||
const { dependencies, children } = props, rest = tslib_1.__rest(props, ["dependencies", "children"]); | ||
const registrations = react_1.useMemo(() => dependencies, []); | ||
return (jsx_runtime_1.jsx(SelfProvider_1.SelfProvider, Object.assign({ dependencies: registrations }, rest, { children: children }), void 0)); | ||
const { dependencies, children } = props, rest = (0, 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 }), void 0)); | ||
}; | ||
exports.SelfOneTimeProvider = SelfOneTimeProvider; | ||
//# sourceMappingURL=SelfOneTimeProvider.js.map |
@@ -10,11 +10,11 @@ "use strict"; | ||
const SelfProvider = props => { | ||
const { dependencies, children } = props, rest = tslib_1.__rest(props, ["dependencies", "children"]); | ||
const [registrationFunctions, setRegistrationFunctions] = react_1.useState(() => dependencies.map(mapper)); | ||
react_1.useEffect(() => { | ||
const { dependencies, children } = props, rest = (0, 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_runtime_1.jsx(Provider_1.Provider, Object.assign({ dependencies: registrationFunctions }, rest, { children: children }), void 0)); | ||
return ((0, jsx_runtime_1.jsx)(Provider_1.Provider, Object.assign({ dependencies: registrationFunctions }, rest, { children: children }), void 0)); | ||
}; | ||
exports.SelfProvider = SelfProvider; | ||
//# sourceMappingURL=SelfProvider.js.map |
@@ -1,2 +0,2 @@ | ||
import { AbstractConstructor, Constructor, ContainerImpl, ImplementationTypeWithInjection } from 'cheap-di'; | ||
import { AbstractConstructor, Constructor, ContainerImpl, ImplementationType, ImplementationTypeWithInjection, RegistrationType } from 'cheap-di'; | ||
export declare class ReactContainer extends ContainerImpl { | ||
@@ -8,5 +8,14 @@ parentContainer?: ReactContainer | undefined; | ||
constructor(parentContainer?: ReactContainer | undefined); | ||
registerType<TInstance>(implementationType: ImplementationType<TInstance>): { | ||
as: <TBase extends Partial<TInstance>>(type: RegistrationType<TBase>) => { | ||
with: (...injectionParams: any[]) => void; | ||
}; | ||
asSingleton: <TBase_1 extends Partial<TInstance>>(type?: RegistrationType<TBase_1> | undefined) => { | ||
with: (...injectionParams: any[]) => void; | ||
}; | ||
with: (...injectionParams: any[]) => void; | ||
}; | ||
get rootContainer(): ReactContainer; | ||
sameParent(parentContainer?: ContainerImpl): boolean; | ||
getDependencies(): Map<import("cheap-di/dist/types").RegistrationType<any>, Object | ImplementationTypeWithInjection<any>>; | ||
getDependencies(): Map<RegistrationType<any>, Object | ImplementationTypeWithInjection<any>>; | ||
localScope<Callback extends (container: ReactContainer) => any>(callback: Callback): Callback extends (container: ReactContainer) => infer Result ? Result : void; | ||
@@ -13,0 +22,0 @@ skipParentInstanceResolvingOnce(): void; |
@@ -5,2 +5,4 @@ "use strict"; | ||
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 { | ||
@@ -14,2 +16,14 @@ constructor(parentContainer) { | ||
} | ||
registerType(implementationType) { | ||
if (!(0, decorators_1.isStateful)(implementationType)) { | ||
(0, decorators_1.stateful)(implementationType); | ||
} | ||
const registration = super.registerType(implementationType); | ||
const superAsSingleton = registration.asSingleton; | ||
registration.asSingleton = (type) => { | ||
(0, stateful_1.removeStateful)(implementationType); | ||
return superAsSingleton(type); | ||
}; | ||
return registration; | ||
} | ||
get rootContainer() { | ||
@@ -16,0 +30,0 @@ return this.findRootContainer(); |
"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); | ||
(0, tslib_1.__exportStar)(require("./InternalLogger"), exports); | ||
(0, tslib_1.__exportStar)(require("./reconfigureObject"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "cheap-di-react", | ||
"version": "3.0.0", | ||
"version": "3.2.1", | ||
"description": "", | ||
@@ -17,9 +17,6 @@ "scripts": { | ||
"tslib": "2.2.0", | ||
"typescript": "4.2.4", | ||
"cheap-di": "3.0.0", | ||
"react": "17.0.2", | ||
"react-dom": "17.0.2" | ||
"typescript": "4.4.2" | ||
}, | ||
"peerDependencies": { | ||
"cheap-di": ">= 3.0.0", | ||
"cheap-di": ">= 3.2.1", | ||
"react": ">= 16.14.0", | ||
@@ -26,0 +23,0 @@ "react-dom": ">= 16.14.0" |
103
README.md
@@ -103,5 +103,46 @@ # cheap-di-react | ||
`stateful` | ||
```tsx | ||
import { SelfOneTimeProvider, use, stateful } from 'cheap-di-react'; | ||
@stateful | ||
class MyStateful { | ||
message: string = 'initial'; | ||
} | ||
const RootComponent = () => { | ||
return ( | ||
<SelfOneTimeProvider dependencies={[MyStateful]}> | ||
<LoadComponent message="level 1"/> | ||
<ReadComponent/> | ||
<SelfOneTimeProvider dependencies={[MyStateful]}> | ||
<LoadComponent message="level 2"/> | ||
<ReadComponent/> | ||
</SelfOneTimeProvider> | ||
</SelfOneTimeProvider> | ||
); | ||
}; | ||
const LoadComponent = ({ message }: { message: string }) => { | ||
const myStateful = use(MyStateful); | ||
return ( | ||
<button onClick={() => { myStateful.message = message; }}/> | ||
); | ||
}; | ||
const ReadComponent = () => { | ||
const myStateful = use(MyStateful); | ||
return ( | ||
<span> | ||
{myStateful.message} | ||
</span> | ||
); | ||
}; | ||
``` | ||
`singleton` | ||
```tsx | ||
import { singleton } from 'cheap-di'; | ||
import { SelfOneTimeProvider, use } from 'cheap-di-react'; | ||
import { singleton, stateful } from 'cheap-di'; | ||
@@ -117,11 +158,2 @@ @singleton | ||
@stateful | ||
class MyStateful { | ||
data: string[] = ['initial']; | ||
async loadData() { | ||
this.data = await Promise.resolve(['some']); | ||
} | ||
} | ||
const RootComponent = () => { | ||
@@ -155,1 +187,52 @@ return ( | ||
``` | ||
You can configure your services with more classic-way - when your class know nothing about how and where it will be | ||
used. | ||
`stateful` | ||
```tsx | ||
import { OneTimeProvider, use } from 'cheap-di-react'; | ||
class MyStateful { | ||
message: string = 'initial'; | ||
} | ||
const RootComponent = () => { | ||
return ( | ||
<OneTimeProvider dependencies={[ dr => dr.registerType(MyStateful) ]}> | ||
<LoadComponent message="level 1"/> | ||
<ReadComponent/> | ||
<OneTimeProvider dependencies={[ dr => dr.registerType(MyStateful) ]}> | ||
<LoadComponent message="level 2"/> | ||
<ReadComponent/> | ||
</OneTimeProvider> | ||
</OneTimeProvider> | ||
); | ||
}; | ||
``` | ||
`singleton` | ||
```tsx | ||
import { OneTimeProvider, use } from 'cheap-di-react'; | ||
class MySingleton { | ||
data: string[] = ['initial']; | ||
async loadData() { | ||
this.data = await Promise.resolve(['some']); | ||
} | ||
} | ||
const RootComponent = () => { | ||
return ( | ||
<OneTimeProvider dependencies={[ dr => dr.registerType(MySingleton).asSingleton() ]}> | ||
<Component/> | ||
</OneTimeProvider> | ||
); | ||
}; | ||
``` | ||
But remember, if you want to use auto resolving your dependencies with typescript reflection, you need | ||
`"emitDecoratorMetadata": true,` in `tsconfig.ts` and any class-decorator for your service-class (read more in | ||
`cheap-di` README.md) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
122518
8
1458
236
0