Skip to content

Commit

Permalink
fix eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
radex committed Mar 24, 2021
1 parent 3df7941 commit e32a23d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 132 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ rules:
import/prefer-default-export: 0
import/named: 0 # doesn't seem to work with Flow
import/no-extraneous-dependencies: 0
import/no-cycle: 0 # FIXME: broken with flow https://github.com/benmosher/eslint-plugin-import/issues/1343
import/no-cycle: 0
jest/no-large-snapshots: 1
jest/no-disabled-tests: 0
global-require: 0
no-plusplus: 0
max-classes-per-file: 0
prefer-object-spread: 0
react/jsx-props-no-spreading: 0

overrides:
- files:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@
"chokidar": "^3.0.0",
"concurrently": "^6.0.0",
"eslint": "^6.5.1",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-airbnb": "18.0.1",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-flowtype": "^4.3.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^22.17.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.16.0",
Expand Down
28 changes: 18 additions & 10 deletions src/adapters/lokijs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export type LokiAdapterOptions = $Exact<{
// This happens if there's another open tab of the same app that's making changes.
// You might use it as an opportunity to alert user to the potential loss of data
onDidOverwrite?: () => void,
...,
...
},
// -- internal --
_testLokiAdapter?: LokiMemoryAdapter,
Expand Down Expand Up @@ -109,17 +109,23 @@ export default class LokiJSAdapter implements DatabaseAdapter {
this._dbName = dbName

if (process.env.NODE_ENV !== 'production') {
invariant('useWebWorker' in options,
'LokiJSAdapter `useWebWorker` option is required. Pass `{ useWebWorker: false }` to adopt the new behavior, or `{ useWebWorker: true }` to supress this warning with no changes',
)
invariant(
'useWebWorker' in options,
'LokiJSAdapter `useWebWorker` option is required. Pass `{ useWebWorker: false }` to adopt the new behavior, or `{ useWebWorker: true }` to supress this warning with no changes',
)
if (options.useWebWorker === true) {
logger.warn('LokiJSAdapter {useWebWorker: true} option is now deprecated. If you rely on this feature, please file an issue')
}
invariant('useIncrementalIndexedDB' in options,
'LokiJSAdapter `useIncrementalIndexedDB` option is required. Pass `{ useIncrementalIndexedDB: true }` to adopt the new behavior, or `{ useIncrementalIndexedDB: false }` to supress this warning with no changes',
logger.warn(
'LokiJSAdapter {useWebWorker: true} option is now deprecated. If you rely on this feature, please file an issue',
)
}
invariant(
'useIncrementalIndexedDB' in options,
'LokiJSAdapter `useIncrementalIndexedDB` option is required. Pass `{ useIncrementalIndexedDB: true }` to adopt the new behavior, or `{ useIncrementalIndexedDB: false }` to supress this warning with no changes',
)
if (options.useIncrementalIndexedDB === false) {
logger.warn('LokiJSAdapter {useIncrementalIndexedDB: false} option is now deprecated. If you rely on this feature, please file an issue')
logger.warn(
'LokiJSAdapter {useIncrementalIndexedDB: false} option is now deprecated. If you rely on this feature, please file an issue',
)
}
// TODO(2021-05): Remove this
invariant(
Expand Down Expand Up @@ -242,7 +248,9 @@ export default class LokiJSAdapter implements DatabaseAdapter {

// if we can't, but can filter to it, it means that Loki indices are corrupted
const didFindByFilter = !!lokiCollection.data.filter(doc => doc.id === id)
logger.log(`Did find ${table}#${id} in Loki collection by filtering the collection? ${didFindByFilter}`)
logger.log(
`Did find ${table}#${id} in Loki collection by filtering the collection? ${didFindByFilter}`,
)
}
}
}
78 changes: 35 additions & 43 deletions src/sync/impl/applyRemote.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// @flow

import {
mapObj,
filterObj,
pipe,
toPairs,
} from '../../utils/fp'
import { mapObj, filterObj, pipe, toPairs } from '../../utils/fp'
import splitEvery from '../../utils/fp/splitEvery'
import allPromisesObj from '../../utils/fp/allPromisesObj'
import { logError, invariant, logger } from '../../utils/common'
Expand Down Expand Up @@ -168,27 +163,28 @@ const getAllRecordsToApply = (
): AllRecordsToApply =>
allPromisesObj(
pipe(
filterObj((_changes, tableName: TableName<any>) => {
const collection = db.get((tableName: any))
filterObj((_changes, tableName: TableName<any>) => {
const collection = db.get((tableName: any))

if (!collection) {
logger.warn(
`You are trying to sync a collection named ${tableName}, but it does not exist. Will skip it (for forward-compatibility). If this is unexpected, perhaps you forgot to add it to your Database constructor's modelClasses property?`,
)
}
if (!collection) {
logger.warn(
`You are trying to sync a collection named ${tableName}, but it does not exist. Will skip it (for forward-compatibility). If this is unexpected, perhaps you forgot to add it to your Database constructor's modelClasses property?`,
)
}

return !!collection
}),
mapObj((changes, tableName: TableName<any>) => {
return recordsToApplyRemoteChangesTo(db.get((tableName: any)), changes)
}))(remoteChanges)
return !!collection
}),
mapObj((changes, tableName: TableName<any>) => {
return recordsToApplyRemoteChangesTo(db.get((tableName: any)), changes)
}),
)(remoteChanges),
)

