Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
rename dispatcher to event emitter
  • Loading branch information
eliknebel committed Nov 9, 2024
1 parent 845f053 commit 6bd2652
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 65 deletions.
30 changes: 15 additions & 15 deletions client/src/modules/rawHtml.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function updateInnerHtml (oldVNode, vNode) {
const {data: oldData = {}} = oldVNode
const {data = {}, elm} = vNode
const html = data.innerHtml || false

if (!html) return

if (html && oldData.innerHtml !== html) {
elm.innerHTML = html
}
function updateInnerHtml(oldVNode, vNode) {
const { data: oldData = {} } = oldVNode;
const { data = {}, elm } = vNode;
const html = data.innerHtml || false;

if (!html) return;

if (html && oldData.innerHtml !== html) {
elm.innerHTML = html;
}

export const rawHtmlModule = {
create: updateInnerHtml,
update: updateInnerHtml,
}
}

export const rawHtmlModule = {
create: updateInnerHtml,
update: updateInnerHtml,
};
31 changes: 14 additions & 17 deletions src/sprocket.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import gleam/option.{type Option, None, Some}
import gleam/result
import ids/cuid
import sprocket/component.{component}
import sprocket/context.{
type Dispatcher, type Element, type FunctionalComponent, Dispatcher, Updater,
}
import sprocket/context.{type Element, type FunctionalComponent, Updater}
import sprocket/internal/logger
import sprocket/internal/patch
import sprocket/internal/reconcile.{type ReconciledResult, ReconciledResult}
Expand Down Expand Up @@ -115,12 +113,12 @@ pub fn handle_ws(spkt: Sprocket(p), msg: String) -> Result(Response(p), String)

use runtime <- require_runtime(spkt)

let reply_dispatcher = fn(event, payload) {
let reply_emitter = fn(event, payload) {
hook_event_to_json(id, event, payload)
|> spkt.ws_send()
}

runtime.process_client_hook(runtime, id, event, payload, reply_dispatcher)
runtime.process_client_hook(runtime, id, event, payload, reply_emitter)

Ok(Empty)
}
Expand Down Expand Up @@ -166,18 +164,17 @@ fn connect(
|> spkt.ws_send()
})

let dispatcher =
Dispatcher(dispatch: fn(id, event, payload) {
let _ =
hook_event_to_json(id, event, payload)
|> spkt.ws_send()
let emitter = fn(id, event, payload) {
let _ =
hook_event_to_json(id, event, payload)
|> spkt.ws_send()

Ok(Nil)
})
Ok(Nil)
}

let view = component(spkt.component, spkt.initialize_props(initial_props))

