vigetlabs / microcosm

Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.
http://code.viget.com/microcosm/
MIT License
487 stars 22 forks source link

Pass options to setup #163

Closed nhunzaker closed 7 years ago

nhunzaker commented 7 years ago

I'd like to start playing around with the idea of general purpose domains that could be used for a wide variety of use cases.

For example, on a lot of data visualization prototypes, we have constant values for display properties. Like:

http://code.viget.com/artifacts/09-04-2015-particles/index.html

It's pretty common to use something like DAT GUI to make it configurable. What if we made a domain library that could just do that for us?

https://workshop.chromeexperiments.com/examples/gui/#1--Basic-Usage

The usage could look something like:

repo.addDomain('settings', ConstantsDomain, [
  { name: 'fill', type: 'color', default: 'blue' },
  { name: 'stroke', type: 'color', default: 'white' },
  { name: 'friction', type: 'range', default: 0.4, min: 0, max: 1, step: 2 }
})

class ConstantsDomain extends Domain {
    setup (repo, options) {
        let onUpdate = repo.prepare(updateConstants)

        this.datgui = new DatGui(options)

        this.datgui.on('change', onUpdate)
    }

    teardown (repo, options) {
        this.datgui.remove()
    }
}

Since we have forks now, maybe these could even be additive, applying folders like this:

https://workshop.chromeexperiments.com/examples/gui/#3--Folders

Other use case: Web socket connections

I could see this being useful for web socket connections. Spin up a domain responsible for a connection, have it push actions into a repo maybe. Maybe that's a bad example, but sort of covers the use case I'd like to explore.

greypants commented 7 years ago

Yeah, that seems useful, especially the web socket use case. I think I'd use it.

nhunzaker commented 7 years ago

Probably need to make an 11.0 release for this. We have some funkiness around function arity to figure out if a Domain should be mounted at the root or at a key path.

Probably should just make it something like:

// root
repo.addDomain(<null | ''>, Domain, options)

// nested
repo.addDomain('key', Domain, options)
nhunzaker commented 7 years ago

This will land in the next version. We'll ship an 11.0.