Skip to content

Commit

Permalink
Cleanup the release
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Mar 9, 2017
1 parent 262e284 commit 87dd8c2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-rest",
"version": "2.0.0",
"version": "2.0.2",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
52 changes: 23 additions & 29 deletions src/Collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { observable, action, IObservableArray, runInAction } from 'mobx'
import { observable, action, IObservableArray, runInAction, toJS } from 'mobx'
import Model from './Model'
import {
isEmpty,
Expand All @@ -11,12 +11,14 @@ import {
map,
last
} from 'lodash'
import ErrorObject from './ErrorObject'
import Request from './Request'
import apiClient from './apiClient'
import type { Label, CreateOptions, ErrorType, Request, SetOptions, Id } from './types'
import type { Label, CreateOptions, SetOptions, Id } from './types'

export default class Collection<T: Model> {
request: ?Request = observable.shallowObject(null)
error: ?ErrorType = observable.shallowObject(null)
@observable request: ?Request = null
@observable error: ?ErrorObject = null
models: IObservableArray<T> = observable.shallowArray([])

constructor (data: Array<{[key: string]: any}> = []) {
Expand All @@ -39,6 +41,14 @@ export default class Collection<T: Model> {
return Model
}

/**
* Returns a JSON representation
* of the collection
*/
toJS () {
return toJS(this.models)
}

/**
* Questions whether the request exists
* and matches a certain label
Expand Down Expand Up @@ -192,18 +202,10 @@ export default class Collection<T: Model> {
model = attributesOrModel instanceof Model
? attributesOrModel
: last(this.add([attributesOrModel]))
model.request = {
label,
abort,
progress: 0
}
model.request = new Request(label, abort, 0)
}

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

let data: {}

Expand All @@ -214,7 +216,7 @@ export default class Collection<T: Model> {
if (model) {
this.remove([model.id])
}
this.error = { label, body }
this.error = new ErrorObject(label, body)
this.request = null
})

Expand Down Expand Up @@ -249,23 +251,19 @@ export default class Collection<T: Model> {
options.data
)

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

let data: Array<{[key: string]: any}>

try {
data = await promise
} catch (err) {
} catch (body) {
runInAction('fetch-error', () => {
this.error = { label, body: err }
this.error = new ErrorObject(label, body)
this.request = null
})

throw err
throw body
}

runInAction('fetch-done', () => {
Expand All @@ -292,11 +290,7 @@ export default class Collection<T: Model> {
body || {}
)

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

let response

Expand All @@ -305,7 +299,7 @@ export default class Collection<T: Model> {
} catch (body) {
runInAction('accept-fail', () => {
this.request = null
this.error = { label, body }
this.error = new ErrorObject(label, body)
})

throw body
Expand Down
12 changes: 12 additions & 0 deletions src/ErrorObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @flow
import type { Label } from './types'

export default class Request {
label: Label
body: {}

constructor (label: Label, body: {}) {
this.label = label
this.body = body
}
}
61 changes: 25 additions & 36 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import {
action,
ObservableMap,
computed,
runInAction
runInAction,
toJS
} from 'mobx'
import Collection from './Collection'
import { uniqueId, isString, debounce } from 'lodash'
import apiClient from './apiClient'
import Request from './Request'
import ErrorObject from './ErrorObject'
import type {
OptimisticId,
ErrorType,
Request,
Id,
Label,
DestroyOptions,
Expand All @@ -21,17 +22,25 @@ import type {
} from './types'

export default class Model {
request: ?Request = observable.shallowObject(null)
error: ?ErrorType = observable.shallowObject(null)
@observable request: ?Request = null
@observable error: ?ErrorObject = null
attributes: ObservableMap

optimisticId: OptimisticId = uniqueId('i_')
collection: ?Collection<*> = null
attributes: ObservableMap

constructor (attributes: {[key: string]: any} = {}) {
this.attributes = observable.map(attributes)
}

/**
* Returns a JSON representation
* of the model
*/
toJS () {
return toJS(this.attributes)
}

/**
* Return the base url used in
* the `url` method
Expand Down Expand Up @@ -133,19 +142,15 @@ export default class Model {
options.data
)

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

let data

try {
data = await promise
} catch (body) {
runInAction('fetch-error', () => {
this.error = { label, body }
this.error = new ErrorObject(label, body)
this.request = null
})

Expand Down Expand Up @@ -205,11 +210,7 @@ export default class Model {

if (optimistic) this.set(newAttributes)

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

let response

Expand All @@ -219,7 +220,7 @@ export default class Model {
runInAction('save-fail', () => {
this.request = null
this.set(originalAttributes)
this.error = { label, body }
this.error = new ErrorObject(label, body)
})

throw isString(body) ? new Error(body) : body
Expand Down Expand Up @@ -256,11 +257,7 @@ export default class Model {
)

if (optimistic) {
this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)
}

let data: {}
Expand All @@ -269,7 +266,7 @@ export default class Model {
data = await promise
} catch (body) {
runInAction('create-error', () => {
this.error = { label, body }
this.error = new ErrorObject(label, body)
this.request = null
})

Expand Down Expand Up @@ -305,11 +302,7 @@ export default class Model {
this.collection.remove([this.id])
}

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

try {
await promise
Expand All @@ -318,7 +311,7 @@ export default class Model {
if (optimistic && this.collection) {
this.collection.add([this.attributes.toJS()])
}
this.error = { label, body }
this.error = new ErrorObject(label, body)
this.request = null
})

Expand Down Expand Up @@ -351,11 +344,7 @@ export default class Model {
body || {}
)

this.request = {
label,
abort,
progress: 0
}
this.request = new Request(label, abort, 0)

let response

Expand All @@ -364,7 +353,7 @@ export default class Model {
} catch (body) {
runInAction('accept-fail', () => {
this.request = null
this.error = { label, body }
this.error = new ErrorObject(label, body)
})

throw body
Expand Down
15 changes: 15 additions & 0 deletions src/Request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @flow
import { observable } from 'mobx'
import type { Label } from './types'

export default class Request {
label: Label
abort: () => void
progress: number

constructor (label: Label, abort: () => void, progress: number) {
this.label = label
this.abort = abort
this.progress = observable(progress)
}
}
18 changes: 6 additions & 12 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ export type SaveOptions = {
onProgress?: () => mixed;
}

export type ErrorType = {
label: Label;
body: {};
}

export type Request = {
label: Label;
export type Response = {
abort: () => void;
progress: number;
promise: Promise<*>;
}

export type SetOptions = {
Expand All @@ -38,8 +32,8 @@ export type SetOptions = {
}

export type Adapter = {
get (path: string, data?: {}, options?: {}): Request;
post (path: string, data?: {}, options?: {}): Request;
put (path: string, data?: {}, options?: {}): Request;
del (path: string, options?: {}): Request;
get (path: string, data?: {}, options?: {}): Response;
post (path: string, data?: {}, options?: {}): Response;
put (path: string, data?: {}, options?: {}): Response;
del (path: string, options?: {}): Response;
}

0 comments on commit 87dd8c2

Please sign in to comment.