# Custom Subject in Rocket.jl (for mouse events from GLFW)

**URL:** <https://discourse.julialang.org/t/custom-subject-in-rocket-jl-for-mouse-events-from-glfw/65133>\
**Category:** General Usage\
**Tags:** reactiveprogramming\
**Created:** [July 22, 2021, 6:52pm UTC](https://discourse.julialang.org/t/custom-subject-in-rocket-jl-for-mouse-events-from-glfw/65133 "2021-07-22T18:52:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Mitura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_mitura/32/19496_2.png) [@Jakub\_Mitura](https://discourse.julialang.org/u/Jakub_Mitura)\
**Post date:** [July 22, 2021, 6:52pm UTC](https://discourse.julialang.org/t/custom-subject-in-rocket-jl-for-mouse-events-from-glfw/65133/1 "2021-07-22T18:52:47Z")

</div>

Hello I am trying to use the Rocket.jl - reactive programming and I do not understand how to create custom Observable/Subject I have an GLFW function that gives back information when user use scroll and in this case prints either +1 or -1 depending on the direction;

So obviously function acts as a producer of infinite stream - in theory perfect for reactive programming yet I do not have any idea How to connect it - how to create Observable/Subject that will “produce +1 and -1” in order to attach actors that will invoke approriate functions on mouse scroll.

GLFW.SetScrollCallback(window, (\_, xoff, yoff) → print(yoff) )

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [July 22, 2021, 7:01pm UTC](https://discourse.julialang.org/t/custom-subject-in-rocket-jl-for-mouse-events-from-glfw/65133/2 "2021-07-22T19:01:36Z")

</div>

cc @bvdmitri 👆

---

<div class="post-metadata">

**Author:** ![bvdmitri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvdmitri/32/22935_2.png) [@bvdmitri](https://discourse.julialang.org/u/bvdmitri)\
**Post date:** [July 23, 2021, 9:57am UTC](https://discourse.julialang.org/t/custom-subject-in-rocket-jl-for-mouse-events-from-glfw/65133/3 "2021-07-23T09:57:35Z")

</div>

Hey! Thank you for trying out Rocket.jl

My approach would be something like:

```nohighlight

using Rocket
using GLFW

mutable struct ScrollCallbackSubscribable <: Subscribable{Int}
    xoff_previous :: Float64
    yoff_previous :: Float64
    subject :: Subject{Int}

    ScrollCallbackSubscribable() = new(0.0, 0.0, Subject(Int))
end

function Rocket.on_subscribe!(handler::ScrollCallbackSubscribable, actor)
    return subscribe!(handler.subject, actor)
end

function (handler::ScrollCallbackSubscribable)(_, xoff, yoff)
      # I'm not sure how GLFW represents offsets so it might be wrong here
      # here you actually need to implement your scrolling logic
      if handler.yoff_previous > yoff
          next!(handler.subject, 1)
      else
          next!(handler.subject, -1)
      end 
      handler.xoff_previous = xoff
      handler.yoff_previous = yoff
end

const scrollback = ScrollCallbackSubscribable()

GLFW.SetScrollCallback(window, (a, xoff, yoff) -> scrollback(a, xoff, yoff))

# Than later in your application you can do smth like

subscription = subscribe!(scrollback, (direction) -> println(direction))

```

For example:

```nohighlight
function main()
   # Create a window and its OpenGL context
    window = GLFW.CreateWindow(640, 480, "GLFW.jl")

    # Make the window's context current
    GLFW.MakeContextCurrent(window)
    
    GLFW.SetScrollCallback(window, (a, xoff, yoff) -> scrollback(a, xoff, yoff))
    
    subscription = subscribe!(scrollback, (direction) -> println(direction))
    
    # Loop until the user closes the window
    while !GLFW.WindowShouldClose(window)

        # Render here

        # Swap front and back buffers
        GLFW.SwapBuffers(window)

        # Poll for and process events
        GLFW.PollEvents()
    end
    
    unsubscribe!(subscription)
    
    GLFW.DestroyWindow(window) 
end

```

It’s not complete, but I think you get the idea. Also please you can find something useful in the Rocket.jl documentation about [Subjects](https://biaslab.github.io/Rocket.jl/stable/subjects/about/).
