Skip to content
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

Split ScriptMsg into common and unique messages for main-thread and worker event loops #3734

Closed
jdm opened this issue Oct 20, 2014 · 26 comments
Closed

Comments

@jdm
Copy link
Member

@jdm jdm commented Oct 20, 2014

Enums like CommonScriptMsg, WorkerScriptMsg, and MainThreadScriptMsg, where the latter two have a variant for CommonScriptMsg, would make a lot of sense. Conflating messages that both event loops need to handle with ones that should never be dispatched to one or the other is unnecessary and confusing.

@catchmrbharath
Copy link
Contributor

@catchmrbharath catchmrbharath commented Jan 5, 2015

@jdm What do you mean by "latter two have a variant for CommonScriptMessage" ?

If that is cleared up, I would like to have at go at this issue.

@catchmrbharath
Copy link
Contributor

@catchmrbharath catchmrbharath commented Jan 5, 2015

My idea was to have three enums CommonScriptMsg, WorkerScriptMsg and MainThreadScriptMsg as separate enums. Things that are dispatched to both dedicatedworkerglobalscope and ScriptTask should are present in the CommonScriptMsg.

@jdm
Copy link
Member Author

@jdm jdm commented Jan 5, 2015

@catchmrbharath I mean that we'll have something like enum WorkerScriptMsg { A(whatever), Common(CommonScriptMsg) }.

@jdm jdm added the C-assigned label Jan 5, 2015
@jdm
Copy link
Member Author

@jdm jdm commented Feb 20, 2015

Still working on this, @catchmrbharath?

@catchmrbharath
Copy link
Contributor

@catchmrbharath catchmrbharath commented Feb 22, 2015

@jdm Sorry about that. I am presently not working on this. Can you remove the C-assigned tag? I should have updated the issue long back. Sorry again :).

@jdm jdm removed the C-assigned label Feb 22, 2015
@jdm
Copy link
Member Author

@jdm jdm commented Feb 22, 2015

No problem!

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 5, 2015

I'll take a stab at this issue @jdm, could you just give me some guidance as to what attributes of the ScriptMsg enum should be part of each of the others?

@jdm
Copy link
Member Author

@jdm jdm commented Mar 5, 2015

Thanks @wldcordeiro! Any event that is processed only by the worker event loop in dedicatedworkerglobal.rs (handle_event) belongs in one enum, while any event that is only processed by the event loop in script_task.rs (handle_msgs) belongs in another. Any event that is processed by both of them belongs in a common enum. Does that make sense?

@jdm jdm added the C-assigned label Mar 5, 2015
@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 5, 2015

@jdm that does make sense, but would it also make sense to move the enums to different modules? Or would it be fine if they all were in script_task.rs?

@jdm
Copy link
Member Author

@jdm jdm commented Mar 5, 2015

Moving the worker-related enum to dedicatedworkerglobalscope.rs would make sense. Splitting up parts of script_task.rs is another worthy goal, but probably best done elsewhere.

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 5, 2015

Good to know @jdm. So the end result is that there will be three enums CommonScriptMsg, WorkerScriptMsg, and MainThreadScriptMsg. With WorkerScriptMsg in dedicatedworkerglobalscope.rs and the others being housed in script_task.rs? Splitting up that file would be good but maybe as its own issue?

@jdm
Copy link
Member Author

@jdm jdm commented Mar 5, 2015

Yep!

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 7, 2015

So I've been working on this and wanted to get a little input so I understand better.

The current ScriptMsg enum currently has these variants.

ScriptMsg

  • TriggerFragment
  • TriggerLoad
  • FireTimer
  • ExitWindow
  • DOMMessage
  • WorkerDispatchErrorEvent
  • RunnableMessage
  • RefcountCleanup
  • PageFetchComplete

Now I've been trying to figure out which variants would fit in each of the three new enums, so far I've figured it to this.

CommonScriptMsg

  • RefcountCleanup
  • FireTimer
  • RunnableMessage

MainThreadScriptMsg

  • TriggerLoad
  • TriggerFragment
  • DOMMessage
  • ExitWindow

WorkerScriptMsg

  • WorkerDispatchErrorEvent

Would you say that's on the right path? Also how would you go about using the common variants within the other enums? I saw your example of enum WorkerScriptMsg { A(whatever), Common(CommonScriptMsg) } but how would that look as implemented? Like this?

pub enum WorkerScriptMsg {
    CommonRunnableMessage(CommonScriptMsg::RunnableMessage),
    CommonRefcountCleanup(CommonScriptMsg::RefcountCleanup)
}

Sorry if I'm asking too many questions @jdm, I'm learning Rust's ways and thought this would be a great project to learn from.

@jdm
Copy link
Member Author

@jdm jdm commented Mar 7, 2015

Never be sorry for asking questions! WorkerDispatchErrorEvent can actually be dispatched to both the worker and main thread event loops, so it belongs in the common enum. DOMMessage, on the other hand, only is sent to worker theads, and is actually documented as such,

With respect to the enum, you don't need to duplicate common variants. You can just declare that the enum contains a variant named Common (which contains a value of type CommonScriptMsg). Then you can use WorkerScriptMsg::Common(CommonScriptMsg::RunnableMsg) to actually create such values when necessary.

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 7, 2015

