Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
5bcdd787
Commit
5bcdd787
authored
Dec 09, 2020
by
Robert Knight
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve several variable names and comments
Make several improvements following PR review.
parent
8108f0fc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
13 deletions
+13
-13
use-store-test.js
src/sidebar/store/test/use-store-test.js
+0
-2
use-store.js
src/sidebar/store/use-store.js
+13
-11
No files found.
src/sidebar/store/test/use-store-test.js
View file @
5bcdd787
...
@@ -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'
});
...
...
src/sidebar/store/use-store.js
View file @
5bcdd787
...
@@ -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.
* c
omponent when that data c
hanges.
*
*
* 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
;
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment