(fs = AppNodeBuilder.build(FSUtil.node))
| 137 | } |
| 138 | |
| 139 | function createLayer(fs = AppNodeBuilder.build(FSUtil.node)) { |
| 140 | return Layer.fresh( |
| 141 | Layer.effect( |
| 142 | Service, |
| 143 | Effect.gen(function* () { |
| 144 | const file = yield* FSUtil.Service |
| 145 | |
| 146 | const read = Effect.fn("RunVariant.read")(function* () { |
| 147 | return yield* file.readJson(MODEL_FILE).pipe( |
| 148 | Effect.map(state), |
| 149 | Effect.catchCause(() => Effect.succeed(state(undefined))), |
| 150 | ) |
| 151 | }) |
| 152 | |
| 153 | const resolveSavedVariant = Effect.fn("RunVariant.resolveSavedVariant")(function* (model: RunInput["model"]) { |
| 154 | if (!model) { |
| 155 | return undefined |
| 156 | } |
| 157 | |
| 158 | return (yield* read()).variant?.[variantKey(model)] |
| 159 | }) |
| 160 | |
| 161 | const saveVariant = Effect.fn("RunVariant.saveVariant")(function* ( |
| 162 | model: RunInput["model"], |
| 163 | variant: string | undefined, |
| 164 | ) { |
| 165 | if (!model) { |
| 166 | return |
| 167 | } |
| 168 | |
| 169 | const current = yield* read() |
| 170 | const next = { |
| 171 | ...current.variant, |
| 172 | } |
| 173 | const key = variantKey(model) |
| 174 | if (variant) { |
| 175 | next[key] = variant |
| 176 | } |
| 177 | |
| 178 | if (!variant) { |
| 179 | delete next[key] |
| 180 | } |
| 181 | |
| 182 | yield* file |
| 183 | .writeJson(MODEL_FILE, { |
| 184 | ...current, |
| 185 | variant: next, |
| 186 | }) |
| 187 | .pipe(Effect.orElseSucceed(() => undefined)) |
| 188 | }) |
| 189 | |
| 190 | return Service.of({ |
| 191 | resolveSavedVariant, |
| 192 | saveVariant, |
| 193 | }) |
| 194 | }), |
| 195 | ).pipe(Layer.provide(fs)), |
| 196 | ) |
no test coverage detected