Skip to content

Commit

Permalink
♻️ Replace deprecated unsafe_coerce calls (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored Aug 20, 2024
1 parent 2b1f6b5 commit 5414cd6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/lustre-escape.ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export function first(string) {
export function drop_first(string) {
return string.slice(1);
}

export function coerce(x) {
return x;
}
16 changes: 9 additions & 7 deletions src/lustre/internals/patch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,7 @@ fn attribute_dict(
case dict.get(dict, "class") {
Ok(Attribute(_, classes, _)) -> {
let classes =
dynamic.from(
dynamic.unsafe_coerce(classes)
<> " "
<> dynamic.unsafe_coerce(value),
)
dynamic.from(unsafe_coerce(classes) <> " " <> unsafe_coerce(value))
dict.insert(dict, "class", Attribute("class", classes, False))
}

Expand All @@ -366,8 +362,8 @@ fn attribute_dict(
Ok(Attribute(_, styles, _)) -> {
let styles =
dynamic.from(list.append(
dynamic.unsafe_coerce(styles),
dynamic.unsafe_coerce(value),
unsafe_coerce(styles),
unsafe_coerce(value),
))
dict.insert(dict, "style", Attribute("style", styles, False))
}
Expand Down Expand Up @@ -437,3 +433,9 @@ pub fn is_empty_element_diff(diff: ElementDiff(msg)) -> Bool {
fn is_empty_attribute_diff(diff: AttributeDiff(msg)) -> Bool {
diff.created == set.new() && diff.removed == set.new()
}

// FFI -------------------------------------------------------------------------

@external(erlang, "lustre_escape_ffi", "coerce")
@external(javascript, "../../../lustre-escape.ffi.mjs", "coerce")
fn unsafe_coerce(value: a) -> b
8 changes: 7 additions & 1 deletion src/lustre/internals/runtime.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn loop(
}

Debug(ForceModel(model)) -> {
let model = dynamic.unsafe_coerce(model)
let model = unsafe_coerce(model)
let html = state.view(model)
let diff = patch.elements(state.html, html)
let next =
Expand Down Expand Up @@ -235,3 +235,9 @@ fn run_effects(effects: Effect(msg), self: Subject(Action(msg, runtime))) -> Nil

effect.perform(effects, dispatch, emit)
}

// FFI -------------------------------------------------------------------------

@external(erlang, "lustre_escape_ffi", "coerce")
@external(javascript, "../../../lustre-escape.ffi.mjs", "coerce")
fn unsafe_coerce(value: a) -> b
16 changes: 11 additions & 5 deletions src/lustre/internals/vdom.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -124,39 +124,39 @@ pub fn attribute_to_json(
Ok(
json.object([
#("0", json.string(name)),
#("1", json.string(dynamic.unsafe_coerce(value))),
#("1", json.string(unsafe_coerce(value))),
]),
)

"Atom" if value == true_atom || value == false_atom ->
Ok(
json.object([
#("0", json.string(name)),
#("1", json.bool(dynamic.unsafe_coerce(value))),
#("1", json.bool(unsafe_coerce(value))),
]),
)

"Bool" | "Boolean" ->
Ok(
json.object([
#("0", json.string(name)),
#("1", json.bool(dynamic.unsafe_coerce(value))),
#("1", json.bool(unsafe_coerce(value))),
]),
)

"Int" ->
Ok(
json.object([
#("0", json.string(name)),
#("1", json.int(dynamic.unsafe_coerce(value))),
#("1", json.int(unsafe_coerce(value))),
]),
)

"Float" ->
Ok(
json.object([
#("0", json.string(name)),
#("1", json.float(dynamic.unsafe_coerce(value))),
#("1", json.float(unsafe_coerce(value))),
]),
)

Expand Down Expand Up @@ -397,3 +397,9 @@ pub fn attribute_to_event_handler(
}
}
}

// FFI -------------------------------------------------------------------------

@external(erlang, "lustre_escape_ffi", "coerce")
@external(javascript, "../../../lustre-escape.ffi.mjs", "coerce")
fn unsafe_coerce(value: a) -> b

0 comments on commit 5414cd6

Please sign in to comment.