|
1 | 1 | import { getAbsoluteDependencies } from '@codesandbox/common/lib/utils/dependencies'; |
2 | | -import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator'; |
3 | | - |
4 | 2 | import { getGlobal } from '@codesandbox/common/lib/utils/global'; |
5 | | -import { Module } from '@codesandbox/common/lib/types'; |
| 3 | +import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator'; |
| 4 | +import { json } from 'overmind'; |
6 | 5 |
|
7 | 6 | const global = getGlobal() as Window & { BrowserFS: any }; |
8 | 7 |
|
9 | 8 | const fs = global.BrowserFS.BFSRequire('fs'); |
10 | 9 | const SERVICE_URL = 'https://ata-fetcher.cloud/api/v5/typings'; |
11 | 10 |
|
12 | | -let fileInterval; |
13 | 11 | let lastMTime = new Date(0); |
14 | 12 |
|
15 | 13 | function sendTypes() { |
@@ -101,81 +99,61 @@ async function syncDependencyTypings( |
101 | 99 | } |
102 | 100 | } |
103 | 101 |
|
104 | | -let getCurrentSandboxId; |
105 | | -let getModulesByPath; |
106 | | - |
107 | | -export default { |
108 | | - initialize(options: { |
109 | | - getCurrentSandboxId: () => string; |
110 | | - getModulesByPath: () => { |
111 | | - [path: string]: Module; |
112 | | - }; |
113 | | - }) { |
114 | | - getCurrentSandboxId = options.getCurrentSandboxId; // eslint-disable-line prefer-destructuring |
115 | | - getModulesByPath = options.getModulesByPath; // eslint-disable-line prefer-destructuring |
116 | | - }, |
117 | | - syncCurrentSandbox() { |
118 | | - if (fileInterval) { |
119 | | - clearInterval(fileInterval); |
120 | | - } |
| 102 | +function sendFiles(modulesByPath) { |
| 103 | + global.postMessage( |
| 104 | + { |
| 105 | + $broadcast: true, |
| 106 | + $type: 'file-sync', |
| 107 | + $data: json(modulesByPath), |
| 108 | + }, |
| 109 | + protocolAndHost() |
| 110 | + ); |
121 | 111 |
|
122 | | - const sendFiles = () => { |
123 | | - if (getCurrentSandboxId()) { |
124 | | - const modulesByPath = getModulesByPath(); |
125 | | - |
126 | | - global.postMessage( |
127 | | - { |
128 | | - $broadcast: true, |
129 | | - $type: 'file-sync', |
130 | | - $data: modulesByPath, |
131 | | - }, |
132 | | - protocolAndHost() |
133 | | - ); |
| 112 | + try { |
| 113 | + fs.stat('/sandbox/package.json', (packageJsonError, stat) => { |
| 114 | + if (packageJsonError) { |
| 115 | + return; |
134 | 116 | } |
135 | | - }; |
136 | | - |
137 | | - fileInterval = setInterval(() => { |
138 | | - sendFiles(); |
139 | 117 |
|
140 | | - try { |
141 | | - fs.stat('/sandbox/package.json', (packageJsonError, stat) => { |
142 | | - if (packageJsonError) { |
143 | | - return; |
| 118 | + if (stat.mtime.toString() !== lastMTime.toString()) { |
| 119 | + lastMTime = stat.mtime; |
| 120 | + |
| 121 | + fs.readFile( |
| 122 | + '/sandbox/package.json', |
| 123 | + async (packageJsonReadError, rv) => { |
| 124 | + if (packageJsonReadError) { |
| 125 | + console.error(packageJsonReadError); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + fs.stat('/sandbox/tsconfig.json', (tsConfigError, result) => { |
| 130 | + // If tsconfig exists we want to sync the types |
| 131 | + syncDependencyTypings( |
| 132 | + rv.toString(), |
| 133 | + Boolean(tsConfigError) || !result |
| 134 | + ); |
| 135 | + }); |
144 | 136 | } |
145 | | - |
146 | | - if (stat.mtime.toString() !== lastMTime.toString()) { |
147 | | - lastMTime = stat.mtime; |
148 | | - |
149 | | - fs.readFile( |
150 | | - '/sandbox/package.json', |
151 | | - async (packageJsonReadError, rv) => { |
152 | | - if (packageJsonReadError) { |
153 | | - console.error(packageJsonReadError); |
154 | | - return; |
155 | | - } |
156 | | - |
157 | | - fs.stat('/sandbox/tsconfig.json', (tsConfigError, result) => { |
158 | | - // If tsconfig exists we want to sync the types |
159 | | - syncDependencyTypings( |
160 | | - rv.toString(), |
161 | | - Boolean(tsConfigError) || !result |
162 | | - ); |
163 | | - }); |
164 | | - } |
165 | | - ); |
166 | | - } |
167 | | - }); |
168 | | - } catch (e) { |
169 | | - // Do nothing |
| 137 | + ); |
170 | 138 | } |
171 | | - }, 1000); |
| 139 | + }); |
| 140 | + } catch (e) { |
| 141 | + // Do nothing |
| 142 | + } |
| 143 | +} |
172 | 144 |
|
173 | | - // eslint-disable-next-line |
| 145 | +export default { |
| 146 | + initialize(options: { |
| 147 | + onModulesByPathChange: (modulesByPath: any) => void; |
| 148 | + getModulesByPath: () => any; |
| 149 | + }) { |
174 | 150 | self.addEventListener('message', evt => { |
175 | 151 | if (evt.data.$type === 'request-data') { |
176 | 152 | sendTypes(); |
177 | | - sendFiles(); |
| 153 | + sendFiles(options.getModulesByPath()); |
178 | 154 | } |
179 | 155 | }); |
| 156 | + |
| 157 | + options.onModulesByPathChange(sendFiles); |
180 | 158 | }, |
181 | 159 | }; |
0 commit comments