Skip to content

Commit

Permalink
Merge pull request #19 from jeffijoe/refactor/ts
Browse files Browse the repository at this point in the history
Port to TypeScript + TaskGroup feature
  • Loading branch information
jeffijoe committed Sep 18, 2019
2 parents 724b543 + 5262d6b commit 0e4e7df
Show file tree
Hide file tree
Showing 27 changed files with 4,604 additions and 4,025 deletions.
23 changes: 0 additions & 23 deletions .babelrc

This file was deleted.

3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
4 changes: 0 additions & 4 deletions .eslintrc

This file was deleted.

7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
node_modules
npm-debug.log
yarn-error.log
coverage
.nyc_output
.DS_Store
lib
.test-out
.DS_Store
.idea/workspace.xml
.sublime-workspace
.npmrc
7 changes: 4 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
examples
test
src
coverage
.nyc-output
.test-out
src
**/__tests__/**
**/__helpers__/**
9 changes: 0 additions & 9 deletions .nycrc

This file was deleted.

21 changes: 18 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# Oh yeah!
language: node_js
cache:
directories:
- node_modules
notifications:
email: false

# Add additional versions here as appropriate.
node_js:
- "10"
- 'stable'
- '10'
- '8'

# Lint errors should trigger a failure.
before_script: npm run lint
before_script:
- npm run lint
- npm run build

# Runs the coverage script (which runs the tests)
script: npm run cover

# Code coverage
after_success: npm run coveralls
after_success:
- npm run coveralls
- npm run semantic-release

branches:
except:
- /^v\d+\.\d+\.\d+$/
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"coverage": true,
".idea": true,
"lib": true,
"**/*.sublime-*": true,
"node_modules": true
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.0.0

- TypeScript definitions for public API based on the DefinitelyTyped ones.
- Ported codebase to basic TypeScript. A lot of exotic things going on, so definitely not idiomatic.
- Added `TaskGroup` for combining task result reactions into a single state.

## 1.0.4

- Add name argument for `setState` action ([#18](https://github.com/jeffijoe/mobx-task/pull/18) by [@cyclops26](https://github.com/cyclops26))
Expand Down
74 changes: 71 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
[![Coveralls](https://img.shields.io/coveralls/jeffijoe/mobx-task.svg?maxAge=1000)](https://coveralls.io/github/jeffijoe/mobx-task)
[![npm](https://img.shields.io/npm/dt/mobx-task.svg?maxAge=1000)](https://www.npmjs.com/package/mobx-task)
[![npm](https://img.shields.io/npm/l/mobx-task.svg?maxAge=1000)](https://github.com/jeffijoe/mobx-task/blob/master/LICENSE.md)
[![node](https://img.shields.io/node/v/mobx-task.svg?maxAge=1000)](https://www.npmjs.com/package/mobx-task)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![TypeScript definitions on DefinitelyTyped](https://definitelytyped.org/badges/standard-flat.svg)](http://definitelytyped.org)

Takes the suck out of managing state for async functions in MobX.

Expand All @@ -21,6 +18,7 @@ Table of Contents
* [Full example with classes and decorators](#full-example-with-classes-and-decorators)
* [Full example with plain observables](#full-example-with-plain-observables)
* [How does it work?](#how-does-it-work)
* [Task Groups](#task-groups)
* [API documentation](#api-documentation)
* [The task factory](#the-task-factory)
* [As a decorator](#as-a-decorator)
Expand All @@ -35,6 +33,7 @@ Table of Contents
* [`setState()`](#setstate)
* [`bind()`](#bind)
* [`reset()`](#reset)
* [`TaskGroup`](#taskgroup)
* [Gotchas](#gotchas)
* [Wrapping the task function](#wrapping-the-task-function)
* [Using the decorator on React Components](#using-the-decorator-on-react-components)
Expand Down Expand Up @@ -248,6 +247,54 @@ autorun(() => {
func().then(todos => { /*...*/ })
```

# Task Groups

<small>since `mobx-task v2.0.0` </small>

A `TaskGroup` is useful when you want to track pending, resolved and rejected state for multiple tasks but treat them as one.

Under the hood, a `TaskGroup` reacts to the start of any of the tasks (when `pending` flips to `true`), tracks the latest started task, and proxies all getters
to it. The first `pending` task (or the first task in the input array, if none are `pending`) is used as the initial task to proxy to.

**IMPORTANT**: Running the tasks concurrently will lead to wonky results. The intended use is for
tracking `pending`, `resolved` and `rejected` states of the _last run task_. You should prevent your users from
concurrently running tasks in the group.

```js
import { task, TaskGroup } from 'mobx-task'

const toggleTodo = task.resolved((id) => api.toggleTodo(id))
const deleteTodo = task.resolved((id) => { throw new Error('deleting todos is for quitters') })

const actions = TaskGroup([
toggleTodo,
deleteTodo
])

autorun(() => {
const whatsGoingOn = actions.match({
pending: () => 'Todo is being worked on',
resolved: () => 'Todo is ready to be worked on',
rejected: (err) => `Something failed on the todo: ${err.message}`
})
console.log(whatsGoingOn)
})

// initial log from autorun setup
// <- Todo is ready to be worked on

await toggleTodo('some-id')

// <- Todo is being worked on
// ...
// <- Todo is ready to be worked on

await deleteTodo('some-id')
// <- Todo is being worked on
// ...
// <- Something failed on the todo: deleting todos is for quitters
```

# API documentation

There's only a single exported member; `task`.
Expand Down Expand Up @@ -453,6 +500,27 @@ Resets the state to what it was when the task was initialized.

This means if you use `const t = task.resolved(fn)`, calling `t.reset()` will set the state to `resolved`.

## `TaskGroup`

Creates a `TaskGroup`. Takes an array of tasks to track. Implements the readable parts of the `Task`.

Uses the first task in the array as the proxy target.

```js
import { task, TaskGroup } from 'mobx-task'

const task1 = task(() => 42)
const task2 = task(() => 1337)
const group = TaskGroup([
task1,
task2
])

console.log(group.state)
console.log(group.resolved)
console.log(group.result)
```

# Gotchas

## Wrapping the task function
Expand Down
Loading

0 comments on commit 0e4e7df

Please sign in to comment.