Skip to content

Commit

Permalink
improve wait condition for opening documents
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimon-au committed Nov 27, 2023
1 parent 4255de2 commit 0dbc990
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/vscode/contextive/test/helpers/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,39 @@ open Node.Api
open Fable.Core
open Fable.Mocha

let waitForTimeout timeoutMs (condition: unit -> JS.Promise<bool>) =
promise {
let endTime = System.DateTime.Now.AddMilliseconds(timeoutMs)

let mutable conditionResult = true

while (not conditionResult && System.DateTime.Now < endTime) do
let! result = condition ()
conditionResult <- result

if (not conditionResult) then
failwith $"Condition still untrue after {timeoutMs}ms"
}

let waitFor = waitForTimeout 5000

let getDocUri relativeFile =
vscode.Uri.file (path.resolve (__dirname, relativeFile))

let findUriInVisibleEditors (path: Uri) =
window.visibleTextEditors.Find(fun te -> te.document.uri.path = path.path)

let documentIsOpen docUri () =
promise {
let editor = findUriInVisibleEditors (docUri)
return editor <> null
}

let openDocument (docUri: Uri) =
promise {
let! doc = workspace.openTextDocument (docUri)
do! window.showTextDocument (doc, ViewColumn.Active, false) |> Thenable.Ignore
do! Promise.sleep 500
do! waitFor <| documentIsOpen docUri
return docUri
}

Expand Down Expand Up @@ -54,9 +79,6 @@ let getFullPathFromConfig () =
let config = getConfig ()
config["path"].Value :?> string |> pathInWorkspace

let findUriInVisibleEditors (path: Uri) =
window.visibleTextEditors.Find(fun te -> te.document.uri.path = path.path)

let updateConfig newPath =
promise {
let config = getConfig ()
Expand All @@ -79,22 +101,6 @@ let deleteFile fullPath =
let deleteConfiguredDefinitionsFile () =
promise { do! deleteFile <| getFullPathFromConfig () }

let waitForTimeout timeoutMs (condition: unit -> JS.Promise<bool>) =
promise {
let endTime = System.DateTime.Now.AddMilliseconds(timeoutMs)

let mutable conditionResult = true

while (not conditionResult && System.DateTime.Now < endTime) do
let! result = condition ()
conditionResult <- result

if (not conditionResult) then
failwith $"Condition still untrue after {timeoutMs}ms"
}

let waitFor = waitForTimeout 5000

let util: obj = JsInterop.importAll "node:util"

[<Emit("util.inspect($0, { showHidden: true, depth: null, getters: true })")>]
Expand Down

0 comments on commit 0dbc990

Please sign in to comment.