Commit 5bcdd787 authored by Robert Knight's avatar Robert Knight

Improve several variable names and comments

Make several improvements following PR review.
parent 8108f0fc
...@@ -179,8 +179,6 @@ describe('sidebar/store/use-store', () => { ...@@ -179,8 +179,6 @@ describe('sidebar/store/use-store', () => {
const { proxy } = renderTestComponent(); const { proxy } = renderTestComponent();
assert.ok(proxy);
// Test proxied selector method. // Test proxied selector method.
assert.deepEqual(proxy.getThing('foo'), { id: 'foo' }); assert.deepEqual(proxy.getThing('foo'), { id: 'foo' });
assert.deepEqual(proxy.getThing('bar'), { id: 'bar' }); assert.deepEqual(proxy.getThing('bar'), { id: 'bar' });
......
...@@ -131,8 +131,8 @@ class CacheEntry { ...@@ -131,8 +131,8 @@ class CacheEntry {
* extract data from the store and call actions on it. * extract data from the store and call actions on it.
* *
* Unlike using the `store` service directly, the wrapper tracks what data from * Unlike using the `store` service directly, the wrapper tracks what data from
* the store the current component uses and re-renders the component when it * the store the current component uses, via selector methods, and re-renders the
* changes. * component when that data changes.
* *
* The returned wrapper has the same API as the store itself. * The returned wrapper has the same API as the store itself.
* *
...@@ -158,9 +158,11 @@ export function useStoreProxy() { ...@@ -158,9 +158,11 @@ export function useStoreProxy() {
const [, forceUpdate] = useReducer(x => x + 1, 0); const [, forceUpdate] = useReducer(x => x + 1, 0);
// Cache of store method calls made by the current UI component and associated // Cache of store method calls made by the current UI component and associated
// results. This is currently just an array on the assumption that it will // results. There is one entry per combination of method and arguments.
//
// This is currently just an array on the assumption that it will
// only have a small number of entries. It could be changed to a map keyed // only have a small number of entries. It could be changed to a map keyed
// by method name to handle many entries better. // by method to handle many entries better.
const cacheRef = useRef(/** @type {CacheEntry[]} */ ([])); const cacheRef = useRef(/** @type {CacheEntry[]} */ ([]));
const cache = cacheRef.current; const cache = cacheRef.current;
...@@ -171,13 +173,13 @@ export function useStoreProxy() { ...@@ -171,13 +173,13 @@ export function useStoreProxy() {
const wrappedMethods = {}; const wrappedMethods = {};
/** /**
* @param {typeof store} target * @param {typeof store} store
* @param {string} prop * @param {string} prop
*/ */
const get = (target, prop) => { const get = (store, prop) => {
const original = target[prop]; const method = store[prop];
if (typeof original !== 'function') { if (typeof method !== 'function') {
return original; return method;
} }
// Check for pre-existing method wrapper. // Check for pre-existing method wrapper.
...@@ -196,11 +198,11 @@ export function useStoreProxy() { ...@@ -196,11 +198,11 @@ export function useStoreProxy() {
// Call the original method. It may be a selector that does not modify // Call the original method. It may be a selector that does not modify
// the store but returns a result, or an action that modifies the state. // the store but returns a result, or an action that modifies the state.
const prevState = store.getState(); const prevState = store.getState();
const result = original.apply(target, args); const result = method.apply(store, args);
const newState = store.getState(); const newState = store.getState();
if (prevState === newState) { if (prevState === newState) {
cache.push(new CacheEntry(prop, original, args, result)); cache.push(new CacheEntry(prop, method, args, result));
} }
return result; return result;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment