cheap-di-react
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -7,32 +7,7 @@ "use strict"; | ||
const Context_1 = require("./Context"); | ||
function reconfigureObject(instance) { | ||
const newConfiguration = Object.keys(instance).reduce((config, property) => { | ||
config[`_${property}`] = { | ||
value: instance[property], | ||
writable: true, | ||
configurable: false, | ||
enumerable: false, | ||
}; | ||
config[property] = { | ||
configurable: false, | ||
enumerable: true, | ||
get() { | ||
return instance[`_${property}`]; | ||
}, | ||
set(value) { | ||
instance[`_${property}`] = value; | ||
if (instance.constructor && instance.constructor.__updateKey) { | ||
instance.constructor.__updateKey(); | ||
} | ||
} | ||
}; | ||
return config; | ||
}, {}); | ||
Object.defineProperties(instance, newConfiguration); | ||
} | ||
const reconfigureObject_1 = require("./reconfigureObject"); | ||
const SingletonStateProvider = ({ children }) => { | ||
const [singletonConstructors, setSingletonConstructors] = react_1.useState([]); | ||
const container = react_1.useContext(Context_1.Context); | ||
const [, rerender] = react_1.useState(); | ||
react_1.useEffect(() => { | ||
const getConstructors = () => { | ||
const constructors = []; | ||
@@ -42,22 +17,35 @@ container.dependencies.forEach((impl, reg) => { | ||
const constructor = instance.constructor; | ||
if (constructor === null || constructor === void 0 ? void 0 : constructor.__singleton) { | ||
const time = new Date().getTime(); | ||
constructor.__key = time; | ||
constructor.__value = time; | ||
constructor.__updateContext = () => { | ||
constructor.__value = new Date().getTime(); | ||
rerender({}); | ||
}; | ||
if (!constructor.__configured) { | ||
reconfigureObject(instance); | ||
constructor.__configured = true; | ||
} | ||
constructor.__reactContext = react_1.createContext(''); | ||
constructors.push(constructor); | ||
if (!(constructor === null || constructor === void 0 ? void 0 : constructor.__singleton)) { | ||
return; | ||
} | ||
const time = new Date().getTime().toString(); | ||
constructor.__key = time; | ||
constructor.__value = time; | ||
constructor.__updateContext = () => { | ||
constructor.__value = new Date().getTime().toString(); | ||
rerender({}); | ||
}; | ||
if (!constructor.__configured) { | ||
reconfigureObject_1.reconfigureObject(instance); | ||
constructor.__configured = true; | ||
} | ||
constructor.__reactContext = react_1.createContext(''); | ||
constructors.push(constructor); | ||
}); | ||
setSingletonConstructors(constructors); | ||
return constructors; | ||
}; | ||
const ref = react_1.useRef({ firstRender: true }); | ||
const [singletonConstructors, setSingletonConstructors] = react_1.useState(() => getConstructors()); | ||
react_1.useEffect(() => { | ||
var _a; | ||
if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.firstRender) { | ||
ref.current.firstRender = false; | ||
} | ||
else { | ||
const constructors = getConstructors(); | ||
setSingletonConstructors(constructors); | ||
} | ||
}, [container.dependencies]); | ||
const tree = singletonConstructors.reduce((Tree, constructor) => { | ||
return (jsx_runtime_1.jsx(constructor.__reactContext.Provider, Object.assign({ value: constructor.__value.toString() }, { children: Tree }), constructor.__key.toString())); | ||
return (jsx_runtime_1.jsx(constructor.__reactContext.Provider, Object.assign({ value: constructor.__value }, { children: Tree }), constructor.__key)); | ||
}, jsx_runtime_1.jsx(jsx_runtime_1.Fragment, { children: children }, void 0)); | ||
@@ -64,0 +52,0 @@ return tree; |
{ | ||
"name": "cheap-di-react", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
21528
33
241