# streamMixin
# Feature Description
Provides SSE (Server-Sent Events) data parsing and interruption capabilities, supporting the following features:
- Supports parsing and processing of SSE data streams
- Provides a complete set of lifecycle events
- Supports interruption control for streaming requests
- Automatically handles stream data status
- Error handling mechanism
- Supports custom transform streams
- Supports a utility function version for non-component scenarios
# Import and Usage
import { streamMixin } from 'vue-element-ui-x';
export default {
mixins: [streamMixin],
// ...
};
Note
The import method in the following example is to resolve an error during the documentation site's packaging. Under normal circumstances, please import it in the standard way.
# Usage Examples
# SSE Stream Data Example
Demonstrates how to handle SSE stream data and display Markdown content in the Bubble component.
Copy
# SIP Protocol Data Handling Example
Demonstrates how to handle SIP protocol stream data and use a custom transform stream.
Copy
# Mixin Properties
Parameter | Description | Type | Default |
---|---|---|---|
streamData | Stream data array | Array | [] |
streamError | Stream error info | Error | null |
streamLoading | Stream processing status | Boolean | false |
# Mixin Methods
Method Name | Description | Parameters | Return Value |
---|---|---|---|
startStream | Starts a streaming request | options: { readableStream, transformStream } | Promise |
cancelStream | Cancels the streaming request | - | - |
resetStream | Resets the stream state | - | - |
createStreamProcessor | A utility method to create a stream processor | readableStream, transformStream | Object |
# Events
Event Name | Description | Callback Parameter |
---|---|---|
stream-data | Fired when new data is received | item: The stream data item |
stream-complete | Fired when the stream is complete | data: All stream data |
stream-error | Fired when a stream error occurs | error: The error object |
stream-finish | Fired when stream processing ends | - |
stream-cancel | Fired when the stream is canceled | - |
# Utility Function Version
For non-component scenarios, a utility function version is provided:
import { createStreamUtils } from 'vue-element-ui-x';
// Create stream processing utilities
const streamUtils = createStreamUtils({
onData: item => console.log('Data:', item),
onComplete: allData => console.log('Complete:', allData.length, 'items'),
onError: error => console.error('Error:', error),
onCancel: () => console.log('Canceled'),
onFinish: () => console.log('Processing finished'),
});
// Use utilities to process stream data
async function processStreamData() {
const response = await fetch('/api/stream');
await streamUtils.startStream({ readableStream: response.body });
}
// Cancel processing
function cancelProcessing() {
streamUtils.cancel();
}
// Get current status
function getStatus() {
return {
isLoading: streamUtils.state.loading,
hasError: !!streamUtils.state.error,
dataCount: streamUtils.state.data.length,
};
}
# Precautions
- This mixin depends on the modern browser's Streams API. Please ensure your browser supports it before use.
- Errors that occur during stream processing are provided through the
streamError
property and thestream-error
event. - Relevant resources are automatically cleaned up when the component is destroyed.
- Custom transform streams need to implement
transform
and optionalflush
methods. - For processing large amounts of data streams, it is recommended to implement throttling or batching to improve performance.