Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move MVars to elm #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const rl = readline.createInterface({
});

let nextCounter = 0;
const mVars = {};
const lockedFiles = {};
const processes = {};

Expand Down Expand Up @@ -353,67 +352,3 @@ app.ports.sendReplGetInputLineWithInitial.subscribe(function ({ index, prompt, l
app.ports.recvReplGetInputLineWithInitial.send({ index, value });
});
});

// MVARS

app.ports.sendNewEmptyMVar.subscribe(function (index) {
nextCounter += 1;
mVars[nextCounter] = { subscribers: [], value: undefined };
app.ports.recvNewEmptyMVar.send({ index, value: nextCounter });
});

app.ports.sendReadMVar.subscribe(function ({ index, id }) {
if (typeof mVars[id].value === "undefined") {
mVars[id].subscribers.push({ index, action: "read" });
} else {
app.ports.recvReadMVar.send({ index, value: mVars[id].value });
}
});

app.ports.sendTakeMVar.subscribe(function ({ index, id }) {
if (typeof mVars[id].value === "undefined") {
mVars[id].subscribers.push({ index, action: "take" });
} else {
const value = mVars[id].value;
mVars[id].value = undefined;

if (
mVars[id].subscribers.length > 0 &&
mVars[id].subscribers[0].action === "put"
) {
const subscriber = mVars[id].subscribers.shift();
mVars[id].value = subscriber.value;
app.ports.recvPutMVar.send(subscriber.index);
}

app.ports.recvReadMVar.send({ index, value });
}
});

app.ports.sendPutMVar.subscribe(function ({ index, id, value }) {
if (typeof mVars[id].value === "undefined") {
mVars[id].value = value;

mVars[id].subscribers = mVars[id].subscribers.filter((subscriber) => {
if (subscriber.action === "read") {
app.ports.recvReadMVar.send({ index: subscriber.index, value });
}

return subscriber.action !== "read";
});

const subscriber = mVars[id].subscribers.shift();

if (subscriber) {
app.ports.recvReadMVar.send({ index: subscriber.index, value });

if (subscriber.action === "take") {
mVars[id].value = undefined;
}
}

app.ports.recvPutMVar.send(index);
} else {
mVars[id].subscribers.push({ index, action: "put", value });
}
});
6 changes: 3 additions & 3 deletions src/Builder/File.elm
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ readBinary decoder path =

writeUtf8 : FilePath -> String -> IO ()
writeUtf8 path content =
IO (\s -> ( s, IO.WriteString IO.pure path content ))
IO (\_ s -> ( s, IO.WriteString IO.pure path content ))



Expand All @@ -104,7 +104,7 @@ writeUtf8 path content =

readUtf8 : FilePath -> IO String
readUtf8 path =
IO (\s -> ( s, IO.Read IO.pure path ))
IO (\_ s -> ( s, IO.Read IO.pure path ))



Expand All @@ -113,7 +113,7 @@ readUtf8 path =

writeBuilder : FilePath -> String -> IO ()
writeBuilder path builder =
IO (\s -> ( s, IO.WriteString IO.pure path builder ))
IO (\_ s -> ( s, IO.WriteString IO.pure path builder ))



Expand Down
6 changes: 3 additions & 3 deletions src/Builder/Http.elm
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type Method
fetch : Method -> Manager -> String -> List Header -> (Error -> e) -> (String -> IO (Result e a)) -> IO (Result e a)
fetch methodVerb _ url headers _ onSuccess =
IO
(\s ->
(\_ s ->
( s
, IO.HttpFetch IO.pure
(case methodVerb of
Expand Down Expand Up @@ -171,7 +171,7 @@ shaToChars =

getArchive : Manager -> String -> (Error -> e) -> e -> (( Sha, Zip.Archive ) -> IO (Result e a)) -> IO (Result e a)
getArchive _ url _ _ onSuccess =
IO (\s -> ( s, IO.GetArchive IO.pure "GET" url ))
IO (\_ s -> ( s, IO.GetArchive IO.pure "GET" url ))
|> IO.bind (\shaAndArchive -> onSuccess shaAndArchive)


Expand All @@ -188,7 +188,7 @@ type MultiPart
upload : Manager -> String -> List MultiPart -> IO (Result Error ())
upload _ url parts =
IO
(\s ->
(\_ s ->
( s
, IO.HttpUpload IO.pure
url
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Json/Encode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ writeUgly path value =
-}
fileWriteBuilder : String -> String -> IO ()
fileWriteBuilder path value =
IO (\s -> ( s, IO.WriteString IO.pure path value ))
IO (\_ s -> ( s, IO.WriteString IO.pure path value ))



Expand Down
4 changes: 2 additions & 2 deletions src/Control/Monad/State/Strict.elm
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ pure value =

get : StateT s IO.ReplState
get =
IO (\s -> ( s, IO.Pure s.state ))
IO (\_ s -> ( s, IO.Pure s.state ))
|> liftIO


put : IO.ReplState -> IO ()
put state =
IO (\s -> ( { s | state = state }, IO.Pure () ))
IO (\_ s -> ( { s | state = state }, IO.Pure () ))
2 changes: 1 addition & 1 deletion src/System/Exit.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ExitCode
exitWith : ExitCode -> IO a
exitWith exitCode =
IO
(\s ->
(\_ s ->
let
code : Int
code =
Expand Down
289 changes: 158 additions & 131 deletions src/System/IO.elm

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/System/Process.elm
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ proc cmd args =
withCreateProcess : CreateProcess -> (Maybe IO.Handle -> Maybe IO.Handle -> Maybe IO.Handle -> ProcessHandle -> IO Exit.ExitCode) -> IO Exit.ExitCode
withCreateProcess createProcess f =
IO
(\s ->
(\_ s ->
( s
, IO.ProcWithCreateProcess IO.pure
(Encode.object
Expand Down Expand Up @@ -115,7 +115,7 @@ withCreateProcess createProcess f =

waitForProcess : ProcessHandle -> IO Exit.ExitCode
waitForProcess (ProcessHandle ph) =
IO (\s -> ( s, IO.ProcWaitForProcess IO.pure ph ))
IO (\_ s -> ( s, IO.ProcWaitForProcess IO.pure ph ))
|> IO.fmap
(\exitCode ->
case exitCode of
Expand Down
Loading
Loading