Comparing version 0.3.9 to 0.3.10
@@ -91,4 +91,23 @@ "use strict"; | ||
exports.observe = observe; | ||
function use(Class, deps) { | ||
return react_1.useMemo(() => (deps ? new Class(...deps) : new Class()), deps || []); | ||
function use(Class, deps = []) { | ||
if (!Array.isArray(deps)) { | ||
throw 'TypeError: deps argument should be an array'; | ||
} | ||
const [inst, effect] = react_1.useMemo(() => { | ||
const inst = new Class(...deps); | ||
const effect = () => { | ||
const un = inst.effect && inst.effect(); | ||
return () => { | ||
try { | ||
un && un(); | ||
} | ||
finally { | ||
inst.free && inst.free(); | ||
} | ||
}; | ||
}; | ||
return [inst, effect]; | ||
}, deps); | ||
react_1.useEffect(effect, [inst]); | ||
return inst; | ||
} | ||
@@ -99,3 +118,3 @@ exports.use = use; | ||
shareds.forEach(instance => { | ||
instance.destructor && instance.destructor(); | ||
instance.free && instance.free(); | ||
}); | ||
@@ -102,0 +121,0 @@ } |
{ | ||
"name": "realar", | ||
"version": "0.3.9", | ||
"version": "0.3.10", | ||
"description": "React state manager", | ||
@@ -24,2 +24,3 @@ "repository": { | ||
"test": "jest", | ||
"jest:clear-cache": "jest --clearCache", | ||
"publish": "npm run clear && npm run build && lerna publish", | ||
@@ -53,4 +54,4 @@ "format": "prettier --write src tests jest", | ||
"prettier": "2.2.1", | ||
"react": "17.0.1", | ||
"react-dom": "17.0.1", | ||
"react": "16.14.0", | ||
"react-dom": "16.14.0", | ||
"rimraf": "3.0.2", | ||
@@ -92,3 +93,3 @@ "typescript": "4.1.3" | ||
}, | ||
"gitHead": "1c1fb3266f8e9942b37099f61daa5bfa7fd897be" | ||
"gitHead": "d7aa04620279cf4556da90dc51520fd0cf9097b3" | ||
} |
@@ -105,4 +105,22 @@ import { useRef, useReducer, useEffect, useMemo, FC, memo } from 'react'; | ||
function use<T extends unknown[], M>(Class: new (...args: T) => M, deps?: T): M { | ||
return useMemo(() => (deps ? new Class(...deps) : new (Class as any)()), deps || []); | ||
function use<T extends unknown[], M>(Class: new (...args: T) => M, deps = [] as T): M { | ||
if (!Array.isArray(deps)) { | ||
throw 'TypeError: deps argument should be an array'; | ||
} | ||
const [inst, effect] = useMemo(() => { | ||
const inst = new Class(...(deps as any)) as any; | ||
const effect = () => { | ||
const un = inst.effect && inst.effect(); | ||
return () => { | ||
try { | ||
un && un(); | ||
} finally { | ||
inst.free && inst.free(); | ||
} | ||
}; | ||
}; | ||
return [inst, effect]; | ||
}, deps); | ||
useEffect(effect, [inst]); | ||
return inst; | ||
} | ||
@@ -113,3 +131,3 @@ | ||
shareds.forEach(instance => { | ||
instance.destructor && instance.destructor(); | ||
instance.free && instance.free(); | ||
}); | ||
@@ -116,0 +134,0 @@ } finally { |
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
16765
260