npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wicke/retask

v0.6.0

Published

Elm task but for ReScript

Downloads

5

Readme

npm Coveralls

Elm task but with ReScript and React

It's ${currentYear} and there're sufficient reasons for not using Elm, yet there isn't a real alternative(except Elmish, but it has its own problem too). Not until the release of React hook, in which useReducer alone is powerful enough for mimicking TEA. However there is one big thing missing: side effect.

This package is intended to port said system to ReScript so you can write Elm without really having to write Elm, or more shortly, Elm as a hook. You could also use it in plain JavaScript or TypeScript.

Effect Manager:

  • [x] Task composition
  • [x] Time
  • [ ] Ajax
  • [x] WebSocket
  • [x] Dom Event

Quick start

Install @wicke/retask use your favourite package manager.

And get the following example run on your favourite build system.

open ReTask;
@react.component
let make = () => {
  let (state, dispatch) = useReducerT({
    init: (0, Time.delay(1000, _ => 1)),
    update: (state, action) => (state + action, Cmd.none),
    sub: state => DomEvent.onDocument("click", ev => state),
  })

  <div> {state->React.int} </div>
}

or in JavaScript

import React from 'react'
import { useReducerT, delay, noCmd, onDocument } from '@wicke/retask'

export function FooComp() {
    const [state, dispatch] = useReducerT({
        init: [0, delay(1000, () => 1)]
        update: (state, action) => [state + action, noCmd],
        sub: state => onDocument('click', ev => state)
    })

    return <div>{state}</div>
}

You would see a 0 on screen and then a 1 one second later, and every time you click on the screen, that number will double.

Intro to ReTask

There are three elements in ReTask: Cmd, Task and Sub.

A Cmd is for one shot job like setTimeout. You may use Cmd to send message to your component or simply for performing side effects. For the former its constructor will take a tagger to map payload to your intended message. Don't worry if your jobs hasn't finished when your component is unmounting, they will be cancelled.

And what do you use when you want to compose two Cmds? It doesn't make sense to provide each of them a tagger, so you should use Task. Task is like Cmd without tagger. Using andThen/thenTask you could make two tasks runing one by one. And then by attempt/attemptTask you could turn it into a Cmd. If the Task won't fail, perform/performTask is more simple.

Sub, on the other hand, is for long running job like setInterval. Every time it see fit, a Sub would send message to your component. And if such message cause a state change, your sub function will be recalled with updated state and calcuate a new Sub. If it's of different type or be constructed with different parameter, your old Sub will be cancelled. Beware that although ReScript is not a pure language unlike Elm, here parameter changes are still detected with ===

How to write your own effect manager

Currently there are some effect managers missing and things will occur that maybe you have some specific task to run. So here's a guide on how to write your own Cmd | Task | Sub.

A Task is merely a function who takes a callback as an argument and return a function for cancelling ongoing task.

function voidTask(cb) {
    cb()
    return () => {}
}

And a Cmd is not far from it. In the function body, you call register so your task will be registered to your component. The returned value is for cancellation.

import { register } from '@wicke/retask/lib/src/es6/cmd'

function voidCmd(tagger) {
    register(send => {
        send(tagger(payload))
        return () => {}
    })
}

TODO: Sub

API Doc

TODO