ngxtension / ngxtension-platform

Utilities for Angular
https://ngxtension.netlify.app/
MIT License
592 stars 87 forks source link

feat(signal-slice): add "Updated" signal for action sources #363

Closed joshuamorony closed 2 months ago

joshuamorony commented 5 months ago

NOTE: This depends on https://github.com/ngxtension/ngxtension-platform/pull/361 being merged, this is branched off of that feature

This is a replacement for the removal of the actionEffect API, instead it allows triggering side effects as a result of an actionSource emitting via a notifier/version signal (same general concept we used for createNotifier).

It adds a <actionSourceName>Updated signal for each action source e.g:

  state = signalSlice({
    initialState: this.initialState,
    actionSources: {
      load: (_state, $: Observable<void>) => $.pipe(
        switchMap(() => this.someService.load()),
        map(data => ({ someProperty: data })
      )
    }
  })

  effect(() => {
    // triggered when `load` emits/completes and on init
    console.log(state.loadUpdated())
  })

  effect(() => {
    if (state.loadUpdated()) {
      // triggered ONLY when `load` emits/completes
      // NOT on init
    }
  });

This is a much simpler API and avoids the typing issues (described in: https://github.com/ngxtension/ngxtension-platform/pull/361)

michael-small commented 5 months ago

Sounds like a fair tradeoff to me. If the pattern of this PR and the base PR is to de-couple signalSlice from formal effect blocks, then having some sort distinction between other effect in a file unrelated to the slice and effect related to the slice is a nice tradeoff.

eneajaho commented 5 months ago

Hello @joshuamorony Can you add the breaking change in the commit description if this is a breaking change?

joshuamorony commented 2 months ago

@eneajaho we ended up merging #361 and this is ready for review now (no breaking changes in this PR)

eneajaho commented 2 months ago

@joshuamorony can you rebase? Then I can merge

joshuamorony commented 2 months ago

@eneajaho done