Skip to content

Commit c816d34

Browse files
Update FFI to uncurried (#33)
1 parent edeeee7 commit c816d34

File tree

3 files changed

+41
-65
lines changed

3 files changed

+41
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Other improvements:
1414
- Update CI `node` version to `lts/*` (#31, #32 by @JordanMartinez)
1515
- Update CI actions to `v3` (#31, #32 by @JordanMartinez)
1616
- Format code via `purs-tidy`; enforce formatting in CI (#31, #32 by @JordanMartinez)
17+
- Update FFI to use uncurried functions (#33 by @JordanMartinez)
1718

1819
## [v7.0.0](https://github.com/purescript-node/purescript-node-readline/releases/tag/v7.0.0) - 2022-04-29
1920

src/Node/ReadLine.js

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,22 @@
22

33
import { createInterface } from "readline";
44

5-
export function createInterfaceImpl(options) {
6-
return () => createInterface({
7-
input: options.input,
8-
output: options.output,
9-
completer: options.completer && (line => {
10-
const res = options.completer(line)();
11-
return [res.completions, res.matched];
12-
}),
13-
terminal: options.terminal,
14-
historySize: options.historySize,
15-
});
16-
}
5+
export const createInterfaceImpl = (options) => createInterface({
6+
input: options.input,
7+
output: options.output,
8+
completer: options.completer && (line => {
9+
const res = options.completer(line)();
10+
return [res.completions, res.matched];
11+
}),
12+
terminal: options.terminal,
13+
historySize: options.historySize,
14+
});
1715

18-
export function close(readline) {
19-
return () => {
20-
readline.close();
21-
};
22-
}
23-
24-
export function prompt(readline) {
25-
return () => {
26-
readline.prompt();
27-
};
28-
}
29-
30-
export function question(text) {
31-
return callback => readline => () => {
32-
readline.question(text, result => {
33-
callback(result)();
34-
});
35-
};
36-
}
37-
38-
export function setPrompt(prompt) {
39-
return readline => () => {
40-
readline.setPrompt(prompt);
41-
};
42-
}
43-
44-
export function setLineHandler(callback) {
45-
return readline => () => {
46-
readline.removeAllListeners("line");
47-
readline.on("line", line => {
48-
callback(line)();
49-
});
50-
};
51-
}
16+
export const closeImpl = (readline) => readline.close();
17+
export const promptImpl = (readline) => readline.prompt();
18+
export const questionImpl = (readline, text, cb) => readline.question(text, cb);
19+
export const setPromptImpl = (readline, prompt) => readline.setPrompt(prompt);
20+
export const setLineHandlerImpl = (readline, cb) => {
21+
readline.removeAllListeners("line");
22+
readline.on("line", cb);
23+
};

src/Node/ReadLine.purs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ module Node.ReadLine
2121

2222
import Prelude
2323

24+
import Data.Options (Options, Option, (:=), options, opt)
2425
import Effect (Effect)
25-
26+
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, runEffectFn1, runEffectFn2, runEffectFn3)
2627
import Foreign (Foreign)
27-
import Data.Options (Options, Option, (:=), options, opt)
28-
2928
import Node.Process (stdin, stdout)
3029
import Node.Stream (Readable, Writable)
3130

@@ -85,32 +84,36 @@ noCompletion :: Completer
8584
noCompletion s = pure { completions: [], matched: s }
8685

8786
-- | Prompt the user for input on the specified `Interface`.
88-
foreign import prompt :: Interface -> Effect Unit
87+
prompt :: Interface -> Effect Unit
88+
prompt iface = runEffectFn1 promptImpl iface
89+
90+
foreign import promptImpl :: EffectFn1 (Interface) (Unit)
8991

9092
-- | Writes a query to the output, waits
9193
-- | for user input to be provided on input, then invokes
9294
-- | the callback function
93-
foreign import question
94-
:: String
95-
-> (String -> Effect Unit)
96-
-> Interface
97-
-> Effect Unit
95+
question :: String -> (String -> Effect Unit) -> Interface -> Effect Unit
96+
question text cb iface = runEffectFn3 questionImpl iface text cb
97+
98+
foreign import questionImpl :: EffectFn3 (Interface) (String) ((String -> Effect Unit)) Unit
9899

99100
-- | Set the prompt.
100-
foreign import setPrompt
101-
:: String
102-
-> Interface
103-
-> Effect Unit
101+
setPrompt :: String -> Interface -> Effect Unit
102+
setPrompt newPrompt iface = runEffectFn2 setPromptImpl iface newPrompt
103+
104+
foreign import setPromptImpl :: EffectFn2 (Interface) (String) (Unit)
104105

105106
-- | Close the specified `Interface`.
106-
foreign import close :: Interface -> Effect Unit
107+
close :: Interface -> Effect Unit
108+
close iface = runEffectFn1 closeImpl iface
109+
110+
foreign import closeImpl :: EffectFn1 (Interface) (Unit)
107111

108112
-- | A function which handles each line of input.
109113
type LineHandler a = String -> Effect a
110114

111115
-- | Set the current line handler function.
112-
foreign import setLineHandler
113-
:: forall a
114-
. LineHandler a
115-
-> Interface
116-
-> Effect Unit
116+
setLineHandler :: forall a. LineHandler a -> Interface -> Effect Unit
117+
setLineHandler cb iface = runEffectFn2 setLineHandlerImpl iface cb
118+
119+
foreign import setLineHandlerImpl :: forall a. EffectFn2 (Interface) (LineHandler a) (Unit)

0 commit comments

Comments
 (0)