Git Product home page Git Product logo

Comments (21)

fabienjuif avatar fabienjuif commented on May 24, 2024 3

We can then imagine that for all cases we have to (or can) decorate our reactions:
todos.js

import { reaction } from 'k-simple-state'
 
export const complete = reaction((action, store) => {
  const { todos } = store.data 
  const todo = todos.get(action.payload)
  todos.set({ ...todo, completed: true })
})

listeners.js

import { when } from 'k-simple-state'
import { complete } from './todos'

export default [
  complete.when('@@ui/CLICKED_COMPLETE'),
  // or
  when('@@ui/CLICKED_COMPLETE')(complete),
]

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024 1

yes it can:

// todos.js
import { takeWith } from 'k-simple-state'
 
export const completeWhen = takeWith(action, store) => {
  const { todos } = store.data 
  const todo = todos.get(action.payload)
  todos.set({ ...todos, completed: true })
}

// listeners.js
import { completeWhen } from './todos' 

export default [
  // here it seems odd to me to call a fonction 'complete' passing an action type :)
  completeWhen('@@ui/CLICKED_COMPLETE'),
]

?

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024 1

this is more completeWhen(latest('@@ui/CLICKED_COMPLETE')) ?
Because when I read latest(completeWhen('@@ui/CLICKED_COMPLETE')) I understand this the latest reaction to happen and triggered.

What we want is the latest action not the reaction (this is to late)

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

It's remind me event-based vocabulary :

  • take means on(event) handle reactions
  • takeWith means handle(reactions) on event

so it will be :

 // take
 on(filters)(reaction)

 // takeWith
 handle(reaction)(filters)

And your example can be written like that :

// todos.js
import { handle } from 'k-simple-state'
 
export const handleComplete = handle((action, store) => {
  const { todos } = store.data 
  const todo = todos.get(action.payload)
  todos.set({ ...todos, completed: true })
})

// listeners.js
import { handleComplete } from './todos' 

export default [
  handleComplete('@@ui/CLICKED_COMPLETE'),
]

And this is an common vocabulary for listeners

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

We can also use when instead of on :

 // take
 when(filters)(reaction)

 // takeWith
 handle(reaction)(filters)

from k-ramel.

EmrysMyrddin avatar EmrysMyrddin commented on May 24, 2024

The issue here is more the name of the generated function, not the API.
Here handleComplete doesn't represent well the fact that it's a reaction waiting for filters. With this name, I expect a function that can handle the Complete action.

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

Ok, my bad...

So, your looking for something like that :

// todos.js
import { takeWith } from 'k-simple-state'
 
export const complete = takeWith((action, store) => {
  const { todos } = store.data 
  const todo = todos.get(action.payload)
  todos.set({ ...todos, completed: true })
})

// listeners.js
import { complete } from './todos' 

export default [
  complete.on('@@ui/CLICKED_COMPLETE'),
]

from k-ramel.

EmrysMyrddin avatar EmrysMyrddin commented on May 24, 2024

Yes, perhaps it's a good way to explore.
For complete.on, I have the expectation to handle an envent on the complete object πŸ˜„
Perhaps with another word ? complete.if() for example ?

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024
complete.when('@@ui/CLICKED_COMPLETE')

from k-ramel.

EmrysMyrddin avatar EmrysMyrddin commented on May 24, 2024
complete.match('@@ui/CLICKED_COMPLETE')

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024

I prefer the complete.when() this is more fluent I think

from k-ramel.

EmrysMyrddin avatar EmrysMyrddin commented on May 24, 2024

At this time, compleWhen can be ok isn't it ?

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024

I am waiting for @bpetetot POV

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

I'm agree with the wording : when

But I'm not really convinced by the naming convention instead of a DSL.

A DSL could be more extensible :

  complete.when('@@ui/CLICKED_COMPLETE')
  complete.whenLatest('@@ui/CLICKED_COMPLETE')
  complete.debounce('@@ui/CLICKED_COMPLETE', 500)
  ...

But it could be more complicated to implement.

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024

I don't think this more complicated to implement.
We can even write both API I think.

complete('@@ui/CLICKED_COMPLETE') // or rename the function completeWhen
complete.when('@@ui/CLICKED_COMPLETE')

from k-ramel.

EmrysMyrddin avatar EmrysMyrddin commented on May 24, 2024

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024

@EmrysMyrddin the problem with this kind of approach is the number of different exports, and maybe collisions

Or maybe we can have:

import { reaction } from 'k-simple-state'

const { latest, take /* or when */, etc } = reaction

// ...

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

So in summary, maybe something more like that ?

import { latest, take, takeWith } from 'k-simple-state/reactions'

// take version
const onComplete = latest(take('@@ui/CLICKED_COMPLETE'))
onComplete(() => /* do something */)

// takeWith version
const completeWhen = takeWith(() => /* do something */)
latest(completeWhen('@@ui/CLICKED_COMPLETE'))

is it easily understandable when you read the code ?

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

Maybe more understandable with composition :

// take version
const onComplete = compose(latest, take)('@@ui/CLICKED_COMPLETE')
onComplete(() => /* do something */)

// takeWith version
const completeWhen = takeWith(() => /* do something */)
compose(completeWhen, latest)('@@ui/CLICKED_COMPLETE')

from k-ramel.

fabienjuif avatar fabienjuif commented on May 24, 2024

If we use composition of fonctions maybe we have to rename APIs as you suggested before @bpetetot.

  • takeWith -> reaction
  • take -> when
const complete = (action, store) => { /* do something */}

// debounce events and call `complete`
compose(
  when('@@ui/CLICKED_COMPLETE')
  debounce, 
)(complete)

// same thing different approach
const debouncedComplete = debounce(complete)
when('@@ui/CLICKED_COMPLETE')(debouncedComplete)
when('@@ui/OTHER_CLICKED')(debouncedComplete)

// calls `complete` on every events
when('@@ui/CLICKED_COMPLETE')(complete)

I think we agree BUT take care this is not the concern of this lib, you can find a debounce on lodash, a log in other lib, etc. I don't think this is our purpose.

The main purpose of the lib is to respond with ease to 90% of our developpement.
Which will be something like:

when('@@ui/CLICKED_COMPLETE')(complete)

or:

reaction(complete).when('@@ui/CLICKED_COMPLETE')
reaction(complete).when('@@ui/CLICKED_COMPLETE_2')

from k-ramel.

bpetetot avatar bpetetot commented on May 24, 2024

Seems nice ! πŸ‘Œ
And I'm agree that is not the purpose of the lib to bring debounce, it was just to illustrate my thoughts.

from k-ramel.

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.