-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Exposing Prismic Events #68
Comments
We can talk about it but I would not expose events like this, it would be a complete hack to me, we need a proper channel for this kind of use. |
Yes that's what I felt too, let's think about a dedicated "public" event layer then 🎉 Updated RFC to reflect that~ |
Following what we talked about this morning: Having events could be misleading to users and could lead to more harm than anything because of toolbar complexity. The middleware approach sounds safer and more obvious to use. These middlewares would be executed if found inside a We'll start by working on two events:
|
Instead of
That way you expose only two functions Implementation could be as simple as: // utils/pubsub.js
const events = new Map()
// also exported in window.prismic
export function on(name, cb) {
const cbs = events.get(name)
if (cbs) cbs.add(cb)
else events.set(name, new Set([cb]))
}
// also exported in window.prismic
export function off(name, cb) {
const subs = events.get(name)
if (subs) {
subs.delete(cb)
}
}
export function dispatch(name, data) {
const subs = events.get(name)
if (!subs) return
for (const s of subs) {
s(data)
}
} |
I agree on the |
Yes it's true, this is the drawback of pubsub. In that case exposing functions is better, I agree. |
Shipped in #93 |
Summary
Making some Prismic events accessible through an interface.
Examples
Examples with
window.prismic.addEventListener
andwindow.prismic.removeEventListener
as an interface.Refreshing Nuxt.js without a hard reload (or with any framework capable of behaving like an SPA):
Closing preview session cookies on Next.js (would fix #64):
Motivation
Exposing some Prismic events would allow users to extend or alter previews or other features behaviors of the toolbar, therefore allowing improved experience.
Implementation
The "hacky" way would be to just expose
this.events
,addEventListener
andremoveEventListener
methods in it although as pointed out it's definitely better to work on a proper, public, event layer.Preventing Default Behavior
For some events like
update_preview
users need to take ownership of default behavior (location hard reload), therefore apreventDefault
API should be included.Events
TBD
Old & not relevant anymore
Unknowns
update_preview
event, if preview ref needs to be updated in the process it could make the SPA refresh harder to achieve 🤔update_preview
event is currently not an event, therefore it needs to be reworked into one (or wrapped and fired onif (reload)
)The text was updated successfully, but these errors were encountered: