跳到主要內容

[分享] watchFile 陷阱與進階討探 (Node.js), watchFile tricky way on Node.js

watchFile 陷阱與進階討探


這兩天因為之前寫得一個 simple-livereload 無法支援 Windows, Mac ,開始進行小改版,也稍微對 file systemPath 做些小小測試,其中發現了一個關於 watchfile 小小的問題。

問題探討


先來看一下程式碼
var fs = require('fs');
var fullpath = '/home/clonn/test.html';

fs.watchFile(fullpath, function (curr, prev) {
    console.log('file update')
});

接著進行檔案執行之後,會發現出現一個問題,當我修改 test.html 的時候,照理來說應該只有出現一次 file update ,可是卻出現 兩次 ,這實在有點不合乎邏輯。
拜了一下 Google 大神之後,終於找到原因,因為 file system 的 watchFile 實做,當關注的檔案片段(data chunk) 被更動的時候,就會觸發(trriger),接著檔案修改完成,又會重新觸發一次事件,所以導致當一個檔案修改,會被觸發兩次。

處理方法


在 filewatch 的 listen 事件當中會提供 curr, prev,之前和目前的物件(Object) ,接著使用此事件進行時間比對,所以剛才的檔案我們修改如下,
var fs = require('fs');
var fullpath = '/home/clonn/test.html';

fs.watchFile(fullpath, function (curr, prev) {
    if (curr.mtime.getTime() !== prev.mtime.getTime())
        console.log('file update');
});
如此就可以得到正確結果,檔案修改,就只會進行一次觸發。

進階處理


其實 fs.watch, fs.watchFile 會回傳 listener 物件,如果要使用可以將程式碼修改如下,
var fs = require('fs');
var fullpath = '/home/clonn/test.html';
var listen = fs.watchFile(fullpath, function (curr, prev) {});
listen.on('change', , function (curr, prev) {
    if (curr.mtime.getTime() !== prev.mtime.getTime())
        console.log('file update');
});

這樣的好處是在于,可以對於此物件進行處理,同時使用事件包裝等方式,讓 listen 可以重複被使用。
剛才的程式如果稍嫌太長,可以在 callback 裡面稍微修改一下
if (+curr.mtime !== +prev.mtime)    
    console.log('file update');

會得到相同效果,主要是 JavaScript 的 Date type 轉型處理,當進行運算時,會自動轉為 Number,前面增加一個 + 就會進行加號運算。

結語


Node.js v0.8.0 在這次對於 file system 的效能加強許多,同時檔案的監控,尋找都可以支援跨平台(windows, Mac, linux),用來開發一些跨平台管理工具,是個不錯的選擇。

Node.js Taiwan

如果對於此篇內容有任何問題,歡迎透過 Node.js 台灣社群進行討論

參考資料

程式碼參考

留言

這個網誌中的熱門文章

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。 可是在IE7 卻發生了這樣的情況。 Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。

[教學] 快快樂樂刪除CodeIgniter index.php

預設的CI網址預設都設定為index.php同一層級,因此所有的程式都必須指定index.php導向才能開始,例如 http://localhost/ci/index.php/welcome/test http://localhost/ci/welcome/test 本文將說明如何將惱人的index.php消除,還你一個漂亮的URL。 設定開始: 接下來說明如何使用rewrite方式將惱人的index.php去除。 rewrite不清楚的人,煩請先自行google 首先要先確定Apache的 mod_rewrite 有 開啟 ,如果沒有開啟請設定好之後重新啟動apache。 接著,在根目錄底下建立一個新檔案,檔名為 .htaccess ,裡面程式碼如下: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 接著到 application/config/config.php ,開啟檔案修改 $config['index_page'] = ""; 注意: /index.php/$1 要根據你目錄,例如 http://localhost/index.php ,網站根目錄為 /ci/index.php 則要寫成 /ci/index.php/$1 接著至CI目錄下,尋找 config\config.php , 修改一下裡面的檔案,修改如下: $config['index_page'] = ""; 存檔後,如此一來大功告成。 參考資料 官方網站說明

[教學] Mojito 安裝與入門,install mojito for beginner.

mojito ,最近終於從 YDN 對外公開此專案,這個套件主要用於解決前端多重裝置及瀏覽器端的問題,後端服務採取 node.js ,因此使用上必定要先準備以下幾個元素 準備素材 c++ complier git Node.js > 0.4.x NPM > 1.0.x 安裝方式 git clone git://github.com/yahoo/mojito.git cd mojito/source sudo npm install -g . npm install . 以上四個簡單的步驟,就可以把 mojito module 完整安裝到服務器上,接著就可以開始進入 mojito 的世界 使用方式 mojito 提供了完整的 command line 給開發者使用,接著先建立一個基本 hello world 專案,跟著以下步驟完成第一個專案。首先建立一個 mojito application, mojito create app hello cd hello 切換到目錄之後,再接著建立自己的 mojit,這邊的 mojit 就像是一個應用(application)可能會包含許多個獨立網站體,擁有獨立架構的 MVC ,包含內部設定等,詳細資料可以參考官方的 說明 ,建立 mojit mojito create mojit HelloMojit 輸入指令後,會看到顯示結果如下, creating mojit called 'HelloMojit' (using "default" archetype) ✔ mojit: HelloMojit created! ✔ mojito done. 接著修改 application.json 這個 mojit 設定檔案,讓剛才新建立的 hellp application 指定底下有一個 mojit -> HelloMojit ,讓應用可以去執行 mojit controller ,修改如下, [ { "settings": [ "master" ], "appPort": 8666, "specs": { ...