From d81b1986b67ccf8024deeb3d1c223452116fdb4d Mon Sep 17 00:00:00 2001 From: Anton Bachin Date: Mon, 13 Dec 2021 17:51:48 +0300 Subject: [PATCH] Delete per-server "global" variables as a concept --- example/4-counter/README.md | 8 -------- src/dream.mli | 15 +-------------- src/http/error_handler.ml | 20 ++++++++------------ src/pure/dream_pure.mli | 8 -------- src/pure/inmost.ml | 24 ------------------------ 5 files changed, 9 insertions(+), 66 deletions(-) diff --git a/example/4-counter/README.md b/example/4-counter/README.md index f65f3f3d..f976ed66 100644 --- a/example/4-counter/README.md +++ b/example/4-counter/README.md @@ -42,14 +42,6 @@ promise with [Lwt](https://github.com/ocsigen/lwt#readme), the promise library used by Dream. The next example, [**`5-promise`**](../5-promise#files), does exactly that! -
**Next steps:** diff --git a/src/dream.mli b/src/dream.mli index 424caf49..a23c5302 100644 --- a/src/dream.mli +++ b/src/dream.mli @@ -2290,14 +2290,11 @@ val decrypt : (** {1 Variables} - Dream provides two variable scopes for use by middlewares. *) + Dream supports user-defined per-message variables for use by middlewares. *) type 'a local (** Per-message variable. *) -type 'a global -(** Per-server variable. *) - val new_local : ?name:string -> ?show_value:('a -> string) -> unit -> 'a local (** Declares a variable of type ['a] in all messages. The variable is initially unset in each message. The optional [~name] and [~show_value] are used by @@ -2309,16 +2306,6 @@ val local : 'a local -> 'b message -> 'a option val with_local : 'a local -> 'a -> 'b message -> 'b message (** Sets the per-message variable to the value. *) -val new_global : - ?name:string -> ?show_value:('a -> string) -> (unit -> 'a) -> 'a global -(** Declares a variable of type ['a] in all servers. The first time the variable - is accessed, the given initializer function is called to get its value. - Global variables cannot be changed. So, they are typically refs or other - mutable data structures, such as hash tables. *) - -val global : 'a global -> request -> 'a -(** Retrieves the value of the per-server variable. *) - (** {1 Testing} *) diff --git a/src/http/error_handler.ml b/src/http/error_handler.ml index d6a4cd0e..04178541 100644 --- a/src/http/error_handler.ml +++ b/src/http/error_handler.ml @@ -94,18 +94,14 @@ let dump (error : Dream.error) = Dream.all_headers last |> List.iter (fun (name, value) -> p "\n%s: %s" name value); - let show_variables kind = - kind (fun name value first -> - if first then - p "\n"; - p "\n%s: %s" name value; - false) - true - request - |> ignore - in - show_variables Dream.fold_locals; - show_variables Dream.fold_globals + Dream.fold_locals (fun name value first -> + if first then + p "\n"; + p "\n%s: %s" name value; + false) + true + request + |> ignore end; Buffer.contents buffer diff --git a/src/pure/dream_pure.mli b/src/pure/dream_pure.mli index 95ec6e1e..ed8fbcb0 100644 --- a/src/pure/dream_pure.mli +++ b/src/pure/dream_pure.mli @@ -453,14 +453,6 @@ val local : 'a local -> 'b message -> 'a option val with_local : 'a local -> 'a -> 'b message -> 'b message val fold_locals : (string -> string -> 'a -> 'a) -> 'a -> 'b message -> 'a -type 'a global -val new_global : - ?name:string -> ?show_value:('a -> string) -> (unit -> 'a) -> 'a global -val global : 'a global -> request -> 'a -(* TODO Get rid of globals completely as a concept, once the site_prefix - middleware is clarified. *) -val fold_globals : (string -> string -> 'a -> 'a) -> 'a -> request -> 'a - (* TODO Delete once requests are mutable. *) diff --git a/src/pure/inmost.ml b/src/pure/inmost.ml index 4e712a7c..57e5dc07 100644 --- a/src/pure/inmost.ml +++ b/src/pure/inmost.ml @@ -70,7 +70,6 @@ and server = { } and app = { - globals : Scope.t ref; mutable app_debug : bool; mutable https : bool; mutable secrets : string list; @@ -142,7 +141,6 @@ let site_prefix request = request.specific.app.site_prefix let new_app error_handler site_prefix = { - globals = ref Scope.empty; app_debug = false; https = false; secrets = []; @@ -427,28 +425,6 @@ let with_local key value message = let fold_locals f initial message = fold_scope f initial message.locals -type 'a global = { - key : 'a Scope.key; - initializer_ : unit -> 'a; -} - -let new_global ?name ?show_value initializer_ = { - key = Scope.Key.create (name, show_value); - initializer_; -} - -let global {key; initializer_} request = - match Scope.find key !(request.specific.app.globals) with - | Some value -> value - | None -> - let value = initializer_ () in - request.specific.app.globals := - Scope.add key value !(request.specific.app.globals); - value - -let fold_globals f initial request = - fold_scope f initial !(request.specific.app.globals) - let app request = request.specific.app