Pattern to trigger one callback per page when some block is present #52514
Replies: 5 comments 8 replies
-
Back in the "old days", for that kind of initialization, we had in Frontity some lifecycle initialization actions: They helped packages do their stuff in different moments of the Frontity set-up. And it is pretty much what we want to do here, am I right? 😄 So, we could use that approach here as well, e.g.: store({
actions: {
namespace: {
// This special action could run after the store is ready.
init: ({ state }) => {
library(state.namespace.someValue);
},
},
},
}); Another thing I have in mind (and I prefer) is to add a second argument to the store(
{
state: {
namespace: { /* &c. */ },
},
},
// Optional object with callback definitions.
{
init: ({ state }) => {
library(state.namespace.someValue);
},
}
); Those would receive the whole store as argument and run once at the appropriate moment. For the case you mentioned, @luisherranz, in which different types of blocks could want to run the same initialization, you could solve that by defining an action and calling it inside the // Block A
store({
actions: {
namespace: {
init: ({ state }) => {
library(state.namespace.someValue);
},
},
},
});
// Block B, C, etc.
store({ /* store props (if any) */ }, {
init: (store) {
store.actions.namespace.init(store);
},
}); We could also define different initialization callbacks ( In any case, for the initialization purposes you mentioned, I don't think requiring adding a directive in the HTML would be a good DX. If it is for certain block instances, though, then it's OK. 👍 |
Beta Was this translation helpful? Give feedback.
-
I feel the approach David suggests seems easier regarding DX 🙂 Does it make sense to include the different initialization callbacks ( On another note, maybe calling everything |
Beta Was this translation helpful? Give feedback.
-
I think we should discard the wp_add_body_directive( 'data-wp-context', '{ ... some global state ... }' ); |
Beta Was this translation helpful? Give feedback.
-
@DAreRodz started a PR to add this functionality: |
Beta Was this translation helpful? Give feedback.
-
This is no longer needed with the new We will remove it from the API. |
Beta Was this translation helpful? Give feedback.
-
Sometimes, blocks need to do something global on a page.
Imagine a block that needs to run some util from an external library. If only one block is included in the page, we could get away with
data-wp-init
:But if multiple blocks are included, the
data-wp-init
will be run multiple times.We could add some initialization flag, like this:
But it's not very clean and we will include
data-wp-init
in the HTML more times than necessary.One idea that we have is to let store initialization be a function:
It's not bad, although in this case,
state
may not be fully initialized ifsomeValue
comes from anotherstore()
call, so people would need to be careful about that, and it might force us into introducing some sort of priority system for thestore()
calls.Also, there may be a case where this function needs to be run, not only by one block but by many blocks. In that case, I guess all the blocks should import the same JavaScript file that does a call to
store()
to initialize the external library.Another idea could be to let blocks add a
data-wp-init
directive to thebody
, for example:With a PHP API like this:
Any other ideas?
Beta Was this translation helpful? Give feedback.
All reactions