Skip to content

Commit

Permalink
Remove unnecessary imports and add imports contexts to patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
omer88 committed Aug 17, 2017
1 parent cfd9a02 commit 8831963
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 157 deletions.
10 changes: 5 additions & 5 deletions __mocks__/react-native-firebase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

export class Database {
ref = (path) => {
ref = path => {
if (!this[path]) {
this[path] = new Reference(path)
}
Expand All @@ -12,7 +12,7 @@ export class Database {
export class Reference {
constructor(path) {
this.path = path
this.snap = { val: () => this._val()}
this.snap = { val: () => this._val() }
this.data = null
}

Expand All @@ -21,7 +21,7 @@ export class Reference {
})

once = jest.fn((param, callback) => {
const promise = new Promise ((resolve, reject) => {
const promise = new Promise((resolve, reject) => {
if (callback) {
callback(this.snap)
resolve()
Expand All @@ -34,7 +34,7 @@ export class Reference {
})

on = jest.fn((param, callback) => {
const promise = new Promise ((resolve, reject) => {
const promise = new Promise((resolve, reject) => {
if (callback) {
callback(this.snap)
resolve()
Expand All @@ -52,7 +52,7 @@ export class Reference {
return promise
})

update = jest.fn((data) => {
update = jest.fn(data => {
const promise = Promise.resolve()
RNFirebase.promises.push(promise)
return promise
Expand Down
10 changes: 1 addition & 9 deletions __tests__/actions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import * as actions from '../actions'
import * as types from '../types'
import firebase from '../firebase'
import { metaTypes } from '../types'
import RNFirebase from 'react-native-firebase'
import configureMockStore from 'redux-mock-store'
import { getInitialState } from '../reducer'

const mockStore = configureMockStore()

describe('firebase actions', () => {
beforeEach(() => {
RNFirebase.reset()
})

test(types.firebase.FIREBASE_LISTEN_REQUESTED, () => {
const ref = firebase.database().ref('someRef')
const expectedAction = {
Expand Down
14 changes: 2 additions & 12 deletions __tests__/sagas.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import * as sagas from '../sagas'
import * as types from '../types'
import firebase from '../firebase'
import {
all,
put,
takeEvery,
take,
call,
fork,
cancel,
flush,
cancelled,
} from 'redux-saga/effects'
import { cloneableGenerator, createMockTask } from 'redux-saga/utils'
import * as actions from '../actions'
import { metaTypes, eventTypes } from '../types'
import { cloneableGenerator, createMockTask } from 'redux-saga/utils'
import { put, take, call, fork, cancel, flush } from 'redux-saga/effects'

describe('database saga', () => {
test(`watchUpdateRequested ${metaTypes.userContacts}`, () => {
Expand Down
26 changes: 3 additions & 23 deletions patterns/listener_actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as types from './types'
import { metaTypes } from './types'

export function firebaseListenRequested(ref, metaType) {
return {
type: types.firebase.FIREBASE_LISTEN_REQUESTED,
Expand Down Expand Up @@ -30,22 +33,6 @@ export function firebaseListenChildAdded(id, value, metaType) {
}
}

export function firebaseListenChildChanged(id, value, metaType) {
return {
type: types.firebase.FIREBASE_LISTEN_CHILD_CHANGED,
payload: { id, value },
meta: { type: metaType },
}
}

export function firebaseListenChildRemoved(id, metaType) {
return {
type: types.firebase.FIREBASE_LISTEN_CHILD_REMOVED,
payload: { id },
meta: { type: metaType },
}
}

export function firebaseListenRemoved(clearItems, metaType) {
return {
type: types.firebase.FIREBASE_LISTEN_REMOVED,
Expand All @@ -61,10 +48,3 @@ export function firebaseRemoveListenerRequested(clearItems, metaType) {
meta: { type: metaType },
}
}

export function firebaseRemoveAllListenersRequested() {
return {
type: types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED,
payload: { clearItems: true },
}
}
30 changes: 4 additions & 26 deletions patterns/listener_reducer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as types from './types'
import { metaTypes } from './types'
...

[types.firebase.FIREBASE_LISTEN_REQUESTED](state, action) {
const property = action.meta.type
const propertyState = state[property]
Expand Down Expand Up @@ -45,32 +49,6 @@
}
return newState
},
[types.firebase.FIREBASE_LISTEN_CHILD_CHANGED](state, action) {
const property = action.meta.type
const propertyState = state[property]
const items = {
...propertyState.items,
[action.payload.id]: action.payload.value,
}

let newState = {
...state,
[property]: { ...propertyState, inProgress: false, error: '', items },
}
return newState
},
[types.firebase.FIREBASE_LISTEN_CHILD_REMOVED](state, action) {
const property = action.meta.type
const propertyState = state[property]
const items = { ...propertyState.items }
delete items[action.payload.id]

let newState = {
...state,
[property]: { ...propertyState, inProgress: false, error: '', items },
}
return newState
},
[types.firebase.FIREBASE_LISTEN_REMOVED](state, action) {
const property = action.meta.type
const propertyState = state[property]
Expand Down
59 changes: 14 additions & 45 deletions patterns/listener_sagas.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import * as types from './types'
import { metaTypes, eventTypes } from './types'
import * as actions from './actions'
import firebase from './firebase'
import { eventChannel, buffers } from 'redux-saga'
import { put, take, call, fork, cancel, flush } from 'redux-saga/effects'

export function* watchListener(metaType) {
while (true) {
const listenRequestAction = yield take(
Expand All @@ -13,20 +20,12 @@ export function* watchListener(metaType) {
const action = yield take([
types.firebase.FIREBASE_REMOVE_LISTENER_REQUESTED,
types.firebase.FIREBASE_LISTEN_REQUESTED,
types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED,
])

if (
action.type ===
types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED ||
action.meta.type === metaType
) {
if (action.meta.type === metaType) {
yield cancel(task)
yield put(
FirebaseActions.firebaseListenRemoved(
!!action.payload.clearItems,
metaType
)
actions.firebaseListenRemoved(!!action.payload.clearItems, metaType)
)

if (action.type === types.firebase.FIREBASE_LISTEN_REQUESTED) {
Expand All @@ -53,19 +52,6 @@ export function createEventChannel(ref) {
value: snap.val(),
})
})

ref.on('child_changed', snap => {
const val = snap.val()
emit({
eventType: eventTypes.CHILD_CHANGED,
key: snap.key,
value: snap.val(),
})
})

ref.on('child_removed', snap => {
emit({ eventType: eventTypes.CHILD_REMOVED, key: snap.key })
})
return () => {
ref.off()
}
Expand All @@ -81,34 +67,17 @@ export function* getDataAndListenToChannel(ref, metaType) {
yield flush(chan)
const val = snap.val()
const value = val ? val : {}
yield put(FirebaseActions.firebaseListenFulfilled(value, metaType))
yield put(actions.firebaseListenFulfilled(value, metaType))
} catch (error) {
yield put(FirebaseActions.firebaseListenRejected(error, metaType))
yield put(actions.firebaseListenRejected(error, metaType))
}
while (true) {
const data = yield take(chan)
yield put(getUpdateAction(data, metaType))
yield put(
actions.firebaseListenChildAdded(data.key, data.value, metaType)
)
}
} finally {
chan.close()
}
}

export function getUpdateAction(data, metaType) {
switch (data.eventType) {
case eventTypes.CHILD_ADDED:
return FirebaseActions.firebaseListenChildAdded(
data.key,
data.value,
metaType
)
case eventTypes.CHILD_CHANGED:
return FirebaseActions.firebaseListenChildChanged(
data.key,
data.value,
metaType
)
case eventTypes.CHILD_REMOVED:
return FirebaseActions.firebaseListenChildRemoved(data.key, metaType)
}
}
3 changes: 3 additions & 0 deletions patterns/remove_actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as types from './types'
import { metaTypes } from './types'

export function firebaseRemoveRequested(payload, metaType) {
return {
type: types.firebase.FIREBASE_REMOVE_REQUESTED,
Expand Down
5 changes: 5 additions & 0 deletions patterns/remove_reducer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import * as types from './types'
import { metaTypes } from './types'

...

[types.firebase.FIREBASE_REMOVE_REQUESTED](state, action) {
const property = action.meta.type
let newState = { ...state, [property]: { inProgress: true, error: '' } }
Expand Down
10 changes: 8 additions & 2 deletions patterns/remove_sagas.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import * as types from './types'
import { metaTypes } from './types'
import * as actions from './actions'
import firebase from './firebase'
import { put, take, call, fork } from 'redux-saga/effects'

export function* watchRemoveRequested() {
while (true) {
const action = yield take(types.firebase.FIREBASE_REMOVE_REQUESTED)
Expand All @@ -23,8 +29,8 @@ export function* removeItem(path, metaType) {
try {
const ref = firebase.database().ref(path)
yield call([ref, ref.remove])
yield put(FirebaseActions.firebaseRemoveFulfilled(metaType))
yield put(actions.firebaseRemoveFulfilled(metaType))
} catch (error) {
yield put(FirebaseActions.firebaseRemoveRejected(error, metaType))
yield put(actions.firebaseRemoveRejected(error, metaType))
}
}
4 changes: 4 additions & 0 deletions patterns/update_action_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as actions from '../actions'
import * as types from '../types'
import { metaTypes } from '../types'

test('updateUserContactsRequested', () => {
const uid = '1'
const contactId = '123'
Expand Down
3 changes: 3 additions & 0 deletions patterns/update_actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as types from './types'
import { metaTypes } from './types'

export function firebaseUpdateRequested(payload, metaType) {
return {
type: types.firebase.FIREBASE_UPDATE_REQUESTED,
Expand Down
5 changes: 5 additions & 0 deletions patterns/update_reducer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import * as types from './types'
import { metaTypes } from './types'

...

[types.firebase.FIREBASE_UPDATE_REQUESTED](state, action) {
const property = action.meta.type
let newState = { ...state, [property]: { inProgress: true, error: '' } }
Expand Down
10 changes: 8 additions & 2 deletions patterns/update_sagas.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import * as types from './types'
import { metaTypes } from './types'
import * as actions from './actions'
import firebase from './firebase'
import { put, take, call, fork } from 'redux-saga/effects'

export function* watchUpdateRequested() {
while (true) {
const action = yield take(types.firebase.FIREBASE_UPDATE_REQUESTED)
Expand All @@ -18,9 +24,9 @@ export function* updateItems(updates, metaType) {
try {
const ref = firebase.database().ref()
yield call([ref, ref.update], updates)
yield put(FirebaseActions.firebaseUpdateFulfilled(metaType))
yield put(actions.firebaseUpdateFulfilled(metaType))
} catch (error) {
yield put(FirebaseActions.firebaseUpdateRejected(error, metaType))
yield put(actions.firebaseUpdateRejected(error, metaType))
}
}

Expand Down
9 changes: 9 additions & 0 deletions patterns/update_sagas_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import * as sagas from '../sagas'
import * as types from '../types'
import * as actions from '../actions'
import firebase from '../firebase'
import { put, take, call, fork, cancel, flush } from 'redux-saga/effects'

import { cloneableGenerator } from 'redux-saga/utils'
import { metaTypes } from '../types'

test(`watchUpdateRequested ${metaTypes.userContacts}`, () => {
const generator = sagas.watchUpdateRequested()
const updates = { updates: { a: '1', b: '2' } }
Expand Down
Loading

0 comments on commit 8831963

Please sign in to comment.