Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] core: Use AtomWatcher on macOS #1677

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ function loadOrDeleteFile(configPath /*: string */) /*: FileConfig */ {
/** Detect which local watcher will be used. */
function watcherType(
fileConfig /*: FileConfig */ = {},
{ env, platform } /*: * */ = process
{ env } /*: * */ = process
) /*: WatcherType */ {
return (
fileWatcherType(fileConfig) ||
environmentWatcherType(env) ||
platformDefaultWatcherType(platform)
platformDefaultWatcherType()
)
}

Expand All @@ -250,12 +250,7 @@ function environmentWatcherType(
return validateWatcherType(COZY_FS_WATCHER)
}

function platformDefaultWatcherType(
platform /*: string */ = process.platform
) /*: WatcherType */ {
if (platform === 'darwin') {
return 'chokidar'
}
function platformDefaultWatcherType() /*: WatcherType */ {
return 'atom'
}

Expand Down
19 changes: 17 additions & 2 deletions core/local/atom/producer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const watcher = require('@atom/watcher')
const Channel = require('./channel')
const { INITIAL_SCAN_DONE } = require('./event')
const defaultStater = require('../stater')
const LocalEventBuffer = require('../chokidar/event_buffer')
const logger = require('../../utils/logger')

/*::
import type { Ignore } from '../../ignore'
import type { AtomEvent } from './event'
import type { AtomBatch, AtomEvent } from './event'

export type Scanner = (string) => Promise<void>
*/
Expand Down Expand Up @@ -61,12 +62,21 @@ class Producer {
syncPath: string
ignore: Ignore
watcher: *
macOSBuffer: ?LocalEventBuffer<AtomBatch>
*/
constructor(opts /*: { syncPath: string, ignore: Ignore } */) {
this.channel = new Channel()
this.syncPath = opts.syncPath
this.ignore = opts.ignore
this.watcher = null
if (process.platform === 'darwin') {
const timeoutInMs = process.env.NODE_ENV === 'test' ? 1000 : 10000
this.macOSBuffer = new LocalEventBuffer(timeoutInMs, batches => {
for (const batch of batches) {
this.process(batch)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await?

}
})
}
autoBind(this)
}

Expand All @@ -90,10 +100,15 @@ class Producer {
* the recursive option.
*/
async start() {
const macOSBuffer = this.macOSBuffer
const onEventBatch = macOSBuffer
? macOSBuffer.push.bind(macOSBuffer)
: this.process

this.watcher = await watcher.watchPath(
this.syncPath,
{ recursive: true },
this.process
onEventBatch
Copy link
Contributor Author

@sebn sebn Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we regenerate all captures with buffer? (I believe they were regenerated before reintroducing the buffer)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also is the buffer necessary or not?

)
log.info(`Now watching ${this.syncPath}`)
if (process.platform === 'linux') {
Expand Down
6 changes: 1 addition & 5 deletions core/local/atom/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ const steps = _.compact([

/** The producer for the current platform. */
const producer = opts => {
if (['linux', 'win32'].includes(process.platform)) {
return new Producer(opts)
} else {
throw new Error('The atom watcher is not available on this platform')
}
return new Producer(opts)
}

const stepsInitialState = (
Expand Down
27 changes: 27 additions & 0 deletions test/scenarios/add_delete_dir_and_file/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
[
{
"action": "created",
"kind": "directory",
"path": "dir"
},
{
"action": "created",
"kind": "file",
"path": "file"
}
],
[],
[
{
"action": "deleted",
"kind": "directory",
"path": "dir"
},
{
"action": "deleted",
"kind": "file",
"path": "file"
}
]
]
9 changes: 9 additions & 0 deletions test/scenarios/add_file/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
[
{
"action": "created",
"kind": "file",
"path": "file"
}
]
]
17 changes: 17 additions & 0 deletions test/scenarios/add_trash_dir/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
[
{
"action": "created",
"kind": "directory",
"path": "dir"
}
],
[],
[
{
"action": "deleted",
"kind": "directory",
"path": "dir"
}
]
]
16 changes: 16 additions & 0 deletions test/scenarios/add_trash_file/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
[
{
"action": "created",
"kind": "file",
"path": "file"
}
],
[
{
"action": "deleted",
"kind": "file",
"path": "file"
}
]
]
9 changes: 9 additions & 0 deletions test/scenarios/change_file/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
[
{
"action": "modified",
"kind": "file",
"path": "file"
}
]
]
16 changes: 16 additions & 0 deletions test/scenarios/create_dir_into_moved_one/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
[
{
"action": "renamed",
"kind": "directory",
"path": "dst/dir1",
"oldPath": "src/dir1"
},
{
"action": "created",
"kind": "directory",
"path": "dst/dir1/dir2"
}
],
[]
]
43 changes: 43 additions & 0 deletions test/scenarios/create_dirs/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
[
{
"action": "created",
"kind": "directory",
"path": "foo"
},
{
"action": "created",
"kind": "directory",
"path": "foo/bar"
}
],
[],
[
{
"action": "scan",
"path": "foo/bar",
"kind": "directory",
"stats": {
"dev": 16777226,
"mode": 16877,
"nlink": 2,
"uid": 501,
"gid": 20,
"rdev": 0,
"blksize": 4194304,
"ino": 8600863105,
"size": 64,
"blocks": 0,
"atimeMs": 1554478410553.5947,
"mtimeMs": 1554478410533.3728,
"ctimeMs": 1554478410533.3728,
"birthtimeMs": 1554478410533.3728,
"atime": "2019-04-05T15:33:30.554Z",
"mtime": "2019-04-05T15:33:30.533Z",
"ctime": "2019-04-05T15:33:30.533Z",
"birthtime": "2019-04-05T15:33:30.533Z"
}
}
],
[]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
[
{
"action": "renamed",
"kind": "directory",
"path": "c",
"oldPath": "a"
}
],
[
{
"action": "renamed",
"kind": "directory",
"path": "a",
"oldPath": "b"
}
]
]
24 changes: 24 additions & 0 deletions test/scenarios/move_and_trash_dir/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
[
{
"action": "renamed",
"kind": "directory",
"path": "dst/subdir",
"oldPath": "src/subdir"
}
],
[
{
"action": "deleted",
"kind": "file",
"path": "dst/subdir/file"
}
],
[
{
"action": "deleted",
"kind": "directory",
"path": "dst/subdir"
}
]
]
17 changes: 17 additions & 0 deletions test/scenarios/move_and_trash_file/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
[
{
"action": "renamed",
"kind": "file",
"path": "dst/file",
"oldPath": "src/file"
}
],
[
{
"action": "deleted",
"kind": "file",
"path": "dst/file"
}
]
]
17 changes: 17 additions & 0 deletions test/scenarios/move_and_update_file/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
[
{
"action": "renamed",
"kind": "file",
"path": "dst/file",
"oldPath": "src/file"
}
],
[
{
"action": "modified",
"kind": "file",
"path": "dst/file"
}
]
]
26 changes: 26 additions & 0 deletions test/scenarios/move_dir_a_to_b_to_c_to_b/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
[
{
"action": "renamed",
"kind": "directory",
"path": "src/B",
"oldPath": "src/A"
}
],
[
{
"action": "renamed",
"kind": "directory",
"path": "src/C",
"oldPath": "src/B"
}
],
[
{
"action": "renamed",
"kind": "directory",
"path": "src/B",
"oldPath": "src/C"
}
]
]
17 changes: 17 additions & 0 deletions test/scenarios/move_dir_and_update_subfile/atom/darwin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
[
{
"action": "renamed",
"kind": "directory",
"path": "dst",
"oldPath": "src"
}
],
[
{
"action": "modified",
"kind": "file",
"path": "dst/file"
}
]
]
Loading