-
Notifications
You must be signed in to change notification settings - Fork 0
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
createStore
hook & Strong typed store values
#7
Conversation
225f82a
to
1b71292
Compare
abde3e4
to
71de9a9
Compare
createStore
hook & Strong typed store values
@tcarac what do you think about the switch to initializing the stores in a single file and async manner? // controllers/stores.js
import { createStore } from "stimulus-store";
export let counterStore;
export let anotherStore;
(async function initializeStores() {
counterStore = await createStore({ name: 'counterStore', initialValue: 0, type: Number });
anotherStore = await createStore({ name: 'anotherStore', initialValue: 'anotherStore', type: String });
})(); |
1a3332d
to
2ff4d8d
Compare
2ff4d8d
to
6e08477
Compare
6e08477
to
c4fd30e
Compare
Seems useful, but I'm not a fan of IIF. |
I agree I'm not huge fan of using IIFE here too! import { createStore } from "stimulus-store";
export const counterStore = await createStore({ name: 'counterStore', initialValue: 0, type: Number }); this completely legal syntax! the only "gotcha" here is that the store module will not load if the promise is not resolved which can be handled with import { createStore } from "stimulus-store";
try {
export const counterStore = await createStore({ name: 'counterStore', initialValue: 0, type: Number });
} catch (error) {
// do something with the error here
} |
I think this is ok since most modern JS envs including node.js and browsers that support ES modules allow the use of await outsite async functions at top lvl
this PR:
createStore
that simplifies the process of creating new instances of the Store class. It takes an options object as a parameter, which includes the name, initialValue, and type for the new store. This function ensures that the store is properly initialized and that the name is initially a string and converts it to a symbol for uniqueness.index.ts
to export the createStore function instead of the Store class. This change encourages the use of the createStore function to create new stores, which ensures that they are properly initialized and named. This makes the Store class more of an implementation detail, which can help to simplify the API and make the library easier to use.