From 13068842d3f1e089bbcb4c2a3128069ecfca9128 Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Wed, 4 May 2022 11:17:24 -0500 Subject: [PATCH 01/11] type the returned ref --- osmosis/src/setupStore.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 31216e0..e6b8b4f 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -27,9 +27,10 @@ export const configureSetupStore = config => { */ /** - * @param {useCustomHook} useCustomHook + * @template T + * @param {function(object) : T} useCustomHook * @param {SetupStoreConfig} [config = { proxyEnabled: false }] - The setup store config - * @returns {Store} + * @returns {T & Store} */ const setupStore = (useCustomHook, config = {}) => { config = { ..._defaultConfig, ...config }; From 00f2df68f41cd0c02dc021371dffb5bd43f8e4aa Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Wed, 4 May 2022 11:52:35 -0500 Subject: [PATCH 02/11] and context --- osmosis/src/setupStore.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index e6b8b4f..32de29c 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -1,5 +1,5 @@ /* eslint-disable react/prop-types */ -import React, { createContext } from 'react'; +import React, { createContext, useContext } from 'react'; let _defaultConfig = { proxyEnabled: true, legacyReturnStoreAsArray: false }; @@ -20,9 +20,9 @@ export const configureSetupStore = config => { */ /** + * @template T * @typedef Store - * @type {Object} - * @property {Object} Context - The React Context for the store + * @property {React.Context} Context - The React Context for the store * @property {Object} Provider - The higher order component provider for the store */ @@ -30,7 +30,7 @@ export const configureSetupStore = config => { * @template T * @param {function(object) : T} useCustomHook * @param {SetupStoreConfig} [config = { proxyEnabled: false }] - The setup store config - * @returns {T & Store} + * @returns {T & Store} */ const setupStore = (useCustomHook, config = {}) => { config = { ..._defaultConfig, ...config }; From 02e5e596bac0a54ae675a59c0b397f497612429f Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Wed, 4 May 2022 11:52:58 -0500 Subject: [PATCH 03/11] remove import --- osmosis/src/setupStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 32de29c..56984b0 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -1,5 +1,5 @@ /* eslint-disable react/prop-types */ -import React, { createContext, useContext } from 'react'; +import React, { createContext } from 'react'; let _defaultConfig = { proxyEnabled: true, legacyReturnStoreAsArray: false }; From 511eba3177bd37f39a78b8e59dfc202b2817fed3 Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Thu, 19 May 2022 11:03:33 -0500 Subject: [PATCH 04/11] useStore hook returned on Store ref --- osmosis/src/setupStore.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 56984b0..6c50509 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -1,5 +1,5 @@ /* eslint-disable react/prop-types */ -import React, { createContext } from 'react'; +import React, { createContext, useContext } from 'react'; let _defaultConfig = { proxyEnabled: true, legacyReturnStoreAsArray: false }; @@ -19,11 +19,18 @@ export const configureSetupStore = config => { * @property {boolean} proxyEnabled - Determines if the store setup should use proxies internally for the store ref, only if proxies are supported */ +/** + * @template T + * @callback useStore + * @returns {T} + */ + /** * @template T * @typedef Store * @property {React.Context} Context - The React Context for the store * @property {Object} Provider - The higher order component provider for the store + * @property {useStore} useStore - Returns result of useContext(Context) */ /** @@ -107,7 +114,10 @@ const setupStore = (useCustomHook, config = {}) => { return Wrapper; }; + const useStore = () => useContext(StoreContext); + if (!!Proxy && config.proxyEnabled) { + storeProxyObject['useStore'] = useStore; storeProxy = new Proxy(storeProxyObject, { get: (target, property) => { if (property === 'Context') return StoreContext; @@ -121,6 +131,7 @@ const setupStore = (useCustomHook, config = {}) => { storeRef.Context = StoreContext; storeRef.Provider = withStoreContext; storeRef.LegacyProvider = withLegacyStoreContext; + storeRef.useStore = useStore; } return storeProxy || storeRef; From 6cf00c5fc71f51e89003d088c9617a97b7e546a9 Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Thu, 19 May 2022 12:04:20 -0500 Subject: [PATCH 05/11] warning for useStore --- osmosis/src/setupStore.js | 1 + 1 file changed, 1 insertion(+) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 6c50509..8f68f82 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -55,6 +55,7 @@ const setupStore = (useCustomHook, config = {}) => { let store = useCustomHook(props); if (!!store.Context) throw new Error("'Context' property is protected and cannot exist on a store object"); if (!!store.Provider) throw new Error("'Provider' property is protected and cannot exist on a store object"); + if (!!store.useStore) throw new Error("'Provider' property is protected and cannot exist on a store object"); if (storeProxy) { if (storeKey) { From 0d1ea045d7a388682cde213a3cd7228a252bb8ff Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Thu, 19 May 2022 12:13:28 -0500 Subject: [PATCH 06/11] .ref --- osmosis/src/setupStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 8f68f82..76a615d 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -118,7 +118,7 @@ const setupStore = (useCustomHook, config = {}) => { const useStore = () => useContext(StoreContext); if (!!Proxy && config.proxyEnabled) { - storeProxyObject['useStore'] = useStore; + storeProxyObject.ref['useStore'] = useStore; storeProxy = new Proxy(storeProxyObject, { get: (target, property) => { if (property === 'Context') return StoreContext; From 0de7f7926350549cfc94cf779c3565f296e0bc15 Mon Sep 17 00:00:00 2001 From: Kyle Easterling Date: Mon, 23 May 2022 11:14:08 -0500 Subject: [PATCH 07/11] fix error message, add new error message to legacy, add useStore trap --- osmosis/src/setupStore.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 76a615d..55b43df 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -55,7 +55,7 @@ const setupStore = (useCustomHook, config = {}) => { let store = useCustomHook(props); if (!!store.Context) throw new Error("'Context' property is protected and cannot exist on a store object"); if (!!store.Provider) throw new Error("'Provider' property is protected and cannot exist on a store object"); - if (!!store.useStore) throw new Error("'Provider' property is protected and cannot exist on a store object"); + if (!!store.useStore) throw new Error("'useStore' property is protected and cannot exist on a store object"); if (storeProxy) { if (storeKey) { @@ -87,6 +87,7 @@ const setupStore = (useCustomHook, config = {}) => { let store = useCustomHook(props); if (!!store.Context) throw new Error("'Context' property is protected and cannot exist on a store object"); if (!!store.Provider) throw new Error("'Provider' property is protected and cannot exist on a store object"); + if (!!store.useStore) throw new Error("'useStore' property is protected and cannot exist on a store object"); if (storeProxy) { if (storeKey) { @@ -118,12 +119,12 @@ const setupStore = (useCustomHook, config = {}) => { const useStore = () => useContext(StoreContext); if (!!Proxy && config.proxyEnabled) { - storeProxyObject.ref['useStore'] = useStore; storeProxy = new Proxy(storeProxyObject, { get: (target, property) => { if (property === 'Context') return StoreContext; if (property === 'Provider') return withStoreContext; if (property === 'LegacyProvider') return withLegacyStoreContext; + if (property === 'useStore') return target.ref[property] ?? useStore; return target.ref[property]; }, set: (target, property, value) => (target.ref[property] = value) From 3ecef95a3b74e6b2ed7e4d5dd8a51e81dfffac43 Mon Sep 17 00:00:00 2001 From: chaceburnette Date: Mon, 23 May 2022 11:57:21 -0500 Subject: [PATCH 08/11] Update package.json --- osmosis/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmosis/package.json b/osmosis/package.json index d0f6ce9..7131330 100644 --- a/osmosis/package.json +++ b/osmosis/package.json @@ -1,6 +1,6 @@ { "name": "@shipt/osmosis", - "version": "2.0.1", + "version": "2.0.2", "description": "A lightweight state management library for React and React Native", "keywords": [ "react", From c4493bc7bc9be91aead7f978c5526ac5e0494644 Mon Sep 17 00:00:00 2001 From: chaceburnette Date: Mon, 23 May 2022 11:58:28 -0500 Subject: [PATCH 09/11] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c99760a..1934b16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## v2.0.2 +* Updated for better intellisence using templates + ## v2.0.1 * Minor dependency updates From 31d62bfaa173649a1ba6676651cf7037f6cd5a7f Mon Sep 17 00:00:00 2001 From: chaceburnette Date: Tue, 24 May 2022 12:05:39 -0500 Subject: [PATCH 10/11] remove legacy store provider (#103) * remove legacy store provider * update examples to latest version in lock file --- examples/counter-react/yarn.lock | 2 +- osmosis/src/setupStore.js | 35 ++------------------------------ 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/examples/counter-react/yarn.lock b/examples/counter-react/yarn.lock index a752590..4829812 100644 --- a/examples/counter-react/yarn.lock +++ b/examples/counter-react/yarn.lock @@ -1502,7 +1502,7 @@ integrity sha512-DetpxZw1fzPD5xUBrIAoplLChO2VB8DlL5Gg+I1IR9b2wPqYIca2WSUxL5g1vLeR4MsQq1NeWriXAVffV+U1Fw== "@shipt/osmosis@../../osmosis": - version "2.0.0" + version "2.0.2" "@sinonjs/commons@^1.7.0": version "1.8.2" diff --git a/osmosis/src/setupStore.js b/osmosis/src/setupStore.js index 56984b0..80fb2a2 100644 --- a/osmosis/src/setupStore.js +++ b/osmosis/src/setupStore.js @@ -35,6 +35,7 @@ export const configureSetupStore = config => { const setupStore = (useCustomHook, config = {}) => { config = { ..._defaultConfig, ...config }; const StoreContext = createContext(); + // If proxy is not supported let storeRef = {}; @@ -42,37 +43,7 @@ const setupStore = (useCustomHook, config = {}) => { let storeProxy; let storeProxyObject = { ref: {} }; - // Legacy Store Provider - const withLegacyStoreContext = WrappedComponent => props => { - let storeKey = props.storeKey; - let store = useCustomHook(props); - if (!!store.Context) throw new Error("'Context' property is protected and cannot exist on a store object"); - if (!!store.Provider) throw new Error("'Provider' property is protected and cannot exist on a store object"); - - if (storeProxy) { - if (storeKey) { - storeProxyObject.ref[storeKey] = store; - } else storeProxyObject.ref = store; - } else { - if (storeKey) { - storeRef[storeKey] = store; - } else { - for (let key in store) { - storeRef[key] = store[key]; - } - } - } - - const value = config.legacyReturnStoreAsArray ? [store] : store; - - return ( - - - - ); - }; - - // New Store Provider + // Store Provider const withStoreContext = WrappedComponent => { const StoreContextWrapper = ({ children, ...props }) => { let storeKey = props.storeKey; @@ -112,7 +83,6 @@ const setupStore = (useCustomHook, config = {}) => { get: (target, property) => { if (property === 'Context') return StoreContext; if (property === 'Provider') return withStoreContext; - if (property === 'LegacyProvider') return withLegacyStoreContext; return target.ref[property]; }, set: (target, property, value) => (target.ref[property] = value) @@ -120,7 +90,6 @@ const setupStore = (useCustomHook, config = {}) => { } else { storeRef.Context = StoreContext; storeRef.Provider = withStoreContext; - storeRef.LegacyProvider = withLegacyStoreContext; } return storeProxy || storeRef; From 07c692f5b6abebbb16a5f8c75f04cd9f6a282283 Mon Sep 17 00:00:00 2001 From: chaceburnette Date: Tue, 24 May 2022 13:49:49 -0500 Subject: [PATCH 11/11] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1934b16..bc1fe4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## v2.0.2 * Updated for better intellisence using templates +* Added `useStore` to be attached to objects returned from `setupStore`, which wraps and abstracts the need to explicitly call `useContext(StoreContext)` when subscribing to a store. This also helps with mocking store objects when unit testing components subscribed to a store. ## v2.0.1 * Minor dependency updates