ChrisPenner / rasa

Extremely modular text editor built in Haskell
GNU General Public License v3.0
614 stars 42 forks source link

Event return values #41

Closed ChrisPenner closed 7 years ago

ChrisPenner commented 7 years ago

This provides the ability for extensions to actually have return values from event listeners; onEveryTrigger is now type:

onEveryTrigger :: (Typeable event, Typeable response, Monoid response) => (event -> Action response) -> Action ListenerId

When you dispatch an event you have the option to return a Monoidal result:

dispatchEvent :: (Typeable event, Typeable response, Monoid response) => event -> Action response

It calls all corresponding listeners; mconcats the results together; or returns mempty if no listeners exist.

I plan to use this to for some nifty things going forwards; for instance collecting styling information from all extensions before rendering; collecting status-bar information; rendering information, etc.

If anyone has opinions I'd love to hear them!

ChrisPenner commented 7 years ago

Thanks for the feedback @samcal! At the moment I'm actually second-guessing whether this is necessary; I believe it could be replace by something on the extension level of abstraction rather than in core; like this:

data Things = Things [Action Thing]
addThing :: Action Thing -> Action ()
addThing thing = do
  overExt addTheThing
  where addTheThing (Things ts) = Thing (thing:ts)

getThings :: Action [Action Thing]
getThings = do
  Things ts <- getExt
  sequence ts

This is probably simpler to understand, has more concrete types, and requires less change to the internal API with equivalent power. Modifications to this system can add the ability to 'unregister' hooks as well simply by storing the Action Things in a Map instead of a list.

ChrisPenner commented 7 years ago

Unneeded as of https://github.com/ChrisPenner/rasa/pull/43