alloc / saus

Vite SSR/SSG framework that aspires to be a layer for opinionated web frameworks to build upon
Other
38 stars 1 forks source link

Make state modules more ergonomic #52

Open aleclarson opened 2 years ago

aleclarson commented 2 years ago

Currently, state modules typically look like normal variables (lowercased), and you use their .get or .load methods as appropriate. When unwrapping a state module into its raw value, it can be awkward to decide on what the unwrapped value's variable will be named. This proposal is a possible solution.

import { defineStateModule } from 'saus/client'

export const [getFoo, loadFoo] = defineStateModule(
  'foo',
  async function() {
    // TODO: Load the foo
  }
)

With this pattern, the awkwardness is avoided:

import { getFoo, loadFoo } from '../state/foo'

let foo = getFoo(1, 2, 3)

foo = await loadFoo(3, 4, 5)
aleclarson commented 2 years ago

This proposal could be supported in a backwards compatible manner by simply defining 0 and 1 on the StateModule object, in addition to the pre-existing get and load methods.