Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 42bd8ca

Browse files
committed
Remove Api and Env dependencies from Workspace
1 parent 3e5dced commit 42bd8ca

File tree

4 files changed

+70
-39
lines changed

4 files changed

+70
-39
lines changed

src/Code/EntityId.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Code.EntityId exposing (EntityId, toString)
1+
module Code.EntityId exposing (EntityId(..), toString)
22

33
import Code.FullyQualifiedName as FQN exposing (FQN)
44
import Code.Hash as Hash exposing (Hash)

src/Code/Workspace.elm

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ module Code.Workspace exposing
1010
, view
1111
)
1212

13-
import Api exposing (ApiRequest)
1413
import Browser.Dom as Dom
14+
import Code.CodebaseApi as CodebaseApi
15+
import Code.Config exposing (Config)
1516
import Code.Definition.Doc as Doc
1617
import Code.Definition.Reference as Reference exposing (Reference)
18+
import Code.EntityId as EntityId
1719
import Code.FullyQualifiedName exposing (FQN)
1820
import Code.Hash as Hash
1921
import Code.HashQualified as HQ
20-
import Code.Perspective exposing (Perspective)
2122
import Code.Workspace.WorkspaceItem as WorkspaceItem exposing (Item, WorkspaceItem)
2223
import Code.Workspace.WorkspaceItems as WorkspaceItems exposing (WorkspaceItems)
23-
import Env exposing (Env)
2424
import Html exposing (Html, article, div, section)
2525
import Html.Attributes exposing (class, id)
2626
import Http
27-
import Lib.OperatingSystem as OperatingSystem
27+
import Lib.Api as Api exposing (ApiRequest)
28+
import Lib.OperatingSystem as OperatingSystem exposing (OperatingSystem)
2829
import Task
2930
import UI.Button as Button
3031
import UI.Icon as Icon
@@ -44,12 +45,12 @@ type alias Model =
4445
}
4546

4647

47-
init : Env -> Maybe Reference -> ( Model, Cmd Msg )
48-
init env mRef =
48+
init : Config -> Maybe Reference -> ( Model, Cmd Msg )
49+
init config mRef =
4950
let
5051
model =
5152
{ workspaceItems = WorkspaceItems.init Nothing
52-
, keyboardShortcut = KeyboardShortcut.init env.operatingSystem
53+
, keyboardShortcut = KeyboardShortcut.init config.operatingSystem
5354
}
5455
in
5556
case mRef of
@@ -59,7 +60,7 @@ init env mRef =
5960
Just ref ->
6061
let
6162
( m, c ) =
62-
open env model ref
63+
open config model ref
6364
in
6465
( m, c )
6566

@@ -86,8 +87,8 @@ type OutMsg
8687
| ChangePerspectiveToNamespace FQN
8788

8889

89-
update : Env -> Msg -> Model -> ( Model, Cmd Msg, OutMsg )
90-
update env msg ({ workspaceItems } as model) =
90+
update : Config -> Msg -> Model -> ( Model, Cmd Msg, OutMsg )
91+
update config msg ({ workspaceItems } as model) =
9192
case msg of
9293
NoOp ->
9394
( model, Cmd.none, None )
@@ -177,14 +178,14 @@ update env msg ({ workspaceItems } as model) =
177178
KeyboardShortcut.fromKeyboardEvent model.keyboardShortcut event
178179

179180
( nextModel, cmd, out ) =
180-
handleKeyboardShortcut env { model | keyboardShortcut = keyboardShortcut } shortcut
181+
handleKeyboardShortcut config.operatingSystem { model | keyboardShortcut = keyboardShortcut } shortcut
181182
in
182183
( nextModel, Cmd.batch [ cmd, Cmd.map KeyboardShortcutMsg kCmd ], out )
183184

184185
WorkspaceItemMsg wiMsg ->
185186
case wiMsg of
186187
WorkspaceItem.OpenReference relativeToRef ref ->
187-
openReference env model relativeToRef ref
188+
openReference config model relativeToRef ref
188189

189190
WorkspaceItem.Close ref ->
190191
let
@@ -254,30 +255,30 @@ replaceWorkspaceItemReferencesWithHashOnly model =
254255
{ model | workspaceItems = workspaceItems }
255256

