From c0c82a7bfe8300b1bd50fee11074837ff32d3da0 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 7 Feb 2022 13:40:36 +0100 Subject: Renamed fonctions in application framework, updated doc --- css/merger.ml | 6 +-- lib/application/application.ml | 82 ++++++++--------------------------------- lib/application/application.mli | 64 ++++++++++++++++++++++++++++++++ script.it/script.ml | 9 +---- 4 files changed, 84 insertions(+), 77 deletions(-) create mode 100644 lib/application/application.mli diff --git a/css/merger.ml b/css/merger.ml index 47fa906..ea6efe2 100755 --- a/css/merger.ml +++ b/css/merger.ml @@ -108,8 +108,7 @@ let file_list Ev.listen Ev.click (fun _ -> sender ( - App.E( f.file - , (module DelFile : App.Event with type t = DelFile.t)))) + App.dispatch (module DelFile) f.file)) (El.as_target button); match f.css with @@ -215,8 +214,7 @@ let main id = with | _ -> None in - App.E ( { file ; css } - , (module AddFile: App.Event with type t = AddFile.t ))) + App.dispatch (module AddFile) { file ; css}) file_event in let state = diff --git a/lib/application/application.ml b/lib/application/application.ml index 422aa4f..b6ece93 100755 --- a/lib/application/application.ml +++ b/lib/application/application.ml @@ -1,73 +1,23 @@ -(** The Make module build the main application loop.contents - - The function [run] update the state on each event, and return a new state. - Each event must follow the [event] type, which is composed from the type - [t], and a module with a fonction [update]. - - This example create an application with the state containing a simple - counter. An even which increment this counter is created and can be used to - update the state. - - - [ - type state = { value : int } - - (** Increment the state. *) - module Incr = struct - type t = unit - - let update () state = { value = state.value + 1 } - end - - (** Decrement the state. *) - module Incr = struct - type t = unit - - let update () state = { value = state.value - 1 } - end - - module App = Make(struct type t = state end) - - (* Create the events *) - let incr_event = App.E ((), (module Incr:App.Event with type t = Incr.t)) - let decr_event = App.E ((), (module Decr:App.Event with type t = Decr.t)) - - let init = { value = 0 } in - - (* Run the main loop *) - let state = App.run - init - (E.select - [ incr_event - ; decr_event ] ) in … - ] - -*) -module Make(S:sig type t end) = struct - module type Event = sig - +module Make (S : sig + type t +end) = +struct + module type Procesor = sig type t - val process: t -> S.t -> S.t - + val process : t -> S.t -> S.t end - type event = E : 'a * (module Event with type t = 'a) -> event + type event = E : 'a * (module Procesor with type t = 'a) -> event (** Simple helper for the main event loop *) - let run - : ?eq:(S.t -> S.t -> bool) -> S.t -> event Note.E.t -> S.t Note.S.t - = fun ?eq init event -> - let action = Note.E.map (fun (E (t, (module Event))) st -> Event.process t st) event in - Note.S.accum ?eq init action - - let dispatch - : (module Event with type t = 's) -> 's -> event - = fun (type s) (module M: Event with type t = s) v -> - E - ( v - , (module M : Event with type t = M.t )) - + let run : ?eq:(S.t -> S.t -> bool) -> S.t -> event Note.E.t -> S.t Note.S.t = + fun ?eq init event -> + let action = + Note.E.map (fun (E (t, (module P))) st -> P.process t st) event + in + Note.S.accum ?eq init action + + let dispatch : (module Procesor with type t = 's) -> 's -> event = + fun (type s) (module P : Procesor with type t = s) v -> E (v, (module P)) end - - diff --git a/lib/application/application.mli b/lib/application/application.mli new file mode 100644 index 0000000..5bf0556 --- /dev/null +++ b/lib/application/application.mli @@ -0,0 +1,64 @@ +(** The Make module build the main application loop.contents + + The function [run] update the state on each event, and return a new state. + Each event must follow the [event] type, which is composed from the type + [t], and a module with a fonction [update]. + + This example create an application with the state containing a simple + counter. An even which increment this counter is created and can be used to + update the state. + + + [ + type state = { value : int } + + (** Increment the state. *) + module Incr = struct + type t = unit + + let process () state = { value = state.value + 1 } + end + + (** Decrement the state. *) + module Incr = struct + type t = unit + + let process () state = { value = state.value - 1 } + end + + module App = Make(struct type t = state end) + + (* Create the event processor *) + let incr_event = App.dispatch (module Incr) () + and decr_event = App.dispatch (module Decr) () in + + let init = { value = 0 } in + + (* Run the main loop *) + let state = App.run + init + (E.select + [ incr_event + ; decr_event ] ) in … + ] + +*) +module Make (S : sig + type t +end) : sig + module type Procesor = sig + type t + + val process : t -> S.t -> S.t + end + + type event + + val dispatch : (module Procesor with type t = 's) -> 's -> event + (** [dispatch (module P) v] will create an event holding a value [v] and + associated with the processor [P] *) + + val run : ?eq:(S.t -> S.t -> bool) -> S.t -> event Note.E.t -> S.t Note.S.t + (** The function [run state ev] will create a signal continually updated + each time the event [ev] occur *) +end diff --git a/script.it/script.ml b/script.it/script.ml index 78a45b3..eb12458 100755 --- a/script.it/script.ml +++ b/script.it/script.ml @@ -171,10 +171,7 @@ let set_sidebar let process t state = { state with State.rendering = t } end in - - State.E - ( render_type - , (module M: State.Event with type t = Layer.Paths.printer )) + State.dispatch (module M) render_type ) rendering' in @@ -440,9 +437,7 @@ let page_main id = (fun pos f -> let module Tick = Script_event.Tick in Option.map (fun p -> - State.E - ( (f, p) - , (module Tick: State.Event with type t = Tick.t ))) + State.dispatch (module Tick) (f, p)) pos ) in (* The first evaluation is the state. Which is the result of all the -- cgit v1.2.3