RFC: variant #3
Replies: 10 comments 2 replies
-
You can get key from store: const $move = createStore({ distance: 0 });
const $direction = createStore('left')
const moveLeft = createEvent();
const moveRight = createEvent();
variant({
source: $move,
key: $direction,
target: {
left: moveLeft,
right: moveRight,
}
}) |
Beta Was this translation helpful? Give feedback.
-
Variant itself returns |
Beta Was this translation helpful? Give feedback.
-
There can be a default case ( variant({
source: $account,
key: 'kind',
target: {
admin: adminAccount,
user: userAccount,
__: guestAccount,
},
}) |
Beta Was this translation helpful? Give feedback.
-
Here is a comparison with const message = createEvent()
const bob = createEvent()
const alice = createEvent()
const guest = createEvent()
variant({
source: message,
key: 'user',
target: { bob, alice, __: guest }),
})
bob.watch(({text}) => {
console.log('[bob]: ', text)
})
alice.watch(({text}) => {
console.log('[alice]: ', text)
})
message({user: 'bob', text: 'Hello'})
// => [bob]: Hello
message({user: 'alice', text: 'Hi bob'})
// => [alice]: Hi bob
/* default case, triggered if no one condition met */
guest.watch(({text}) => {
console.log('[guest]: ', text)
})
message({user: 'unregistered', text: 'hi'})
// => [guest]: hi Compare with split: const message = createEvent()
const messageByAuthor = split(message, {
bob: ({user}) => user === 'bob',
alice: ({user}) => user === 'alice',
})
messageByAuthor.bob.watch(({text}) => {
console.log('[bob]: ', text)
})
messageByAuthor.alice.watch(({text}) => {
console.log('[alice]: ', text)
})
message({user: 'bob', text: 'Hello'})
// => [bob]: Hello
message({user: 'alice', text: 'Hi bob'})
// => [alice]: Hi bob
/* default case, triggered if no one condition met */
const {__: guest} = messageByAuthor
guest.watch(({text}) => {
console.log('[guest]: ', text)
})
message({user: 'unregistered', text: 'hi'})
// => [guest]: hi |
Beta Was this translation helpful? Give feedback.
-
variant({
source: message,
key: 'user',
target: { bob, alice },
fallback: guest,
}) With guards: guard({
source: message,
filter: ({user}) => user === 'alice',
target: alice,
});
guard({
source: message,
filter: ({user}) => user === 'bob',
target: bob,
});
guard({
source: message,
filter: ({user}) => user !== 'alice' && user !== 'bob',
target: guest,
}); With forwards + filters: forward({
from: message.filter({ fn: ({user}) => user === 'alice' }),
to: alice,
})
forward({
from: message.filter({ fn: ({user}) => user === 'bob' }),
to: bob,
})
forward({
from: message.filter({ fn: ({user}) => user !== 'alice' && user !== 'bob' }),
to: guest,
}) P.S. guard({
source: message,
filter: ({user}) => user === 'alice',
target: alice,
fallback: notAlice
}); |
Beta Was this translation helpful? Give feedback.
-
Check this out: variant({
source: sample(page, nextPage),
target: createApi(page, {
'/intro': () => '/article',
'/article': () => '/pricing',
'/pricing': () => '/signup',
})
}) |
Beta Was this translation helpful? Give feedback.
-
variant({
source: uploadFx.done,
key: gate.status,
cases: {
true: $uploadResult,
false: showNotification.prepend(() => “uploaded!”)
}
}) |
Beta Was this translation helpful? Give feedback.
-
Variant implementation for effector export function variant({ source, key, cases, clock, fn }) {
const keyReader = typeof key === 'function'
? key
: is.store(key)
? () => String(key.getState())
: value => String(value[key])
if (clock || fn) {
source = sample({ source, ...clock && { clock }, fn })
}
let defaultCase = false
for (const caseName in cases) {
if (caseName === '__') {
defaultCase = true
continue
}
guard({
source,
filter: value => keyReader(value) === caseName,
target: cases[caseName],
})
}
if (defaultCase) {
const namedCases = Object.keys(cases)
guard({
source,
filter: value => !namedCases.includes(keyReader(value)),
target: cases.__,
})
}
} https://gist.github.com/doasync/afae239f1214ea9b9acc434c39d5d149 |
Beta Was this translation helpful? Give feedback.
-
Work in progress... |
Beta Was this translation helpful? Give feedback.
-
What about to close this issue and PR, because effector released split https://effector.now.sh/docs/api/effector/split The second option rename this method and change its purpose |
Beta Was this translation helpful? Give feedback.
-
Select a target event by value from a store or event.
When the value of the store will be
left
, eventmoveLeft
will be triggered. Ifright
—moveRight
.Select property from object with key:
Transform value before trigger target:
Get key with function:
With clock:
Beta Was this translation helpful? Give feedback.
All reactions