Skip to content

Commit

Permalink
Delete per-server "global" variables as a concept
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Dec 13, 2021
1 parent bb2924d commit d81b198
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 66 deletions.
8 changes: 0 additions & 8 deletions example/4-counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

<!-- TODO
<br>
Advanced example [**`w-globals`**](../w-globals/#files) shows how to replace
global state like `count` by state scoped to the application. This is useful if
you are writing middleware to publish in a library. It's fine to use a global
`ref` in private code!
-->
<br>

**Next steps:**
Expand Down
15 changes: 1 addition & 14 deletions src/dream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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} *)
Expand Down
20 changes: 8 additions & 12 deletions src/http/error_handler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/pure/dream_pure.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down
24 changes: 0 additions & 24 deletions src/pure/inmost.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ and server = {
}

and app = {
globals : Scope.t ref;
mutable app_debug : bool;
mutable https : bool;
mutable secrets : string list;
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d81b198

Please sign in to comment.