Skip to content

Commit

Permalink
Remove Responses enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sdgluck committed Jun 7, 2016
1 parent 1046c3f commit 52b1f01
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 50 deletions.
5 changes: 0 additions & 5 deletions fetchSync/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,3 @@ export const Requests = {
CANCEL_SYNC: 'CANCEL_SYNC',
CANCEL_ALL: 'CANCEL_ALL'
}

export const Responses = {
SUCCESS: 'SUCCESS',
FAILURE: 'FAILURE'
}
2 changes: 1 addition & 1 deletion fetchSync/client/createSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function createSync (name, request, options = {}) {
name,
request,
options,
id: uId(),
id: name || uId(),
createdOn: Date.now(),
syncedOn: null,
response: null
Expand Down
24 changes: 2 additions & 22 deletions fetchSync/client/store/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { addSync, addSyncs, removeSync, setCommsOpen, removeAllSyncs, requestOpe
requestCancelSync, requestRegisterSync, requestCancelAllSyncs } from './creators'

import { CommsChannelStatus } from '../../constants'
import { Responses } from '../../actionTypes'
import store from './index'

export function registerSync (sync) {
Expand Down Expand Up @@ -132,20 +131,11 @@ function postMessage (data) {
function receiveFetchResponse (event) {
return (dispatch, getState) => {
const { syncs } = getState()
const { type, data } = JSON.parse(event.data)
const data = JSON.parse(event.data)
const sync = syncs[data.id]

if (sync) {
switch (type) {
case Responses.SUCCESS:
handleSyncSuccess(dispatch, sync, data)
return
case Responses.FAILURE:
handleSyncFailure(dispatch, sync, data)
return
default:
throw new Error(`Unknown response type '${type}'`)
}
handleSyncSuccess(dispatch, sync, data)
}
}
}
Expand All @@ -161,13 +151,3 @@ function handleSyncSuccess (dispatch, sync, data) {
dispatch(removeSync(sync))
}
}

function handleSyncFailure (dispatch, sync, data) {
sync.reject(data.error)

if (sync.name) {
sync.response = null
} else {
dispatch(removeSync(sync))
}
}
11 changes: 1 addition & 10 deletions fetchSync/worker/Channel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

import { Responses } from '../actionTypes'

export default class Channel {
constructor (worker, messageHandlers) {
worker.onmessage = this.onMessageEvent.bind(this)
Expand Down Expand Up @@ -34,13 +32,6 @@ export default class Channel {
onMessageEvent (event) {
return this
.handleMessage(event)
.catch((err) => this.postMessage({
type: Responses.FAILURE,
data: { error: err.message }
}, event.ports[0]))
.then((data) => this.postMessage({
type: Responses.SUCCESS,
data
}, event.ports[0]))
.then((data) => this.postMessage(data, event.ports[0]))
}
}
14 changes: 2 additions & 12 deletions fetchSync/worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const serialiseResponse = require('serialise-response')

const Channel = require('./Channel')
const { Requests, Responses } = require('../actionTypes')
const { Requests } = require('../actionTypes')

const store = new IDBStore({
dbVersion: 1,
Expand Down Expand Up @@ -71,17 +71,7 @@
.then((response) => {
const syncedOn = Date.now()
store.put({ ...sync, response, syncedOn })
channel.postMessage({
type: Responses.SUCCESS,
data: { id, lastChance, response }
})
})
.catch((err) => {
store.remove(id)
channel.postMessage({
type: Responses.FAILURE,
data: { error: err.message }
})
channel.postMessage({ id, lastChance, response })
})
})
)
Expand Down

0 comments on commit 52b1f01

Please sign in to comment.