|
| 1 | +/** |
| 2 | + * @module Effect/History/Live |
| 3 | + * @description |
| 4 | + * Live implementation of HistoryService backed by Mountain's navigation |
| 5 | + * history store via Tauri IPC. Drives back/forward navigation in the editor. |
| 6 | + * |
| 7 | + * IPC channels (WindServiceHandlers.rs): |
| 8 | + * history:goBack → navigate to previous location |
| 9 | + * history:goForward → navigate to next location |
| 10 | + * history:canGoBack → whether back navigation is available |
| 11 | + * history:canGoForward → whether forward navigation is available |
| 12 | + * history:push → push a URI onto the stack |
| 13 | + * history:clear → clear the navigation stack |
| 14 | + * history:getStack → return the full history stack |
| 15 | + */ |
| 16 | + |
| 17 | +import { Effect, Layer } from "effect"; |
| 18 | + |
| 19 | +import { IPC } from "../IPC.js"; |
| 20 | +import type { HistoryService } from "./Interface/HistoryService.js"; |
| 21 | +import { HistoryServiceTag } from "./Tag/HistoryServiceTag.js"; |
| 22 | +import type { HistoryProblem } from "./Type/HistoryProblem.js"; |
| 23 | + |
| 24 | +const MakeHistoryProblem = (error: unknown): HistoryProblem => ({ |
| 25 | + _tag: "HistoryOperationFailed", |
| 26 | + error: error instanceof Error ? error : new Error(String(error)), |
| 27 | +}); |
| 28 | + |
| 29 | +export const LiveHistoryServiceLayer = Layer.effect( |
| 30 | + HistoryServiceTag, |
| 31 | + Effect.gen(function* () { |
| 32 | + const IPCService = yield* IPC; |
| 33 | + |
| 34 | + const Service: HistoryService = { |
| 35 | + GoBack: () => |
| 36 | + IPCService.invoke("history:goBack")([]).pipe( |
| 37 | + Effect.map(() => undefined as void), |
| 38 | + Effect.mapError(MakeHistoryProblem), |
| 39 | + ), |
| 40 | + |
| 41 | + GoForward: () => |
| 42 | + IPCService.invoke("history:goForward")([]).pipe( |
| 43 | + Effect.map(() => undefined as void), |
| 44 | + Effect.mapError(MakeHistoryProblem), |
| 45 | + ), |
| 46 | + |
| 47 | + CanGoBack: () => |
| 48 | + IPCService.invoke("history:canGoBack")([]).pipe( |
| 49 | + Effect.map((Result) => Result === true), |
| 50 | + Effect.mapError(MakeHistoryProblem), |
| 51 | + ), |
| 52 | + |
| 53 | + CanGoForward: () => |
| 54 | + IPCService.invoke("history:canGoForward")([]).pipe( |
| 55 | + Effect.map((Result) => Result === true), |
| 56 | + Effect.mapError(MakeHistoryProblem), |
| 57 | + ), |
| 58 | + |
| 59 | + Push: (uri) => |
| 60 | + IPCService.invoke("history:push")([uri]).pipe( |
| 61 | + Effect.map(() => undefined as void), |
| 62 | + Effect.mapError(MakeHistoryProblem), |
| 63 | + ), |
| 64 | + |
| 65 | + Clear: () => |
| 66 | + IPCService.invoke("history:clear")([]).pipe( |
| 67 | + Effect.map(() => undefined as void), |
| 68 | + Effect.mapError(MakeHistoryProblem), |
| 69 | + ), |
| 70 | + |
| 71 | + GetStack: () => |
| 72 | + IPCService.invoke("history:getStack")([]).pipe( |
| 73 | + Effect.map((Result) => |
| 74 | + Array.isArray(Result) |
| 75 | + ? (Result as readonly string[]) |
| 76 | + : [], |
| 77 | + ), |
| 78 | + Effect.mapError(MakeHistoryProblem), |
| 79 | + ), |
| 80 | + }; |
| 81 | + |
| 82 | + return Service; |
| 83 | + }), |
| 84 | +); |
| 85 | + |
| 86 | +export default LiveHistoryServiceLayer; |
0 commit comments