Skip to content

Instantly share code, notes, and snippets.

@newyankeecodeshop
Last active February 6, 2018 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newyankeecodeshop/7a014da8127a8db700d13d8dc681f87a to your computer and use it in GitHub Desktop.
Save newyankeecodeshop/7a014da8127a8db700d13d8dc681f87a to your computer and use it in GitHub Desktop.
A GongFu updater pattern
function update1(msg, model, appContext) {
// This approach uses effects to implement application logic to do other UI actions.
switch (msg.type) {
case "ButtonPressed":
if (userCanDo(msg)) {
return {
model: assocTyped("doo", doo, model),
effect: Effect(DoSomething(msg.what))
};
else
return { model, effect: Effect(ShowAnotherCrazyModal()) };
break;
case "DoSomething":
...
break;
case "ShowAnotherCrazyModal":
...
break;
}
}
function update2(msg, model, appContext) {
// This approach calls the update function directly with the appropriate message.
switch (msg.type) {
case "ButtonPressed":
if (userCanDo(msg)) {
model = assocTyped("doo", doo, model);
return update2(DoSomething(msg.what), model, appContext);
else
return update2(ShowAnotherCrazyModal());
break;
case "DoSomething":
...
break;
case "ShowAnotherCrazyModal":
...
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment