This repository contains an implementation of a Bloc pattern for state management using RxJS. It supports dispatching events, managing subscriptions, handling errors, and provides an autoExec
option to manually execute events when needed.
autoExec
.abort
function.To use the Bloc implementation, you'll need to install RxJS:
npm install rxjs
First, create a Bloc
class instance:
import Bloc from './bloc';
const userBloc = new Bloc();
You can dispatch an event like this:
const fetchUserDispatcher = userBloc.dispatch("fetchUser", { autoExec: false });
In this case, the autoExec
option is set to false
, meaning the event will not execute automatically.
autoExec
boolean
true
false
, the event will not automatically execute. You can manually trigger the event using the exec()
method.autoUnsubscribe
boolean
true
true
, the subscription will automatically unsubscribe when the event completes or an error occurs.multiple
boolean
false
true
, allows multiple active subscriptions for the same event.timeout
number
5000
You can manually execute the dispatched event using the exec()
method:
// Manually execute the event after component mounts
onMount(() => fetchUserDispatcher.exec());
// Manually execute the event after a delay
setTimeout(() => {
fetchUserDispatcher.exec();
}, 10000);
You can subscribe to the event's responses (next values and errors) by using the subscribe
method:
fetchUserDispatcher.subscribe({
next: (value) => {
console.log('User data fetched:', value);
},
error: (err) => {
console.error('Error fetching user data:', err);
}
});
If you need to abort an event, you can use the abort
method:
fetchUserDispatcher.abort();
This will immediately stop the event logic from running, and it will notify any subscribed error handlers that the event was aborted.
import { onMount, onDestroy } from 'svelte';
import Bloc from './bloc';
const userBloc = new Bloc();
// Dispatch with autoExec set to false
const fetchUserDispatcher = userBloc.dispatch("fetchUser", { autoExec: false });
// Manually execute the event when component mounts
onMount(() => fetchUserDispatcher.exec());
// Execute the event after 10 seconds
setTimeout(() => {
fetchUserDispatcher.exec();
}, 10000);
// Subscribe to event results
fetchUserDispatcher.subscribe({
next: (data) => {
console.log("Data received:", data);
},
error: (err) => {
console.error("Error occurred:", err);
}
});
// Optionally, abort the event if needed
setTimeout(() => {
fetchUserDispatcher.abort();
}, 5000);
Here is an example of dispatching an event with multiple options:
const fetchUserDispatcher = userBloc.dispatch("fetchUser", { autoExec: false }, {
autoUnsubscribe: true,
multiple: false,
timeout: 10000,
});
// Subscribe to event responses
fetchUserDispatcher.subscribe({
next: (data) => {
console.log("Fetched user data:", data);
},
error: (err) => {
console.error("Error:", err);
}
});
// Manually execute the event after a delay
setTimeout(() => {
fetchUserDispatcher.exec();
}, 5000);
If you want to allow multiple subscriptions to the same event, set the multiple
option to true
:
const fetchUserDispatcher1 = userBloc.dispatch("fetchUser", { autoExec: true }, { multiple: true });
const fetchUserDispatcher2 = userBloc.dispatch("fetchUser", { autoExec: true }, { multiple: true });
// You can now have multiple active subscriptions for the same event
fetchUserDispatcher1.subscribe({ next: data => console.log('Dispatcher 1:', data) });
fetchUserDispatcher2.subscribe({ next: data => console.log('Dispatcher 2:', data) });
You can set a timeout to automatically handle events that take too long to complete:
const fetchUserDispatcher = userBloc.dispatch("fetchUser", { autoExec: false }, { timeout: 3000 });
// Subscribe to event responses
fetchUserDispatcher.subscribe({
next: (data) => {
console.log('User data fetched:', data);
},
error: (err) => {
console.error('Error:', err);
}
});
// Manually execute the event after 2 seconds
setTimeout(() => {
fetchUserDispatcher.exec();
}, 2000);
If the event takes longer than 3 seconds, an error will be triggered automatically.
dispatch(eventName: string, options: object = {}, dispatchOptions: DispatchOptions = {})
Dispatches an event. Accepts the following options:
"fetchUser"
).autoExec
: If set to false
, the event will not automatically execute. Default is true
.autoUnsubscribe
: If set to true
, the subscription will automatically unsubscribe when the event completes. Default is true
.multiple
: If set to true
, allows multiple subscriptions to the same event.timeout
: Timeout duration in milliseconds (default is 5000ms
).exec()
Manually executes the event logic. Useful when autoExec
is set to false
and you want to trigger the event at a later time.
abort()
Aborts the current event if it is still in progress. This will immediately stop the event and trigger an error for any subscribed error handlers.
subscribe(input: { next?: function, error?: function })
Subscribes to the event. You can pass in next
and error
handlers to handle successful results and errors respectively.
dispose()
Cleans up all active subscriptions and abort controllers.
MIT