Skip to content

Commit

Permalink
Add refResponse to WorkspaceItem Success
Browse files Browse the repository at this point in the history
  • Loading branch information
yoching committed Jul 6, 2024
1 parent 7164ff1 commit 6567574
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 42 deletions.
29 changes: 16 additions & 13 deletions src/Code/Workspace.elm
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ type OutMsg
| ChangePerspectiveToSubNamespace (Maybe Reference) FQN


updateOneItem : ItemWithReference -> ( WorkspaceItems, Cmd Msg ) -> ( WorkspaceItems, Cmd Msg )
updateOneItem itemWithReference agg =
updateOneItem : Reference -> ItemWithReference -> ( WorkspaceItems, Cmd Msg ) -> ( WorkspaceItems, Cmd Msg )
updateOneItem refRequest itemWithReference agg =
let
i =
itemWithReference.item

ref =
refResponse =
itemWithReference.ref

workspaceItems =
Expand All @@ -112,23 +112,23 @@ updateOneItem itemWithReference agg =
Cmd.none

else
isDocCropped ref
isDocCropped refResponse

isDupe wi =
let
ref_ =
WorkspaceItem.reference wi

refEqs =
Reference.equals ref ref_
Reference.equals refResponse ref_

hashEqs =
wi
|> WorkspaceItem.hash
|> Maybe.map (Hash.equals (WorkspaceItem.itemHash i))
|> Maybe.withDefault False
in
(Reference.same ref ref_ && not refEqs) || (hashEqs && not refEqs)
(Reference.same refResponse ref_ && not refEqs) || (hashEqs && not refEqs)

