sascha245/vuex-simple

No sync between the store properties and tokens

Closed this issue · 2 comments

  • I'm submitting a ...
    [ ] bug report
    [ ] feature request
    [X ] question about the decisions made in the repository
    [ ] question about how to use this project

Hi @sascha245

In the new release I found that there is a place for mistake when using the tokens.
A developer can set the store name and have the wrong token defined.

see this example :

//tokens.ts
export default {
  BAR: new Token('bar'),
  BAR_FOO1: new Token('bar/foo1'),
  BAR_FOO3: new Token('bar/foo2')
};
//index.ts
Container.set(store.bar.foo1, tokens.BAR_FOO1);
Container.set(store.bar.foo3, tokens.BAR_FOO3);

here there should be an exception thrown due to a difference between the token string and the store structure.
Can there be a wat to define it only once instead of twice (in the store and in the tokens) so it will have less room for mistakes.

Hi @shmaram,

As dependency injection has been completely removed from this library, it is up to the user to setup whatever he wants to have in the container.

Here, the tokens are just there as an example to give us a unique key. We will then bind our value / module to this key in the container.
In fact you can also use new Token(), strings or classes (everything that is supported by typedi at least)

// tokens.ts
export default {
  BAR_FOO1: new Token(),
};
// index.ts
import { BarModule } from './modules/bar';

Container.set(BarModule, store.bar);
Container.set(tokens.BAR_FOO1, store.bar.foo1);
Container.set("some_unique_identifier", store.bar.foo2);

// in your component
@Component
class MyComponent extends Vue {
  
  @Inject()
  public bar!: BarModule;

  @Inject(tokens.BAR_FOO1)
  public foo1!: FooModule;

  @Inject("some_unique_identifier")
  public foo2!: FooModule;  
}

Note that vue-typedi reexports all the components of typedi and applies some fixes so the Inject can be used with vue components. That means everything on the README page of typedi also applies to vue-typedi.

Note: I made a small mistake in the readme:

// WRONG: set(value, key)
Container.set(instance.bar, tokens.BAR);
...
// CORRECT: set(key,value)
Container.set(tokens.BAR, instance.bar);
...

I wish you a good evening,

Sascha

As there hasn't been any further questions concerning this issue, I will close it for now.
If you need more information, you can reopen this issue if it is linked to this issue or open a new one.