bySabi / garfio

extending Hookleton Pattern namespaces and more
MIT License
1 stars 0 forks source link

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

Closed mh-alahdadian closed 4 years ago

mh-alahdadian commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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