Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
converse.js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
converse.js
Commits
75d64c04
Commit
75d64c04
authored
Apr 06, 2020
by
JC Brand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a hooks API
for triggering interceptable events with the goal of modifying data.
parent
121a3f9b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
0 deletions
+29
-0
src/headless/converse-core.js
src/headless/converse-core.js
+29
-0
No files found.
src/headless/converse-core.js
View file @
75d64c04
...
...
@@ -440,6 +440,35 @@ const api = _converse.api = {
}
},
/**
* Triggers a hook which can be intercepted by registered listeners via
* {@link _converse.api.listen.on} or {@link _converse.api.listen.once}.
* (see [_converse.api.listen](http://localhost:8000/docs/html/api/-_converse.api.listen.html)).
* A hook is a special kind of event which allows you to intercept a data
* structure in order to modify it, before passing it back.
* @async
* @method _converse.api.hook
* @param {string} name - The hook name
* @param {...any} context - The context to which the hook applies (could be for example, a {@link _converse.ChatBox)).
* @param {...any} data - The data structure to be intercepted and * modified by the hook listeners.
*/
hook
(
name
,
context
,
data
)
{
const
events
=
_converse
.
_events
[
name
]
||
[];
if
(
events
.
length
)
{
// Create a chain of promises, with each one feeding its output to
// the next. The first input is a promise with the original data
// sent to this hook.
const
o
=
events
.
reduce
((
o
,
e
)
=>
o
.
then
(
d
=>
e
.
callback
(
context
,
d
)),
Promise
.
resolve
(
data
));
o
.
catch
(
e
=>
{
log
.
error
(
e
)
throw
e
;
});
return
o
;
}
else
{
return
data
;
}
},
/**
* This grouping collects API functions related to the current logged in user.
*
...
...
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