-
Notifications
You must be signed in to change notification settings - Fork 35
suggestion/question: withDataService
support for Observables
#85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@michael-small, I don't see a problem in supporting RxJs. So feel free to contribute your implementation |
Sounds good. I in fact do have some changes to make as I messed some stuff up, but once I have it in a better state I will update this issue and then get to a PR.< 8000 /p> |
Link to my implementation in a project: https://github.com/michael-small/signal-store-playground/tree/main/src/app/store/withDataService-rxjs Once I have ironed it out a bit more I will make a fork of the toolkit and start adding an RXJS implementation. |
I have been grappling with types and generics when trying to adapt export interface DataService<
E extends Entity,
F extends Filter,
ArrEntityData = Promise<E[]> | Observable<E[]>,
SingleEntityData = Promise<E> | Observable<E>,
VoidEntityData = Promise<void> | Observable<void>>
{
load(filter: F): ArrEntityData;
loadById(id: EntityId): SingleEntityData;
create(entity: E): SingleEntityData;
update(entity: E): SingleEntityData;
updateAll(entity: E[]): ArrEntityData;
delete(entity: E): VoidEntityData ;
} Provided an interface like this one is agreeable, I still have to figure out how each Bottom line: naming aside, would you say that an interface like this is a good starting point? |
@michael-small, what about that alternative Signature? type PromiseOrObservable<Entity> = Promise<Entity> | Observable<Entity>;
export interface DataService<
E extends Entity,
F extends Filter
{
load(filter: F): PromiseOrObservable<Entity[]>;
loadById(id: EntityId): PromiseOrObservable<Entity>;
create(entity: E): PromiseOrObservable<Entity>;
update(entity: E): PromiseOrObservable<Entity>;
updateAll(entity: E[]): PromiseOrObservable<Entity[]>;
delete(entity: E): PromiseOrObservable<void> ;
} |
@rainerhahnekamp that's a lot better, thank you. |
Great, waiting for your PR then 😃 |
Thank you. Once I figure out how I can get each method to either narrow down what to do based on the type, or overload for the top level async or rxMethod signature, then I'll submit the PR. Will you want tests for these? I don't see tests for the promises version, but I can try to make a test suite. |
Yes please. Tests would be great |
@rainerhahnekamp I have a question: since I don't suppose we would make some My approach right now is basically using type guards for telling if the implementing service method returns a promise or observable, so this is a particular edge case that I aught to handle before narrowing down other checks. So handle the logic based on the input first, and then presumably safely assume I use an |
@michael-small I'd say for the first version, it should be fine of you check if the response is of type Promise or Observable. I guess you would do an automatic subscribe. If there's demand, we can later maybe add the |
Sounds good, thanks. It didn't occur to me that the RXJS calls could be an old fashioned subscription. I'm just so I'll proceed with manual subscriptions. I assume in that case I can handle subscriptions with some type of self cleaning up subscription, or perhaps pass in a destroyRef. I'll go with the former to keep it simple. |
@michael-small @rainerhahnekamp , any chance this will get picked up again to be officially supported by the ngrx-toolkit library? I have a huge need for it, but didn't want to introduce it to my project if the spec was likely to change after official adoption... Thoughts? |
@jryan719 after developing my own adjacent sort of variant of a RXJS type CRUD signalStoreFeature, I personally lost interest in developing this feature RXJS variant of Rainer did help me with that feature I shifted my focus to, though. Here is my repo with the link to our livestream talking about it in depth: https://github.com/michael-small/ngrx-signal-store-feature-conditional. Other links include an example Stackblitz and slides among other things. It works similar in many ways the core of the now closed PR on the RXJS variant of
But it isn't entity based or have custom collection name. However, the unique benefits is: you do not need to fulfill the whole CRUD service interface.
Examples: Basic boolean version withCrudConditional(TodoReadAndDeleteOnlyService, {
create: false,
readAll: true,
readOne: true,
update: false,
delete: true,
}) WIP conditional mapping, a bit rough around the edges still withFeatureFactory((store) =>
withCrudMappings({
readAll: () => store.serv.getAllDifferentName(),
readOne: (value: number) => store.serv.getOneDifferentName(value),
create: (value: Partial<Todo>) => store.serv.createDifferent(value),
update: (value: Partial<Todo>) => store.serv.updateDifferent(value),
delete: (value: Partial<Todo>) => store.serv.deleteDifferent(value),
})
) |
@michael-small Thanks for responding with such an informative reply! I appreciate it. This looks very interesting. Nice work! Any chance you'll integrate entities into it? My primary goal was to leverage a feature that was going to be officially maintained as NGRX Signals/SignalStore matures. That's what drove me towards the NGRX Toolkit library (it appears to be well maintained). However, I'll have to weigh that risk with my need to have this type of feature as soon as yesterday 🤪 . Nonetheless, thanks again for responding to my nagging question, I appreciate it :) |
I don't have plans to personally |
Maybe I can share my perspective as well. |
Uh oh!
There was an error while loading. Please reload this page.
My problem
withDataService
is super smart and interesting. Honestly the most boilerplate reducing Angular code I have seen. One limitation for my own use, however, is that I userxMethod
rather thanpromise
.My suggestions/questions
I copied over the relevant files of this library's
withDataService
into a private project full of more signal store experimentation I adapted all the feature methods to userxMethod
. I have not tested it too much yet, but it seems to work good so far. I can make it public if anyone is interested.withDataService
to support RXJS?copies or substantial portions of the Software."?
edit: link to my implementation in one of my projects: https://github.com/michael-small/signal-store-playground/tree/main/src/app/store/withDataService-rxjs
The text was updated successfully, but these errors were encountered: