angularangular-signals

Is there a way to set a default value to signal's rxResource() such as signal()


it seems that there is no way to set a default value to rxResource such as classique signals.

I do it like that:

  #errorsResource = rxResource<ParseError[], any>({
    request:() => ({
      action: "getErrors", 
      IdParse: this.#idParse(),
    }),
    loader: (request) => this.#_parseApi.request(request.request)
  })

  #errors = linkedSignal({
    source: this.#errorsResource.value,
    computation: () => {
      let val = this.#errorsResource.value()
      if ( val ) return val
      return []
    }
  })

is there a better way I don't see to initialize rxResources to a default value until it's loader triggers ?


Solution

  • An easier solution would be with computed:

      #errorsResource = rxResource<ParseError[], any>({
        request:() => ({
          action: "getErrors", 
          IdParse: this.#idParse(),
        }),
        loader: (request) => this.#_parseApi.request(request.request)
      })
    
      #errors = computed(() => this.#errorsResource.value() ?? []);
    

    If the signal needs to be writable, you can use a less verbose syntax of linkedSignal like the following:

      #errorsResource = rxResource<ParseError[], any>({
        request:() => ({
          action: "getErrors", 
          IdParse: this.#idParse(),
        }),
        loader: (request) => this.#_parseApi.request(request.request)
      })
    
      #errors = linkedSignal(() => this.#errorsResource.value() ?? []);