Skip to content

Commit 593a5df

Browse files
committed
Add dispose function
1 parent d6b75e4 commit 593a5df

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

sketch/README.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ import sketch/lustre as sketch_lustre
8888
pub fn main() {
8989
// Initialise the cache. Two strategies can be used. Ephemeral caches are designed as throw-away caches.
9090
let assert Ok(stylesheet) = sketch.stylesheet(strategy: sketch.Ephemeral)
91+
// Setup the cache. This should be done once before using the render.
92+
let assert Ok(stylesheet) = sketch.setup(stylesheet)
9193
// Generate the partial view function, compatible with Lustre's runtime.
9294
lustre.simple(init, update, view(_, stylesheet))
9395
// And voilà!
@@ -96,7 +98,7 @@ pub fn main() {
9698
9799
fn view(model, stylesheet) {
98100
// Add the sketch CSS generation "view middleware".
99-
use <- sketch_lustre.render(stylesheet, [sketch_lustre.node()])
101+
use <- sketch_lustre.render(stylesheet:, in: [sketch_lustre.node()])
100102
// Run your actual view function.
101103
my_view(model)
102104
}
@@ -149,24 +151,6 @@ And you're done! Enjoy your Lustre app, Sketch-enhanced!
149151

150152
### Final notes
151153

152-
#### On Sketch Lustre Element
153-
154-
A Sketch `Element(msg)` is extremely similar to a Lustre `Element(msg)`,
155-
excepted it carries styles information on top. Going from a
156-
`sketch/lustre/element.Element(msg)` to a `lustre/element.Element(msg)` is
157-
straightforward, by using `sketch/lustre/element.unstyled`. The opposite (going
158-
from a Lustre element to a Sketch Lustre element) is also possible by using
159-
`sketch/lustre/element.styled`!
160-
161-
#### Sketch Lustre Experimental
162-
163-
Because sometimes you may want to avoid the `Element(msg)` overhead, you can try
164-
the experimental Sketch Lustre runtime, `sketch_lustre_experimental`. That
165-
runtime works in the same way, excepts it does not implements its own `Element`
166-
type on top of Lustre's `Element`. Most of the time, you should not see any
167-
differences. Keep in mind that it can bug though, as it's still experimental. If
168-
you try to use it, please, report any bugs you can find.
169-
170154
#### Usage with Shadow DOM
171155

172156
In browser, Sketch can work with a Shadow DOM, in order to hide the compiled

sketch/src/sketch.gleam

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ pub fn stylesheet(
110110
StyleSheet(cache:, id:)
111111
}
112112

113+
@target(javascript)
114+
/// Delete a persistent stylesheet. That function should be called for Persistent
115+
/// Erlang stylesheet, and won't have any effect otherwise.
116+
pub fn dispose(stylesheet: StyleSheet) {
117+
Nil
118+
}
119+
120+
@target(erlang)
121+
pub fn dispose(stylesheet: StyleSheet) {
122+
actor.dispose(stylesheet.cache)
123+
}
124+
113125
@external(erlang, "erlang", "unique_integer")
114126
@external(javascript, "./sketch.ffi.mjs", "uniqueId")
115127
fn unique_id() -> Int

sketch/src/sketch/internals/cache/actor.gleam

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,14 @@ pub fn loop(msg: Request, cache: cache.Cache) -> actor.Next(a, cache.Cache) {
100100
}
101101
}
102102
}
103+
104+
@target(erlang)
105+
pub fn dispose(cache: Cache) {
106+
case cache {
107+
Ephemeral(_) -> Nil
108+
Persistent(proc:) -> {
109+
process.subject_owner(proc)
110+
|> process.send_exit
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)