byte-fe / react-model

The next generation state management library for React
236 stars 23 forks source link

Enhance the subscribe api #86

Open ArrayZoneYour opened 5 years ago

ArrayZoneYour commented 5 years ago

I have a:

  1. [x] Idea:

    • [x] What problem would it solve for you? 🐛 Developer can subscribe the state changes. v2.x api

      // subscribe actions
      // (storeName, actionList, callback) => void
      subscribe('Counter', ['increment'], () => (count += 1))
      // subscribe properties changes
      // (storeName, mapStateFunction, callback) => void
      subscribe('Counter', state => state.count, () => alert('counter count change!'))

      v3.x api

      const store = Store({
        // ...
       actions: {
         updateA: (s, payload) => { ... },
         updateC: (s, payload) => { ... },
         updateD: (s, payload) => { ... }
       }
      })
      // subscribe actions
      store.subscribe(['updateA'], () => (count += 1))
      // subscribe properties changes
      store.subscribe(state => state.A, () => alert('A change!'))
    • [x] Do you think others will benefit from this change as well and it should in core package? 💡 Yes

    • [x] Are you willing to (attempt) a PR yourself? ⚔ Yes