Converting function in different version

how to changes this code: there is a version difference.

 preferredDeliveryChannelComp: computed(function () {
    const preferredChannels = this.get('otvc.deliveryChannels').filterBy('preferred');
    if (preferredChannels.length > 0){
      const preferredChannelId = preferredChannels[0].id;
      this.get('otvc').setChannel(preferredChannelId); //set the selected option in memory
      return preferredChannelId;
    }
  }),

to equivalent to this one:

    preferredDeliveryChannelComp: computed('showPushOption', function () {
		const preferredChannels = this.get('otvc.deliveryChannels')
			.filter(channel => channel.get('preferred') && channel.get('type') !== 'PUSH');

		if (this.get('showPushOption')) {
			return this.get('otvc.pushEnabledDeviceId');
		} else if (preferredChannels.length > 0) {
			const preferredChannelId = preferredChannels[0].id;
			this.get('otvc').setChannel(preferredChannelId); //set the selected option in memory
			return preferredChannelId;
		}
	}),

I don’t understand your question, but some general pointers about this code:

  • doing any kind of assignment (setChannel) from within a computed getter is almost always a mistake that leads to confusing bugs.
  • this computed function declares no dependent keys even though it depends on otvc.deliveryChannels, showPushOption, and otvc.pushEnabledDeviceId. If any of those change, this won’t update in response.

Thanks @ef4 for better clarity

thanks for the awesome information.