| 87 | }; |
| 88 | |
| 89 | function reloadClients(changedPath,event) { |
| 90 | var customOnChange = options.onChange || {}, |
| 91 | delayTime = customOnChange.delayTime || 100, |
| 92 | guardTime = customOnChange.guardTime || 1000; |
| 93 | |
| 94 | function onChangeFiltered(path, event,action) { |
| 95 | ss.log.info('✎'.green, consoleMessage[action].grey); |
| 96 | var pubs ='__ss:' + action; |
| 97 | if (customOnChange.publish) { |
| 98 | pubs = options.onChange.publish(path, event,action,pubs); |
| 99 | } |
| 100 | if (pubs) { |
| 101 | ss.publish.all(pubs); |
| 102 | } |
| 103 | |
| 104 | lastRun[action].at = Date.now(); |
| 105 | } |
| 106 | |
| 107 | // reload the browser |
| 108 | function onChange(changedPath, event) { |
| 109 | var _ref = path.extname(changedPath), |
| 110 | action = cssExtensions.indexOf(_ref) >= 0 ? 'updateCSS' : 'reload'; |
| 111 | //first change is with delayTime delay , thereafter only once there has been no further changes for guardTime seconds |
| 112 | |
| 113 | //validate the change |
| 114 | if (customOnChange.validate) { |
| 115 | if (!customOnChange.validate(changedPath, event,action)) { return ;} //ignore changes if the app says-so |
| 116 | } |
| 117 | |
| 118 | //avoid multiple rapid changes |
| 119 | var delay=delayTime; |
| 120 | if (lastRun[action].guardTime) { clearTimeout(lastRun[action].guardTime); delay=guardTime;} |
| 121 | if (lastRun[action].delayTime) { clearTimeout(lastRun[action].delayTime); delay=delayTime;} |
| 122 | lastRun[action].delayTime = setTimeout(function(){ |
| 123 | onChangeFiltered(changedPath, event, action); |
| 124 | lastRun[action].guardTime = setTimeout(function(){ |
| 125 | lastRun[action].guardTime=null; |
| 126 | }, delay); |
| 127 | lastRun[action].delayTime=null; |
| 128 | }, delay); |
| 129 | |
| 130 | return Date.now(); |
| 131 | } |
| 132 | |
| 133 | return onChange(changedPath, event); |
| 134 | } |
| 135 | |
| 136 | // Return API |
| 137 | return { |