case runtime.start(view, updater, Some(dispatcher)) {
case runtime.start(view, updater, Some(emitter)) {
Ok(r) -> {
// schedule intitial render
runtime.render_update(r)
Expand Down Expand Up @@ -331,10 +328,10 @@ pub fn render(el: Element, r: Renderer(a)) -> a {
error
})

let ctx =
context.new(el, cuid_channel, None, fn() { Nil }, fn(_index, _updater) {
Nil
})
let render_update = fn() { Nil }
let update_hook = fn(_index, _updater) { Nil }

let ctx = context.new(el, cuid_channel, None, render_update, update_hook)

let ReconciledResult(reconciled: reconciled, ..) =
reconcile(ctx, el, None, None)
Expand Down
33 changes: 22 additions & 11 deletions src/sprocket/context.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ pub type Updater(r) {
Updater(send: fn(r) -> Result(Nil, Nil))
}

pub type Dispatcher {
Dispatcher(dispatch: fn(String, String, Option(String)) -> Result(Nil, Nil))
}
pub type EventEmitter =
fn(String, String, Option(String)) -> Result(Nil, Nil)

pub type ComponentHooks =
OrderedMap(Int, Hook)
Expand Down Expand Up @@ -91,6 +90,19 @@ pub type Hook {
Client(id: Unique, name: String, handle_event: Option(ClientEventHandler))
}

// Returns true if the hook has the given id
pub fn has_id(hook: Hook, hook_id: Unique) -> Bool {
case hook {
Callback(id, _, _) if id == hook_id -> True
Memo(id, _, _) if id == hook_id -> True
Effect(id, _, _, _) if id == hook_id -> True
Handler(id, _) if id == hook_id -> True
State(id, _) if id == hook_id -> True
Client(id, _, _) if id == hook_id -> True
_ -> False
}
}

pub type Compared(a) {
Changed(changed: a)
Unchanged
Expand Down Expand Up @@ -137,7 +149,7 @@ pub type Context {
handlers: List(IdentifiableHandler),
render_update: fn() -> Nil,
update_hook: fn(Unique, fn(Hook) -> Hook) -> Nil,
dispatch_event: fn(Unique, String, Option(String)) -> Result(Nil, Nil),
emit: fn(Unique, String, Option(String)) -> Result(Nil, Nil),
cuid_channel: Subject(cuid.Message),
providers: Dict(String, Dynamic),
)
Expand All @@ -146,7 +158,7 @@ pub type Context {
pub fn new(
view: Element,
cuid_channel: Subject(cuid.Message),
dispatcher: Option(Dispatcher),
emitter: Option(EventEmitter),
render_update: fn() -> Nil,
update_hook: fn(Unique, fn(Hook) -> Hook) -> Nil,
) -> Context {
Expand All @@ -156,10 +168,9 @@ pub fn new(
handlers: [],
render_update: render_update,
update_hook: update_hook,
dispatch_event: fn(id, name, payload) {
case dispatcher {
Some(Dispatcher(dispatch: dispatch)) ->
dispatch(unique.to_string(id), name, payload)
emit: fn(id, name, payload) {
case emitter {
Some(emitter) -> emitter(unique.to_string(id), name, payload)
None -> Error(Nil)
}
},
Expand Down Expand Up @@ -254,13 +265,13 @@ pub fn get_event_handler(
#(ctx, handler)
}

pub fn dispatch_event(
pub fn emit_event(
ctx: Context,
id: Unique,
name: String,
payload: Option(String),
) {
ctx.dispatch_event(id, name, payload)
ctx.emit(id, name, payload)
}

pub fn provider(key: String, value: v, element: Element) -> Element {
Expand Down
2 changes: 1 addition & 1 deletion src/sprocket/hooks.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn client(

// callback to dispatch an event to the client
let dispatch_event = fn(name: String, payload: Option(String)) {
context.dispatch_event(ctx, id, name, payload)
context.emit_event(ctx, id, name, payload)
}

cb(ctx, bind_hook_attr, dispatch_event)
Expand Down
44 changes: 27 additions & 17 deletions src/sprocket/runtime.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import gleam/otp/actor.{type StartError, Spec}
import gleam/result
import ids/cuid
import sprocket/context.{
type ComponentHooks, type Context, type Dispatcher, type EffectCleanup,
type EffectResult, type Element, type Hook, type HookDependencies,
type ComponentHooks, type Context, type EffectCleanup, type EffectResult,
type Element, type EventEmitter, type Hook, type HookDependencies,
type IdentifiableHandler, type Updater, Callback, Changed, Client, Context,
Dispatcher, Effect, EffectResult, Handler, IdentifiableHandler, Memo, Reducer,
Unchanged, Updater, compare_deps,
Effect, EffectResult, Handler, IdentifiableHandler, Memo, Reducer, Unchanged,
Updater, compare_deps,
}
import sprocket/internal/constants.{call_timeout}
import sprocket/internal/exceptions.{throw_on_unexpected_hook_result}
Expand Down Expand Up @@ -62,7 +62,7 @@ pub opaque type Message {
id: String,
event: String,
payload: Option(Dynamic),
reply_dispatcher: fn(String, Option(String)) -> Result(Nil, Nil),
reply_emitter: fn(String, Option(String)) -> Result(Nil, Nil),
)
UpdateHookState(Unique, fn(Hook) -> Hook)
ReconcileImmediate(reply_with: Subject(ReconciledElement))
Expand Down Expand Up @@ -137,7 +137,7 @@ fn handle_message(message: Message, state: State) -> actor.Next(Message, State)
}
}

ProcessClientHook(id, event, payload, reply_dispatcher) -> {
ProcessClientHook(id, event, payload, reply_emitter) -> {
let client_hook = case state.reconciled {
Some(reconciled) -> {
let hook =
Expand All @@ -162,7 +162,7 @@ fn handle_message(message: Message, state: State) -> actor.Next(Message, State)
case client_hook {
Ok(Client(_id, _name, handle_event)) -> {
option.map(handle_event, fn(handle_event) {
handle_event(event, payload, reply_dispatcher)
handle_event(event, payload, reply_emitter)
})

actor.continue(state)
Expand All @@ -178,8 +178,8 @@ fn handle_message(message: Message, state: State) -> actor.Next(Message, State)
UpdateHookState(hook_id, update_fn) -> {
let updated =
state.reconciled
|> option.map(fn(node) {
traverse_rendered_hooks(node, fn(hook) {
|> option.map(fn(reconciled) {
find_and_update_hook(reconciled, hook_id, fn(hook) {
case hook {
// this operation is only applicable to State hooks
context.State(id, _) -> {
Expand Down Expand Up @@ -253,7 +253,7 @@ fn handle_message(message: Message, state: State) -> actor.Next(Message, State)
pub fn start(
view: Element,
updater: Updater(RenderedUpdate),
dispatcher: Option(Dispatcher),
emitter: Option(EventEmitter),
) -> Result(Runtime, StartError) {
let init = fn() {
let self = process.new_subject()
Expand All @@ -280,7 +280,7 @@ pub fn start(
ctx: context.new(
view,
cuid_channel,
dispatcher,
emitter,
render_update,
update_hook,
),
Expand Down Expand Up @@ -350,11 +350,11 @@ pub fn process_client_hook(
id: String,
event: String,
payload: Option(Dynamic),
reply_dispatcher: fn(String, Option(String)) -> Result(Nil, Nil),
reply_emitter: fn(String, Option(String)) -> Result(Nil, Nil),
) {
logger.debug("process.try_call GetClientHook")

actor.send(actor, ProcessClientHook(id, event, payload, reply_dispatcher))
actor.send(actor, ProcessClientHook(id, event, payload, reply_emitter))
}

pub fn render_update(actor) {
Expand Down Expand Up @@ -414,8 +414,6 @@ fn cleanup_hooks(rendered: ReconciledElement) {
}
}

Reducer(_, _, cleanup) -> cleanup()

_ -> Nil
}
})
Expand Down Expand Up @@ -444,8 +442,6 @@ fn run_cleanup_for_disposed_hooks(
}
}

Ok(Reducer(_, _, cleanup)) -> cleanup()

_ -> Nil
}
})
Expand Down Expand Up @@ -513,6 +509,7 @@ fn run_effects(rendered: ReconciledElement) -> ReconciledElement {

Effect(id, effect_fn, deps, Some(result))
}

other -> other
}
})
Expand Down Expand Up @@ -556,6 +553,19 @@ fn maybe_cleanup_and_rerun_effect(
}
}

fn find_and_update_hook(
reconciled: ReconciledElement,
hook_id: Unique,
update_fn: fn(Hook) -> Hook,
) -> ReconciledElement {
traverse_rendered_hooks(reconciled, fn(hook) {
case context.has_id(hook, hook_id) {
True -> update_fn(hook)
False -> hook
}
})
}

type HookProcessor =
fn(Hook) -> Hook

Expand Down
8 changes: 4 additions & 4 deletions test/sprocket/reconcilers/recursive_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub fn render_el(el: Element) -> ReconciledElement {
// and a placeholder ctx but then discards the ctx and returns the result.
let assert Ok(cuid_channel) = cuid.start()

let ctx =
context.new(el, cuid_channel, None, fn() { Nil }, fn(_index, _updater) {
Nil
})
let render_update = fn() { Nil }
let update_hook = fn(_index, _updater) { Nil }

let ctx = context.new(el, cuid_channel, None, render_update, update_hook)

let ReconciledResult(reconciled: reconciled, ..) =
reconcile(ctx, el, None, None)
Expand Down

0 comments on commit 6bd2652

Please sign in to comment.