Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[changed] the watch function callback now accepts one argument - an…
Browse files Browse the repository at this point in the history
… object which describes what files were changed
  • Loading branch information
thealjey committed Apr 21, 2016
1 parent 04cebd3 commit 4124ac4
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 24 deletions.
526 changes: 524 additions & 2 deletions docs/global.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/quicksearch.html

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions docs/typedef.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,33 @@ <h1 class="page-title">Source: typedef.js</h1>
* @param {Object} result - the resulting object
*/
export type ObjectOrErrorCallback = (error: ?string, result: Object) => void;

/**
* Describes a file a change in which was caught.
*
* @typedef {Object} WatchmanFile
* @property {string} name - the relative path to a file
* @property {number} size - file size in bytes
* @property {boolean} exists - true if the file exists
* @property {string} type - e.g. "f"
*/
export type WatchmanFile = {name: string, size: number, exists: boolean, type: string};

/**
* A watchman response object.
*
* @typedef {Object} WatchmanResponse
* @property {string} root - the path to the directory being watched
* @property {string} subscription - random subscription name
* @property {Array&lt;WatchmanFile>} files - an array of file descriptions
*/
export type WatchmanResponse = {root: string, subscription: string, files: Array&lt;WatchmanFile>};

/**
* @callback WatchCallback
* @param {WatchmanResponse} response - a watchman response object
*/
export type WatchCallback = (response: WatchmanResponse) => void;
</pre>
</article>
</section>
Expand Down
11 changes: 6 additions & 5 deletions docs/watch.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ <h1 class="page-title">Source: watch.js</h1>
<pre
class="sunlight-highlight-javascript linenums">/* @flow */

import type {WatchCallback} from './typedef';
import {Client} from 'fb-watchman';

const client = new Client(),
Expand All @@ -89,17 +90,17 @@ <h1 class="page-title">Source: watch.js</h1>
* This watcher's only goal is performance, hence the simplicity.
*
* @function watch
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {Function} callback - a callback function
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {WatchCallback} callback - a callback function
* @see {@link https://facebook.github.io/watchman/ Watchman}
* @example
* import {watch} from 'webcompiler';
* import {join} from 'path';
*
* watch(join(__dirname, 'src'), 'js', someFunction);
*/
export function watch(dir: string, type: string, callback: () => void) {
export function watch(dir: string, type: string, callback: WatchCallback) {
const subscription = Date.now().toString(ALPHANUMERIC_BASE);

client.capabilityCheck({}, capabilityErr => {
Expand Down Expand Up @@ -140,7 +141,7 @@ <h1 class="page-title">Source: watch.js</h1>

client.on('subscription', subscriptionResp => {
if (subscription === subscriptionResp.subscription) {
callback();
callback(subscriptionResp);
}
});
});
Expand Down
8 changes: 4 additions & 4 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var client = new _fbWatchman.Client(),
* This watcher's only goal is performance, hence the simplicity.
*
* @function watch
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {Function} callback - a callback function
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {WatchCallback} callback - a callback function
* @see {@link https://facebook.github.io/watchman/ Watchman}
* @example
* import {watch} from 'webcompiler';
Expand Down Expand Up @@ -68,7 +68,7 @@ function watch(dir, type, callback) {

client.on('subscription', function (subscriptionResp) {
if (subscription === subscriptionResp.subscription) {
callback();
callback(subscriptionResp);
}
});
});
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
"homepage": "https://thealjey.github.io/webcompiler",
"dependencies": {
"autoprefixer": "^6.3.6",
"babel-cli": "^6.7.5",
"babel-core": "^6.7.6",
"babel-eslint": "^6.0.2",
"babel-cli": "^6.7.7",
"babel-core": "^6.7.7",
"babel-eslint": "^6.0.3",
"babel-loader": "^6.2.4",
"babel-plugin-transform-class-properties": "^6.6.0",
"babel-plugin-transform-member-expression-literals": "^6.5.0",
Expand All @@ -68,11 +68,11 @@
"babel-preset-stage-2": "^6.5.0",
"babel-runtime": "^6.6.1",
"cheerio": "^0.20.0",
"codemirror": "^5.13.4",
"codemirror": "^5.14.2",
"eslint": "^2.8.0",
"eslint-plugin-babel": "^3.2.0",
"eslint-plugin-flow-vars": "^0.3.0",
"eslint-plugin-lodash": "^1.6.8",
"eslint-plugin-lodash": "^1.7.0",
"eslint-plugin-react": "^5.0.1",
"fb-watchman": "^1.9.0",
"ink-docstrap": "^1.1.4",
Expand All @@ -83,7 +83,7 @@
"lodash": "^4.11.1",
"memory-fs": "^0.3.0",
"mkdirp": "^0.5.1",
"node-sass": "^3.4.2",
"node-sass": "^3.5.3",
"node-sass-import-once": "^1.2.0",
"null-loader": "^0.1.1",
"postcss": "^5.0.19",
Expand Down
27 changes: 27 additions & 0 deletions src/typedef.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,30 @@ export type NativeProcessCallback = (stderr: ?string, stdout: string) => void;
* @param {Object} result - the resulting object
*/
export type ObjectOrErrorCallback = (error: ?string, result: Object) => void;

/**
* Describes a file a change in which was caught.
*
* @typedef {Object} WatchmanFile
* @property {string} name - the relative path to a file
* @property {number} size - file size in bytes
* @property {boolean} exists - true if the file exists
* @property {string} type - e.g. "f"
*/
export type WatchmanFile = {name: string, size: number, exists: boolean, type: string};

/**
* A watchman response object.
*
* @typedef {Object} WatchmanResponse
* @property {string} root - the path to the directory being watched
* @property {string} subscription - random subscription name
* @property {Array<WatchmanFile>} files - an array of file descriptions
*/
export type WatchmanResponse = {root: string, subscription: string, files: Array<WatchmanFile>};

/**
* @callback WatchCallback
* @param {WatchmanResponse} response - a watchman response object
*/
export type WatchCallback = (response: WatchmanResponse) => void;
11 changes: 6 additions & 5 deletions src/watch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */

import type {WatchCallback} from './typedef';
import {Client} from 'fb-watchman';

const client = new Client(),
Expand All @@ -12,17 +13,17 @@ const client = new Client(),
* This watcher's only goal is performance, hence the simplicity.
*
* @function watch
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {Function} callback - a callback function
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {WatchCallback} callback - a callback function
* @see {@link https://facebook.github.io/watchman/ Watchman}
* @example
* import {watch} from 'webcompiler';
* import {join} from 'path';
*
* watch(join(__dirname, 'src'), 'js', someFunction);
*/
export function watch(dir: string, type: string, callback: () => void) {
export function watch(dir: string, type: string, callback: WatchCallback) {
const subscription = Date.now().toString(ALPHANUMERIC_BASE);

client.capabilityCheck({}, capabilityErr => {
Expand Down Expand Up @@ -63,7 +64,7 @@ export function watch(dir: string, type: string, callback: () => void) {

client.on('subscription', subscriptionResp => {
if (subscription === subscriptionResp.subscription) {
callback();
callback(subscriptionResp);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/watch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('watch', () => {
});

it('calls callback', () => {
expect(callback).called;
expect(callback).calledWith({subscription: 'qwerty'});
});

});
Expand Down

0 comments on commit 4124ac4

Please sign in to comment.