-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use Metro HMR socket to listen for changes (#65)
* refactor: remove bundle delta listeners and code from Atlas * feature: use HMR endpoint to listen for changes * refactor: remove the legact bundle delta toast component * refactor: remove the legacy `registerMetro` to access private apis * refactor: drop console.log references * fix: clean up socket when in connecting state
- Loading branch information
Showing
16 changed files
with
393 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Return correctly formatted URLs from JSC safe URLs. | ||
* - input http://127.0.0.1:8081/index.bundle//&platform=ios&dev=true&minify=false | ||
* - output: http://127.0.0.1:8081/index.bundle?platform=ios&dev=true&minify=false | ||
*/ | ||
export function getUrlFromJscSafeUrl(jscSafeUrl: string) { | ||
return jscSafeUrl.replace('//&', '?'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { getSource } from '~/utils/atlas'; | ||
|
||
export async function GET(_request: Request, params: Record<'bundle', string>) { | ||
try { | ||
const source = getSource(); | ||
if (!source.hasHmrSupport()) { | ||
return Response.json({ error: 'HMR not supported' }, { status: 406 }); | ||
} | ||
|
||
return Response.json(source.getBundleHmr(params.bundle)); | ||
} catch (error: any) { | ||
return Response.json({ error: error.message }, { status: 406 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { getSource } from '~/utils/atlas'; | ||
import { AtlasBundle } from '~core/data/types'; | ||
|
||
export async function POST(_request: Request) { | ||
try { | ||
const source = getSource(); | ||
|
||
// Only reload bundles when HMR is enabled | ||
if (!source.hasHmrSupport()) { | ||
return Response.json({ error: 'HMR not supported' }); | ||
} | ||
|
||
// Fetch all known bundles from Metro to trigger a data update through the `customSerializer` hook | ||
const bundles = await source.listBundles(); | ||
// Trigger a new reload on all bundles | ||
await Promise.all( | ||
bundles.map((bundle) => Promise.resolve(source.getBundle(bundle.id)).then(fetchBundle)) | ||
); | ||
|
||
return Response.json({ success: true, bundles }); | ||
} catch (error: any) { | ||
return Response.json({ error: error.message }, { status: 406 }); | ||
} | ||
} | ||
|
||
function fetchBundle(bundle: AtlasBundle) { | ||
if (!bundle.serializeOptions?.sourceUrl) { | ||
return; // Unknown source URL, can't fetch the bundle | ||
} | ||
|
||
// Convert the source URL to localhost, avoiding "unauthorized requests" in Metro | ||
const sourceUrl = new URL(bundle.serializeOptions.sourceUrl); | ||
sourceUrl.hostname = 'localhost'; | ||
|
||
return fetch(sourceUrl) | ||
.then((response) => (response.ok ? response : null)) | ||
.then((response) => response?.text()); | ||
} |
Oops, something went wrong.