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

Delay execution of dispatched events #359

Open
piranna opened this issue May 1, 2022 · 5 comments
Open

Delay execution of dispatched events #359

piranna opened this issue May 1, 2022 · 5 comments

Comments

@piranna
Copy link

piranna commented May 1, 2022

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Events from server to client needs to have some delay to mimic network delay.

Here is the diff that solved my problem:

diff --git a/node_modules/mock-socket/dist/mock-socket.js b/node_modules/mock-socket/dist/mock-socket.js
index 2478234..414d406 100644
--- a/node_modules/mock-socket/dist/mock-socket.js
+++ b/node_modules/mock-socket/dist/mock-socket.js
@@ -793,13 +793,16 @@ EventTarget.prototype.dispatchEvent = function dispatchEvent (event) {
     return false;
   }
 
-  listeners.forEach(function (listener) {
-    if (customArguments.length > 0) {
-      listener.apply(this$1, customArguments);
-    } else {
-      listener.call(this$1, event);
-    }
-  });
+  delay(function()
+  {
+    listeners.forEach(function (listener) {
+      if (customArguments.length > 0) {
+        listener.apply(this$1, customArguments);
+      } else {
+        listener.call(this$1, event);
+      }
+    });
+  })
 
   return true;
 };

This issue body was partially generated by patch-package.

@Atrue
Copy link
Collaborator

Atrue commented May 24, 2022

@piranna The dispatchEvent function is already called with the delay helper in the websocket. The server event can be called with delay manually in your code.
Can you please give more details of your problem, what the exact place you need the delay?

@piranna
Copy link
Author

piranna commented May 24, 2022

Just the part of the server event. I don't want to provide a manual delay on my code, I want the listeners to be executed at least in the next event loop when they are dispatched, not in the same one. This allows me to register some events in the WebSocket, as I would do normally in real code.

@Atrue
Copy link
Collaborator

Atrue commented May 25, 2022

Make sense, you can create a PR, but add the delay in the server's methods where it's needed, not in the dispatchEvent;
It would be very helpful if you also provide a test or example that demonstrates the problem

@piranna
Copy link
Author

piranna commented May 25, 2022

I think the server method is send(). Why dispatchEvent() is not the correct place? All the server-to-client events are going throught there, so it makes sense to me to add the "network transmission delay" there...

@Atrue
Copy link
Collaborator

Atrue commented May 25, 2022

All the events inside the library are going through dispatchEvent. If the event or the code should be delayed it's wrapped using the delay helper. See websocket.js for examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants