-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add bindings to AbortController/AbortSignal * Add signal variant of question' * Add Aff-based APIs * Add changelog entry
- Loading branch information
1 parent
76aa1fb
commit 61731ba
Showing
8 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const newImpl = () => new AbortController(); | ||
export { newImpl as new }; | ||
export const abortImpl = (controller) => controller.abort(); | ||
export const abortReasonImpl = (controller, reason) => controller.abort(reason); | ||
export const signal = (controller) => controller.signal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Node.Errors.AbortController | ||
( AbortController | ||
, new | ||
, abort | ||
, abort' | ||
, signal | ||
) where | ||
|
||
import Prelude | ||
|
||
import Effect (Effect) | ||
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2) | ||
import Node.Errors.AbortSignal (AbortSignal) | ||
|
||
foreign import data AbortController :: Type | ||
|
||
foreign import new :: Effect (AbortController) | ||
|
||
abort :: AbortController -> Effect Unit | ||
abort c = runEffectFn1 abortImpl c | ||
|
||
foreign import abortImpl :: EffectFn1 (AbortController) (Unit) | ||
|
||
abort' :: forall a. AbortController -> a -> Effect Unit | ||
abort' c reason = runEffectFn2 abortReasonImpl c reason | ||
|
||
foreign import abortReasonImpl :: forall a. EffectFn2 (AbortController) (a) (Unit) | ||
|
||
foreign import signal :: AbortController -> AbortSignal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const newAbort = () => AbortSignal.abort(); | ||
export const newAbortReasonImpl = (reason) => AbortSignal.abort(reason); | ||
export const timeoutImpl = (delay) => AbortSignal.timeout(delay); | ||
export const abortedImpl = (sig) => sig.aborted; | ||
export const reasonImpl = (sig) => sig.reason; | ||
export const throwIfAbortedImpl = (sig) => sig.throwIfAborted(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Node.Errors.AbortSignal | ||
( AbortSignal | ||
, toEventEmitter | ||
, newAbort | ||
, newAbort' | ||
, newTimeout | ||
, abortH | ||
, aborted | ||
, reason | ||
, throwIfAborted | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Time.Duration (Milliseconds) | ||
import Effect (Effect) | ||
import Effect.Uncurried (EffectFn1, runEffectFn1) | ||
import Foreign (Foreign) | ||
import Node.EventEmitter (EventEmitter, EventHandle(..)) | ||
import Node.EventEmitter.UtilTypes (EventHandle0) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
foreign import data AbortSignal :: Type | ||
|
||
toEventEmitter :: AbortSignal -> EventEmitter | ||
toEventEmitter = unsafeCoerce | ||
|
||
foreign import newAbort :: Effect (AbortSignal) | ||
|
||
newAbort' :: forall a. a -> Effect AbortSignal | ||
newAbort' reason' = runEffectFn1 newAbortReasonImpl reason' | ||
|
||
foreign import newAbortReasonImpl :: forall a. EffectFn1 (a) (AbortSignal) | ||
|
||
newTimeout :: Milliseconds -> Effect AbortSignal | ||
newTimeout delay = runEffectFn1 timeoutImpl delay | ||
|
||
foreign import timeoutImpl :: EffectFn1 (Milliseconds) (AbortSignal) | ||
|
||
-- | The 'abort' event is emitted when the abortController.abort() method is called. The callback is invoked with a single object argument with a single type property set to 'abort': | ||
-- | | ||
-- | We recommended that code check that the `abortSignal.aborted` attribute is false before adding an 'abort' event listener. | ||
-- | | ||
-- | Any event listeners attached to the AbortSignal should use the { once: true } option (or, if using the EventEmitter APIs to attach a listener, use the once() method) to ensure that the event listener is removed as soon as the 'abort' event is handled. Failure to do so may result in memory leaks. | ||
abortH :: EventHandle0 AbortSignal | ||
abortH = EventHandle "abort" identity | ||
|
||
aborted :: AbortSignal -> Effect Boolean | ||
aborted sig = runEffectFn1 abortedImpl sig | ||
|
||
foreign import abortedImpl :: EffectFn1 (AbortSignal) (Boolean) | ||
|
||
reason :: AbortSignal -> Effect Foreign | ||
reason sig = runEffectFn1 reasonImpl sig | ||
|
||
foreign import reasonImpl :: EffectFn1 (AbortSignal) (Foreign) | ||
|
||
throwIfAborted :: AbortSignal -> Effect Unit | ||
throwIfAborted sig = runEffectFn1 throwIfAbortedImpl sig | ||
|
||
foreign import throwIfAbortedImpl :: EffectFn1 (AbortSignal) (Unit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module Node.ReadLine.Aff | ||
( question | ||
, question' | ||
, blockUntilClosed | ||
, countLines | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Either (Either(..)) | ||
import Effect.Aff (Aff, effectCanceler, error, makeAff, nonCanceler) | ||
import Effect.Class (liftEffect) | ||
import Effect.Exception (Error, throw) | ||
import Effect.Ref as Ref | ||
import Effect.Uncurried (mkEffectFn1) | ||
import Node.Errors.AbortController (AbortController, abort', signal) | ||
import Node.Errors.AbortSignal (abortH, aborted) | ||
import Node.EventEmitter (EventHandle(..), on, once) | ||
import Node.EventEmitter.UtilTypes (EventHandle1) | ||
import Node.ReadLine (Interface, closeH, lineH) | ||
import Node.ReadLine as RL | ||
|
||
-- | Blocks until receives user input. There is no way to cancel this. | ||
question :: String -> Interface -> Aff String | ||
question txt iface = makeAff \done -> do | ||
RL.question txt (done <<< Right) iface | ||
pure nonCanceler | ||
|
||
-- | Blocks until receives user input. An `AbortController` can be used to cancel this. | ||
-- | If the `AbortSignal` is aborted outside of this function, this computation | ||
-- | will produce an error. If the `AbortSignal` is already aborted, this will throw an error. | ||
question' :: String -> AbortController -> Interface -> Aff String | ||
question' txt controller iface = do | ||
let sig = signal controller | ||
-- Node docs: | ||
-- > We recommended that code check that the `abortSignal.aborted` | ||
-- > attribute is `false` before adding an 'abort' event listener. | ||
liftEffect do | ||
alreadyAborted <- aborted sig | ||
when alreadyAborted do | ||
throw "Signal was already aborted before calling 'question'" | ||
makeAff \done -> do | ||
rmAbortListener <- sig # once abortH do | ||
done $ Left $ error "Signal was aborted after calling 'question'" | ||
RL.question' txt { signal: sig } (done <<< Right) iface | ||
pure $ effectCanceler do | ||
rmAbortListener | ||
abort' controller "Cancelled" | ||
|
||
blockUntilClosed :: Interface -> Aff Unit | ||
blockUntilClosed iface = makeAff \done -> do | ||
rmListener <- iface # once closeH (done $ Right unit) | ||
pure $ effectCanceler rmListener | ||
|
||
-- Note: I'm not sure if this is needed, but it's not clear | ||
-- from the Node docs that a `close` event will occur | ||
-- if there's an error in either the `input` or `output` streams. | ||
-- Moreover, `EventEmitter` docs say it's best practices to listen | ||
-- for `error` events. | ||
-- > As a best practice, listeners should always be added for the 'error' events. | ||
errorH :: EventHandle1 Interface Error | ||
errorH = EventHandle "error" mkEffectFn1 | ||
|
||
countLines :: Interface -> Aff Int | ||
countLines iface = makeAff \done -> do | ||
countRef <- Ref.new 0 | ||
rmErrListener <- iface # once errorH (done <<< Left) | ||
rmCloseListener <- iface # once closeH do | ||
rmErrListener | ||
done <<< Right =<< Ref.read countRef | ||
rmLineListener <- iface # on lineH \_ -> Ref.modify_ (_ + 1) countRef | ||
pure $ effectCanceler do | ||
rmErrListener | ||
rmCloseListener | ||
rmLineListener |