[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/plz-event-source 471a01eb83 10/22: Run handler code via
From: |
ELPA Syncer |
Subject: |
[elpa] externals/plz-event-source 471a01eb83 10/22: Run handler code via a timer in the main loop |
Date: |
Wed, 1 May 2024 09:58:49 -0400 (EDT) |
branch: externals/plz-event-source
commit 471a01eb839a6eae59abc12160a51d927887b48f
Author: Roman Scherer <roman@burningswell.com>
Commit: Roman Scherer <roman@burningswell.com>
Run handler code via a timer in the main loop
---
plz-event-source.el | 9 ++++++++-
tests/test-plz-event-source.el | 20 +++++++++++++-------
2 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/plz-event-source.el b/plz-event-source.el
index e0a4b83a9b..942661f1a5 100644
--- a/plz-event-source.el
+++ b/plz-event-source.el
@@ -290,7 +290,14 @@
(with-slots (handlers) source
(dolist (pair handlers)
(when (equal (car pair) (oref event type))
- (funcall (cdr pair) source event)))))
+ (let ((timer (timer-create)))
+ (timer-set-time timer (current-time))
+ (timer-set-function timer
+ (lambda (handler source event)
+ (with-temp-buffer
+ (funcall handler source event)))
+ (list (cdr pair) source event))
+ (timer-activate timer))))))
(defun plz-event-source-dispatch-events (source events)
"Dispatch the EVENTS to the listeners of event SOURCE."
diff --git a/tests/test-plz-event-source.el b/tests/test-plz-event-source.el
index 8921f33b3c..eb470636cd 100644
--- a/tests/test-plz-event-source.el
+++ b/tests/test-plz-event-source.el
@@ -58,7 +58,6 @@
(ert-deftest test-plz-event-source-parse-event-types ()
(with-temp-buffer
-
(let ((event-1 (plz-event-source-event
:type 'add
:data "73857293"
@@ -213,6 +212,7 @@
(should (equal 'close
(plz-event-source-event-type event)))))))))
(with-slots (parser) source
(plz-event-source-open source)
+ (sit-for 0.1)
(should (equal 1 (length open-events)))
(seq-doseq (event open-events)
(with-slots (data type) event
@@ -222,13 +222,16 @@
(plz-event-source-insert source "data: This is the first message.\n")
(should (null message-events))
(plz-event-source-insert source "\n")
+ (sit-for 0.1)
(should (equal (list event-1) message-events))
(plz-event-source-insert source "data: This is the second message,
it\n")
(plz-event-source-insert source "data: has two lines.\n")
(plz-event-source-insert source "\n")
(plz-event-source-insert source "data: This is the third message.\n")
+ (sit-for 0.1)
(should (equal (list event-2 event-1) message-events))
(plz-event-source-close source)
+ (sit-for 0.1)
(should (equal 1 (length close-events)))
(seq-doseq (event close-events)
(with-slots (data type) event
@@ -289,6 +292,9 @@
(should (equal 200 (plz-response-status data)))
(should (null (plz-response-body data)))))
(should (equal 0 (length error-events)))
+ (should (equal "Hello! How can I assist you today?"
+ (plz-event-source-test-openai-extract-content
message-events)))
+ (sit-for 0.1)
(should (equal 1 (length close-events)))
(seq-doseq (event close-events)
(with-slots (data type) event
@@ -299,9 +305,7 @@
(should (equal (list 'open 'message 'close)
(cl-remove-duplicates
(seq-map #'plz-event-source-event-type
- (reverse all-events)))))
- (should (equal "Hello! How can I assist you today?"
- (plz-event-source-test-openai-extract-content
message-events)))))))
+ (reverse all-events)))))))))
(ert-deftest test-plz-event-source:text/event-stream ()
(plz-event-source-test-with-mock-response (plz-event-source-test-response
"text/event-stream/openai-hello.txt")
@@ -332,6 +336,7 @@
(should (plz-response-p data))
(should (equal 200 (plz-response-status data)))
(should (null (plz-response-body data)))))
+ (sit-for 0.1)
(should (equal 0 (length error-events)))
(should (equal 1 (length close-events)))
(seq-doseq (event close-events)
@@ -364,6 +369,7 @@
:finally (lambda () (push t finally))
:then (lambda (object) (push object then)))))
(plz-event-source-test-wait process)
+ (sit-for 0.1)
(should (null else))
(should (equal '(t) finally))
(should (equal 1 (length open-events)))
@@ -373,6 +379,8 @@
(should (plz-response-p data))
(should (equal 200 (plz-response-status data)))
(should (null (plz-response-body data)))))
+ (should (equal 4 (length message-events)))
+ (should (equal "🙂" (plz-event-source-test-openai-extract-content
message-events)))
(should (equal 0 (length error-events)))
(should (equal 1 (length close-events)))
(seq-doseq (event close-events)
@@ -380,9 +388,7 @@
(should (equal 'close type))
(should (plz-response-p data))
(should (equal 200 (plz-response-status data)))
- (should (null (plz-response-body data)))))
- (should (equal 4 (length message-events)))
- (should (equal "🙂" (plz-event-source-test-openai-extract-content
message-events))))))
+ (should (null (plz-response-body data))))))))
;;;; footer
- [elpa] externals/plz-event-source 436a615c64 05/22: Rename buffer and http event source, (continued)
- [elpa] externals/plz-event-source 436a615c64 05/22: Rename buffer and http event source, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 8e1b769f08 20/22: Tweak vendor note, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 544432be2d 22/22: Split manual and README, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source a1f3cc7da9 14/22: Prefix parser functions with plz-event-source-parser, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source abfc1f5271 18/22: Run tests on 29.3 as well, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source ae322c21ed 01/22: Initial commit, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 1c108a456a 15/22: Move private function, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 7d3c6a19d2 17/22: Update example in README, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 558ba9a77e 19/22: Add vendor note, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source de99b89df6 21/22: ELPA preparations, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 471a01eb83 10/22: Run handler code via a timer in the main loop,
ELPA Syncer <=
- [elpa] externals/plz-event-source 9654a785ef 11/22: Call handler functions only with the event object, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source c3874f9ad2 16/22: Make dispatch functions private, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 4784930adc 13/22: Make insert function private, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 2be6b05dab 08/22: Set coding system for media type and decode each chunk, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source 5fd27ca46e 09/22: Checkout plz with :filter option, ELPA Syncer, 2024/05/01
- [elpa] externals/plz-event-source e7664aa7b0 12/22: Use plz-media-type-decode-coding-string, ELPA Syncer, 2024/05/01