Skip to content

Commit

Permalink
core/local/chokidar: Extract separatePendingChanges() from analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
sebn committed Jul 12, 2019
1 parent 29f3224 commit bb1c968
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 deletions.
57 changes: 2 additions & 55 deletions core/local/chokidar/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* 1. {@link module:core/local/chokidar/analyse_doc_events|analyseDocEvents}
* 2. {@link module:core/local/chokidar/squash_moves|squashMoves}
* 4. {@link module:core/local/chokidar/final_sort|finalSort}
* 5. {@link module:core/local/chokidar/analysis~separatePendingChanges|separatePendingChanges}
* 5. {@link module:core/local/chokidar/separate_pending_changes|separatePendingChanges}
*
* ## Known issues
*
Expand All @@ -29,18 +29,14 @@

const { analyseDocEvents } = require('./analyse_doc_events')
const { finalSort } = require('./final_sort')
const { separatePendingChanges } = require('./separate_pending_changes')
const { squashMoves } = require('./squash_moves')
const logger = require('../../utils/logger')

/*::
import type { LocalEvent } from './local_event'
import type { LocalChange } from './local_change'
*/

const log = logger({
component: 'LocalAnalysis'
})

module.exports = function analysis(
events /*: LocalEvent[] */,
pendingChanges /*: LocalChange[] */
Expand All @@ -50,52 +46,3 @@ module.exports = function analysis(
finalSort(changes)
return separatePendingChanges(changes, pendingChanges)
}

/** Push back pending changes.
*
* More low-level events are expected to come up for those changes to be
* complete. They will be injected back in the next analysis run.
*
* This step helped us fix a bunch of move scenarios with unexpected event
* batches.
*
* ## Known issues
*
* - May break events order.
* - No timeout (some changes may be pending forever).
*/
function separatePendingChanges(
changes /*: LocalChange[] */,
pendingChanges /*: LocalChange[] */
) /*: LocalChange[] */ {
log.trace('Reserve changes in progress for next flush...')

for (let i = 0; i < changes.length; i++) {
const change = changes[i]
if (change.wip) {
if (change.type === 'DirMove' || change.type === 'FileMove') {
log.debug(
{
change: change.type,
oldpath: change.old.path,
path: change.path,
ino: change.ino
},
'incomplete change'
)
} else {
log.debug(
{ change: change.type, path: change.path },
'incomplete change'
)
}
pendingChanges.push(changes[i])
} else {
log.debug(`Identified ${changes.length} change(s).`)
log.debug(`${pendingChanges.length} of them are still pending.`)
return changes.slice(i)
}
}
// All actions are WIP
return []
}
66 changes: 66 additions & 0 deletions core/local/chokidar/separate_pending_changes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @module core/local/chokidar/separate_pending_changes
* @flow
*/

const logger = require('../../utils/logger')

/*::
import type { LocalChange } from './local_change'
*/

const component = 'chokidar/separate_pending_changes'
const log = logger({ component })

/** Push back pending changes.
*
* More low-level events are expected to come up for those changes to be
* complete. They will be injected back in the next analysis run.
*
* This step helped us fix a bunch of move scenarios with unexpected event
* batches.
*
* ## Known issues
*
* - May break events order.
* - No timeout (some changes may be pending forever).
*/
const separatePendingChanges = (
changes /*: LocalChange[] */,
pendingChanges /*: LocalChange[] */
) /*: LocalChange[] */ => {
log.trace('Reserve changes in progress for next flush...')

for (let i = 0; i < changes.length; i++) {
const change = changes[i]
if (change.wip) {
if (change.type === 'DirMove' || change.type === 'FileMove') {
log.debug(
{
change: change.type,
oldpath: change.old.path,
path: change.path,
ino: change.ino
},
'incomplete change'
)
} else {
log.debug(
{ change: change.type, path: change.path },
'incomplete change'
)
}
pendingChanges.push(changes[i])
} else {
log.debug(`Identified ${changes.length} change(s).`)
log.debug(`${pendingChanges.length} of them are still pending.`)
return changes.slice(i)
}
}
// All actions are WIP
return []
}

module.exports = {
separatePendingChanges
}

0 comments on commit bb1c968

Please sign in to comment.