Refactor: move core/edit-post INIT effect to use action-generators and controls#14740
Refactor: move core/edit-post INIT effect to use action-generators and controls#14740
core/edit-post INIT effect to use action-generators and controls#14740Conversation
aduth
left a comment
There was a problem hiding this comment.
My initial reaction is: While the subscribe control is the most faithful adaptation of the current implementation, is it something where we might want to lean instead on other patterns around subscribed component behaviors, optionally as data-only components (example)?
One concern with the proposed subscriptions (and the same applies for the current implementation): How do these handlers become unsubscribed (e.g. in a potential future more single-page app long-lived sessions). I could grant that implementing this as a component is slightly more cumbersome for what amounts to the closest semblance of a true "side effect", but it at least has a built-in mechanism for subscription lifetime being tied to that of the app element root (if considering initialize as simply the public interface to mount an <EditPost /> component).
cc @youknowriad
| Returns an action object used signal a successful meta box update. No newline at end of file | ||
| Returns an action object used signal a successful meta box update. | ||
|
|
||
| ### init |
There was a problem hiding this comment.
I might recommend initialize, both for (a) alignment to the wp.editor.initializeEditor function and (b) to avoid abbreviations which aren't always obvious to the reader.
There was a problem hiding this comment.
Yes, that makes sense. I only went with init because it matched the original side-effect name INIT.
|
|
||
| applyMiddlewares( store ); | ||
| store.dispatch( { type: 'INIT' } ); | ||
| store.dispatch( actions.init() ); |
There was a problem hiding this comment.
Is there any challenge in moving this dispatch to be as part of the initialize function in edit-post/src/index.js?
There was a problem hiding this comment.
I don't know. Certainly could try it. I only did it here to preserve existing behaviour as much as possible. The refactors are already a bit of a chore without considerations for other improvements that could be made. So one the goals with these refactors are:
- preserve existing behaviour as much as possible
- only make changes if it's necessary to keep things working or if there are clear obvious non-complex benefits to doing so.
Certainly, it would make sense to do the dispatch on initialize though, so I could give it a go (especially since its exposed as a action whereas before INIT was only a side effect encapsulated in the store - hence why it was probably here).
I thought something similar as I did this refactor. But as noted in another comment, I've imposed a limit on these refactors to preserve existing behaviour in the implemented control/action. Certainly there's room for further re-thinking on this and implementing something like a data component as you pointed out in another iteration after current effects have been eliminated. Doing these refactors certainly does raise the profile of some of these lingering issues in the current stable of effects. |
aduth
left a comment
There was a problem hiding this comment.
But as noted in another comment, I've imposed a limit on these refactors to preserve existing behaviour in the implemented control/action.
That's fair. I appreciate the pushback in limiting the scope to solving one problem (effects) and not all problems (initialization flow).
On that then, my only hesitation would be about impact to the public API. If we're not certain we want to keep around an initialization action, then we should mark it as either experimental or unstable. For something like the subscribe controls, while I'm not sure I'd want to see it develop into a common pattern (there was some recent mention of something of a data-controls package for common controls), I think it'd be fine enough as an internal implementation detail here in the meantime.
packages/edit-post/CHANGELOG.md
Outdated
| @@ -1,9 +1,13 @@ | |||
| ## V.V.V (Unreleased) | |||
| ## 3.3.0 (Unreleased) | |||
There was a problem hiding this comment.
Per this week's Core JS chat, we should leave this as unset (or change to "Unreleased" or "Master").
| @@ -0,0 +1,2 @@ | |||
| export const STORE_KEY = 'core/edit-post'; | |||
| export const VIEW_AS_LINK_SELECTOR = '#wp-admin-bar-view a'; | |||
There was a problem hiding this comment.
I'd recommend some JSDoc for the constants.
939fc9c to
0f663ee
Compare
|
@aduth this was rebased and I addressed all your comments. I think this is pretty much ready for merge (assuming e2e tests pass). |
| * receives the registry. | ||
| * @return {Object} control descriptor. | ||
| */ | ||
| export function __unstableSubscribe( listenerCallback ) { |
There was a problem hiding this comment.
Since it's not exposed on a public API, I suppose it doesn't strictly need the prefix. But could be a good indicator for internal use as well.
| } | ||
| ) ); | ||
| // hide/show the sidebar depending on size of viewport. | ||
| yield __experimentalAdjustSidebar(); |
There was a problem hiding this comment.
Is the main issue with not being able to inline the implementation here like with other subscribers that it needs to have an initial call to adjuster? I'd wonder if there might be a simpler option to creating the separate action creator and controls, in enhancing onChangeListener to accept an optional argument to trigger an initial call to the callback.
diff --git a/packages/edit-post/src/store/utils.js b/packages/edit-post/src/store/utils.js
index 86b043327..3e0d287dc 100644
--- a/packages/edit-post/src/store/utils.js
+++ b/packages/edit-post/src/store/utils.js
@@ -2,12 +2,19 @@
* Given a selector returns a functions that returns the listener only
* if the returned value from the selector changes.
*
- * @param {function} selector Selector.
- * @param {function} listener Listener.
- * @return {function} Listener creator.
+ * @param {Function} selector Selector.
+ * @param {Function} listener Listener.
+ * @param {boolean} initial Whether to call listener immediately.
+ *
+ * @return {Function} Listener creator.
*/
-export const onChangeListener = ( selector, listener ) => {
+export const onChangeListener = ( selector, listener, initial ) => {
let previousValue = selector();
+
+ if ( initial ) {
+ listener( previousValue );
+ }
+
return () => {
const selectedValue = selector();
if ( selectedValue !== previousValue ) {There was a problem hiding this comment.
oh ya, that would simplify things, good call.
There was a problem hiding this comment.
Added the flag in 8287c2b and implemented in db6f2ca
|
Sorry, I had the previous two comments as pending, and thought I'd submitted them earlier. I don't want this to be held up, but I do wonder if the small revision to |
This leaves room for future refactors that could eliminate these.
0f663ee to
84ed847
Compare
|
There's a failing e2e test that doesn't appear to be related to any work in this branch, but restarting the test doesn't seem to fix it. It's not immediately clear to me what the test is supposed to cover (I don't know what a "dirty dialog" is) so I can't access whether it's an actual failing issue from this branch. |
|
Well looks like restarting it two times did the trick. The test that was failing was: gutenberg/packages/e2e-tests/specs/change-detection.test.js Lines 215 to 229 in 87d7fe4 |
| * CSS selector string for the admin bar view post link anchor tag. | ||
| * @type {string} | ||
| */ | ||
| export const VIEW_AS_LINK_SELECTOR = '#wp-admin-bar-view a'; |
There was a problem hiding this comment.
Aside: It wasn't introduced here, but "View" shows for draft posts with a different ID wp-admin-bar-preview (view vs. preview) and thus wouldn't be updated.
There was a problem hiding this comment.
Aside: It wasn't introduced here, but "View" shows for draft posts with a different ID
wp-admin-bar-preview(viewvs.preview) and thus wouldn't be updated.
Follow-up issue: #15402
Description
As a part of the ongoing effort to refactor effects to controls, this is the first pull to tackle the
core/edit-poststore. Pulls will be granular to aid with easier, shorter reviews.In this pull the
INITefffect has been refactored. Some noteworthy things in this pull:subscribecontrol that gives an idea for how it can be implemented.INITeffect had no unit-tests (understandably). With these changes its now much easier to mock the registry and test the logic of initialization.How has this been tested?
This pull impacts the following things:
edit-post/blockgeneral sidebar when a block is selected/un-selected. Can be tested by selecting a block, the sidebar should switch from "Document" to "Block".Types of changes
This is a non-breaking change.
Checklist: