Skip to content

Commit

Permalink
use default export and change Status name
Browse files Browse the repository at this point in the history
  • Loading branch information
MZanggl committed Jan 15, 2020
1 parent 973cb83 commit 98eb58c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default {
</template>
<script>
import { promistate } from 'promistate'
import promistate from 'promistate'
export default {
data() {
Expand All @@ -82,7 +82,7 @@ export default {
## API

```javascript
import { promistate } from 'promistate'
import promistate from 'promistate'

const userPromise = promistate(async function callback() {
return fetchUser() // any promise
Expand Down Expand Up @@ -118,10 +118,10 @@ It immediately returns an object that has the following properties
- ERROR
- IGNORED (see configurations below)

To avoid hardcoding these, you can import "Status" from the library:
To avoid hardcoding these, you can import "PromistateStatus" from the library:

```javascript
import { promistate, Status } from 'promistate'
import promistate, { PromistateStatus } from 'promistate'

const userPromise = promistate(async function callback() {
return fetchUser() // any promise
Expand All @@ -147,7 +147,7 @@ await userPromise.load(1, 2)
Pass configurations as the second argument

```javascript
import { promistate } from 'promistate'
import promistate from 'promistate'

promistate(async function callback() {
return somethingAsync()
Expand All @@ -166,7 +166,7 @@ promistate(async function callback() {
To type the result of the promise you can make use of generics.

```typescript
import { promistate } from 'promistate'
import promistate from 'promistate'

promistate<string>(async function callback() {
return 'updated'
Expand All @@ -180,7 +180,7 @@ promistate<string>(async function callback() {
As long as you don't use arrow functions you can access the state using `this`.

```javascript
import { promistate } from 'promistate'
import promistate from 'promistate'

promistate(async function callback() {
const result = await fetchItems(this.pageToken)
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ function promistate<T>(action: (...args: CallbackArgs) => Promise<T>, options: P
}
}

export default promistate

export {
promistate,
Status,
Status as PromistateStatus,
}
8 changes: 4 additions & 4 deletions test/promistate.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('japa')
const { promistate, Status } = require('../dist/index')
const { default: promistate, PromistateStatus } = require('../dist/index')

test('can access default properties', (assert) => {
const state = promistate(async () => 1)
Expand All @@ -13,7 +13,7 @@ test('can set value through loading', async (assert) => {
const state = promistate(async () => 1)
const status = await state.load()

assert.equal(status, Status.RESOLVED)
assert.equal(status, PromistateStatus.RESOLVED)
assert.equal(state.value, 1)
assert.isFalse(state.isPending)
assert.isFalse(state.isEmpty)
Expand Down Expand Up @@ -71,7 +71,7 @@ test('does not execute load function when state is still pending', async (assert
])

assert.equal(state.value, 'load this')
assert.deepEqual(promises, [Status.RESOLVED, Status.IGNORED])
assert.deepEqual(promises, [PromistateStatus.RESOLVED, PromistateStatus.IGNORED])
})

test('catches errors', async (assert) => {
Expand All @@ -85,7 +85,7 @@ test('catches errors', async (assert) => {
assert.isFalse(state.isPending)
assert.isTrue(state.isEmpty)
assert.isNull(state.value)
assert.equal(status, Status.ERROR)
assert.equal(status, PromistateStatus.ERROR)
})

test('resets value when crashing', async (assert) => {
Expand Down

0 comments on commit 98eb58c

Please sign in to comment.