256257

257-
open : Env -> WithWorkspaceItems m -> Reference -> ( WithWorkspaceItems m, Cmd Msg )
258-
open env model ref =
259-
openItem env model Nothing ref
258+
open : Config -> WithWorkspaceItems m -> Reference -> ( WithWorkspaceItems m, Cmd Msg )
259+
open config model ref =
260+
openItem config model Nothing ref
260261

261262

262263
{-| openReference opens a definition relative to another definition. This is
263264
done within Workspace, as opposed to from the outside via a URL change. This
264265
function returns a Focused command for the newly opened reference and as such
265266
changes the URL.
266267
-}
267-
openReference : Env -> WithWorkspaceItems m -> Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg, OutMsg )
268-
openReference env model relativeToRef ref =
268+
openReference : Config -> WithWorkspaceItems m -> Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg, OutMsg )
269+
openReference config model relativeToRef ref =
269270
let
270271
( newModel, cmd ) =
271-
openItem env model (Just relativeToRef) ref
272+
openItem config model (Just relativeToRef) ref
272273

273274
out =
274275
openDefinitionsFocusToOutMsg newModel.workspaceItems
275276
in
276277
( newModel, cmd, out )
277278

278279

279-
openItem : Env -> WithWorkspaceItems m -> Maybe Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg )
280-
openItem env ({ workspaceItems } as model) relativeToRef ref =
280+
openItem : Config -> WithWorkspaceItems m -> Maybe Reference -> Reference -> ( WithWorkspaceItems m, Cmd Msg )
281+
openItem config ({ workspaceItems } as model) relativeToRef ref =
281282
-- We don't want to refetch or replace any already open definitions, but we
282283
-- do want to focus and scroll to it
283284
if WorkspaceItems.member workspaceItems ref then
@@ -303,7 +304,7 @@ openItem env ({ workspaceItems } as model) relativeToRef ref =
303304
WorkspaceItems.insertWithFocusBefore workspaceItems r toInsert
304305
in
305306
( { model | workspaceItems = nextWorkspaceItems }
306-
, Cmd.batch [ Api.perform env.apiBasePath (fetchDefinition env.perspective ref), scrollToDefinition ref ]
307+
, Cmd.batch [ Api.perform config.apiBasePath (fetchDefinition config ref), scrollToDefinition ref ]
307308
)
308309

309310

@@ -315,8 +316,8 @@ openDefinitionsFocusToOutMsg openDefs =
315316
|> Maybe.withDefault Emptied
316317

317318

318-
handleKeyboardShortcut : Env -> Model -> KeyboardShortcut -> ( Model, Cmd Msg, OutMsg )
319-
handleKeyboardShortcut env ({ workspaceItems } as model) shortcut =
319+
handleKeyboardShortcut : OperatingSystem -> Model -> KeyboardShortcut -> ( Model, Cmd Msg, OutMsg )
320+
handleKeyboardShortcut os ({ workspaceItems } as model) shortcut =
320321
let
321322
scrollToCmd =
322323
WorkspaceItems.focus
@@ -357,7 +358,7 @@ handleKeyboardShortcut env ({ workspaceItems } as model) shortcut =
357358
( model, Cmd.none, ShowFinderRequest Nothing )
358359

359360
KeyboardShortcut.Chord Meta (K _) ->
360-
if env.operatingSystem == OperatingSystem.MacOS then
361+
if os == OperatingSystem.MacOS then
361362
( model, Cmd.none, ShowFinderRequest Nothing )
362363

363364
else
@@ -432,14 +433,28 @@ handleKeyboardShortcut env ({ workspaceItems } as model) shortcut =
432433
-- EFFECTS
433434

434435

