FiberJW/stature

API Discussion

FiberJW opened this issue · 7 comments

This implementation of "Context" doesn't completely rely on the pattern. It doesn't pass the state the same way Context does. This creates more of a store and manages state in that store instead of in the component tree, and so, Provider can be removed from the equation completely. By doing so, I'd like to make the API more centered around "Stores" instead of "Contexts", as context doesn't really apply technically. It will still use the same implementation as currently exists, but the API would become a lot slimmer and possibly simpler to reason about ;)

I'm thinking of something like this:

module CounterConfig = {
  type t = int;
  let debugName = "Counter";
  let value = 0;
};

module CounterStore = Stature.CreateStore(CounterConfig);


// accessing state in ui
<CounterStore.Spectator
  render=(
    state =>
      <Text style=Styles.status>
        (ReasonReact.stringToElement(string_of_int(state)))
      </Text>
  )
/>

Store.Spectator could be Store.WithState, Store.Observer, Store.Consumer, etc. It's just a matter of choice

The Provider wouldn't be needed because the default value is already set when creating the context/store.

Makes totally sense @FiberJW.
I was using the Provider just for the sake of using it in the router react element.
Indeed the default state is already passed on the configuration created of context

  render: self =>
    self.state.status == `Loading ?
      <Loading /> :
      <CurrentUserContext.Provider
        value=self.state.user
        render=(
          () =>
            <Layout>
              <Router.Container>
                ...(
                     (~currentRoute) =>
                       switch (currentRoute) {
                       | RouterConfig.Home => <Home />
                       ...
                       }
                   )
              </Router.Container>
            </Layout>
        )
      />,

Consumer is a nice name I think 🤔 . But your call!
Btw wtf is that logo? hahaha

Also how can I help here?
Maybe some docs?
We have been using reason/reason react template to do documentation:
https://astrocoders.com/reform/

Yeah, I think that Consumer is a solid name. I'll update the API to remove Provider, and if you could test it out and make sure that it works by installing from github, then i'll get to writing up a readme and cleaning it up to push to NPM!

For docs, I don't think that a huge website is necessary. It's a tiny library with an almost non-existent API, so a Readme will be just fine

@guilhermedecampo I published it to NPM and added a bit to the readme. Will do logo work later. Let me know if i should change anything about the API or any suggestions. happy coding!