Skip to content

Update FFI to EffectFnX types; update repo to purescript-web standards #3

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

Merged
merged 11 commits into from
Jan 11, 2023
Merged
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
55 changes: 27 additions & 28 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"extends": "eslint:recommended",
"env": {
"browser": true
},
"rules": {
"strict": [2, "global"],
"block-scoped-var": 2,
"consistent-return": 2,
"eqeqeq": [2, "smart"],
"guard-for-in": 2,
"no-caller": 2,
"no-extend-native": 2,
"no-loop-func": 2,
"no-new": 2,
"no-param-reassign": 2,
"no-return-assign": 2,
"no-unused-expressions": 2,
"no-use-before-define": 2,
"radix": [2, "always"],
"indent": [2, 2],
"quotes": [2, "double"],
"semi": [2, "always"]
}
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"extends": "eslint:recommended",
"env": {
"browser": true
},
"rules": {
"strict": [2, "global"],
"block-scoped-var": 2,
"consistent-return": 2,
"eqeqeq": [2, "smart"],
"guard-for-in": 2,
"no-caller": 2,
"no-extend-native": 2,
"no-loop-func": 2,
"no-new": 2,
"no-param-reassign": 2,
"no-return-assign": 2,
"no-unused-expressions": 2,
"no-use-before-define": 2,
"radix": [2, "always"],
"indent": [2, 2],
"quotes": [2, "double"],
"semi": [2, "always"]
}

}
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---


16 changes: 16 additions & 0 deletions .github/ISSUE_TEMPLATE/feature-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: Feature request
about: Suggest types, functions, properties that may be missing
title: ''
labels: enhancement
assignees: ''

---

### Prerequisites