435-
fetchDefinition : Perspective -> Reference -> ApiRequest Item Msg
436-
fetchDefinition perspective ref =
436+
fetchDefinition : Config -> Reference -> ApiRequest Item Msg
437+
fetchDefinition config ref =
437438
let
438-
definitionHash =
439-
(Reference.hashQualified >> HQ.toString) ref
439+
definitionId =
440+
case Reference.hashQualified ref of
441+
HQ.NameOnly fqn ->
442+
EntityId.NameId fqn
443+
444+
HQ.HashOnly h ->
445+
EntityId.HashId h
446+
447+
HQ.HashQualified _ h ->
448+
EntityId.HashId h
449+
450+
endpoint =
451+
CodebaseApi.Definition
452+
{ perspective = config.perspective
453+
, definitionId = definitionId
454+
}
440455
in
441-
[ definitionHash ]
442-
|> Api.getDefinition perspective
456+
endpoint
457+
|> config.toApiEndpointUrl
443458
|> Api.toRequest (WorkspaceItem.decodeItem ref) (FetchItemFinished ref)
444459

445460

src/UnisonLocal/App.elm

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ type alias Model =
6868
init : Env -> Route -> ( Model, Cmd Msg )
6969
init env route =
7070
let
71+
codebaseConfig =
72+
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl env
73+
7174
( workspace, workspaceCmd ) =
7275
case route of
7376
Route.Definition _ ref ->
74-
Workspace.init env (Just ref)
77+
Workspace.init codebaseConfig (Just ref)
7578

7679
_ ->
77-
Workspace.init env Nothing
80+
Workspace.init codebaseConfig Nothing
7881

7982
( codebaseTree, codebaseTreeCmd ) =
8083
CodebaseTree.init env
@@ -158,8 +161,11 @@ update msg ({ env } as model) =
158161
case route of
159162
Route.Definition params ref ->
160163
let
164+
codebaseConfig_ =
165+
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl (newEnv params)
166+
161167
( workspace, cmd ) =
162-
Workspace.open (newEnv params) model.workspace ref
168+
Workspace.open codebaseConfig_ model.workspace ref
163169

164170
model3 =
165171
{ model2 | workspace = workspace, env = newEnv params }
@@ -213,7 +219,7 @@ update msg ({ env } as model) =
213219
WorkspaceMsg wMsg ->
214220
let
215221
( workspace, wCmd, outMsg ) =
216-
Workspace.update env wMsg model.workspace
222+
Workspace.update codebaseConfig wMsg model.workspace
217223

218224
model2 =
219225
{ model | workspace = workspace }

src/UnisonShare/App.elm

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ type alias Model =
6262
init : Env -> Route -> ( Model, Cmd Msg )
6363
init env route =
6464
let
65+
codebaseConfig =
66+
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl env
67+
6568
-- TODO: This whole thing should be route driven
6669
( workspace, workspaceCmd ) =
6770
case route of
6871
Route.Project _ (Route.ProjectDefinition ref) ->
69-
Workspace.init env (Just ref)
72+
Workspace.init codebaseConfig (Just ref)
7073

7174
_ ->
72-
Workspace.init env Nothing
75+
Workspace.init codebaseConfig Nothing
7376

7477
( codebaseTree, codebaseTreeCmd ) =
7578
CodebaseTree.init env
@@ -145,6 +148,10 @@ type Msg
145148

146149
update : Msg -> Model -> ( Model, Cmd Msg )
147150
update msg ({ env } as model) =
151+
let
152+
codebaseConfig =
153+
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl env
154+
in
148155
case ( model.route, msg ) of
149156
( _, LinkClicked urlRequest ) ->
150157
case urlRequest of
@@ -184,8 +191,11 @@ update msg ({ env } as model) =
184191

185192
Route.Project params (Route.ProjectDefinition ref) ->
186193
let
194+
codebaseConfig_ =
195+
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl (newEnv params)
196+
187197
( workspace, cmd ) =
188-
Workspace.open (newEnv params) model.workspace ref
198+
Workspace.open codebaseConfig_ model.workspace ref
189199

190200
model3 =
191201
{ model2 | workspace = workspace, env = newEnv params }
@@ -269,7 +279,7 @@ update msg ({ env } as model) =
269279
( Route.Project _ _, WorkspaceMsg wMsg ) ->
270280
let
271281
( workspace, wCmd, outMsg ) =
272-
Workspace.update env wMsg model.workspace
282+
Workspace.update codebaseConfig wMsg model.workspace
273283

274284
model2 =
275285
{ model | workspace = workspace }

0 commit comments

Comments
 (0)