import { KyselyCircuitBreakerStorageAdapter } from "@daiso-tech/core/circuit-breaker/kysely-circuit-breaker-storage-adapter";
import { DatabaseCircuitBreakerAdapter } from "@daiso-tech/core/circuit-breaker/database-circuit-breaker-adapter";
import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"
import Sqlite from "better-sqlite3";
import { Kysely, SqliteDialect } from "kysely";
const serde = new Serde(new SuperJsonSerdeAdapter());
const circuitBreakerStorageAdapter = new KyselyCircuitBreakerStorageAdapter({
kysely: new Kysely({
dialect: new SqliteDialect({
database: new Sqlite("local.db"),
}),
}),
serde
});
// You need initialize the adapter once before using it.
await circuitBreakerStorageAdapter.init();
const circuitBreakerAdapter = new DatabaseCircuitBreakerAdapter({
adapter: circuitBreakerStorageAdapter
});
const circuitBreakerProvider = new CircuitBreakerProvider({
adapter: circuitBreakerAdapter
})
The addListener method is used for listening to a BaseEvent.
The same listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
The create method is used to create an instance of ICircuitBreaker.
The listenOnce method is used for listening to a BaseEvent once.
The removeListener method is used for stop listening to a BaseEvent.
Removing unadded listener will have no effect and nothing will occur.
The subscribe method is used for listening to a BaseEvent and it returns a cleanup function that removes listener when called.
The same listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
The subscribeOnce method is used for listening to a BaseEvent once and it returns a cleanup function that removes listener when called.
The same listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
CircuitBreakerProviderclass can be derived from anyICircuitBreakerAdapter.Note the
ICircuitBreakerinstances created by theCircuitBreakerProviderclass are serializable and deserializable, allowing them to be seamlessly transferred across different servers, processes, and databases. This can be done directly usingISerderRegisteror indirectly through components that rely onISerderRegisterinternally.IMPORT_PATH:
"@daiso-tech/core/circuit-breaker"