const destroyAllDeletedRecords = (db: Database, recordsToApply: AllRecordsToApply): Promise<*> => {
const promises = toPairs(recordsToApply).map(([tableName, { deletedRecordsToDestroy }]) => {
return deletedRecordsToDestroy.length ?
db.adapter.destroyDeletedRecords((tableName: any), deletedRecordsToDestroy) :
null
return deletedRecordsToDestroy.length
? db.adapter.destroyDeletedRecords((tableName: any), deletedRecordsToDestroy)
: null
})
return Promise.all(promises)
}
Expand All @@ -202,13 +198,15 @@ const applyAllRemoteChanges = (
): Promise<void> => {
const allRecords = []
toPairs(recordsToApply).forEach(([tableName, records]) => {
allRecords.push(...prepareApplyRemoteChangesToCollection(
db.get((tableName: any)),
records,
sendCreatedAsUpdated,
log,
conflictResolver,
))
allRecords.push(
...prepareApplyRemoteChangesToCollection(
db.get((tableName: any)),
records,
sendCreatedAsUpdated,
log,
conflictResolver,
),
)
})
return db.batch(allRecords)
}
Expand Down Expand Up @@ -252,21 +250,15 @@ export default function applyRemoteChanges(
// Perform steps concurrently
await Promise.all([
destroyAllDeletedRecords(db, recordsToApply),
_unsafeBatchPerCollection ?
unsafeApplyAllRemoteChangesByBatches(
db,
recordsToApply,
sendCreatedAsUpdated,
log,
conflictResolver,
) :
applyAllRemoteChanges(
db,
recordsToApply,
sendCreatedAsUpdated,
log,
conflictResolver,
),
_unsafeBatchPerCollection
? unsafeApplyAllRemoteChangesByBatches(
db,
recordsToApply,
sendCreatedAsUpdated,
log,
conflictResolver,
)
: applyAllRemoteChanges(db, recordsToApply, sendCreatedAsUpdated, log, conflictResolver),
])
}, 'sync-applyRemoteChanges')
}
38 changes: 17 additions & 21 deletions src/sync/impl/markAsSynced.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// @flow

import {
mapObj,
values,
unnest,
} from '../../utils/fp'
import { mapObj, values, unnest } from '../../utils/fp'
import areRecordsEqual from '../../utils/fp/areRecordsEqual'
import allPromisesObj from '../../utils/fp/allPromisesObj'
import { logError } from '../../utils/common'
Expand All @@ -14,21 +10,18 @@ import { prepareMarkAsSynced, ensureActionsEnabled } from './helpers'
import type { SyncLocalChanges } from '../index'

const unchangedRecordsForRaws = (raws, recordCache) =>
raws.reduce(
(records, raw) => {
const record = recordCache.find(model => model.id === raw.id)
if (!record) {
logError(
`[Sync] Looking for record ${raw.id} to mark it as synced, but I can't find it. Will ignore it (it should get synced next time). This is probably a Watermelon bug — please file an issue!`,
)
return records
}
raws.reduce((records, raw) => {
const record = recordCache.find(model => model.id === raw.id)
if (!record) {
logError(
`[Sync] Looking for record ${raw.id} to mark it as synced, but I can't find it. Will ignore it (it should get synced next time). This is probably a Watermelon bug — please file an issue!`,
)
return records
}

// only include if it didn't change since fetch
return areRecordsEqual(record._raw, raw) ? records.concat(record) : records
},
[],
)
// only include if it didn't change since fetch
return areRecordsEqual(record._raw, raw) ? records.concat(record) : records
}, [])

const recordsToMarkAsSynced = ({ changes, affectedRecords }: SyncLocalChanges): Model[] => {
// $FlowFixMe
Expand All @@ -37,14 +30,17 @@ const recordsToMarkAsSynced = ({ changes, affectedRecords }: SyncLocalChanges):
// $FlowFixMe
changesTables.map(({ created, updated }) =>
unchangedRecordsForRaws(created.concat(updated), affectedRecords),
)
),
)
}

const destroyDeletedRecords = (db: Database, { changes }: SyncLocalChanges): Promise<*> =>
allPromisesObj(
// $FlowFixMe
mapObj(({ deleted }, tableName) => db.adapter.destroyDeletedRecords(tableName, deleted), changes),
mapObj(
({ deleted }, tableName) => db.adapter.destroyDeletedRecords(tableName, deleted),
changes,
),
)

export default function markLocalChangesAsSynced(
Expand Down
Loading

0 comments on commit e32a23d

Please sign in to comment.