Skip to content

Commit d98bbd7

Browse files
committed
Implement ssg.stopWatcher() in cms
1 parent 1a5c9ca commit d98bbd7

File tree

8 files changed

+70
-12
lines changed

8 files changed

+70
-12
lines changed

src/cms/api/models/ssg.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,27 @@ const createSSGModel = (state) => {
1414
},
1515

1616
async watch({ rootDirectory, refreshTheme, debug, cli }) {
17-
return state.setState(
18-
await ssg.watch({
19-
rootDirectory,
20-
refreshTheme,
21-
debug,
22-
cli,
23-
onChange: state.setState
24-
})
25-
)
17+
if (state.isWatching()) {
18+
return false
19+
}
20+
const { result, watcher } = await ssg.watch({
21+
rootDirectory,
22+
refreshTheme,
23+
debug,
24+
cli,
25+
onChange: state.setState
26+
})
27+
state.startWatcher(watcher.stop)
28+
state.setState(result)
29+
return true
30+
},
31+
32+
async stopWatcher() {
33+
if (!state.isWatching()) {
34+
return false
35+
}
36+
await state.stopWatcher()
37+
return true
2638
}
2739
}
2840
}

src/cms/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,27 @@ const createCMS = (initialState = {}) => {
77
ssgOptions: {},
88
settings: {},
99
fileSystemTree: [],
10-
contentModel: {}
10+
contentModel: {},
11+
watcher: {
12+
isRunning: false,
13+
stop: _=>_
14+
}
1115
}, initialState)
1216

1317
return {
1418
getSettings: () => state.settings,
1519
getFileSystemTree: () => state.fileSystemTree,
1620
getContentModel: () => state.contentModel,
1721
getSSGOptions: () => state.ssgOptions,
22+
isWatching: () => state.watcher.isRunning,
23+
stopWatcher: () => {
24+
state.watcher.stop()
25+
state.watcher.isRunning = false
26+
},
27+
startWatcher: (stopFn) => {
28+
state.watcher.isRunning = true
29+
state.watcher.stop = stopFn
30+
},
1831

1932
setState: (newState) => {
2033
Object.assign(state, newState)

src/cms/server/public/api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ const api = {
3030
body: JSON.stringify(options)
3131
})
3232
return response.text()
33+
},
34+
35+
stopWatcher: async () => {
36+
const response = await fetch('/api/ssg/stop-watcher', {
37+
method: 'post'
38+
})
39+
return response.text()
3340
}
3441

3542
},

src/cms/server/public/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ssgBuild from './app/ssgBuild.js'
22
import ssgWatch from './app/ssgWatch.js'
3+
import ssgStopWatcher from './app/ssgStopWatcher.js'
34
import getSSGOptions from './app/getSSGOptions.js'
45
import getSettings from './app/getSettings.js'
56
import updateSettings from './app/updateSettings.js'
@@ -31,6 +32,10 @@ const makeButtonsWork = () => {
3132
await ssgWatch()
3233
setIframeSrc()
3334
})
35+
query('#ssg-stop-watcher-btn').addEventListener('click', async () => {
36+
await ssgStopWatcher()
37+
setIframeSrc()
38+
})
3439
query('#get-ssg-options-btn').addEventListener('click', getSSGOptions)
3540
query('#get-settings-btn').addEventListener('click', getSettings)
3641
query('#update-settings-btn').addEventListener('click', updateSettings)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import api from '../api.js'
2+
3+
const ssgStopWatcher = async () => {
4+
return api.ssg.stopWatcher()
5+
}
6+
7+
export default ssgStopWatcher

src/cms/server/public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<p>ssg</p>
8484
<button type="button" id="ssg-build-btn">ssg.build()</button>
8585
<button type="button" id="ssg-watch-btn">ssg.watch()</button>
86+
<button type="button" id="ssg-stop-watcher-btn">ssg.stopWatcher()</button>
8687
<button type="button" id="get-ssg-options-btn">get ssg options</button>
8788

8889
<p>settings</p>

src/cms/server/router/api/ssg.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ module.exports = express.Router()
2929
res.status(500).send(e)
3030
}
3131
})
32+
.post('/stop-watcher', async (req, res, next) => {
33+
try {
34+
await req.api.ssg.stopWatcher()
35+
res.sendStatus(200)
36+
} catch (e) {
37+
console.log('Error running ssg.stopWatcher', e)
38+
res.status(500).send(e)
39+
}
40+
})

src/ssg/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,13 @@ const watch = async ({ rootDirectory, refreshTheme, debug, cli, onChange = _=>_
103103
}
104104
}
105105

106-
Watcher.init(watcherOptions)
106+
const watcher = await Watcher.init(watcherOptions)
107+
const result = await build(buildOptions)
107108

108-
return build(buildOptions)
109+
return {
110+
result,
111+
watcher
112+
}
109113
}
110114

111115
module.exports = {

0 commit comments

Comments
 (0)