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

createStore hook & Strong typed store values #7

Merged
merged 5 commits into from
Nov 25, 2023

Conversation

omarluq
Copy link
Owner

@omarluq omarluq commented Nov 23, 2023

this PR:

  • introduces a factory function 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.
  • updates Store constructor to accept a type argument and introduces strong typing to the Store class. The Store class now uses a generic T to represent the type of the value it holds. This ensures type safety when getting and setting the store's value. also added type checking in the set method to ensure that the new value matches the expected type.
  • updates 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.

@omarluq omarluq changed the base branch from main to store-async-promise November 23, 2023 22:14
@omarluq omarluq requested a review from tcarac November 23, 2023 22:16
@omarluq omarluq changed the base branch from store-async-promise to main November 23, 2023 23:47
@omarluq omarluq changed the title Strong typed store values createStore hook & Strong typed store values Nov 23, 2023
README.md Outdated Show resolved Hide resolved
@omarluq
Copy link
Owner Author

omarluq commented Nov 24, 2023

@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 });
})();

@omarluq omarluq force-pushed the strong-typed-store-vals branch 3 times, most recently from 1a3332d to 2ff4d8d Compare November 24, 2023 02:53
@tcarac
Copy link
Collaborator

tcarac commented Nov 24, 2023

@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 });
})();

Seems useful, but I'm not a fan of IIF.
If I were to create different stores in different files I would have to rather wrap them in async functions or just create JSON objects. I think I liked the sync version more

@omarluq
Copy link
Owner Author

omarluq commented Nov 24, 2023

@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 });
})();

Seems useful, but I'm not a fan of IIF. If I were to create different stores in different files I would have to rather wrap them in async functions or just create JSON objects. I think I liked the sync version more

I agree I'm not huge fan of using IIFE here too!
on the other hand it makes sense that createStore is async since store.set is also async and can accept a promise or a callback.
This can also be a module lvl await

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 try/catch

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
@omarluq omarluq merged commit b0b77fc into main Nov 25, 2023
4 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants