Architecture / pattern / conventions / best practices? #238
Replies: 5 comments
-
Effector doesn't impose architectural techniques, so this list is only a recommendation
const playerPosition = createStore(0)
const {moveLeft, moveRight} = createApi(playerPosition, {
moveLeft: (x, amount) => x - amount,
moveRight: (x, amount) => x + amount,
})
playerPosition.watch(x => {
console.log(`player position: ${x}`)
})
// player position: 0
moveLeft(2)
// player position: -2
moveRight(4)
// player position: 2 In this example, events are become an api to player movement, thus they are not "in the past" but rather intentions to future changes Also note that you might not need the most of const fetchUserFx = createEffect().use(() => 'alice')
const userName = createStore('guest')
.on(fetchUserFx.done, (state, {result}) => result)
userName.watch(console.log)
// => guest
await fetchUserFx()
// => alice
const removeLast = createEvent()
const columns = createDomain()
columns.onCreateStore(store => {
store.on(removeLast, list => list.slice(0, -1))
})
const names = columns.store(['alice', 'bob'])
const emails = columns.store([
'[email protected]', '[email protected]'
]) |
Beta Was this translation helpful? Give feedback.
-
Rather than naming conventions, effector itself suggests data conventions: how things are calculated in the application For example, instead of spreads, you might use const fetchUser = createEffect({
handler: () => 'alice',
})
const fetchTracks = createEffect({
handler: (userId) => [0, 5, 10],
})
const id = createStore('guest')
.on(fetchUser.done, (_, {result}) => result)
const tracks = createStore<number[]>([])
.on(fetchTracks.done, (_, {result}) => result)
// note also that user type will be inferred without explicit types
const user = combine({id, tracks})
fetchUser.done.watch(({result: userId}) => {
fetchTracks(userId)
})
user.watch(console.log)
// => {id: guest, tracks: []}
await fetchUser()
// => {id: alice, tracks: []}
// => {id: alice, tracks: [0, 5, 10]} |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot @zerobias! |
Beta Was this translation helpful? Give feedback.
-
Just throwing my 2 cents in here as someone just coming to Effector. Im finding these naming conventions really confusing to be honest and the docs are extremely inconsistant about it. IMO it would be easyier to learn this library by either having no convention (as mentioned above) or just using simple words such as "buttonClickedEvent" and "UsersStore" and "fetchDataEffect". There are a few things to learn with this library and having to learn syntax such as what "$users" represents or "fxGetTodo" means ontop of that isnt helpful IMO.. |
Beta Was this translation helpful? Give feedback.
-
@mikecann we working on documentation consistency |
Beta Was this translation helpful? Give feedback.
-
Hello,
I would like to know if there is some official / unofficial best practices for architecturing our apps at scale?
Considering we have a user model
Store
Model name with a
$
prefix?Events
Model name with a simple past verb suffix?
Effect
Nothing special?
File name
Use the model name?
user.js
Export (full example)
Import (full example)
For simple apps, it's very easy to manage, but what about larger apps?
Namespacing?
Is
createDomain
can be a solution, or is it not his purpose? (it's not very clear for me)Or is it ok to handle things like this:
Let's add a tracks model to the previous stuff
Thanks for your advices!
Beta Was this translation helpful? Give feedback.
All reactions