├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── __tests__ ├── test.bs.js └── test.re ├── bsconfig.json ├── example ├── demo.bs.js ├── demo.re ├── index.html ├── worker.bs.js └── worker.re ├── package-lock.json ├── package.json ├── setup.js ├── src ├── WebWorkers.bs.js └── WebWorkers.re └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .merlin 3 | .bsb.lock 4 | npm-debug.log 5 | /lib/bs/ 6 | /node_modules/ 7 | tasks.json 8 | dist/*.js -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Clone the repo and run `npm install`. 4 | 5 | You can then run `npm run start` to start the Bucklescript watcher-compiler. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 David Gomes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reason Bindings for the Web Workers API 2 | 3 | This repository contains bindings for the [Web Workers API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/). 4 | 5 | Read more about these [here](https://davidgom.es/writing-reasonml-bindings-for-javascript-apis/), but keep in mind these bindings have been improved since this article was written. 6 | 7 | ## Example 8 | 9 | The main script creates a new Web Worker from the `worker.re` script and posts a message to it. The Web Worker replies back to that message. The main thread listens to that and prints out the message it received from the Web Worker. 10 | 11 | **main.re** 12 | ```ocaml 13 | let worker = WebWorkers.create_webworker("worker.bs.js"); 14 | 15 | let msg = {"text": "Hello world"}; 16 | 17 | WebWorkers.postMessage(worker, msg); 18 | 19 | let msgBackHandler = (e: WebWorkers.MessageEvent.t) => { 20 | Js.log("I am the main thread and I have received a message back from the worker:"); 21 | Js.log(WebWorkers.MessageEvent.data(e)) 22 | }; 23 | 24 | WebWorkers.onMessage(worker, msgBackHandler); 25 | ``` 26 | 27 | **worker.re** 28 | ```ocaml 29 | WebWorkers.setWorkerOnMessage( 30 | WebWorkers.self, 31 | (e: WebWorkers.MessageEvent.t) => { 32 | Js.log("I am the Web Worker and I have received a message:"); 33 | Js.log(WebWorkers.MessageEvent.data(e)); 34 | WebWorkers.postMessageFromWorker("my result") 35 | } 36 | ); 37 | ``` 38 | 39 | ## Installation 40 | 41 | 1. Install the bindings using `npm install --save bs-webworkers` 42 | 2. Add the bindings to `bsconfig.json`: 43 | 44 | ```json 45 | { 46 | "bs-dependencies": [ 47 | "bs-webworkers" 48 | ] 49 | } 50 | ``` 51 | 52 | Look in the `example/` directory for an example of how to use the bindings. 53 | 54 | ## Build 55 | ``` 56 | npm install 57 | npm run build 58 | ``` 59 | 60 | ## Tests 61 | ``` 62 | npm install 63 | npm run test 64 | ``` 65 | 66 | ## Demo 67 | ``` 68 | npm install 69 | npm run example 70 | ``` 71 | 72 | ## Todo 73 | 74 | The full Web Workers API is still not yet implemented (e.g. Shared Workers, Service Worker, WorkerNavigator, WorkerLocation, etc.). PRs are welcome! 75 | -------------------------------------------------------------------------------- /__tests__/test.bs.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 2.1.0, PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Jest = require("bs-jest/src/jest.js"); 5 | 6 | describe("create_webworkers", (function () { 7 | return Jest.test("to work", (function () { 8 | var worker = new Worker("script.js"); 9 | return Jest.ExpectJs[/* toEqual */12]("script.js")(Jest.ExpectJs[/* expect */0](worker.url)); 10 | })); 11 | })); 12 | 13 | /* Not a pure module */ 14 | -------------------------------------------------------------------------------- /__tests__/test.re: -------------------------------------------------------------------------------- 1 | open Jest; 2 | open ExpectJs; 3 | 4 | 5 | describe( 6 | "create_webworkers", 7 | () => { 8 | test( 9 | "to work", 10 | () => { 11 | let worker = WebWorkers.create_webworker("script.js"); 12 | 13 | expect(worker##url) 14 | |> toEqual("script.js"); 15 | } 16 | ); 17 | } 18 | ); -------------------------------------------------------------------------------- /bsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-webworkers", 3 | "sources": [ 4 | { 5 | "dir": "src" 6 | }, 7 | { 8 | "dir": "example", 9 | "type": "dev" 10 | }, 11 | { 12 | "dir": "__tests__", 13 | "type": "dev" 14 | } 15 | ], 16 | "package-specs": { 17 | "module": "commonjs", 18 | "in-source": true 19 | }, 20 | "suffix": ".bs.js", 21 | "bs-dev-dependencies": [ 22 | "bs-jest" 23 | ], 24 | "refmt": 3 25 | } 26 | -------------------------------------------------------------------------------- /example/demo.bs.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 2.1.0, PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | var worker = new Worker("worker.bs.js"); 6 | 7 | var msg = { 8 | text: "Hello world" 9 | }; 10 | 11 | worker.postMessage(msg); 12 | 13 | function msgBackHandler(e) { 14 | console.log("I am the main thread and I have received a message back from the worker:"); 15 | console.log(e.data); 16 | return /* () */0; 17 | } 18 | 19 | worker.onmessage = msgBackHandler; 20 | 21 | exports.worker = worker; 22 | exports.msg = msg; 23 | exports.msgBackHandler = msgBackHandler; 24 | /* worker Not a pure module */ 25 | -------------------------------------------------------------------------------- /example/demo.re: -------------------------------------------------------------------------------- 1 | let worker = WebWorkers.create_webworker("worker.bs.js"); 2 | 3 | let msg = {"text": "Hello world"}; 4 | 5 | WebWorkers.postMessage(worker, msg); 6 | 7 | let msgBackHandler = (e: WebWorkers.MessageEvent.t) => { 8 | Js.log("I am the main thread and I have received a message back from the worker:"); 9 | Js.log(WebWorkers.MessageEvent.data(e)) 10 | }; 11 | 12 | WebWorkers.onMessage(worker, msgBackHandler); 13 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |