[wp-trac] [WordPress Trac] #21170: JavaScript actions and filters
WordPress Trac
noreply at wordpress.org
Wed Sep 13 11:10:34 UTC 2017
#21170: JavaScript actions and filters
-----------------------------+------------------------------
Reporter: koopersmith | Owner: adamsilverstein
Type: feature request | Status: reopened
Priority: normal | Milestone: 4.9
Component: General | Version: 3.4
Severity: normal | Resolution:
Keywords: | Focuses: javascript
-----------------------------+------------------------------
Comment (by flixos90):
I'd also like to throw in another suggestion (which might be out of scope
for the initial set of functions, but is worth considering IMO):
Due to a lot of processes in JS being asynchronous, I've been thinking
about asynchronous hooks. The currently suggested API monitors pretty much
how the API works in PHP, however there may be cases in JS where none of
these functions work, such as the following:
* Imagine there's a JS filter for some data.
* Someone wants to use that filter and override that data with content
from an AJAX call, like to the WP REST API.
* This is currently not possible since the AJAX call is asynchronous while
the filter is synchronous and expects an immediate result.
A possible approach to make something like the above work would be
something like `doActionAsync( 'hook', callback, arg1, arg2, moreArgs,
finalArg )`, where `callback` is the callback that should fire after all
hooks attached to the action have been fired (should always be parameter-
less). Internally, `doActionAsync()` would work like this:
* If no hooks are attached, immediately call the callback.
* Otherwise call the first hook and pass a callback to it which will call
the next hook (the attached hook would be responsible for calling the
callback after it has been completed).
* If there is no more next hook, all attached hooks have been run and the
original callback passed to `doActionAsync()` would be called to proceed
with the original logic.
Note that the above approach basically waits for each hook's completion
until calling the next one. An alternative approach could be to call all
hooks immediately and only wait more generally until every one has
completed until calling the original callback.
There could also be a `applyFiltersAsync( 'hook', callback, content, arg1,
arg2, moreArgs, finalArg )` which would work almost the same like the
above, with the exception that the `callback` would always require one
parameter, which should be the `content`. This would be passed on from
attached hook to hook.
Due to the complexity of this ticket already it may be better to think
about the above later and deal with it in a separate ticket after the
foundation is set in stone. I just wanted to leave this here for now. :)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/21170#comment:150>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list