@unvt/charites 2.1.3 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -120,35 +120,38 @@ export async function serve(source, options) {
120
120
  }
121
121
  });
122
122
  const wss = new WebSocketServer({ server });
123
- wss.on('connection', (ws) => {
124
- const watcher = watch(path.dirname(sourcePath), { recursive: true, filter: /\.yml$|\.svg$/i }, (event, file) => {
125
- console.log(`${(event || '').toUpperCase()}: ${file}`);
126
- try {
127
- if (file?.toLowerCase().endsWith('.yml')) {
128
- ws.send(JSON.stringify({
129
- event: 'styleUpdate',
130
- }));
131
- }
132
- else if (file?.toLowerCase().endsWith('.svg') &&
133
- typeof spriteRefresher !== 'undefined') {
134
- spriteRefresher().then(() => {
135
- ws.send(JSON.stringify({
136
- event: 'spriteUpdate',
137
- }));
138
- });
123
+ const sockets = new Set();
124
+ const watcher = watch(path.dirname(sourcePath), { recursive: true, filter: /\.yml$|\.svg$/i }, (event, file) => {
125
+ console.log(`${(event || '').toUpperCase()}: ${file}`);
126
+ try {
127
+ if (file?.toLowerCase().endsWith('.yml')) {
128
+ for (const client of sockets) {
129
+ client.send(JSON.stringify({ event: 'styleUpdate' }));
139
130
  }
140
131
  }
141
- catch (_) {
142
- // Nothing to do
132
+ else if (file?.toLowerCase().endsWith('.svg') &&
133
+ typeof spriteRefresher !== 'undefined') {
134
+ spriteRefresher().then(() => {
135
+ for (const client of sockets) {
136
+ client.send(JSON.stringify({ event: 'spriteUpdate' }));
137
+ }
138
+ });
143
139
  }
144
- });
145
- wss.on('close', () => {
146
- watcher.close();
140
+ }
141
+ catch (_) {
142
+ // Nothing to do
143
+ }
144
+ });
145
+ wss.on('connection', (ws) => {
146
+ sockets.add(ws);
147
+ ws.on('close', () => {
148
+ sockets.delete(ws);
147
149
  });
148
150
  });
149
151
  process.on('SIGINT', () => {
150
152
  console.log('Cleaning up...');
151
153
  server.close();
154
+ watcher.close();
152
155
  if (typeof spriteOut !== 'undefined') {
153
156
  fs.rmSync(spriteOut, { recursive: true });
154
157
  spriteOut = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unvt/charites",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "charites": "dist/cli.js"
@@ -5,6 +5,7 @@ import http from 'http'
5
5
  import open from 'open'
6
6
  // tweak to fix WebSocketServer is not a constructor
7
7
  const ws = await import('ws')
8
+ import type { WebSocket as WSWebSocket } from 'ws'
8
9
  const WebSocketServer = ws.default.WebSocketServer || ws.WebSocketServer
9
10
  import watch from 'node-watch'
10
11
 
@@ -145,45 +146,45 @@ export async function serve(source: string, options: serveOptions) {
145
146
  })
146
147
 
147
148
  const wss = new WebSocketServer({ server })
149
+ const sockets = new Set<WSWebSocket>()
148
150
 
149
- wss.on('connection', (ws: WebSocket) => {
150
- const watcher = watch(
151
- path.dirname(sourcePath),
152
- { recursive: true, filter: /\.yml$|\.svg$/i },
153
- (event: string, file: string) => {
154
- console.log(`${(event || '').toUpperCase()}: ${file}`)
155
- try {
156
- if (file?.toLowerCase().endsWith('.yml')) {
157
- ws.send(
158
- JSON.stringify({
159
- event: 'styleUpdate',
160
- }),
161
- )
162
- } else if (
163
- file?.toLowerCase().endsWith('.svg') &&
164
- typeof spriteRefresher !== 'undefined'
165
- ) {
166
- spriteRefresher().then(() => {
167
- ws.send(
168
- JSON.stringify({
169
- event: 'spriteUpdate',
170
- }),
171
- )
172
- })
151
+ const watcher = watch(
152
+ path.dirname(sourcePath),
153
+ { recursive: true, filter: /\.yml$|\.svg$/i },
154
+ (event: string, file: string) => {
155
+ console.log(`${(event || '').toUpperCase()}: ${file}`)
156
+ try {
157
+ if (file?.toLowerCase().endsWith('.yml')) {
158
+ for (const client of sockets) {
159
+ client.send(JSON.stringify({ event: 'styleUpdate' }))
173
160
  }
174
- } catch (_) {
175
- // Nothing to do
161
+ } else if (
162
+ file?.toLowerCase().endsWith('.svg') &&
163
+ typeof spriteRefresher !== 'undefined'
164
+ ) {
165
+ spriteRefresher().then(() => {
166
+ for (const client of sockets) {
167
+ client.send(JSON.stringify({ event: 'spriteUpdate' }))
168
+ }
169
+ })
176
170
  }
177
- },
178
- )
179
- wss.on('close', () => {
180
- watcher.close()
171
+ } catch (_) {
172
+ // Nothing to do
173
+ }
174
+ },
175
+ )
176
+
177
+ wss.on('connection', (ws: WSWebSocket) => {
178
+ sockets.add(ws)
179
+ ws.on('close', () => {
180
+ sockets.delete(ws)
181
181
  })
182
182
  })
183
183
 
184
184
  process.on('SIGINT', () => {
185
185
  console.log('Cleaning up...')
186
186
  server.close()
187
+ watcher.close()
187
188
  if (typeof spriteOut !== 'undefined') {
188
189
  fs.rmSync(spriteOut, { recursive: true })
189
190
  spriteOut = undefined