You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -31,6 +31,72 @@ There is a lot you can do with this, but some examples:
31
31
You need to use Pluto version >= 0.17.2.
32
32
"""
33
33
34
+
# ╔═╡ bc0e4219-a40b-46f5-adb2-f164d8a9bbdb
35
+
"""
36
+
@use_memo(deps::Vector{Any}) do
37
+
# Expensive computation/loading
38
+
end
39
+
40
+
Does a computation only when the deps array has changed.
41
+
This is useful for heavy computations as well as resource fetches like file reading or fetching from the network.
42
+
43
+
```julia
44
+
# Only read a file once
45
+
@use_memo([filename]) do
46
+
read(filename)
47
+
end
48
+
```
49
+
50
+
```julia
51
+
@use_memo([a, b]) do
52
+
a + b # But they're like really big numbers
53
+
end
54
+
```
55
+
"""
56
+
macrouse_memo(f, deps)
57
+
quote
58
+
ref =@use_ref(nothing)
59
+
if@use_did_deps_change($(esc(deps)))
60
+
ref[] =$(esc(f))()
61
+
end
62
+
ref[]
63
+
end
64
+
end
65
+
66
+
# ╔═╡ 0f632b57-ea01-482b-b93e-d69f962a6d92
67
+
md"""
68
+
## Not really hooks but internally very hook-ish
69
+
70
+
These are all for making sure you have some level of Pluto-ness active. These are made to work outside of Pluto as well, but obviously give you the opposite results :P
71
+
"""
72
+
73
+
# ╔═╡ 8c2e9cad-eb63-4af5-8b52-629e8d3439bd
74
+
"""
75
+
is_running_in_pluto_process()
76
+
77
+
This doesn't mean we're in a Pluto cell, e.g. can use @bind and hooks goodies.
78
+
It only means PlutoRunner is available (and at a version that technically supports hooks)
79
+
"""
80
+
functionis_running_in_pluto_process()
81
+
isdefined(Main, :PlutoRunner) &&
82
+
# Also making sure my favorite goodies are present
Experimental function to wrap a bunch of macros in a fake cell that fully refreshes when the deps provided change. This is useful if you make a macro that wraps a bunch of Pluto Hooks, and you just want to refresh the whole block when something changes. This also clears [`@use_ref`](@ref)'s and [`@use_state`](@ref)'s, even though these don't even have a deps argument.
222
+
223
+
Not entirely sure how much this is necessary (or if I'm missing something obvious that doesn't make it necessary).
224
+
225
+
Also, this name does **not** spark joy.
226
+
"""
227
+
macrouse_deps(fn_expr, deps)
228
+
if!is_inside_pluto(__module__)
229
+
returnquote
230
+
$(esc(deps))
231
+
232
+
$(esc(fn_expr))()
233
+
end
234
+
end
235
+
236
+
cell_id_ref =Ref{UUID}(uuid4())
237
+
238
+
quote
239
+
if@use_did_deps_change($(esc(deps)))
240
+
$cell_id_ref[] =uuid4()
241
+
end
242
+
243
+
with_cell_id($(esc(fn_expr)), $cell_id_ref[])
244
+
end
245
+
end
246
+
134
247
# ╔═╡ 89b3f807-2e24-4454-8f4c-b2a98aee571e
135
248
"""
136
249
@use_effect(deps::Vector{Any}) do
@@ -165,6 +278,13 @@ end
165
278
```
166
279
"""
167
280
macrouse_effect(f, deps)
281
+
if!is_inside_pluto(__module__)
282
+
returnquote
283
+
$(esc(deps))
284
+
$(esc(f))()
285
+
end
286
+
end
287
+
168
288
# For some reason the `cleanup_ref` using @use_ref or assigned outside the
169
289
# `register_cleanup_fn() do ... end` (and not interpolated directly into it)
170
290
# is `nothing` when the cleanup function actually ran...
@@ -192,37 +312,8 @@ macro use_effect(f, deps)
192
312
end
193
313
end
194
314
195
-
# ╔═╡ bc0e4219-a40b-46f5-adb2-f164d8a9bbdb
196
-
"""
197
-
@use_memo(deps::Vector{Any}) do
198
-
# Expensive computation/loading
199
-
end
200
-
201
-
Does a computation only when the deps array has changed.
202
-
This is useful for heavy computations as well as resource fetches like file reading or fetching from the network.
These are all for making sure you have some level of Pluto-ness active. These are made to work outside of Pluto as well, but obviously give you the opposite results :P
286
-
"""
287
-
288
-
# ╔═╡ 8c2e9cad-eb63-4af5-8b52-629e8d3439bd
289
-
"""
290
-
is_running_in_pluto_process()
291
-
292
-
This doesn't mean we're in a Pluto cell, e.g. can use @bind and hooks goodies.
293
-
It only means PlutoRunner is available (and at a version that technically supports hooks)
294
-
"""
295
-
functionis_running_in_pluto_process()
296
-
isdefined(Main, :PlutoRunner) &&
297
-
# Also making sure my favorite goodies are present
Experimental function to wrap a bunch of macros in a fake cell that fully refreshes when the deps provided change. This is useful if you make a macro that wraps a bunch of Pluto Hooks, and you just want to refresh the whole block when something changes. This also clears [`@use_ref`](@ref)'s and [`@use_state`](@ref)'s, even though these don't even have a deps argument.
310
-
311
-
Not entirely sure how much this is necessary (or if I'm missing something obvious that doesn't make it necessary).
312
-
313
-
Also, this name does **not** spark joy.
314
-
"""
315
-
macrouse_deps(fn_expr, deps)
316
-
# It's not pretty, but I don't want the macroexpansion to crash already.
317
-
# So I need this check before everything that uses `PlutoRunner`
@@ -346,9 +389,6 @@ These are, I hope, the only parts that need to explicitly reference PlutoRunner.
346
389
Each of these inserts a reference to a special PlutoRunner object into the resulting expression, and that special object will be caught by PlutoRunner while evaluating the cell, and replaced with the actual value.
347
390
348
391
It seems a bit over-engineered, and I guess it is, BUT, this makes it possible to have a very strict sense of what cell is actually running what function. Also it allows other macros (specifically [`@use_deps`](@ref)) to insert it's own values instead of Plutos, thus kinda creating a cell-in-a-cell 😏
349
-
350
-
Not yet sure how these should react when they are called outside of Pluto...
0 commit comments