🚀🚀
Size: a a a
🚀🚀
O
G

G

🚀🚀
code-compressed поля в localStorageG
code-compressed поля в localStorageG
G
🚀🚀
const createFetch = (time, mess) => createEffect(
(prop) => new Promise(
r => setTimeout(
() => r({mess, prop})
, time
)
)
)
const gFx = createFetch(50, 'google')
const aFx = createFetch(100, 'amazon')
const yFx = createFetch(200, 'yan')
/*
fmap :: (a -> b) -> f a -> f b
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4
(<&>) :: Functor f => f a -> (a -> b) -> f b infixl 1
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
*/
const queneA = (fxes) => {
const push = createEvent()
const replace = createEvent()
const reset = createEvent()
const $state = createStore([])
.reset(reset)
.on(push, (s, p) => ([...s, p]))
.on(replace, (s, p) => (
s.map(f => f.id === p.id ? p.replacer(f) : f)
))
fxes.forEach((fx, i) => {
const setInFlightState = createEvent()
const $inFlightState = createStore({ prev: 0, current: 0 })
.on(setInFlightState, (s, p) => ({
prev: s.current,
current: p,
}))
fx.inFlight.watch(setInFlightState)
$inFlightState.watch(s => {
// init
if (s.prev === 0 && s.current === 0) return
// start event
if (s.prev < s.current) {
push({
id: `${i}${s.current}`,fx,
timeStart: Date.now(), timeEnd: null
})
}
// done event
if (s.prev > s.current) {
replace({
id: `${i}${s.prev}`,
replacer: f => ({ ...f, timeEnd: Date.now() })
})
}
})
})
// $state.watch(console.log.bind(null, 'state'))
const allEvents = guard({
source: $state,
filter: fxe => fxe.length > 0
})
allEvents.watch(console.log.bind(null, 'allEvents'))
const firstDone = allEvents
.filter({
fn: fxes => fxes.filter(f => f.timeEnd !== null).length === 1
})
.filterMap(fxes => fxes.find(f => f.timeEnd !== null))
firstDone.watch(console.log.bind(null, 'firstDone'))
const firstDoneData = guard({
source: firstDone,
filter: f => f,
target: firstDone
})
const allDone = guard({
source: allEvents,
filter: fxe => fxe.every(f => f.timeEnd !== null)
})
allDone.watch(console.log.bind(null, 'allDone'))
const lastDone = allDone.filterMap(
fxes => fxes.reduce((pf, cf) => cf.timeEnd > pf.timeEnd ? cf : pf)
)
lastDone.watch(console.log.bind(null, 'lastDone'))
console.log('fx', firstDoneData)
// firstDoneData.doneData.watch(console.log.bind(null, 'kk'))
return {
quenue: $state,
firstDone,
firstDoneData: firstDone.fx,
lastDone,
allDone
}
}
queneA([
gFx, aFx, yFx
])
gFx(1)
gFx(2)
// gFx()
// aFx()
// setTimeout(() => {aFx()}, 400)
G
O
🚀🚀
G
🚀🚀
G
🚀🚀
🚀🚀
G
🚀🚀
🚀🚀