algodynamics-iiith / core / Core

This library tries to address the following issues in building 1. Gives default styles for all elements in an interactive virtual CS experiment. 2. Gives readymade history support with as less code rewriting as possible using elm-community/undo-redo 3. Extends the functionality of undo-redo package by adding support for commands and subscriptions 4. A ready to use logger via ports which logs all states

Wrapper Functions

init : (state -> loggableState) -> (Json.Encode.Value -> Platform.Cmd.Cmd (UndoList.Msg msg)) -> (flags -> ( state, Platform.Cmd.Cmd msg )) -> flags -> ( UndoList state, Platform.Cmd.Cmd (UndoList.Msg msg) )

Init is a wrapper function that maps the original application's init function to new types It also logs the init action

view : (state -> Html msg) -> UndoList state -> Html (UndoList.Msg msg)

View is the wrapper function that maps the original application's view function to new type. It also adds the default undo/redo/reset control buttons to the UI.

update : (state -> loggableState) -> (Json.Encode.Value -> Platform.Cmd.Cmd (UndoList.Msg msg)) -> (msg -> state -> ( state, Platform.Cmd.Cmd msg )) -> (msg -> Basics.Bool) -> Maybe Basics.Int -> Maybe (UndoListCmds msg) -> UndoList.Msg msg -> UndoList state -> ( UndoList state, Platform.Cmd.Cmd (UndoList.Msg msg) )

Update is a wrapper function which adds undo,redo,reset to an updater. This function also logs the action and the newState via sharing it over the port. The function takes logger, port for analytics, original update function, freshStateSetter, undoCount, undoListCommands. The function returns a new partial function which replaces the original update in your application's main function

The details about the arguements 1. logger : maps the program state to a loggable output. For no mapping identity function can be used 2. analyticsPort : the port through which the telemetry data needs to be shared 3. orginal update function : Handlles the updates specific to your application 4. freshStateSetter : A function that sets a fresh state (see undo-redo package) if the result is true. 5. undoCount : Handles cases where a number of steps need to be undoed (useful when the messages are chained) 6. undoListCommands : Tuple of commands which will be executed when undo/redo/reset is encountered

NOTE : Last three parameters are of Maybe type so you can pass Nothing if you don't wish to use them.

subscriptions : (state -> Platform.Sub.Sub msg) -> UndoList state -> Platform.Sub.Sub (UndoList.Msg msg)

Subscriptions is a wrapper function that maps the original application's subscriptions to the new types.

Example

A very basic example which should suit most basic cases

-- import the package into your app and update the main function with the following
Browser.element
{ init = Core.init identity analyticsPort init
, view = Core.view view
, update = Core.update identity analyticsPort update setFresh Nothing Nothing
, subscriptions = Core.subscriptions subscriptions
}

For using advanced features visit the Readme.md file.

Limitations

  1. The library does not support Browser.Document and Browser.Application since it was not required in the present usecase.