├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ ├── bug-report.md │ ├── change-request.md │ └── config.yml ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── ci.yml ├── .gitignore ├── .tidyrc.json ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── bower.json ├── packages.dhall ├── spago.dhall ├── src └── Effect │ └── Aff │ └── Bus.purs └── test └── Main.purs /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Report an issue 4 | title: "" 5 | labels: bug 6 | assignees: "" 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of the bug. 11 | 12 | **To Reproduce** 13 | A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue. 14 | 15 | **Expected behavior** 16 | A clear and concise description of what you expected to happen. 17 | 18 | **Additional context** 19 | Add any other context about the problem here. 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/change-request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Change request 3 | about: Propose an improvement to this library 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | 9 | **Is your change request related to a problem? Please describe.** 10 | A clear and concise description of the problem. 11 | 12 | Examples: 13 | 14 | - It's frustrating to have to [...] 15 | - I was looking for a function to [...] 16 | 17 | **Describe the solution you'd like** 18 | A clear and concise description of what a good solution to you looks like, including any solutions you've already considered. 19 | 20 | **Additional context** 21 | Add any other context about the change request here. 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: PureScript Discourse 4 | url: https://discourse.purescript.org/ 5 | about: Ask and answer questions on the PureScript discussion forum. 6 | - name: PureScript Discord 7 | url: https://purescript.org/chat 8 | about: Ask and answer questions on the PureScript chat. 9 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Description of the change** 2 | Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR. 3 | 4 | --- 5 | 6 | **Checklist:** 7 | 8 | - [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username 9 | - [ ] Linked any existing issues or proposals that this pull request should close 10 | - [ ] Updated or added relevant documentation in the README and/or documentation directory 11 | - [ ] Added a test for the contribution (if applicable) 12 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Set up a PureScript toolchain 17 | uses: purescript-contrib/setup-purescript@main 18 | with: 19 | purescript: "unstable" 20 | purs-tidy: "latest" 21 | 22 | - name: Cache PureScript dependencies 23 | uses: actions/cache@v2 24 | with: 25 | key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }} 26 | path: | 27 | .spago 28 | output 29 | 30 | - name: Install dependencies 31 | run: spago install 32 | 33 | - name: Build source 34 | run: spago build --no-install --purs-args '--censor-lib --strict' 35 | 36 | - name: Run tests 37 | run: spago test --no-install 38 | 39 | - name: Check formatting 40 | run: purs-tidy check src test 41 | 42 | - name: Verify Bower & Pulp 43 | run: | 44 | npm install bower pulp@16.0.0-0 45 | npx bower install 46 | npx pulp build -- --censor-lib --strict 47 | if [ -d "test" ]; then 48 | npx pulp test 49 | fi 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.gitignore 3 | !.github 4 | !.editorconfig 5 | !.tidyrc.json 6 | 7 | output 8 | generated-docs 9 | bower_components 10 | -------------------------------------------------------------------------------- /.tidyrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "importSort": "source", 3 | "importWrap": "source", 4 | "indent": 2, 5 | "operatorsFile": null, 6 | "ribbon": 1, 7 | "typeArrowPlacement": "first", 8 | "unicode": "never", 9 | "width": null 10 | } 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 4 | 5 | ## [Unreleased] 6 | 7 | Breaking changes: 8 | 9 | New features: 10 | 11 | Bugfixes: 12 | 13 | Other improvements: 14 | 15 | ## [v6.0.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v6.0.0) - 2022-04-27 16 | 17 | Breaking changes: 18 | - Update project and deps to PureScript v0.15.0 (#31 by @JordanMartinez) 19 | 20 | New features: 21 | 22 | Bugfixes: 23 | 24 | Other improvements: 25 | - Added `purs-tidy` formatter (#30 by @thomashoneyman) 26 | 27 | ## [v5.0.1](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v5.0.1) - 2021-11-09 28 | 29 | Other improvements: 30 | - Fixed typos in the LICENSE file that made it impossible for `licensee` to detect the license (#29 by @maxdeviant) 31 | 32 | ## [v5.0.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v5.0.0) - 2021-02-26 33 | 34 | Breaking changes: 35 | - Added support for PureScript 0.14 and dropped support for all previous versions (#24) 36 | 37 | New features: 38 | - Added roles declarations to forbid unsafe coercions (#20) 39 | 40 | Bugfixes: 41 | 42 | Other improvements: 43 | - Updated directory structure to match module name for `Effect.Aff.Bus` (#16) 44 | - Changed default branch to `main` from `master` 45 | - Updated to comply with Contributors library guidelines by adding new issue and pull request templates, updating documentation, and migrating to Spago for local development and CI (#21) 46 | 47 | ## [v4.0.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v4.0.0) - 2018-11-26 48 | 49 | - Updated for PureScript 0.12.x (@safareli) 50 | - Fixed leak in make loop (@safareli) 51 | - Fixed `read` to not block forever when a bus is killed (@safareli) 52 | 53 | ## [v3.1.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v3.1.0) - 2018-04-12 54 | 55 | - Relaxed `make` to use a `MonadEff` constraint rather than concrete `Aff` (@safareli) 56 | 57 | ## [v3.0.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v3.0.0) - 2017-11-22 58 | 59 | - Updated for Aff v4 and new AVar 60 | 61 | ## [v2.0.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v2.0.0) - 2017-04-04 62 | 63 | - Updated for PureScript 0.11 64 | 65 | ## [v1.0.0](https://github.com/purescript-contrib/purescript-aff-bus/releases/tag/v1.0.0) - 2016-11-23 66 | 67 | - Initial release. 68 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Aff Bus 2 | 3 | Thanks for your interest in contributing to `aff-bus`! We welcome new contributions regardless of your level of experience or familiarity with PureScript. 4 | 5 | Every library in the Contributors organization shares a simple handbook that helps new contributors get started. With that in mind, please [read the short contributing guide on purescript-contrib/governance](https://github.com/purescript-contrib/governance/blob/main/contributing.md) before contributing to this library. 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Aff Bus 2 | 3 | [![CI](https://github.com/purescript-contrib/purescript-aff-bus/workflows/CI/badge.svg?branch=main)](https://github.com/purescript-contrib/purescript-aff-bus/actions?query=workflow%3ACI+branch%3Amain) 4 | [![Release](https://img.shields.io/github/release/purescript-contrib/purescript-aff-bus.svg)](https://github.com/purescript-contrib/purescript-aff-bus/releases) 5 | [![Pursuit](https://pursuit.purescript.org/packages/purescript-aff-bus/badge)](https://pursuit.purescript.org/packages/purescript-aff-bus) 6 | [![Maintainer: garyb](https://img.shields.io/badge/maintainer-garyb-teal.svg)](https://github.com/garyb) 7 | 8 | Many-to-many broadcasting. 9 | 10 | ## Installation 11 | 12 | Install `aff-bus` with [Spago](https://github.com/purescript/spago): 13 | 14 | ```sh 15 | spago install aff-bus 16 | ``` 17 | 18 | ## Quick start 19 | 20 | The quick start hasn't been written yet (contributions are welcome!). The quick start covers a common, minimal use case for the library, whereas longer examples and tutorials are kept in the [docs directory](./docs). 21 | 22 | ## Documentation 23 | 24 | `aff-bus` documentation is stored in a few places: 25 | 26 | 1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-aff-bus). 27 | 2. Usage examples can be found in [the test suite](./test). 28 | 29 | If you get stuck, there are several ways to get help: 30 | 31 | - [Open an issue](https://github.com/purescript-contrib/purescript-aff-bus/issues) if you have encountered a bug or problem. 32 | - Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat. 33 | 34 | ## Contributing 35 | 36 | You can contribute to `aff-bus` in several ways: 37 | 38 | 1. If you encounter a problem or have a question, please [open an issue](https://github.com/purescript-contrib/purescript-aff-bus/issues). We'll do our best to work with you to resolve or answer it. 39 | 40 | 2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions. 41 | 42 | 3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. 43 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "purescript-aff-bus", 3 | "description": "Many-to-many broadcasting", 4 | "homepage": "https://github.com/slamdata/purescript-aff-bus", 5 | "license": "Apache-2.0", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/purescript-contrib/purescript-aff-bus.git" 9 | }, 10 | "authors": [ 11 | "Nathan Faubion " 12 | ], 13 | "keywords": [ 14 | "purescript", 15 | "aff", 16 | "bus" 17 | ], 18 | "ignore": [ 19 | "**/.*", 20 | "node_modules", 21 | "bower_components", 22 | "test", 23 | "tests" 24 | ], 25 | "dependencies": { 26 | "purescript-avar": "^5.0.0", 27 | "purescript-effect": "^4.0.0", 28 | "purescript-prelude": "^6.0.0" 29 | }, 30 | "devDependencies": { 31 | "purescript-console": "^6.0.0", 32 | "purescript-refs": "^6.0.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /packages.dhall: -------------------------------------------------------------------------------- 1 | let upstream = 2 | https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall 3 | 4 | in upstream 5 | -------------------------------------------------------------------------------- /spago.dhall: -------------------------------------------------------------------------------- 1 | { name = "aff-bus" 2 | , dependencies = 3 | [ "aff" 4 | , "avar" 5 | , "console" 6 | , "effect" 7 | , "either" 8 | , "exceptions" 9 | , "foldable-traversable" 10 | , "lists" 11 | , "prelude" 12 | , "refs" 13 | , "tailrec" 14 | , "transformers" 15 | , "tuples" 16 | ] 17 | , packages = ./packages.dhall 18 | , sources = [ "src/**/*.purs", "test/**/*.purs" ] 19 | } 20 | -------------------------------------------------------------------------------- /src/Effect/Aff/Bus.purs: -------------------------------------------------------------------------------- 1 | module Effect.Aff.Bus 2 | ( make 3 | , read 4 | , write 5 | , split 6 | , kill 7 | , isKilled 8 | , Cap 9 | , Bus 10 | , BusRW 11 | , BusR 12 | , BusR' 13 | , BusW 14 | , BusW' 15 | ) where 16 | 17 | import Prelude 18 | 19 | import Effect.Aff (Aff, attempt, launchAff_) 20 | import Effect.Aff.AVar (AVar) 21 | import Effect.Aff.AVar as AVar 22 | import Effect.AVar as EffAvar 23 | import Effect.Class (class MonadEffect, liftEffect) 24 | import Effect.Exception as Exn 25 | import Control.Monad.Rec.Class (forever) 26 | import Data.Foldable (foldl, sequence_, traverse_) 27 | import Data.List (List(..), (:)) 28 | import Data.Tuple (Tuple(..)) 29 | 30 | data Cap 31 | 32 | data Bus (r :: Row Type) a = Bus (AVar a) (AVar (List (AVar a))) 33 | 34 | type role Bus nominal representational 35 | 36 | type BusR = BusR' () 37 | 38 | type BusR' r = Bus (read :: Cap | r) 39 | 40 | type BusW = BusW' () 41 | 42 | type BusW' r = Bus (write :: Cap | r) 43 | 44 | type BusRW = Bus (read :: Cap, write :: Cap) 45 | 46 | -- | Creates a new bidirectional Bus which can be read from and written to. 47 | make :: forall m a. MonadEffect m => m (BusRW a) 48 | make = liftEffect do 49 | cell <- EffAvar.empty 50 | consumers <- EffAvar.new mempty 51 | launchAff_ $ void $ attempt $ forever do 52 | res <- AVar.take cell 53 | vars <- AVar.take consumers 54 | AVar.put Nil consumers 55 | sequence_ (foldl (\xs a -> AVar.put res a : xs) mempty vars) 56 | pure $ Bus cell consumers 57 | 58 | -- | Blocks until a new value is pushed to the Bus, returning the value. 59 | read :: forall a r. BusR' r a -> Aff a 60 | read (Bus _ consumers) = do 61 | res' <- AVar.empty 62 | cs <- AVar.take consumers 63 | AVar.put (res' : cs) consumers 64 | AVar.take res' 65 | 66 | -- | Pushes a new value to the Bus, yieldig immediately. 67 | write :: forall a r. a -> BusW' r a -> Aff Unit 68 | write a (Bus cell _) = AVar.put a cell 69 | 70 | -- | Splits a bidirectional Bus into separate read and write Buses. 71 | split :: forall a. BusRW a -> Tuple (BusR a) (BusW a) 72 | split (Bus a b) = Tuple (Bus a b) (Bus a b) 73 | 74 | -- | Kills the Bus and propagates the exception to all pending and future consumers. 75 | kill :: forall a r. Exn.Error -> BusW' r a -> Aff Unit 76 | kill err (Bus cell consumers) = unlessM (liftEffect $ EffAvar.isKilled <$> EffAvar.status cell) do 77 | AVar.kill err cell 78 | vars <- AVar.take consumers 79 | traverse_ (AVar.kill err) vars 80 | AVar.kill err consumers 81 | 82 | -- | Synchronously checks whether a Bus has been killed. 83 | isKilled :: forall m a r. MonadEffect m => BusR' r a -> m Boolean 84 | isKilled (Bus cell _) = liftEffect $ EffAvar.isKilled <$> EffAvar.status cell 85 | -------------------------------------------------------------------------------- /test/Main.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2018 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Test.Main where 18 | 19 | import Prelude 20 | 21 | import Effect.Aff (Aff, Milliseconds(..), attempt, delay, forkAff, joinFiber, runAff_) 22 | import Effect.Aff.Bus as Bus 23 | import Effect (Effect) 24 | import Effect.Class (liftEffect) 25 | import Effect.Console (log) 26 | import Effect.Exception (error, throwException) 27 | import Effect.Ref as Ref 28 | import Control.Monad.Error.Class (throwError) 29 | import Data.Either (Either(..), either) 30 | 31 | test_readWrite :: Bus.BusRW Int -> Aff Boolean 32 | test_readWrite bus = do 33 | ref <- liftEffect $ Ref.new 0 34 | 35 | let 36 | proc = do 37 | res <- attempt (Bus.read bus) 38 | case res of 39 | Left _ -> do 40 | void $ liftEffect $ Ref.modify (_ + 100) ref 41 | Right n -> do 42 | void $ liftEffect $ Ref.modify (_ + n) ref 43 | proc 44 | 45 | f1 <- forkAff proc 46 | f2 <- forkAff proc 47 | 48 | Bus.write 1 bus 49 | Bus.write 2 bus 50 | Bus.write 3 bus 51 | 52 | -- without delay kill of bus interpats pending interactions with avar 53 | -- so we need to wait for some time to be sure that all actions are finished 54 | delay $ Milliseconds 10.0 55 | let err = error "Done" 56 | Bus.kill err bus 57 | attempt (Bus.read bus) >>= case _ of 58 | Left err' | show err' == show err -> pure unit 59 | _ -> throwError $ error "read from killed bus should resolve with same error which was used to kill" 60 | unlessM (Bus.isKilled bus) $ throwError $ error "isKilled must return true as bus was killed" 61 | 62 | joinFiber f1 63 | joinFiber f2 64 | 65 | res <- liftEffect $ Ref.read ref 66 | pure $ res == 212 67 | 68 | main :: Effect Unit 69 | main = do 70 | log "Testing read/write/kill..." 71 | runTest $ Bus.make >>= test_readWrite 72 | runTest $ (liftEffect Bus.make) >>= test_readWrite 73 | where 74 | runTest t = do 75 | isFinishedRef <- Ref.new false 76 | runAff_ (isOk isFinishedRef) t 77 | runAff_ (either throwException pure) do 78 | delay (Milliseconds 100.0) 79 | isFinished <- liftEffect $ Ref.read isFinishedRef 80 | unless isFinished $ throwError (error "Timeout") 81 | where 82 | isOk isFinishedRef = case _ of 83 | Left err -> throwException err 84 | Right res -> 85 | if res then do 86 | log "ok" 87 | Ref.write true isFinishedRef 88 | else throwException $ error "failed" 89 | --------------------------------------------------------------------------------