@solid-primitives/memo
Advanced tools
Comparing version 0.1.1 to 0.2.1
@@ -10,3 +10,5 @@ // src/index.ts | ||
runWithOwner, | ||
on | ||
on, | ||
getListener, | ||
createRoot | ||
} from "solid-js"; | ||
@@ -85,20 +87,34 @@ import debounce from "@solid-primitives/debounce"; | ||
function createLazyMemo(calc, value, options) { | ||
var _a; | ||
const owner = (_a = getOwner()) != null ? _a : void 0; | ||
let listeners = 0; | ||
let lastest = value; | ||
let dirty = true; | ||
let memo; | ||
let listeners = 0; | ||
const owner = getOwner(); | ||
const recreateMemo = () => runWithOwner(owner, () => { | ||
memo = createMemo((prev) => { | ||
if (listeners) | ||
return calc(prev); | ||
memo = void 0; | ||
return prev; | ||
}, value, options); | ||
}); | ||
let dispose; | ||
onCleanup(() => dispose == null ? void 0 : dispose()); | ||
const track = createPureReaction(() => dirty = !memo); | ||
return () => { | ||
if (getOwner()) { | ||
listeners++; | ||
onCleanup(() => listeners--); | ||
if (!getListener()) { | ||
if (memo) | ||
return memo(); | ||
if (dirty) | ||
track(() => lastest = calc(lastest)); | ||
dirty = false; | ||
return lastest; | ||
} | ||
if (!memo) | ||
recreateMemo(); | ||
listeners++; | ||
onCleanup(() => listeners--); | ||
if (!memo) { | ||
createRoot((_dispose) => { | ||
dispose = _dispose; | ||
memo = createMemo(() => { | ||
if (listeners) | ||
return lastest = calc(lastest); | ||
dispose(); | ||
dispose = memo = void 0; | ||
return lastest; | ||
}, lastest, options); | ||
}, owner); | ||
} | ||
return memo(); | ||
@@ -105,0 +121,0 @@ }; |
{ | ||
"name": "@solid-primitives/memo", | ||
"version": "0.1.1", | ||
"version": "0.2.1", | ||
"description": "Collection of custom memo primitives. They extend Solid's createMemo functionality while keeping the usage similar.", | ||
@@ -54,21 +54,21 @@ "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>", | ||
"@solid-primitives/throttle": "^1.2.0", | ||
"@solid-primitives/utils": "^1.0.0" | ||
"@solid-primitives/utils": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"@solid-primitives/mouse": "^1.2.0", | ||
"@solid-primitives/mouse": "^2.0.0", | ||
"jsdom": "^19.0.0", | ||
"prettier": "^2.5.1", | ||
"solid-app-router": "^0.2.0", | ||
"solid-register": "^0.1.5", | ||
"tslib": "^2.3.1", | ||
"tsup": "^5.11.1", | ||
"typescript": "^4.5.4", | ||
"unocss": "0.24.3", | ||
"uvu": "^0.5.2", | ||
"vite": "2.8.1", | ||
"vite-plugin-solid": "2.2.5" | ||
"prettier": "^2.6.2", | ||
"solid-app-router": "^0.3.3", | ||
"solid-register": "^0.2.5", | ||
"tslib": "^2.4.0", | ||
"tsup": "^5.12.8", | ||
"typescript": "^4.6.4", | ||
"unocss": "0.34.0", | ||
"uvu": "^0.5.3", | ||
"vite": "2.9.9", | ||
"vite-plugin-solid": "2.2.6" | ||
}, | ||
"peerDependencies": { | ||
"solid-js": "^1.3.1" | ||
"solid-js": "^1.4.2" | ||
} | ||
} |
@@ -87,2 +87,4 @@ <p> | ||
It may be useful for memos that aren't being listened to all the time, to reduce performance cost of wastefull computations. | ||
### How to use it | ||
@@ -110,2 +112,17 @@ | ||
### Usage caveats | ||
There are vary few actual good applications of a lazy memo, that couldn't be solved with other means — like improving the data architecture. For example, you can always only create memos in places that you intend to use it in, instead of declaring it prematurely. | ||
```ts | ||
// instead of memo, distribute only a calculation function | ||
const getDouble = (n: number) => n * 2; | ||
// and only declare memo where you want to use it | ||
const double = createMemo(() => getDouble(count())); | ||
``` | ||
A lazy memo won't work reliably with [Suspense](https://www.solidjs.com/docs/latest/api#<suspense>). | ||
There is a performance cost, from recreating and disposing computations, involved with using it over normal memo. | ||
### Demo | ||
@@ -319,2 +336,6 @@ | ||
0.2.1 | ||
`createLazyMemo` improvements | ||
</details> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33042
467
339
+ Added@solid-primitives/utils@2.2.1(transitive)
- Removed@solid-primitives/utils@1.5.2(transitive)