Skip to content

Commit

Permalink
Updated depenencies
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Jun 5, 2017
1 parent bf11d2f commit 2562f8d
Show file tree
Hide file tree
Showing 11 changed files with 3,982 additions and 100 deletions.
6 changes: 4 additions & 2 deletions __tests__/Collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Collection', () => {

function resolve (attr) {
return () => {
apiClient().resolver = (resolve) => resolve(attr)
apiClient().resolver = resolve => resolve(attr)
}
}

Expand Down Expand Up @@ -47,7 +47,9 @@ describe('Collection', () => {

describe('filter', () => {
it('filters a collection with the given conditions', () => {
expect(collection.filter({ name: 'miles' })[0].get('name')).toBe(item.name)
expect(collection.filter({ name: 'miles' })[0].get('name')).toBe(
item.name
)
expect(collection.filter({ name: 'bob' }).length).toBe(0)
})
})
Expand Down
11 changes: 4 additions & 7 deletions __tests__/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Model', () => {

function resolve (attr) {
return () => {
apiClient().resolver = (resolve) => resolve(attr)
apiClient().resolver = resolve => resolve(attr)
}
}

Expand Down Expand Up @@ -362,10 +362,7 @@ describe('Model', () => {
it('it removes the model', () => {
collection.remove = jest.fn()
model.destroy()
expect(collection.remove).toBeCalledWith(
[model.optimisticId],
{ optimistic: true }
)
expect(collection.remove).toBeCalledWith([model.optimisticId])
})
})

Expand Down Expand Up @@ -493,7 +490,7 @@ describe('Model', () => {
})

it('returns the response', () => {
return model.fetch().then((response) => {
return model.fetch().then(response => {
expect(response.name).toBe('bill')
})
})
Expand Down Expand Up @@ -542,7 +539,7 @@ describe('Model', () => {
})

it('returns the response', () => {
return model.rpc('approve').then((response) => {
return model.rpc('approve').then(response => {
expect(response).toBe('foo')
})
})
Expand Down
14 changes: 9 additions & 5 deletions __tests__/mocks/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
resolver: (resolve) => { resolve() },
resolver: resolve => {
resolve()
},

_mock () {
return {
Expand All @@ -16,10 +18,12 @@ export default {
const ret = this._mock()

// HACK
ret.promise.then(() => {
options.onProgress(100)
options.onProgress.flush()
}).catch(() => {})
ret.promise
.then(() => {
options.onProgress(100)
options.onProgress.flush()
})
.catch(() => {})

return ret
},
Expand Down
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,33 @@
},
"dependencies": {
"lodash": "^4.17.4",
"mobx": "^3.1.5"
"mobx": "^3.1.11"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-jest": "^19.0.0",
"babel-plugin-transform-async-to-generator": "^6.22.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-1": "^6.22.0",
"babel-register": "^6.23.0",
"eslint": "^3.18.0",
"eslint-config-standard": "^7.1.0",
"eslint-plugin-flowtype": "2.30.3",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^4.2.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-1": "^6.24.1",
"babel-register": "^6.24.1",
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-flowtype": "2.34.0",
"eslint-plugin-import": "^2.3.0",
"eslint-plugin-node": "^5.0.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^2.1.1",
"flow-bin": "^0.41.0",
"husky": "^0.13.2",
"jest": "^19.0.2",
"lint-staged": "^3.4.0",
"prettier-standard": "^1.0.6"
"eslint-plugin-standard": "^3.0.1",
"flow-bin": "^0.47.0",
"husky": "^0.13.4",
"jest": "^20.0.4",
"lint-staged": "^3.6.0",
"prettier-standard": "^5.0.0"
},
"main": "lib",
"scripts": {
Expand All @@ -63,11 +63,11 @@
"lint": "eslint src __tests__",
"flow": "flow",
"test": "npm run flow && npm run lint && npm run jest",
"format": "prettier-standard --print-width 60 'src/**/*.js'",
"format": "prettier-standard --print-width 60 \"{src,__tests__}/**/*.js\"",
"prepush": "npm test",
"lint-staged": {
"linters": {
"src/**/*.js": [
"{src|__tests__}/**/*.js": [
"prettier-standard",
"git add"
]
Expand Down
43 changes: 23 additions & 20 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import apiClient from './apiClient'
import type { Label, CreateOptions, SetOptions, Id } from './types'

export default class Collection<T: Model> {
@observable request: ?Request = null;
@observable error: ?ErrorObject = null;
@observable models: IObservableArray<T> = [];
@observable request: ?Request = null
@observable error: ?ErrorObject = null
@observable models: IObservableArray<T> = []

constructor (data: Array<{ [key: string]: any }> = []) {
this.set(data)
Expand Down Expand Up @@ -117,7 +117,8 @@ export default class Collection<T: Model> {
* Adds a collection of models.
* Returns the added models.
*/
@action add (data: Array<{ [key: string]: any }>): Array<T> {
@action
add (data: Array<{ [key: string]: any }>): Array<T> {
const models = data.map(d => this.build(d))
this.models.push(...models)
return models
Expand All @@ -126,7 +127,8 @@ export default class Collection<T: Model> {
/**
* Removes the model with the given ids or uuids
*/
@action remove (ids: Array<Id>): void {
@action
remove (ids: Array<Id>): void {
ids.forEach(id => {
const model = this.get(id)
if (!model) return
Expand All @@ -140,7 +142,8 @@ export default class Collection<T: Model> {
*
* You can disable adding, changing or removing.
*/
@action set (
@action
set (
resources: Array<{ [key: string]: any }>,
{ add = true, change = true, remove = true }: SetOptions = {}
): void {
Expand Down Expand Up @@ -175,7 +178,8 @@ export default class Collection<T: Model> {
* The default behaviour is optimistic but this
* can be tuned.
*/
@action async create (
@action
async create (
attributesOrModel: { [key: string]: any } | Model,
{ optimistic = true }: CreateOptions = {}
): Promise<*> {
Expand All @@ -185,18 +189,15 @@ export default class Collection<T: Model> {
: attributesOrModel
const label: Label = 'creating'

const onProgress = debounce(
function onProgress (progress) {
if (optimistic && model.request) {
model.request.progress = progress
}
const onProgress = debounce(function onProgress (progress) {
if (optimistic && model.request) {
model.request.progress = progress
}

if (this.request) {
this.request.progress = progress
}
},
300
)
if (this.request) {
this.request.progress = progress
}
}, 300)

const { abort, promise } = apiClient().post(this.url(), attributes, {
onProgress
Expand Down Expand Up @@ -248,7 +249,8 @@ export default class Collection<T: Model> {
* use the options to disable adding, changing
* or removing.
*/
@action async fetch (options: SetOptions = {}): Promise<void> {
@action
async fetch (options: SetOptions = {}): Promise<void> {
const label: Label = 'fetching'
const { abort, promise } = apiClient().get(this.url(), options.data)

Expand Down Expand Up @@ -281,7 +283,8 @@ export default class Collection<T: Model> {
* non-REST endpoints that you may have in
* your API.
*/
@action async rpc (method: string, body?: {}): Promise<*> {
@action
async rpc (method: string, body?: {}): Promise<*> {
const label: Label = 'updating' // TODO: Maybe differentiate?
const { promise, abort } = apiClient().post(
`${this.url()}/${method}`,
Expand Down
4 changes: 2 additions & 2 deletions src/ErrorObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import type { Label } from './types'

export default class ErrorObject {
label: Label;
body: {};
label: Label
body: {}

constructor (label: Label, body: {}) {
this.label = label
Expand Down
58 changes: 28 additions & 30 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import type {
} from './types'

export default class Model {
@observable request: ?Request = null;
@observable error: ?ErrorObject = null;
attributes: ObservableMap;
@observable request: ?Request = null
@observable error: ?ErrorObject = null
attributes: ObservableMap

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

constructor (attributes: { [key: string]: any } = {}) {
this.attributes = observable.map(attributes)
Expand Down Expand Up @@ -100,7 +100,8 @@ export default class Model {
* We determine this asking if it contains
* the `primaryKey` attribute (set by the server).
*/
@computed get isNew (): boolean {
@computed
get isNew (): boolean {
return !this.has(this.primaryKey)
}

Expand Down Expand Up @@ -145,14 +146,16 @@ export default class Model {
* Merge the given attributes with
* the current ones
*/
@action set (data: {}): void {
@action
set (data: {}): void {
this.attributes.merge(data)
}

/**
* Fetches the model from the backend.
*/
@action async fetch (options: { data?: {} } = {}): Promise<void> {
@action
async fetch (options: { data?: {} } = {}): Promise<void> {
const label: Label = 'fetching'
const { abort, promise } = apiClient().get(this.url(), options.data)

Expand Down Expand Up @@ -190,7 +193,8 @@ export default class Model {
*
* TODO: Add progress
*/
@action async save (
@action
async save (
attributes: {},
{ optimistic = true, patch = true }: SaveOptions = {}
): Promise<*> {
Expand All @@ -216,14 +220,11 @@ export default class Model {
data = Object.assign({}, originalAttributes, attributes)
}

const onProgress = debounce(
function onProgress (progress) {
if (optimistic && this.request) {
this.request.progress = progress
}
},
300
)
const onProgress = debounce(function onProgress (progress) {
if (optimistic && this.request) {
this.request.progress = progress
}
}, 300)

const { promise, abort } = apiClient().put(this.url(), data, {
method: patch ? 'PATCH' : 'PUT',
Expand Down Expand Up @@ -267,14 +268,11 @@ export default class Model {
): Promise<*> {
const label: Label = 'creating'

const onProgress = debounce(
function onProgress (progress) {
if (optimistic && this.request) {
this.request.progress = progress
}
},
300
)
const onProgress = debounce(function onProgress (progress) {
if (optimistic && this.request) {
this.request.progress = progress
}
}, 300)

const { abort, promise } = apiClient().post(this.url(), attributes, {
onProgress
Expand Down Expand Up @@ -311,11 +309,10 @@ export default class Model {
* requests the backend to delete it there
* too
*/
@action async destroy (
{ optimistic = true }: DestroyOptions = {}
): Promise<*> {
@action
async destroy ({ optimistic = true }: DestroyOptions = {}): Promise<*> {
if (!this.has(this.primaryKey) && this.collection) {
this.collection.remove([this.optimisticId], { optimistic })
this.collection.remove([this.optimisticId])
return Promise.resolve()
}

Expand Down Expand Up @@ -358,7 +355,8 @@ export default class Model {
* non-REST endpoints that you may have in
* your API.
*/
@action async rpc (method: string, body?: {}): Promise<*> {
@action
async rpc (method: string, body?: {}): Promise<*> {
const label: Label = 'updating' // TODO: Maybe differentiate?
const { promise, abort } = apiClient().post(
`${this.url()}/${method}`,
Expand Down
6 changes: 3 additions & 3 deletions src/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { observable } from 'mobx'
import type { Label } from './types'

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

constructor (label: Label, abort: () => void, progress: number) {
this.label = label
Expand Down
Loading

0 comments on commit 2562f8d

Please sign in to comment.