Skip to content

Releases: e-oz/ngx-collection

4.3.1

07 Nov 15:00
Compare
Choose a tag to compare

Callbacks onSuccess() and onError() were called before $items was updated. $items() inside the callbacks had a non-updated value.

4.3.0

20 Oct 07:07
Compare
Choose a tag to compare

New fields:

  • $lastReadError
  • $lastReadOneError
  • $lastReadManyError
  • $lastRefreshError

In params structures, fields readRequest and refreshRequest are deprecated (will be removed in v5).
Use read and refresh instead.

4.2.3

11 Oct 17:18
Compare
Choose a tag to compare

New method: fetchItem()!

This method will check if the item exists in the collection and return it if it does.
If the item does not exist, the request argument will be used to fetch the item and add it to the collection.
If the option fetchItemRequestFactory is set, the request argument is optional.
If both are missing, the resulting Observable will throw an error.

4.2.2

05 Sep 10:22
Compare
Choose a tag to compare

createEffect.forValue() renamed to getEffectFor().

4.2.1

05 Sep 10:03
Compare
Choose a tag to compare

Experimental method for createEffect(): forValue(), which takes a value and returns an observable that will execute the effect when subscribed.

4.2.0

03 Sep 12:44
Compare
Choose a tag to compare
  • New (experimental!) methods: readFrom and readManyFrom. Can be called as part of constructor options.
  • EffectFnMethods renamed to EffectObservables, and lost methods next, error and complete - the same functionality with a less ambiguous API can be achieved with EffectListeners. This API is considered stable now.

4.1.3

15 Aug 18:40
Compare
Choose a tag to compare
  • Use untracked() every time when reactive context should not be affected;
  • Use take(1) instead of first() to prevent no elements in sequence exception.

4.1.2

13 Aug 09:19
Compare
Choose a tag to compare

Improved API for createEffect() listeners, introduced in v4.1.1.

Methods of the function, returned by createEffect():

export type EffectFnMethods = {
  next: (fn: ((v: unknown) => void)) => void,
  error: (fn: ((v: unknown) => void)) => void,
  complete: (fn: (() => void)) => void,
  next$: Observable<unknown>,
  error$: Observable<unknown>,
};

Also, you can set next listener or an object with listeners as a second argument, when you call an effect:

class Component {
  store = inject(Store);
  dialog = inject(Dialog);
  toasts = inject(Toasts);
  
  changeZipCode(zipCode: string) {
    this.store.changeZipCode(zipCode, () => this.dialog.close());
    
    // or:
    this.store.changeZipCode(zipCode, {
      next: () => this.dialog.close(),
      error: () => this.toasts.error('Error, please try again.'),
    });
  }
}

4.1.1

13 Aug 00:16
Compare
Choose a tag to compare

createEffect() now returns not just a function, but a function with methods! :)
API is experimental and might change, so it's documented only here for now.

In your store:

import { createEffect } from './create-effect';

class Store extends Collection<Item> {
  readonly changeZipCode = createEffect<string>(_ => _.pipe(
    // code to change zipcode
  ));
}

In your component:

class Component {
  store = inject(Store);
  dialog = inject(Dialog);
  
  changeZipCode(zipCode: string) {
    this.store.changeZipCode.nextValue(() => this.dialog.close());
    this.store.changeZipCode(zipCode);
  }
}

In this example, the dialog window will be closed only after the service response, and only if it was successful.

Alongside nextValue, there are other methods:

export type EffectFnMethods = {
  nextValue: (fn: ((v: unknown) => void)) => void,
  nextError: (fn: ((v: unknown) => void)) => void,
  onNextValue(): Observable<unknown>,
  onNextError(): Observable<unknown>,
};

Internally, values and errors will not be saved in memory if you don't use these methods.

4.1.0

12 Aug 12:29
Compare
Choose a tag to compare

Sometimes we know in advance the IDs of items we read, and it can be quite useful to know that these items are being read.

Now, the methods read(), readOne(), and readMany() accept a parameter item/items, where you can pass partial items:

coll.read({
  request: req,
  items: [{id: 1}, {id: 2}]
});

This will instantly add {id: 1} and {id: 2} to $readingItems, but not to $items (because they are not in the collection yet).

Params should be objects that have at least the ID field (the field or multiple fields that the comparator will use to find the item). The object can also have any other fields - they will be ignored.

A new method, isItemReading(), will return a Signal<boolean> - you can check (reactively) if an item with a specific ID is being read.

The method isItemProcessing() will now also look for an item in $readingItems (in addition to previous states).

And a new helper method to quickly convert an array of IDs into partial items:
idsToPartialItems(ids: unknown[], field: string): Partial<T>[]