Skip to content

Commit

Permalink
Refactored sync code and added refetchCollections() action (#458)
Browse files Browse the repository at this point in the history
* [tech] Refactored sync code and added reftechCollections() action

* [fix] Added PouchDB mock so that tests can run

* [fix] Linting...
  • Loading branch information
goldoraf authored Nov 8, 2017
1 parent fb0972e commit 1f770a4
Show file tree
Hide file tree
Showing 25 changed files with 2,995 additions and 1,581 deletions.
77 changes: 37 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
"main": "src/main.jsx",
"scripts": {
"build:drive": "npm run build:drive:browser",
"build:drive:browser":
"npm run internal:build -- --env.app=drive --env.target=browser --env.production",
"build:drive:mobile":
"npm run internal:build -- --env.app=drive --env.target=mobile --env.production",
"build:drive:browser": "npm run internal:build -- --env.app=drive --env.target=browser --env.production",
"build:drive:mobile": "npm run internal:build -- --env.app=drive --env.target=mobile --env.production",
"build:photos": "npm run build:photos:browser",
"build:photos:browser":
"npm run internal:build -- --env.app=photos --env.target=browser --env.production",
"build:photos:browser": "npm run internal:build -- --env.app=photos --env.target=browser --env.production",
"clean:browser": "rm -rf build/*",
"clean:drive:browser": "rm -rf build/drive/*",
"clean:photos:browser": "rm -rf build/photos/*",
"clean:mobile": "rm -rf targets/*/mobile/www/*",
"deploy:drive":
"git-directory-deploy --directory=build/drive/ --username=Cozy [email protected] --repo=https://[email protected]/cozy/cozy-drive.git",
"deploy:photos":
"git-directory-deploy --directory=build/photos/ --username=Cozy [email protected] --repo=https://[email protected]/cozy/cozy-drive.git",
"deploy:drive": "git-directory-deploy --directory=build/drive/ --username=Cozy [email protected] --repo=https://[email protected]/cozy/cozy-drive.git",
"deploy:photos": "git-directory-deploy --directory=build/photos/ --username=Cozy [email protected] --repo=https://[email protected]/cozy/cozy-drive.git",
"internal:build": "webpack --config webpack.config.js --bail",
"internal:watch":
"webpack --config webpack.config.js --watch --display-modules --display-chunks",
"internal:watch": "webpack --config webpack.config.js --watch --display-modules --display-chunks",
"tx": "tx pull --all || true",
"preinternal:build": "npm-run-all lint tx",
"prebuild:drive:browser": "npm run clean:drive:browser",
Expand All @@ -37,32 +31,21 @@
"precommit": "lint-staged",
"test": "node_modules/.bin/jest --coverage --config=jest.config.json",
"watch:drive": "npm run watch:drive:browser",
"watch:drive:browser":
"npm run internal:watch -- --env.app=drive --env.target=browser",
"watch:drive:mobile":
"npm run internal:watch -- --env.app=drive --env.target=mobile",
"watch:drive:standalone":
"webpack-dev-server --config webpack.config.js --env.app=drive --env.target=mobile --port 8084 --display-modules --display-chunks --inline --hot",
"watch:drive:browser": "npm run internal:watch -- --env.app=drive --env.target=browser",
"watch:drive:mobile": "npm run internal:watch -- --env.app=drive --env.target=mobile",
"watch:drive:standalone": "webpack-dev-server --config webpack.config.js --env.app=drive --env.target=mobile --port 8084 --display-modules --display-chunks --inline --hot",
"watch:photos": "npm run watch:photos:browser",
"watch:photos:browser":
"npm run internal:watch -- --env.app=photos --env.target=browser",
"mobile:drive:icon":
"(cd targets/drive/mobile && splashicon-generator --imagespath='./res/model')",
"watch:photos:browser": "npm run internal:watch -- --env.app=photos --env.target=browser",
"mobile:drive:icon": "(cd targets/drive/mobile && splashicon-generator --imagespath='./res/model')",
"mobile:drive:prepare": "(cd targets/drive/mobile && cordova prepare)",
"android:drive:run":
"(cd targets/drive/mobile && cordova run android --device)",
"android:drive:run:emulator":
"(cd targets/drive/mobile && cordova run android --emulator)",
"android:drive:release":
"npm run build:drive:mobile && (cd targets/drive/mobile && cordova build android --release)",
"android:drive:signed":
"npm run android:drive:release && (cd targets/drive/mobile && apksigner sign --ks keys/android/cozy-drive-release-key.jks --out build/android/cozy-drive.apk platforms/android/build/outputs/apk/android-release-unsigned.apk)",
"android:drive:run": "(cd targets/drive/mobile && cordova run android --device)",
"android:drive:run:emulator": "(cd targets/drive/mobile && cordova run android --emulator)",
"android:drive:release": "npm run build:drive:mobile && (cd targets/drive/mobile && cordova build android --release)",
"android:drive:signed": "npm run android:drive:release && (cd targets/drive/mobile && apksigner sign --ks keys/android/cozy-drive-release-key.jks --out build/android/cozy-drive.apk platforms/android/build/outputs/apk/android-release-unsigned.apk)",
"android:drive:publish": "(cd targets/drive/mobile && fastlane supply)",
"ios:drive:run": "(cd targets/drive/mobile && cordova run ios --device)",
"ios:drive:run:emulator":
"(cd targets/drive/mobile && cordova run ios --emulator)",
"ios:drive:publish":
"npm run build:drive:mobile && (cd targets/drive/mobile && fastlane ios pushtest)"
"ios:drive:run:emulator": "(cd targets/drive/mobile && cordova run ios --emulator)",
"ios:drive:publish": "npm run build:drive:mobile && (cd targets/drive/mobile && fastlane ios pushtest)"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -90,7 +73,7 @@
"autoprefixer": "^6.5.0",
"babel-core": "^6.0.0",
"babel-eslint": "^8.0.0",
"babel-jest": "^18.0.0",
"babel-jest": "^21.2.0",
"babel-loader": "^6.0.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
Expand Down Expand Up @@ -124,7 +107,7 @@
"husky": "^0.14.3",
"identity-obj-proxy": "^3.0.0",
"imports-loader": "^0.7.0",
"jest": "^20.0.4",
"jest": "^21.2.1",
"json-loader": "^0.5.0",
"lint-staged": "^4.2.1",
"npm-run-all": "^3.1.0",
Expand Down Expand Up @@ -181,12 +164,19 @@
"redux-thunk": "^2.1.0"
},
"jest": {
"moduleFileExtensions": ["js", "jsx", "json", "styl"],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"styl"
],
"moduleNameMapper": {
"\\.(png|gif|jpe?g|svg)$": "<rootDir>/test/__mocks__/fileMock.js",
"styles": "identity-obj-proxy"
},
"transformIgnorePatterns": ["node_modules/(?!cozy-ui)"],
"transformIgnorePatterns": [
"node_modules/(?!cozy-ui)"
],
"globals": {
"__ALLOW_HTTP__": false,
"__TARGET__": "browser",
Expand All @@ -204,7 +194,11 @@
"prettier/react",
"prettier/standard"
],
"plugins": ["react", "prettier", "standard"],
"plugins": [
"react",
"prettier",
"standard"
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
Expand All @@ -231,6 +225,9 @@
"singleQuote": true
},
"lint-staged": {
"{src,test}/**/*.{js,jsx}": ["prettier --write", "git add"]
"{src,test}/**/*.{js,jsx}": [
"prettier --write",
"git add"
]
}
}
8 changes: 5 additions & 3 deletions src/drive/components/File.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ class File extends Component {
<time dateTime={attributes.updated_at || attributes.created_at}>
{f(
attributes.updated_at || attributes.created_at,
`${isExtraLarge
? t('table.row_update_format_full')
: t('table.row_update_format')}`
`${
isExtraLarge
? t('table.row_update_format_full')
: t('table.row_update_format')
}`
)}
</time>
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/drive/components/LightFolderView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class DumbFolderView extends React.Component {
label={t('toolbar.menu_download_folder')}
className={toolbarstyles['fil-public-download']}
onDownload={() =>
this.props.onDownload([this.props.displayedFolder])}
this.props.onDownload([this.props.displayedFolder])
}
/>
<Menu
title={t('toolbar.item_more')}
Expand All @@ -68,7 +69,8 @@ class DumbFolderView extends React.Component {
<a
className={toolbarstyles['fil-action-download']}
onClick={() =>
this.props.onDownload([this.props.displayedFolder])}
this.props.onDownload([this.props.displayedFolder])
}
>
{t('toolbar.menu_download_folder')}
</a>
Expand Down
5 changes: 3 additions & 2 deletions src/drive/mobile/containers/onboarding/Welcome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export class Welcome extends Component {
</button>
{!isIos() && (
<a
href={`https://cozy.io/fr/try-it?from=io.cozy.drive.mobile&os=${this
.platform}`}
href={`https://cozy.io/fr/try-it?from=io.cozy.drive.mobile&os=${
this.platform
}`}
className={styles['link']}
>
{t('mobile.onboarding.welcome.no_account_link')}
Expand Down
36 changes: 22 additions & 14 deletions src/lib/cozy-client/DataAccessFacade.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
/* global cozy, PouchDB */
/* global cozy */
import CozyStackAdapter from './adapters/CozyStackAdapter'
import PouchdbAdapter, {
SYNC_BIDIRECTIONAL,
SYNC_TO,
SYNC_FROM
} from './adapters/PouchdbAdapter'

const FILES_DOCTYPE = 'io.cozy.files'

// const isOnline = () =>
// typeof navigator !== 'undefined' ? navigator.onLine : true

export default class DataAccessFacade {
constructor() {
this.stackAdapter = new CozyStackAdapter()
if (typeof PouchDB !== 'undefined') {
this.pouchAdapter = new PouchdbAdapter()
this.pouchAdapter = new PouchdbAdapter()
}

setup(cozyUrl, options) {
const { offline, ...rest } = options
// TODO: For now we let cozy-client-js handle offline for files so that we don't break cozy-drive
const config =
offline &&
offline.doctypes &&
offline.doctypes.indexOf(FILES_DOCTYPE) !== -1
? { cozyURL: cozyUrl, offline: { doctypes: [FILES_DOCTYPE] }, ...rest }
: { cozyURL: cozyUrl, ...rest }
// TODO: Get rid of cozy-client-js
cozy.client.init(config)
if (offline && offline.doctypes) {
this.pouchAdapter.registerDoctypes(offline.doctypes)
// TODO: strategy injection
this.strategy = new PouchFirstStrategy()
} else {
this.strategy = new StackOnlyStrategy()
}
}

setup(cozyUrl, options) {
const config = { cozyURL: cozyUrl, ...options }
cozy.client.init(config) // TODO: For now we let cozy-client-js creates PouchDB instances
if (config.offline) {
this.pouchAdapter.registerDoctypes(config.offline.doctypes)
}
}

getAdapter(doctype) {
return this.strategy.getAdapter(
doctype,
Expand All @@ -38,15 +46,15 @@ export default class DataAccessFacade {
}

startSync(dispatch) {
return this.pouchAdapter.sync(dispatch, SYNC_BIDIRECTIONAL)
return this.pouchAdapter.startSync(dispatch, SYNC_BIDIRECTIONAL)
}

startReplicationTo(dispatch) {
return this.pouchAdapter.sync(dispatch, SYNC_TO)
return this.pouchAdapter.startSync(dispatch, SYNC_TO)
}

startReplicationFrom(dispatch) {
return this.pouchAdapter.sync(dispatch, SYNC_FROM)
return this.pouchAdapter.startSync(dispatch, SYNC_FROM)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/cozy-client/__mocks__/pouchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var PouchDB = jest.genMockFunction()

module.exports = PouchDB
14 changes: 7 additions & 7 deletions src/lib/cozy-client/adapters/CozyStackAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ export default class CozyStackAdapter {
// WARN: the stack API is probably not ideal here: referencedFiles are in the 'included' property
// (that should be used when fetching an entity AND its relations) and the 'data' property
// only contains uplets { id, type }
const {
included,
meta
} = await cozy.client.data.fetchReferencedFiles(normalized, {
skip,
limit: FETCH_LIMIT
})
const { included, meta } = await cozy.client.data.fetchReferencedFiles(
normalized,
{
skip,
limit: FETCH_LIMIT
}
)
// we forge a standard response with a 'data' property
return {
data: !included
Expand Down
Loading

0 comments on commit 1f770a4

Please sign in to comment.