-
Notifications
You must be signed in to change notification settings - Fork 11
/
LiveReloadSample.hx
61 lines (48 loc) · 2.47 KB
/
LiveReloadSample.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package sample;
import haxe.Timer;
import interpret.Env;
import interpret.DynamicModule;
import interpret.LiveReload;
import sample.interpretable.WatchedClass;
/** A sample that instanciate a native class and automatically watch the source file.
When the source file changes, the interpretable method (marked with @interpret) is updated on the fly
without restarting the app. */
class LiveReloadSample {
public static function main() {
#if js
try {
untyped require('source-map-support').install();
} catch (e:Dynamic) {}
#end
// Assign a callback that will expose the modules we want to
// interpretable code
Env.configureInterpretableEnv = function(env) {
// We expose this class because it need to be accessible
// from its interpretable methods when they are reloaded as script
env.addModule('sample.interpretable.WatchedClass', DynamicModule.fromStatic(sample.interpretable.WatchedClass));
// sample.native.NativeClass can be imported and used from interpretable code because we exposed it
env.addModule('sample.native.NativeClass', DynamicModule.fromStatic(sample.native.NativeClass));
// StringTools can be used with `using` in dynamic classes because we exposed it as well
env.addModule('StringTools', DynamicModule.fromStatic(StringTools));
};
// Optional: provide a custom file watcher
// that will be used to watch interpreted files
//LiveReload.defaultWatcher = new MyCustomWatcher();
// Start live reload
LiveReload.start();
// Regularly call LiveReload.tick as it is necessary
// to make it check files regularly
var interval = 0.5; // Time in seconds between each tick
var timer = new Timer(Math.round(interval * 1000));
timer.run = function() LiveReload.tick(interval); // We call LiveReload.tick() with the elapsed time delta as
// Create a (native) instance of WatchedClass
var watchedClassInstance = new WatchedClass();
// Call printSomething() once every second
var timer2 = new Timer(1000);
timer2.run = watchedClassInstance.printSomething;
// Log some info
Sys.println('interpret is watching changes at path: sample/interpretable/WatchedClass.hx');
Sys.println('Try to change its content to see it get updated');
Sys.println('press CTRL+C to exit program.');
} //main
} //LiveReloadSample