Okay thanks for the response @jdm so if I'm not mistaken then in the Common variant I can have a comma separated list of variants? WorkerScriptMsg::Common(CommonScriptMsg::Runable, CommonScriptMsg::WorkerDispatchErrorEvent) for example? Also I'm curious about how to handle the method handle_msg_from_script. Should I split it into more methods that work with the other enums?

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 8, 2015

So this is what I was thinking for the three ScriptMsg enums, I just wanted to confirm which are common and which are local to the MainScriptMsg and WorkerScriptMsg.

/// Messages used to control script event loops, such as ScriptTask and
/// DedicatedWorkerGlobalScope.
pub enum CommonScriptMsg {
    /// Fires a JavaScript timeout
    /// TimerSource must be FromWindow when dispatched to ScriptTask and
    /// must be FromWorker when dispatched to a DedicatedGlobalWorkerScope
    FireTimer(TimerSource, TimerId),
    /// Sends a message to the Worker object (dispatched to all tasks) regarding error.
    WorkerDispatchErrorEvent(TrustedWorkerAddress, DOMString, DOMString, u32, u32),
    /// Generic message that encapsulates event handling.
    RunnableMsg(Box<Runnable+Send>),
    /// A DOM object's last pinned reference was removed (dispatched to all tasks).
    RefcountCleanup(TrustedReference),
    /// The final network response for a page has arrived.
    PageFetchComplete(PipelineId, Option<SubpageId>, LoadResponse),
}

pub enum MainThreadScriptMsg {
    /// Acts on a fragment URL load on the specified pipeline (only dispatched
    /// to ScriptTask).
    TriggerFragment(PipelineId, String),
    /// Begins a content-initiated load on the specified pipeline (only
    /// dispatched to ScriptTask).
    TriggerLoad(PipelineId, LoadData),
    /// Notifies the script that a window associated with a particular pipeline
    /// should be closed (only dispatched to ScriptTask).
    ExitWindow(PipelineId),
    /// Common Variants to all ScriptMsg types
    Common(
        CommonScriptMsg::FireTimer,
        CommonScriptMsg::WorkerDispatchErrorEvent,
        CommonScriptMsg::RunnableMsg,
        CommonScriptMsg::RefcountCleanup,
        CommonScriptMsg::PageFetchComplete
    ),
}

pub enum WorkerScriptMsg {
    /// Message sent through Worker.postMessage (only dispatched to
    /// DedicatedWorkerGlobalScope).
    DOMMessage(StructuredCloneData),
    /// Common Variants to all ScriptMsg types
    Common(
        CommonScriptMsg::FireTimer,
        CommonScriptMsg::WorkerDispatchErrorEvent,
        CommonScriptMsg::RunnableMsg,
        CommonScriptMsg::RefcountCleanup,
        CommonScriptMsg::PageFetchComplete
    ),
}
@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 8, 2015

I've pushed a commit to my fork, but I'm getting one build error currently. Could I get some help with debugging and/or guidance on what to do.

@jdm
Copy link
Member Author

@jdm jdm commented Mar 8, 2015

Common(
        CommonScriptMsg::FireTimer,
        CommonScriptMsg::WorkerDispatchErrorEvent,
        CommonScriptMsg::RunnableMsg,
        CommonScriptMsg::RefcountCleanup,
        CommonScriptMsg::PageFetchComplete
    ),

This isn't valid, since the arguments to the Common variant are not types, they are values. Enum variants are only allowed to contain types (such as WorkerScriptMsg, or CommonScriptMsg). Does that make more sense?

@jdm
Copy link
Member Author

@jdm jdm commented Mar 8, 2015

Oh, I guess you sorted that out on your own. I guess the PR builds now? :)

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Mar 9, 2015

@jdm PR is building! Looks like it's working. 😄

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Apr 8, 2015

@jdm You can remove the assigned tag. I've been too busy lately to work on this.

@wafflespeanut
Copy link
Member

@wafflespeanut wafflespeanut commented Aug 14, 2015

Now, this has become a total nightmare! (not even close to E-easy) :P

@wldcordeiro
Copy link
Contributor

@wldcordeiro wldcordeiro commented Aug 14, 2015

@wafflespeanut That's kind of what I was running into. It was definitely more complex than the description suggests.

@jdm
Copy link
Member Author

@jdm jdm commented Aug 14, 2015

Yep, my mistake. I'll make sure to think through the ramifications of such changes more thoroughly in the future before proposing them to eager new contributors.

@wafflespeanut
Copy link
Member

@wafflespeanut wafflespeanut commented Aug 14, 2015

Well, on the brighter side, it's gonna be fixed soon :)

bors-servo pushed a commit that referenced this issue Aug 14, 2015
Splitting ScriptMsg into various enums...

... for #3734, which is also one of the oldest issues. (/cc @jdm)

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7006)
<!-- Reviewable:end -->
bors-servo pushed a commit that referenced this issue Aug 15, 2015
Splitting ScriptMsg into various enums...

... for #3734, which is also one of the oldest issues. (/cc @jdm)

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7006)
<!-- Reviewable:end -->
@waddlesplash
Copy link

@waddlesplash waddlesplash commented Aug 15, 2015

PR was merged, this should be closed now.

@Ms2ger Ms2ger closed this Aug 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.