import { dual, pipe } from "@effect/data/Function" import { pipeArguments } from "@effect/data/Pipeable" import type * as Effect from "@effect/io/Effect" import type * as Fiber from "@effect/io/Fiber" import * as core from "@effect/io/internal/core" import * as circular from "@effect/io/internal/effect/circular" import * as metric from "@effect/io/internal/metric" import * as schedule from "@effect/io/internal/schedule" import type * as Metric from "@effect/io/Metric" import type * as PollingMetric from "@effect/io/MetricPolling" import type * as Schedule from "@effect/io/Schedule" import type * as Scope from "@effect/io/Scope" /** @internal */ const PollingMetricSymbolKey = "@effect/io/MetricPolling" /** @internal */ export const PollingMetricTypeId: PollingMetric.PollingMetricTypeId = Symbol.for( PollingMetricSymbolKey ) as PollingMetric.PollingMetricTypeId /** @internal */ export const make = ( metric: Metric.Metric, poll: Effect.Effect ): PollingMetric.PollingMetric => { return { [PollingMetricTypeId]: PollingMetricTypeId, pipe() { return pipeArguments(this, arguments) }, metric, poll } } /** @internal */ export const collectAll = ( iterable: Iterable> ): PollingMetric.PollingMetric, Array, R, E, Array> => { const metrics = Array.from(iterable) return { [PollingMetricTypeId]: PollingMetricTypeId, pipe() { return pipeArguments(this, arguments) }, metric: metric.make( Array.of(void 0) as Array, (inputs: Array, extraTags) => { for (let i = 0; i < inputs.length; i++) { const pollingMetric = metrics[i]! const input = pipe(inputs, (x) => x[i]) pollingMetric.metric.unsafeUpdate(input, extraTags) } }, (extraTags) => Array.from( metrics.map((pollingMetric) => pollingMetric.metric.unsafeValue(extraTags)) ) ), poll: core.forEachSequential(metrics, (metric) => metric.poll) } } /** @internal */ export const launch = dual< ( schedule: Schedule.Schedule ) => ( self: PollingMetric.PollingMetric ) => Effect.Effect>, ( self: PollingMetric.PollingMetric, schedule: Schedule.Schedule ) => Effect.Effect> >(2, (self, schedule) => pipe( pollAndUpdate(self), core.zipRight(metric.value(self.metric)), circular.scheduleForked(schedule) )) /** @internal */ export const poll = ( self: PollingMetric.PollingMetric ): Effect.Effect => self.poll /** @internal */ export const pollAndUpdate = ( self: PollingMetric.PollingMetric ): Effect.Effect => core.flatMap(self.poll, (value) => metric.update(self.metric, value)) /** @internal */ export const retry = dual< ( policy: Schedule.Schedule ) => ( self: PollingMetric.PollingMetric ) => PollingMetric.PollingMetric, ( self: PollingMetric.PollingMetric, policy: Schedule.Schedule ) => PollingMetric.PollingMetric >(2, (self, policy) => ({ [PollingMetricTypeId]: PollingMetricTypeId, pipe() { return pipeArguments(this, arguments) }, metric: self.metric, poll: schedule.retry_Effect(self.poll, policy) })) /** @internal */ export const zip = dual< ( that: PollingMetric.PollingMetric ) => ( self: PollingMetric.PollingMetric ) => PollingMetric.PollingMetric< readonly [Type, Type2], readonly [In, In2], R | R2, E | E2, readonly [Out, Out2] >, ( self: PollingMetric.PollingMetric, that: PollingMetric.PollingMetric ) => PollingMetric.PollingMetric< readonly [Type, Type2], readonly [In, In2], R | R2, E | E2, readonly [Out, Out2] > >(2, (self, that) => ({ [PollingMetricTypeId]: PollingMetricTypeId, pipe() { return pipeArguments(this, arguments) }, metric: pipe(self.metric, metric.zip(that.metric)), poll: core.zip(self.poll, that.poll) }))