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

using store module with store object reference instead of string key #1

Closed
mh-alahdadian opened this issue Dec 28, 2019 · 5 comments
Closed

Comments

@mh-alahdadian
Copy link

mh-alahdadian commented Dec 28, 2019

I had like store functions very much but I don't want to name my stores and worrying about unique key for them
I think that it might be a good idea that createStore can return a store object which have .get method and module can have another function for useStore which will received store as params
I think that this type of store might not be compatible with already version of it so you can find another name for it

Thanks

@bySabi
Copy link
Owner

bySabi commented Dec 28, 2019

@mha15
I do not understand very well that what you do not like and what is your proposal. Can you give an example of how you would like the API to be?

The purpose of Garfio is to capture patterns that are used in other libraries and re-implement it on top of Hookleton so that anyone improves even if it implies the change in the API is welcome. We have not yet reached version 1.0.0 :-)

@mh-alahdadian
Copy link
Author

mh-alahdadian commented Dec 29, 2019

Can you give an example of how you would like the API to be?

for example

create

createStore(hook, ...initial?): Store<T>;

use

useStore(store, ...initial?): T

interface Store<T> {
   get(): T;
   destroy(): void;
}

@bySabi
Copy link
Owner

bySabi commented Dec 30, 2019

Ah, I see.
The current API of garfio/store is more general. The store identifier can be anything choose by user. Say a string, a Symbol, a object, a uuid string, ... The uniqueness of id is left to the user.

You example API can be recreate easely in top of the current API:

Ex:

import { createStore as _createStore, useStore as _useStore, getStore, removeStore } from 'garfio/store';

export function createStore(hook, ...initial) {
   const id = Symbol();
   _createStore(id, hook, ...initial);
   return { id, get: () => getStore(id), destroy: () => removeStore(id) }
}

export function useStore({ id }, ...initial) {
   return _useStore(id, ...initial);
}

I am not sure if these types of cases that are more specific should be added to the module. Or just point them out as a pattern in the Doc

Actually the store is a Map in the store.js module: https://github.com/bySabi/garfio/blob/master/src/store.js#L3. Which becomes global thanks to the fact that node imports each module only once, per app, regardless of all the sites where it is required

@bySabi
Copy link
Owner

bySabi commented Dec 31, 2019

Or even more simple with:

import { createStore as _createStore,  getStore, removeStore } from 'garfio/store';

export function createStore(hook, ...initial) {
   const store = {}
   _createStore(store, hook, ...initial);
   store.get = () => getStore(store);
   store.destroy = () => removeStore(store)
   return store;
}

It is quite likely that you will use this API in the next version, 0.2.0. It is much simpler and functional.

Thanks for suggesting it.

@bySabi bySabi closed this as completed Dec 31, 2019
@bySabi
Copy link
Owner

bySabi commented Dec 31, 2019

Hey @mha15 I add your suggested changes to Store API
We will let typescript for 2020
Happy new year!!

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

2 participants