Skip to content

Commit 615d38d

Browse files
committed
minor tweaks
1 parent 808115a commit 615d38d

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

mesop/server/server.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
from mesop.utils.url_utils import remove_url_query_param
2525
from mesop.warn import warn
2626

27+
UI_PATH = "/__ui__"
28+
2729

2830
def configure_flask_app(
29-
*,
30-
prod_mode: bool = True,
31-
exceptions_to_propagate: Sequence[type] = (),
32-
# TODO: plumb this from an env var
33-
is_websockets_enabled=True,
31+
*, prod_mode: bool = True, exceptions_to_propagate: Sequence[type] = ()
3432
) -> Flask:
3533
flask_app = Flask(__name__)
3634

@@ -224,7 +222,7 @@ def generate_data(ui_request: pb.UiRequest) -> Generator[str, None, None]:
224222
error=pb.ServerError(exception=str(e), traceback=format_traceback())
225223
)
226224

227-
@flask_app.route("/__ui__", methods=["POST"])
225+
@flask_app.route(UI_PATH, methods=["POST"])
228226
def ui_stream() -> Response:
229227
# Prevent CSRF by checking the request site matches the site
230228
# of the URL root (where the Flask app is being served from)
@@ -234,7 +232,7 @@ def ui_stream() -> Response:
234232
if not runtime().debug_mode and not is_same_site(
235233
request.headers.get("Origin"), request.url_root
236234
):
237-
abort(403, "Rejecting cross-site POST request to /__ui__")
235+
abort(403, "Rejecting cross-site POST request to " + UI_PATH)
238236
data = request.data
239237
if not data:
240238
raise Exception("Missing request payload")
@@ -251,12 +249,12 @@ def teardown_clear_stale_state_sessions(error=None):
251249
if not prod_mode:
252250
configure_debug_routes(flask_app)
253251

254-
if is_websockets_enabled:
252+
if MESOP_WEBSOCKETS_ENABLED:
255253
from flask_socketio import SocketIO, emit
256254

257255
socketio = SocketIO(flask_app)
258256

259-
@socketio.on("message", namespace="/__ui__")
257+
@socketio.on("message", namespace=UI_PATH)
260258
def handle_message(message):
261259
if not message:
262260
emit("error", {"error": "Missing request payload"})

mesop/server/wsgi_app.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def run(self):
3131
use_reloader=False,
3232
allow_unsafe_werkzeug=True,
3333
)
34-
self._flask_app.run(host=get_local_host(), port=port(), use_reloader=False)
34+
else:
35+
self._flask_app.run(
36+
host=get_local_host(), port=port(), use_reloader=False
37+
)
3538

3639

3740
def create_app(

mesop/web/src/services/channel.ts

+13-16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {getQueryParams} from '../utils/query_params';
2222
import {ExperimentService} from './experiment_service';
2323
import {io, Socket} from 'socket.io-client'; // Import Socket.IO client
2424

25+
const STREAM_END = '<stream_end>';
26+
2527
// Pick 500ms as the minimum duration before showing a progress/busy indicator
2628
// for the channel.
2729
// See: https://github.com/google/mesop/issues/365
@@ -137,17 +139,14 @@ export class Channel {
137139
// Looks like Angular has a bug where it's not intercepting EventSource onmessage.
138140
zone.run(() => {
139141
const data = (e as any).data;
140-
if (data === '<stream_end>') {
142+
if (data === STREAM_END) {
141143
this.eventSource.close();
142144
this.status = ChannelStatus.CLOSED;
143145
clearTimeout(this.isWaitingTimeout);
144146
this.isWaiting = false;
145147
this._isHotReloading = false;
146148
this.logger.log({type: 'StreamEnd'});
147-
if (this.queuedEvents.length) {
148-
const queuedEvent = this.queuedEvents.shift()!;
149-
queuedEvent();
150-
}
149+
this.dequeueEvent();
151150
return;
152151
}
153152

@@ -159,9 +158,6 @@ export class Channel {
159158
});
160159
}
161160

162-
/**
163-
* Initialize WebSocket connection using Socket.IO.
164-
*/
165161
private initWebSocket(initParams: InitParams, request: UiRequest) {
166162
if (this.socket) {
167163
this.status = ChannelStatus.OPEN;
@@ -194,14 +190,11 @@ export class Channel {
194190
const prefix = 'data: ';
195191
const payloadData = (data.data.slice(prefix.length) as string).trimEnd();
196192
zone.run(() => {
197-
if (payloadData === '<stream_end>') {
193+
if (payloadData === STREAM_END) {
198194
this._isHotReloading = false;
199195
this.status = ChannelStatus.CLOSED;
200196
this.logger.log({type: 'StreamEnd'});
201-
if (this.queuedEvents.length) {
202-
const queuedEvent = this.queuedEvents.shift()!;
203-
queuedEvent();
204-
}
197+
this.dequeueEvent();
205198
return;
206199
}
207200

@@ -229,9 +222,6 @@ export class Channel {
229222
});
230223
}
231224

232-
/**
233-
* Handle UiResponse from the server.
234-
*/
235225
private handleUiResponse(
236226
request: UiRequest,
237227
uiResponse: UiResponse,
@@ -408,6 +398,13 @@ export class Channel {
408398
}
409399
}
410400

401+
private dequeueEvent() {
402+
if (this.queuedEvents.length) {
403+
const queuedEvent = this.queuedEvents.shift()!;
404+
queuedEvent();
405+
}
406+
}
407+
411408
checkForHotReload() {
412409
const pollHotReloadEndpoint = async () => {
413410
try {

0 commit comments

Comments
 (0)