Giter Club home page Giter Club logo

Comments (5)

ShMcK avatar ShMcK commented on May 18, 2024

Consider the following models as an example:

{
 name: 'count',
 state: 0,
 reducers: {
    addOne: (state) => state + 1,
    addBy: (state, payload) => state + payload,
  },
  /* ...hooks */
}

{
  name: 'form',
  state: {},
  reducers: {
    submit: () => {}
  }
}

Hooks API Specs

A hooks API should fit all possible combinations of things that someone might want to happen:

  • 1. form/submit triggers addOne, but only the first time, then the subscriber disappears. In "redux-saga" this is called take

  • 2. form/submit triggers addOne every time it is fired. In "redux-saga" this is called takeEvery

  • 3. form/submit can trigger addBy with the payload 5

  • 4. form/submit triggers both addOne and addBy, one as a "take" and the other as a "takeEvery"

  • 5. form/submit triggers addOne, but only if addBy is not triggered, as addBy is given precendence

  • 6. A user should be able to pattern match a hook.

take('*/submit', addOne())
take('form/*', addOne())

In the example above, all submit actions would be taken.

  • 7. A hook should not need to trigger an action in another model

  • 8. Someone may want to capture data from an action name, as a possibility

  • 9. There probably isn't a situation where both "take" and "takeEvery" are needed for the same action.

  • 10. It would be nice if a user were able to add or remove hooks dynamically at later points in time.


There may be other scenarios. @blairbodnar, let me know if you can think of any.

from rematch.

ShMcK avatar ShMcK commented on May 18, 2024

Consider this a kind of summary.

At its simplest form there are two parts. I'll call them an "Action Matcher" and "On Action" for now. But those definitely aren't good names.

Action Matcher

The action you're listening for.

Examples: form/submit, */submit, form/*

  • The action may come from any model, but we might want to prevent it from coming from the same model to stop potential loops.

On Action

The function called when the action is matched.

  • This could probably be restricted to effects or reducers only. I'm not sure at this point.

  • There should be a way to pass a custom payload to a given "effect" or "reducer" call.

  • It may also be interesting to be able to pull data from an action name, and use that in the payload. For example, form/5 may capture the number "5", and pass that into a reducer payload. Just a thought, maybe off the deep end with this one.

  • There should also be a way to differentiate between take (first only) and takeEvery (every).

from rematch.

ShMcK avatar ShMcK commented on May 18, 2024

My thoughts so far....

{
  hooks: {
    'model/actionName': /* something here */
    '*/actionName': /* something here */
  }
}

from rematch.

ShMcK avatar ShMcK commented on May 18, 2024

A possibility:

{
  hooks: {
    'model/actionName': () => take(this.AddOne()),
    '*/actionName': (action) => takeEvery(this.addBy(action.payload))
  }
}

from rematch.

ShMcK avatar ShMcK commented on May 18, 2024

How matchers might work:

  • 1) look for an exact match
  • 2) look for a pattern match

Matchers would have to be performant to make it work well. Pattern matching a lot of hooks may create performance issues on every action triggered.

from rematch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.