Skip to content
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

Add new variation of inlineStoreInClass #492

Closed
EricPoul opened this issue Oct 13, 2023 · 2 comments
Closed

Add new variation of inlineStoreInClass #492

EricPoul opened this issue Oct 13, 2023 · 2 comments

Comments

@EricPoul
Copy link
Contributor

EricPoul commented Oct 13, 2023

Which @ngneat/elf-* package(s) are relevant/releated to the feature request?

No response

Description

Since inject function is already mainstream in the Angular and constructor is becoming less and less used, I guess, it'll be good to add a new variation of the generation of a repo.

Proposed solution

Maybe make inlineStoreInClass accept boolean | 'constructorless'

The output will be

@Injectable({ providedIn: 'root' })
export class TodoRepository {
  private readonly store = this.createStore();

  todos$ = this.store.pipe(selectAllEntities());
  activeTodo$ = this.store.pipe(selectActiveEntity());
  activeTodos$ = this.store.pipe(selectActiveEntities());

  private createStore(): typeof store {
    const store = createStore(
      { name: 'todo' },
      withProps<State>(createInitialState()),
      withEntities<Todo>(),
      withActiveId(), 
      withActiveIds(),
    );

    return store;
  }
}

If someone will need to inject a dependency it'll be easy to do:

@Injectable({ providedIn: 'root' })
export class TodoRepository {
  private readonly storage = inject(LOCAL_FORAGE);
  private readonly store = this.createStore();

  todos$ = this.store.pipe(selectAllEntities());
  activeTodo$ = this.store.pipe(selectActiveEntity());
  activeTodos$ = this.store.pipe(selectActiveEntities());

  private createStore(): typeof store {
    const store = createStore(
      { name: 'todo' },
      withProps<State>(createInitialState()),
      withEntities<Todo>(),
      withActiveId(), 
      withActiveIds(),
    );

    persistState(store, {
      key: 'todo',
      storage: this.storage.createInstance({
        driver: this.storage.INDEXEDDB,
      }),
    });

    return store;
  }
}

Alternatives considered

Do you want to create a pull request?

Yes

@netanel-utila
Copy link
Contributor

LGTM. It can even be the default

@EricPoul
Copy link
Contributor Author

inlineStoreInClass by default is set to false, do you want to change it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants