WebReflection / usignal

A blend of @preact/signals-core and solid-js basic reactivity API
ISC License
220 stars 15 forks source link

any example usage of usignal with calling rest api? #32

Closed btoaelbore closed 1 year ago

btoaelbore commented 1 year ago

basically just wondering if you could guide me how to use usignal with calling rest api? thanks

WebReflection commented 1 year ago

you fetch and set signal.value = result once done?

btoaelbore commented 1 year ago

just want to understand also how does signal works, and what to avoid it

import { signal, effect } from 'usignal'

const name = signal('')
const fname = signal('')

effect(() => {
  console.log(fname.value)

  effect(() => {

    /// this logs 2x
    console.log('set name')

    /// this execute 2x
    new Promise((resolve) => {
      setTimeout(() => {
        console.log('set name to joe')
        name.value = 'joe'
        resolve(null)
      }, 2000)
    })

    /// this logs 2x
    console.log('Hello')
  })

})

fname.value = 'my last name'

outputs

set name
Hello
my last name
set name
Hello
set name to joe
set name to joe
WebReflection commented 1 year ago

one happens because you register a signal for the outer effect, the second one happens because you change a signal in another effect ... for multipl updates you have batch(callback) but honestly this looks like a request for generic documentation around signals ... there's plenty of that on the internet. Closing as this is not related to this library.

See Preact or Solid.js signals documentation which is well written too, everything in here should work the same.

WebReflection commented 1 year ago

not sure why I re-opened this ...