How to use the effector.guard function in effector

To help you get started, we’ve selected a few effector examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zerobias / effector / examples / serverless-ssr / src / app.tsx View on Github external
<ol>
      
    </ol>
    
    <h3>Users:</h3>
    
    
  
)

export const installHistory = app.createEvent()
const changeLocation = app.createEvent()

// triggered when current location not equal new location
// and new location is valid user ID
const onValidLocation = guard(changeLocation, {
  filter: sample({
    source: combine({loc: location$, users: userList}),
    clock: changeLocation,
    fn: ({loc, users}, path) =&gt;
      loc !== path &amp;&amp; users.includes(path.replace('/user/', '')),
  }),
})

installHistory.watch(history =&gt; {
  const locationUpdate = scopeBind(changeLocation)
  history.listen(location =&gt; {
    locationUpdate(location.pathname)
  })
})

forward({
github zerobias / effector / examples / react-ssr / src / app.tsx View on Github external
<ol>
      
    </ol>
    
    <h3>Users:</h3>
    
    
  
)

export const installHistory = app.createEvent()
const changeLocation = app.createEvent()

// triggered when current location not equal new location
// and new location is valid user ID
const onValidLocation = guard(changeLocation, {
  filter: sample({
    source: location$,
    clock: changeLocation,
    fn: (loc, path) =&gt; loc !== path &amp;&amp; path.slice(1) in users,
  }),
})

installHistory.watch(history =&gt; {
  const locationUpdate = scopeBind(changeLocation)
  history.listen(location =&gt; {
    locationUpdate(location.pathname)
  })
})

forward({
  from: startClient,
github zerobias / effector / website / editor / src / flow / init.js View on Github external
typeNode.style.opacity = '1'
  typeNode.style.visibility = 'inherit'
})
guard({
  source: hideTypeNode,
  filter: typeHoverToggle,
}).watch(() => {
  typeNode.style.opacity = '0'
  typeNode.style.visibility = 'hidden'
})
guard({
  source: typeAtPos.fail,
  filter: typeHoverToggle,
  target: hideTypeNode,
})
guard({
  source: typeHint.map(data => {
    if (data === null) return 'Loading...'
    return data
  }),
  filter: typeHoverToggle,
}).watch(hint => {
  typeNode.innerText = hint
})
github tanyaisinmybed / effector-undo / src / index.ts View on Github external
}))
    .on(clear, ({ states, head }) =&gt; ({
      states: [states[head]],
      head: 0
    }));

  const current = history.map(({ states, head }) =&gt; states[head]);

  const shouldSave = createStore({
    next: initialState,
    prev: initialState
  })
    .on(store, ({ next: prev }, next) =&gt; ({ next, prev }))
    .map(({ next, prev }) =&gt; filter(next, prev));

  guard({
    source: sample(store, merge(events)),
    filter: shouldSave,
    target: push
  });

  forward({
    from: current,
    to: store
  });

  return { undo, redo, clear, history };
}
github zerobias / effector / website / editor / src / flow / init.js View on Github external
}
  performLint()
})

typeErrors
  .on(checkContent.done, (state, {result}) => {
    if (result.code === 'fail') return state
    return result.code
  })
  .reset(checkContent.fail)

typeHint
  //$todo
  .on(typeAtPos.done, (_, {result}) => result.code.c)
  .reset(typeAtPos.fail)
guard({
  source: showTypeNode,
  filter: typeHoverToggle,
}).watch(() => {
  typeNode.style.opacity = '1'
  typeNode.style.visibility = 'inherit'
})
guard({
  source: hideTypeNode,
  filter: typeHoverToggle,
}).watch(() => {
  typeNode.style.opacity = '0'
  typeNode.style.visibility = 'hidden'
})
guard({
  source: typeAtPos.fail,
  filter: typeHoverToggle,
github zerobias / effector / website / editor / src / share / controller.js View on Github external
forward,
  sample,
  guard,
  type Effect,
  type Store,
  type Event,
} from 'effector'
import {sourceCode} from '../editor/state'
import {shareCode} from '../graphql'
import {isShareAPISupported} from '../device'

const pressCtrlS = createEvent()

sample({
  source: sourceCode,
  clock: guard(pressCtrlS, {
    filter: shareCode.pending.map(pending => !pending),
  }),
  target: shareCode,
})

document.addEventListener(
  'keydown',
  e => {
    if ((e.metaKey || e.ctrlKey) && e.keyCode === 83) {
      e.preventDefault()
      pressCtrlS()
    }
  },
  false,
)