Skip to content

Commit

Permalink
Add Ocsigen_loader.add_module_init_function
Browse files Browse the repository at this point in the history
  • Loading branch information
balat committed May 9, 2024
1 parent 78d1825 commit be06ab6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
48 changes: 28 additions & 20 deletions src/baselib/ocsigen_loader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,39 @@ let loadfiles pre post force modules =
aux modules

let set_module_init_function name f =
init_functions := M.add name f !init_functions;
(* print_endline ("Added init_function for " ^ name); *)
(* print_endline ("get_init_on_load: " ^ string_of_bool (get_init_on_load ())); *)
init_functions := M.add name [f] !init_functions;
if get_init_on_load () then f ()

let add_module_init_function name f =
let update = function
| None (* no binding so far *) -> Some [f]
| Some old -> Some (f :: old)
in
init_functions := M.update name update !init_functions;
if get_init_on_load () then f ()

let init_module pre post force name =
let f =
try M.find name !init_functions
with Not_found as e -> raise (Dynlink_error ("named module " ^ name, e))
try
let l = List.rev @@ M.find name !init_functions in
fun () -> List.iter (fun f -> f ()) l
with Not_found ->
Lwt_log.ign_info_f ~section "No init function for named module %s." name;
fun () -> ()
in
try
if force
then (
pre ();
Lwt_log.ign_info_f ~section
"Initializing %s (will be initialized every time)" name;
try f (); post () with e -> post (); raise e)
else if not (isloaded name)
then (
pre ();
Lwt_log.ign_info_f ~section "Initializing module %s " name;
(try f (); post () with e -> post (); raise e);
addloaded name)
else Lwt_log.ign_info_f ~section "Module %s already initialized." name
with e -> raise (Dynlink_error (name, e))
if force
then (
pre ();
Lwt_log.ign_info_f ~section
"Initializing %s (will be initialized every time)" name;
try f (); post () with e -> post (); raise e)
else if not (isloaded name)
then (
pre ();
Lwt_log.ign_info_f ~section "Initializing module %s " name;
(try f (); post () with e -> post (); raise e);
addloaded name)
else Lwt_log.ign_info_f ~section "Module %s already initialized." name

(************************************************************************)
(* Manipulating Findlib's search path *)
Expand Down
8 changes: 7 additions & 1 deletion src/baselib/ocsigen_loader.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ val loadfiles : (unit -> unit) -> (unit -> unit) -> bool -> string list -> unit
but the last one, and [loadfile pre post force] for the last one
(if any). *)

val add_module_init_function : string -> (unit -> unit) -> unit
(** [add_module_init_function name f] adds function [f]
to the initialisation functions to be run
when [init_module name] is called. *)

val set_module_init_function : string -> (unit -> unit) -> unit
(** [set_module_init_function name f] registers the function [f], which will
be used to initialize the module when [init_module name] is called. *)
be used to initialize the module when [init_module name] is called.
Will replace the prvious value. *)

val init_module : (unit -> unit) -> (unit -> unit) -> bool -> string -> unit
(** [init_module pre post force name] runs the init function for the module
Expand Down
2 changes: 1 addition & 1 deletion src/server/ocsigen_server.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ module Site : sig
configuration file options defined by extensions (<staticmod/> ...)*)

val register : ?site:t -> instruction -> unit
(** [register t s e] registers instruction [e] to be run inside site [s].
(** [register ~site:s e] registers instruction [e] to be run inside site [s].
Use this if you want to create an extension yourself. *)
end

0 comments on commit be06ab6

Please sign in to comment.