All files / app update.ts

98.5% Statements 66/67
100% Branches 0/0
98.21% Functions 55/56
98.38% Lines 61/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                    2x 12x 12x   12x       2x 2x 2x 2x       2x 6x 6x     6x   5x 1x               14x             2x   6x 6x       2x       6x 3x 3x         2x   13x   13x 7x     2x   6x             2x   17x 17x 3x       2x   9x 9x 6x         2x 73x 1x 1x     2x   2x         2x   3x   3x       4x     4x 1x 2x 1x           3x             3x           1x 1x     1x 1x     13x     1x   13x   17x   9x    
import { Option } from 'effect'
import { Update } from 'foldkit'
import { modifyFields } from 'foldkit/struct'
 
import { type Box, type Camera, LatLon } from '../core/domain'
import * as CameraRequest from '../map/cameraRequest'
import * as Copy from '../submodels/copy'
import * as GoTo from '../submodels/goTo'
import * as Settings from '../submodels/settings'
import { ReplaceUrl } from './command'
import { Message } from './message'
import { CursorState, MapState, type Model } from './model'
 
type UpdateReturn = Update.Return<Model, Message>
type Step = Update.Step<Model, Message>
 
// STEPS
 
const setCue =
  (maybePoint: Option.Option<LatLon.LatLon>): Step =>
  model => ({
    model: modifyFields(model, {
      maybeCue: () => Option.map(maybePoint, LatLon.clampToWorld),
    }),
  })
 
const setCamera =
  (camera: Camera.Camera): Step =>
  model => ({
    model: modifyFields(model, { maybeCamera: () => Option.some(camera) }),
  })
 
/** Ask the map for one animated move. Each request gets the next id, so the map can tell it is new. */
const requestCamera =
  (target: CameraRequest.CameraTarget): Step =>
  model => ({
    model: modifyFields(model, {
      maybeCameraRequest: maybeRequest =>
        Option.some({
          id: Option.match(maybeRequest, {
            onNone: () => 0,
            onSome: ({ id }) => id + 1,
          }),
          target,
        }),
    }),
  })
 
/** Write the cue and camera as the Model has them now, so this runs after the Steps that change them. */
const replaceUrl: Step = model => ({
  model,
  commands: [
    ReplaceUrl({ maybeCue: model.maybeCue, maybeCamera: model.maybeCamera }),
  ],
})
 
const clearGoToStatus: Step = Update.foldChildStep({
  update: GoTo.clearStatus,
  read: (model: Model) => Option.some(model.goTo),
  write: (model, nextGoTo) => modifyFields(model, { goTo: () => nextGoTo }),
  toParentMessage: message => Message.GotGoToMessage({ message }),
})
 
const cameraTarget = (
  point: LatLon.LatLon,
  maybeBox: Option.Option<Box.Box>,
): CameraRequest.CameraTarget =>
  Option.match(maybeBox, {
    onNone: () => CameraRequest.CameraTarget.FlyTo({ point }),
    onSome: box => CameraRequest.CameraTarget.FitBox({ box }),
  })
 
// FOLDS
 
const foldSettings = Update.foldChild({
  update: Settings.update,
  read: (model: Model) => Option.some(model.settings),
  write: (model, nextSettings) =>
    modifyFields(model, { settings: () => nextSettings }),
  toParentMessage: message => Message.GotSettingsMessage({ message }),
})
 
const foldGoToOutMessage = GoTo.OutMessage.match<Step>({
  ChoseLocation: ({ point, maybeBox }) =>
    Update.combine([
      setCue(Option.some(point)),
      requestCamera(cameraTarget(point, maybeBox)),
      replaceUrl,
    ]),
})
 
const foldGoTo = Update.foldChild({
  update: GoTo.update,
  read: (model: Model) => Option.some(model.goTo),
  write: (model, nextGoTo) => modifyFields(model, { goTo: () => nextGoTo }),
  toParentMessage: message => Message.GotGoToMessage({ message }),
  foldOutMessage: foldGoToOutMessage,
})
 
const foldCopy = Update.foldChild({
  update: Copy.update,
  read: (model: Model) => Option.some(model.copy),
  write: (model, nextCopy) => modifyFields(model, { copy: () => nextCopy }),
  toParentMessage: message => Message.GotCopyMessage({ message }),
})
 
// UPDATE
 
export const update = (model: Model, message: Message) =>
  Message.match<UpdateReturn>(message, {
    LoadedMap: () => ({
      model: modifyFields(model, { mapState: () => MapState.Ready() }),
    }),
 
    FailedLoadMap: ({ reason }) => ({
      model: modifyFields(model, {
        mapState: () => MapState.Failed({ reason }),
      }),
    }),
 
    MovedMap: ({ camera }) =>
      Update.combine(model, [setCamera(camera), replaceUrl]),
 
    MovedCursor: ({ point }) => ({
      model: modifyFields(model, {
        cursor: () => CursorState.OnMap({ point: LatLon.clampToWorld(point) }),
      }),
    }),
 
    LeftMap: () => ({
      model: modifyFields(model, {
        cursor: cursor =>
          CursorState.match(cursor, {
            Idle: () => cursor,
            OnMap: ({ point }) => CursorState.OffMap({ point }),
            OffMap: () => cursor,
          }),
      }),
    }),
 
    ClickedMap: ({ point }) =>
      Update.combine(model, [
        setCue(Option.some(point)),
        clearGoToStatus,
        replaceUrl,
      ]),
 
    ClickedClearCue: () =>
      Update.combine(model, [
        setCue(Option.none()),
        clearGoToStatus,
        replaceUrl,
      ]),
 
    UpdatedLayout: ({ layout }) => ({
      model: modifyFields(model, { layout: () => layout }),
    }),
 
    UpdatedPointer: ({ pointer }) => ({
      model: modifyFields(model, { pointer: () => pointer }),
    }),
 
    SucceededReplaceUrl: () => ({ model }),
 
    // NOTE: a URL that cannot be replaced only loses the shareable link; the app carries on.
    FailedReplaceUrl: () => ({ model }),
 
    GotSettingsMessage: ({ message }) => foldSettings(model, message),
 
    GotGoToMessage: ({ message }) => foldGoTo(model, message),
 
    GotCopyMessage: ({ message }) => foldCopy(model, message),
  })