Skip to content

Commit

Permalink
fix: stop ticks when there are no more subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Dec 22, 2023
1 parent 90df2ff commit cf5f854
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import io.github.metacosm.power.sensors.Measures;
import io.github.metacosm.power.sensors.PowerSensor;
import io.github.metacosm.power.sensors.RegisteredPID;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import java.time.Duration;
import java.util.Map;

@ApplicationScoped
public class PowerMeasurer {
Expand All @@ -30,7 +28,9 @@ public Multi<SensorMeasure> startTracking(String pid) throws Exception {
periodicSensorCheck = Multi.createFrom().ticks()
.every(Duration.ofMillis(SAMPLING_FREQUENCY_IN_MILLIS))
.map(sensor::update)
.broadcast().toAllSubscribers()
.broadcast()
.withCancellationAfterLastSubscriberDeparture()
.toAtLeast(1)
.runSubscriptionOn(Infrastructure.getDefaultWorkerPool());
}
final var registeredPID = sensor.register(parsedPID);
Expand Down

0 comments on commit cf5f854

Please sign in to comment.