- [ ] Before opening an issue, please check the [W3C Web Workers standard](https://html.spec.whatwg.org/multipage/workers.html). If it doesn't appear in this spec, it may be present in the spec for one of the other `purescript-web` projects. Although MDN is a great resource, it is not a suitable reference for this project.

### Description

<!-- A summary of the thing to include, with a link to the relevant section of the spec -->
16 changes: 16 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
**Prerequisites**

- [ ] Before opening a pull request, please check the DOM standard (https://dom.spec.whatwg.org/). If it doesn't appear in this spec, it may be present in the spec for one of the other `purescript-web` projects. Although MDN is a great resource, it is not a suitable reference for this project.

**Description of the change**

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.

---

**Checklist:**

- [ ] Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
- [ ] Linked any existing issues or proposals that this pull request should close
- [ ] Updated or added relevant documentation
- [ ] Added a test for the contribution (if applicable)
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up a PureScript toolchain
uses: purescript-contrib/setup-purescript@main
with:
purescript: "unstable"
purs-tidy: "latest"

- name: Cache PureScript dependencies
uses: actions/cache@v2
with:
key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
path: |
.spago
output

- name: Install dependencies
run: spago install

- name: Build source
run: spago build --no-install --purs-args '--censor-lib --strict'

- name: Check formatting
run: purs-tidy check src
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Changelog

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).

## [Unreleased]

Breaking changes:
- Refactored `Worker` to better match the spec
- Refactored `Credentials`' `Show` instance to be more `Show`-like; use `printCredentials` now

New features:
- Added `SharedWorker` bindings
- Added `Show` instance to `WorkerType`

Bugfixes:

Other improvements:
- Added documentation
- Migrated from `pulp`/`bower` to `spago`
- Updated FFI to use `EffectFnX` types
- Added CI and GitHub issue/pr templates
- Added `purs-tidy` formatter to CI

## [v1.1.0](https://github.com/purescript-web/purescript-web-workers/releases/tag/v1.1.0) - 2022-05-2

- purty and export MessageEvent.ports
-
## [v1.0.0](https://github.com/purescript-web/purescript-web-workers/releases/tag/v1.0.0) - 2022-05-17

- Update README.md

## [v0.2.0](https://github.com/purescript-web/purescript-web-workers/releases/tag/v0.2.0) - 2022-05-16

Update `bower.json`

## [v0.1.0](https://github.com/purescript-web/purescript-web-workers/releases/tag/v0.1.0) - 2022-05-16

- Initial release
100 changes: 0 additions & 100 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -1,103 +1,3 @@
{-
Welcome to your new Dhall package-set!

Below are instructions for how to edit this file for most use
cases, so that you don't need to know Dhall to use it.

## Use Cases

Most will want to do one or both of these options:
1. Override/Patch a package's dependency
2. Add a package not already in the default package set

This file will continue to work whether you use one or both options.
Instructions for each option are explained below.

### Overriding/Patching a package

Purpose:
- Change a package's dependency to a newer/older release than the
default package set's release
- Use your own modified version of some dependency that may
include new API, changed API, removed API by
using your custom git repo of the library rather than
the package set's repo

Syntax:
where `entityName` is one of the following:
- dependencies
- repo
- version
-------------------------------
let upstream = --
in upstream
with packageName.entityName = "new value"
-------------------------------

Example:
-------------------------------
let upstream = --
in upstream
with halogen.version = "master"
with halogen.repo = "https://example.com/path/to/git/repo.git"

with halogen-vdom.version = "v4.0.0"
with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies
-------------------------------

### Additions

Purpose:
- Add packages that aren't already included in the default package set

Syntax:
where `<version>` is:
- a tag (i.e. "v4.0.0")
- a branch (i.e. "master")
- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977")
-------------------------------
let upstream = --
in upstream
with new-package-name =
{ dependencies =
[ "dependency1"
, "dependency2"
]
, repo =
"https://example.com/path/to/git/repo.git"
, version =
"<version>"
}
-------------------------------

Example:
-------------------------------
let upstream = --
in upstream
with benchotron =
{ dependencies =
[ "arrays"
, "exists"
, "profunctor"
, "strings"
, "quickcheck"
, "lcg"
, "transformers"
, "foldable-traversable"
, "exceptions"
, "node-fs"
, "node-buffer"
, "node-readline"
, "datetime"
, "now"
]
, repo =
"https://github.com/hdgarrood/purescript-benchotron.git"
, version =
"v7.0.0"
}
-------------------------------
-}
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20230105/packages.dhall
sha256:3e9fbc9ba03e9a1fcfd895f65e2d50ee2f5e86c4cd273f3d5c841b655a0e1bda
Expand Down
10 changes: 9 additions & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{ name = "web-workers"
, dependencies = [ "effect", "foreign", "maybe", "prelude", "unsafe-coerce", "web-events" ]
, dependencies =
[ "effect"
, "foreign"
, "functions"
, "maybe"
, "prelude"
, "unsafe-coerce"
, "web-events"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs" ]
}
28 changes: 8 additions & 20 deletions src/Web/Worker/DedicatedWorkerGlobalScope.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/* @globals self */

export function name () {
export function name() {
return self.name;
}

export function postMessageImpl(data) {
return function(tr) {
return function () {
self.postMessage(data, tr.length > 0 ? tr : undefined);
};
};
export function postMessageImpl(data, tr) {
self.postMessage(data, tr.length > 0 ? tr : undefined);
}

export function close() {
self.close();
}

export function onMessage(f) {
return function () {
self.onmessage = function (ev) {
f(ev)();
};
};
export function onMessageImpl(f) {
self.onmessage = f;
}

export function onMessageError(f) {
return function () {
self.onmessageerror = function (ev) {
f(ev)();
};
};
}
export function onMessageErrorImpl(f) {
self.onmessageerror = f;
}
29 changes: 17 additions & 12 deletions src/Web/Worker/DedicatedWorkerGlobalScope.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,38 @@ module Web.Worker.DedicatedWorkerGlobalScope
, postMessage
, postMessage'
, module GlobalScope
)
where
) where

import Prelude

import Effect (Effect)
import Web.Worker.Types (Transferable)
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn1, runEffectFn2)
import Web.Worker.GlobalScope (importScripts, location, navigator, onError, onLanguageChange, onOffline, onOnline, onRejectionHandled, onUnhandledRejection) as GlobalScope
import Web.Worker.MessageEvent (MessageEvent)
import Web.Worker.GlobalScope (importScripts, location, navigator, onError, onLanguageChange, onOffline
, onOnline, onRejectionHandled, onUnhandledRejection
) as GlobalScope
import Web.Worker.Types (Transferable)

foreign import name :: Effect String

foreign import postMessageImpl :: forall msg. msg -> Array Transferable -> Effect Unit

-- | sends a message to the main thread that spawned it.
postMessage :: forall msg. msg -> Effect Unit
postMessage msg = postMessageImpl msg []
postMessage msg = postMessage' msg []

postMessage' :: forall msg. msg -> Array Transferable -> Effect Unit
postMessage' = postMessageImpl
postMessage' msg tr = runEffectFn2 postMessageImpl msg tr

foreign import postMessageImpl :: forall msg. EffectFn2 msg (Array Transferable) Unit

-- | upon calling close, any queued tasks present in the event loop are discarded and the web worker scope is closed.
foreign import close :: Effect Unit

-- | fired when the worker receives a message from its parent.
foreign import onMessage :: (MessageEvent -> Effect Unit) -> Effect Unit
onMessage :: (MessageEvent -> Effect Unit) -> Effect Unit
onMessage cb = runEffectFn1 onMessageImpl (mkEffectFn1 cb)

foreign import onMessageImpl :: EffectFn1 (EffectFn1 MessageEvent Unit) Unit

-- | fired when a worker receives a message that can't be deserialized.
foreign import onMessageError :: (MessageEvent -> Effect Unit) -> Effect Unit
onMessageError :: (MessageEvent -> Effect Unit) -> Effect Unit
onMessageError cb = runEffectFn1 onMessageErrorImpl (mkEffectFn1 cb)

foreign import onMessageErrorImpl :: EffectFn1 (EffectFn1 MessageEvent Unit) Unit
Loading