DEV Community

artydev
artydev

Posted on

5

Frontend : If you !(love(Mithril)) then what else ?

Perhaps Mithril does not fit your brain,pitty😊, and you are in search of a very lightweight frontend, I suggest you to give an eye on Sinuous

In the following example, I have tried to implement the Meiosis pattern with Sinuous and Mergerino.

I must say I am really impressed by Sinuous

If you want to test its "lightness" look here : sinuousmeiosis


const o = sinuous.o  // observable :-)
const html = sinuous.html
const map = sinuous.map
const merge = mergerino  //enhanced Object.assign

// Here we observe an array, I have tried to observe the
// object directely with no success
// So we have to "map" on state to retrieve the object
// Look at the App function below
let state = o([
  {
     nom: "Albert",
     age: 58,
     points: 100
  }
]);

// Little helper
state.merge = function (patch) {
  //not very friendly but handy
  //'this' refers to 'state'
  let current = this()[0] 
  let newstate =  merge(current, patch)
  state([newstate])
}

// mergerino accepts functions for updating properties
// very handy
let actions  = {
  incPoints : () => state.merge({
    points : p => p + 1
  }),
  incAge : () =>  state.merge({
    age: (a) => a + 1
  })
}

// The view is a function of the state.
// All the actions are centralized in a unique object
function view(s) {
  return  html`
    <div>
      Hello <strong>${s.nom}</strong>, 
      you are <strong>${s.age}</strong> years old and have
      <strong>${s.points}</strong> points
    </div>
    <p>
      <button onclick=${actions.incPoints}>Inc Points</button>
      <button onclick=${actions.incAge}>Inc Age</button>
    </p>
    `
}

const App = function () {
  return html`${map(state, view)}`
}

document.querySelector('.app')
        .append(App());

You can test it here sinuous

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay