diff --git a/src/baselib/ocsigen_loader.ml b/src/baselib/ocsigen_loader.ml index 2714ccc89..a0df48fc0 100644 --- a/src/baselib/ocsigen_loader.ml +++ b/src/baselib/ocsigen_loader.ml @@ -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 *) diff --git a/src/baselib/ocsigen_loader.mli b/src/baselib/ocsigen_loader.mli index 976c6c843..1d4a7bf0a 100644 --- a/src/baselib/ocsigen_loader.mli +++ b/src/baselib/ocsigen_loader.mli @@ -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 diff --git a/src/server/ocsigen_server.mli b/src/server/ocsigen_server.mli index 89ae13806..c57e04a9d 100644 --- a/src/server/ocsigen_server.mli +++ b/src/server/ocsigen_server.mli @@ -58,6 +58,6 @@ module Site : sig configuration file options defined by extensions ( ...)*) 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