Permalink
Browse files

[feature] file,notification: bind nodejs file notification

  • Loading branch information...
1 parent b40fc5b commit f096095b90791ee59de2116a1d58a003a7c8767c @OpaOnWindowsNow OpaOnWindowsNow committed Sep 6, 2012
Showing with 62 additions and 0 deletions.
  1. +10 −0 lib/plugins/unix/bslFile.ml
  2. +18 −0 lib/plugins/unix/bslFile.nodejs
  3. +34 −0 lib/stdlib/io/file/file.opa
@@ -137,6 +137,16 @@ let content_opt x =
##register content : string -> binary
let content x = buffer_of_string (File.content x)
+(**
+ {1 Todo}
+*)
+##extern-type File.watcher
+
+##register onchange : string, bool, (string, string -> void) -> File.watcher
+let onchange _ _ _ = assert false
+
+##register filewatcher_stop : File.watcher -> void
+let filewatcher_stop _ = assert false
##register [cps-bypass] readdir : string, continuation(tuple_2(string,llarray(string))) -> void
let readdir dir cont =
@@ -157,6 +157,24 @@ function mlstate_dir(_) {
}
+/** @externType File.watcher */
+
+/**
+ * @register {string,bool, (string, string -> void) -> File.watcher}
+ */
+function onchange(filename,persistent,listener)
+{
+ FileSystem.watch(filename,{persitent:persitent},listener)
+}
+
+/**
+ * @register {File.watcher -> void}
+ */
+function filewatcher_stop(watcher)
+{
+ FileSystem.watcher.close()
+}
+
/** @register { string, continuation(tuple_2(string,llarray(string))) -> void }
* @cpsBypass
*/
@@ -26,6 +26,24 @@ import-plugin unix
* {1 Interface}
*/
+/**
+ * Type of file event.
+ * For :
+ * - directory, 'rename' can be a sub-file/directory creation/deletion, 'change' is a sub-file change
+ * - file, 'rename' is file deletion and creation, 'change' is a file modification
+ * The string indicates which files has been modified, changed ...
+ */
+type File.raw_event = {rename:string} / {change:string}
+
+/** Equivalent to File.raw_event */
+type File.event = File.raw_event
+
+/** A file watcher handler */
+type File.watcher = external
+
+/** Options for File.onchange function */
+type File.onchange = {persistent:bool}
+
/**
* A module for very basic file access
*/
@@ -49,9 +67,25 @@ File = {{
then {success=r}
else {failure=err}
+ onchange_default = {persistent=false} : File.onchange
+
+ /** if the path of the file is changed, the behaviour is undefined */
+ onchange(path1,conf_opt:option(File.onchange))(f) =
+ conf = conf_opt ? onchange_default
+ g(event,path2) = f(path1,Raw.conv(event,path2))
+ watcher = Raw.onchange(path1,conf.persistent,g)
+ {stop() = Raw.watcher_stop(watcher)}
@private
Raw = {{
+ conv(event,path2) = match event
+ "rename" -> {rename=path2}
+ "change" -> {change=path2}
+ _ -> do Log.warning("File.onchange","Unknown event {Debug.dump(event)}")
+ conv("change",path2)
+ end
+ onchange = %% BslFile.onchange %% : string, bool, (string,string->void) -> File.watcher
+ watcher_stop = %% BslFile.filewatcher_stop %% : File.watcher -> void
readdir = %% BslFile.readdir %% : string -> (string,llarray(string))
}}

0 comments on commit f096095

Please sign in to comment.