-- In some cases (like using the back button between
-- perspectives) we try and fetch the same item twice, not
Expand All @@ -142,7 +142,7 @@ updateOneItem itemWithReference agg =
|> Maybe.map (WorkspaceItems.remove workspaceItems)
|> Maybe.withDefault workspaceItems
in
( WorkspaceItems.replaceOrPrependWithFocus deduped ref (WorkspaceItem.fromItem ref i)
( WorkspaceItems.replaceOrPrependWithFocus deduped refResponse (WorkspaceItem.fromItem refRequest refResponse i)
, Cmd.batch [ aggCmd, cmd ]
)

Expand All @@ -153,10 +153,10 @@ update config viewMode msg ({ workspaceItems } as model) =
NoOp ->
( model, Cmd.none, None )

FetchItemFinished ref itemResult ->
FetchItemFinished refRequest itemResult ->
case itemResult of
Err e ->
( { model | workspaceItems = WorkspaceItems.replace workspaceItems ref (WorkspaceItem.Failure ref e) }
( { model | workspaceItems = WorkspaceItems.replace workspaceItems refRequest (WorkspaceItem.Failure refRequest e) }
, Cmd.none
, None
)
Expand All @@ -165,11 +165,11 @@ update config viewMode msg ({ workspaceItems } as model) =
let
-- remove loading element (with `ref` used for request)
loadingRemoved =
WorkspaceItems.remove workspaceItems ref
WorkspaceItems.remove workspaceItems refRequest

-- update items with fetched result
( nextWorkspaceItems, cmd ) =
List.foldl updateOneItem ( loadingRemoved, Cmd.none ) items
List.foldl (updateOneItem refRequest) ( loadingRemoved, Cmd.none ) items
in
( { model | workspaceItems = nextWorkspaceItems }, cmd, None )

Expand Down Expand Up @@ -436,7 +436,10 @@ openItem config ({ workspaceItems } as model) relativeToRef ref =
WorkspaceItems.insertWithFocusBefore workspaceItems r toInsert
in
( { model | workspaceItems = nextWorkspaceItems }
, Cmd.batch [ HttpApi.perform config.api (fetchDefinition config ref), scrollToDefinition ref ]
, Cmd.batch
[ HttpApi.perform config.api (fetchDefinition config ref)
, scrollToDefinition ref
]
)


Expand Down Expand Up @@ -567,7 +570,7 @@ fetchDefinition config ref =
in
endpoint
|> config.toApiEndpoint
|> HttpApi.toRequest WorkspaceItem.decodeList (FetchItemFinished ref)
|> HttpApi.toRequest (WorkspaceItem.decodeList ref) (FetchItemFinished ref)


isDocCropped : Reference -> Cmd Msg
Expand Down
50 changes: 33 additions & 17 deletions src/Code/Workspace/WorkspaceItem.elm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import UI.ViewMode as ViewMode exposing (ViewMode)
type WorkspaceItem
= Loading Reference
| Failure Reference Http.Error
| Success Reference ItemData
| Success Reference Reference ItemData -- refRequest / refResponse


type DocVisibility
Expand Down Expand Up @@ -81,6 +81,7 @@ type Item
type alias ItemWithReference =
{ item : Item
, ref : Reference
, refRequest : Reference
}


Expand Down Expand Up @@ -136,8 +137,8 @@ type Msg
| NoOp


fromItem : Reference -> Item -> WorkspaceItem
fromItem ref item =
fromItem : Reference -> Reference -> Item -> WorkspaceItem
fromItem refRequest refResponse item =
let
zoom =
-- Doc items always have docs
Expand All @@ -157,7 +158,8 @@ fromItem ref item =
else
Unknown
in
Success ref
Success refRequest
refResponse
{ item = item
, zoom = zoom
, docFoldToggles = Doc.emptyDocFoldToggles
Expand All @@ -174,8 +176,8 @@ reference item =
Failure r _ ->
r

Success r _ ->
r
Success _ refResponse _ ->
refResponse


{-| Convert the Reference of a WorkspaceItem to be HashOnly
Expand All @@ -195,8 +197,8 @@ toHashReference workspaceItem =
HQ.HashOnly h
in
case workspaceItem of
Success r d ->
Success (Reference.map (toHashOnly (itemHash d.item)) r) d
Success refRequest refResponse d ->
Success (Reference.map (toHashOnly (itemHash d.item)) refRequest) (Reference.map (toHashOnly (itemHash d.item)) refResponse) d

-- Can't change references where we don't have hash information
_ ->
Expand Down Expand Up @@ -313,7 +315,7 @@ hash wItem =
let
itemHash_ =
case wItem of
Success _ d ->
Success _ _ d ->
Just (itemHash d.item)

_ ->
Expand Down Expand Up @@ -717,7 +719,7 @@ view { definitionSummaryTooltip, namespaceActionMenu } viewMode workspaceItem is
]
Nothing

Success ref data ->
Success _ ref data ->
let
linkedWithTooltipConfig =
Syntax.linkedWithTooltipConfig
Expand Down Expand Up @@ -975,23 +977,37 @@ decodeTermsWithRef =
Decode.keyValuePairs decodeTermDetails |> Decode.map buildTerms


decodeList : Decode.Decoder (List ItemWithReference)
decodeList =
decodeList : Reference -> Decode.Decoder (List ItemWithReference)
decodeList refRequest =
let
termDefinitions =
field
"termDefinitions"
(decodeTermsWithRef
|> Decode.map
(List.map (\( decodedRef, term ) -> { ref = decodedRef, item = TermItem term }))
(List.map
(\( decodedRef, term ) ->
{ ref = decodedRef
, item = TermItem term
, refRequest = refRequest
}
)
)
)

typeDefinitions =
field
"typeDefinitions"
(decodeTypesWithRef
|> Decode.map
(List.map (\( decodedRef, typeDef ) -> { ref = decodedRef, item = TypeItem typeDef }))
(List.map
(\( decodedRef, typeDef ) ->
{ ref = decodedRef
, item = TypeItem typeDef
, refRequest = refRequest
}
)
)
)
in
Decode.map2
Expand All @@ -1000,9 +1016,9 @@ decodeList =
typeDefinitions


decodeItem : Decode.Decoder ItemWithReference
decodeItem =
Decode.map List.head decodeList
decodeItem : Reference -> Decode.Decoder ItemWithReference
decodeItem refRequest =
Decode.map List.head (decodeList refRequest)
|> Decode.andThen
(Maybe.map Decode.succeed
>> Maybe.withDefault (Decode.fail "Empty list")
Expand Down
12 changes: 9 additions & 3 deletions src/Code/Workspace/WorkspaceItems.elm
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ remove items ref =

{-| TODO: Support NameOnly better
-}



-- TODO: update this member method, considering there's now refRequest and refResponse


member : WorkspaceItems -> Reference -> Bool
member items ref =
items |> references |> List.member ref
Expand Down Expand Up @@ -455,9 +461,9 @@ updateData f ref wItems =
let
update_ workspaceItem =
case workspaceItem of
WorkspaceItem.Success r d ->
if ref == r then
WorkspaceItem.Success r (f d)
WorkspaceItem.Success refRequest refResponse d ->
if ref == refResponse then
WorkspaceItem.Success refRequest refResponse (f d)

else
workspaceItem
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Workspace/WorkspaceMinimap.elm
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ viewItem { selectItemMsg, closeItemMsg, keyboardShortcut } index ( item, focused
[ class "workspace-minimap_item_content" ]
[ StatusBanner.bad "Couldn't fetch definition" ]

Success _ itemData ->
Success _ _ itemData ->
let
( info, category ) =
case itemData.item of
Expand Down
5 changes: 3 additions & 2 deletions storybook/stories/Stories/Code/PageContent.elm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ getSampleResponse index url termName =
|> Reference.TypeReference

decoder =
Decode.map (\itemWithRef -> itemWithRef.item) decodeItem
Decode.map (\itemWithRef -> itemWithRef.item) (decodeItem reference)
in
Http.get
{ url = url
Expand Down Expand Up @@ -107,7 +107,8 @@ update message model =
let
newWorkspaceItems =
item
|> fromItem reference
|> fromItem reference reference
-- TODO: is this right?
|> WorkspaceItems.prependWithFocus model.workspaceItems

newModel =
Expand Down
18 changes: 14 additions & 4 deletions storybook/stories/Stories/Code/WorkspaceItem.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Stories.Code.WorkspaceItem exposing (..)

import Browser
import Code.Definition.Reference as Reference
import Code.FullyQualifiedName as FQN
import Code.HashQualified exposing (HashQualified(..))
import Code.Syntax exposing (..)
import Code.Workspace.WorkspaceItem exposing (Item, ItemWithReference, Msg(..), WorkspaceItem(..), decodeItem, fromItem)
import Code.Workspace.Zoom exposing (Zoom(..))
Expand Down Expand Up @@ -33,10 +36,17 @@ main =

init : () -> ( Model, Cmd Message )
init _ =
let
reference =
"increment"
|> FQN.fromString
|> NameOnly
|> Reference.TypeReference
in
( { workspaceItem = Nothing }
, Http.get
{ url = "/increment_term_def.json"
, expect = Http.expectJson GotItem decodeItem
, expect = Http.expectJson GotItem (decodeItem reference)
}
)

Expand All @@ -53,14 +63,14 @@ update msg model =

Just item ->
case item of
Success ref itemData ->
Success refRequest refResponse itemData ->
let
updatedData =
{ itemData | zoom = zoom }

updatedModel =
{ model
| workspaceItem = Just <| Success ref updatedData
| workspaceItem = Just <| Success refRequest refResponse updatedData
}
in
( updatedModel, Cmd.none )
Expand All @@ -78,7 +88,7 @@ update msg model =

Ok item ->
( { model
| workspaceItem = Just (fromItem sampleReference item.item)
| workspaceItem = Just (fromItem sampleReference sampleReference item.item)
}
, Cmd.none
)
Expand Down
4 changes: 2 additions & 2 deletions storybook/stories/Stories/Code/WorkspaceMinimap.elm
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ getSampleResponse url termName =
decoder =
Decode.map
(\itemWithRef -> itemWithRef.item)
WorkspaceItem.decodeItem
(WorkspaceItem.decodeItem reference)
in
Http.get
{ url = url
Expand Down Expand Up @@ -149,7 +149,7 @@ update message model =
let
newWorkspaceItems =
item
|> WorkspaceItem.fromItem reference
|> WorkspaceItem.fromItem reference reference
|> WorkspaceItems.replace model.workspaceItems reference
in
( { model | workspaceItems = newWorkspaceItems }, Cmd.none )
Expand Down

0 comments on commit 6567574

Please sign in to comment.