Commit ee163e84 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Adding new application framework

parent 516eb216937b
......@@ -29,14 +29,14 @@ type viewKind =
(* A specification *)
type viewSpecification = {
(* The view's name *)
(* The location and thus identifier of this view *)
location: string;
(* The view's human readable name *)
name: string;
(* The kind of view *)
kind: viewKind;
(* The priority given to the view *)
priority: int;
(* The location of the view's definition *)
definition: string;
}
(* A piece of fetched data *)
......@@ -48,9 +48,6 @@ type fetchedData =
(* A fetched collection of view specifications *)
| FetchedSpecifications of viewSpecification list
(* An resource to be rendered *)
type resource = {
(* The path to the resource *)
......@@ -61,36 +58,32 @@ type resource = {
data: Hypermedia.entityValue option;
}
(* Callback when a link is being activated *)
type linker = string -> unit
(* Type of a rendering function *)
type ('state, 'initialState, 'retainedProps, 'initialRetainedProps, 'action) renderer =
('state, 'retainedProps, 'action) ReasonReact.self
-> linker
-> (resource list)
-> ('state, 'initialState, 'retainedProps, 'initialRetainedProps, 'action) ReasonReact.componentSpec
(* Type of a rendering function *)
type renderer =
(resource list)
-> appState
-> ReasonReact.reactElement
(* The definition of a view *)
type ('state, 'initialState, 'retainedProps, 'initialRetainedProps, 'action) view = {
and view = {
(* The specification of this view *)
specfication: viewSpecification;
specification: viewSpecification;
(* The view rendering function *)
renderer: ('state, 'initialState, 'retainedProps, 'initialRetainedProps, 'action) renderer;
renderer: renderer;
}
(* A registry of views *)
type 'a viewRegistry = {
and viewRegistry = {
(* The known specifications *)
specifications: viewSpecification list;
specifications: viewSpecification StringMap.t;
(* The known definitions *)
definitions: 'a list;
definitions: view StringMap.t;
}
(* The state of an app while loading *)
type 'a appStateLoading = {
and appStateLoading = {
(* the location of the current resource *)
location: string;
(* The already loaded resources *)
......@@ -98,29 +91,29 @@ type 'a appStateLoading = {
(* The data remaining to be fetched *)
requested: requestedData list;
(* The view registry for the app *)
registry: 'a viewRegistry;
registry: viewRegistry;
}
(* The state of an app while loading *)
type 'a appStateLoaded = {
and appStateLoaded = {
(* the location of the current resource *)
location: string;
(* The already loaded resources *)
resources: resource StringMap.t;
(* The view registry for the app *)
registry: 'a viewRegistry;
registry: viewRegistry;
}
(* The possible states of an app *)
type 'a appState =
and appState =
(* The app failed with a message *)
| Failed of string * 'a viewRegistry
| Failed of string * viewRegistry
(* Initial state *)
| Init
| Init of viewRegistry
(* The app is loading *)
| Loading of 'a appStateLoading
| Loading of appStateLoading
(* The completed state of an app *)
| Ready of 'a appStateLoaded
| Ready of appStateLoaded
(* The possible actions of an app *)
type appAction =
......@@ -152,8 +145,14 @@ let fold_into fetchedData (resources: resource StringMap.t) =
StringMap.add target {path=target; schema=None; data=Some(data)} resources
| _ -> resources
(* Register a new view into a registry *)
let register_view (registry: viewRegistry) (view: view) = {
specifications = StringMap.add view.specification.location view.specification registry.specifications;
definitions = StringMap.add view.specification.location view registry.definitions;
}
(* Get the initial state for the application *)
let initial_state () = Init
let initial_state viewRegistry = Init(viewRegistry)
(* Apply the reduction on a GoTo action *)
let reduce_on_goto state target = match state with
......@@ -174,14 +173,11 @@ let reduce_on_goto state target = match state with
requested = [RequestedSchema(target)];
registry = registry;
}))
| Init -> ReasonReact.Update(Loading({
| Init(registry) -> ReasonReact.Update(Loading({
location = target;
loaded = StringMap.empty;
requested = [RequestedSchema(target)];
registry = {
specifications =[];
definitions = [];
};
registry = registry;
}))
(* Apply the reduction on a Request action *)
......@@ -208,7 +204,7 @@ let reduce_on_requested state requestedData = match state with
registry = registry;
}))
| Failed(_, _)
| Init-> ReasonReact.NoUpdate
| Init(_) -> ReasonReact.NoUpdate
let reduce_on_request_launched state _ = match state with
| Loading({
......@@ -224,7 +220,7 @@ let reduce_on_request_launched state _ = match state with
}))
| Ready(_)
| Failed(_, _)
| Init-> ReasonReact.NoUpdate
| Init(_) -> ReasonReact.NoUpdate
(* Apply the reduction on a Loading Failed action *)
let reduce_on_failed state requestedData =
......@@ -242,16 +238,13 @@ let reduce_on_failed state requestedData =
registry = registry;
})
| Failed(_, registry)-> ReasonReact.Update(Failed("Failed to fetch " ^ what ^ " at " ^ target, registry))
| Init -> ReasonReact.Update(Failed("Failed to fetch " ^ what ^ " at " ^ target, {
specifications = [];
definitions = [];
}))
| Init(registry) -> ReasonReact.Update(Failed("Failed to fetch " ^ what ^ " at " ^ target, registry))
(* Apply the reduction on a Loaded Data action *)
let reduce_on_loaded state fetchedData =
match state with
| Failed(_, _)
| Init -> ReasonReact.NoUpdate
| Init(_) -> ReasonReact.NoUpdate
| Loading({
location = location;
loaded = loaded;
......@@ -274,13 +267,6 @@ let reduce action state = match action with
| LoadingFailed(requestedData) -> reduce_on_failed state requestedData
| LoadingCompleted(fetchedData) -> reduce_on_loaded state fetchedData
(* When the app moved to a new target resource *)
let on_moved_to (self: ('a, 'b, 'c) ReasonReact.self) target =
Hypermedia.Fetch.fetch_shema
target
(fun () -> self.send(LoadingFailed(RequestedSchema(target))))
(fun data -> self.send(LoadingCompleted(FetchedSchema(target, data))))
(* Launched a request for some data *)
let do_fetch requestedData (self: (_, _, _) ReasonReact.self) = match requestedData with
| RequestedData(target) -> begin
......@@ -312,4 +298,25 @@ let on_updated (oldNewSelf: (_, _, _) ReasonReact.oldNewSelf) =
do_fetch (List.hd requested) oldNewSelf.newSelf;
()
end
| _ -> ()
\ No newline at end of file
| _ -> ()
(* Follow a link *)
let follow_link link =
ReasonReact.Router.push link
(* Module for the default views *)
(*
module Defaults = struct
let defaultViewRoot = {
specification = {
location = "::defaults::root";
name = "Default root view";
kind = RootView;
priority = 0;
};
renderer = Defaults_rendering.renderRoot;
}
end
*)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment