Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee78057

Browse files
authored
Wait for sending console messages until codesandbox api is intitialized (codesandbox#3350)
* Wait for sending console messages until codesandbox api is intitialized * Bump codesandbox-api * Fix typing issue * Move console logic to startup * Remove add
1 parent 9149f1c commit ee78057

File tree

13 files changed

+34
-21
lines changed

13 files changed

+34
-21
lines changed

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"browser-resolve": "CompuIves/node-browser-resolve",
102102
"circular-json": "^0.4.0",
103103
"codemirror": "^5.27.4",
104-
"codesandbox-api": "0.0.23",
104+
"codesandbox-api": "0.0.24",
105105
"codesandbox-import-utils": "^2.1.11",
106106
"color": "^0.11.4",
107107
"compare-versions": "^3.1.0",

packages/app/src/sandbox/index.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import requirePolyfills from '@codesandbox/common/lib/load-dynamic-polyfills';
88
import { getModulePath } from '@codesandbox/common/lib/sandbox/modules';
99
import { generateFileFromSandbox } from '@codesandbox/common/lib/templates/configuration/package-json';
1010
import { getSandboxId } from '@codesandbox/common/lib/utils/url-generator';
11-
import setupConsole from 'sandbox-hooks/console';
12-
import setupHistoryListeners from 'sandbox-hooks/url-listeners';
13-
import {
14-
listenForPreviewSecret,
15-
getPreviewSecret,
16-
} from 'sandbox-hooks/preview-secret';
11+
import { getPreviewSecret } from 'sandbox-hooks/preview-secret';
1712
import { show404 } from 'sandbox-hooks/not-found-screen';
1813

1914
import compile, { getCurrentManager } from './compile';
@@ -68,10 +63,6 @@ requirePolyfills().then(() => {
6863
sendReady();
6964

7065
if (!window.opener) {
71-
// Means we're in the editor
72-
setupHistoryListeners();
73-
setupConsole();
74-
listenForPreviewSecret();
7566
window.addEventListener('message', ({ data }) => {
7667
switch (data.type) {
7768
case 'activate':

packages/app/src/sandbox/startup.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
/* eslint-disable import/default */
22
import BabelWorker from 'worker-loader?publicPath=/&name=babel-transpiler.[hash:8].worker.js!./eval/transpilers/babel/worker/index';
33
/* eslint-enable import/default */
4+
import hookConsole from 'sandbox-hooks/console';
5+
import setupHistoryListeners from 'sandbox-hooks/url-listeners';
6+
import { listenForPreviewSecret } from 'sandbox-hooks/preview-secret';
47

58
window.babelworkers = [];
69
for (let i = 0; i < 3; i++) {
710
window.babelworkers.push(new BabelWorker());
811
}
12+
13+
if (!window.opener) {
14+
// Means we're in the editor
15+
setupHistoryListeners();
16+
hookConsole();
17+
listenForPreviewSecret();
18+
}

packages/codesandbox-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codesandbox-api",
3-
"version": "0.0.23",
3+
"version": "0.0.24",
44
"description": "",
55
"keywords": [],
66
"repository": {

packages/codesandbox-api/src/dispatcher/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ function checkIsStandalone() {
1818
// Whether the tab has a connection with the editor
1919
export const isStandalone = checkIsStandalone();
2020

21+
let initializeResolved: () => void;
22+
/**
23+
* Resolves when the handshake between the frame and the editor has succeeded
24+
*/
25+
export const intializedPromise = new Promise(resolve => {
26+
initializeResolved = resolve;
27+
});
28+
2129
// Field used by a "child" frame to determine its parent origin
2230
let parentOrigin: string | null = null;
2331

2432
const parentOriginListener = (e: MessageEvent) => {
2533
if (e.data.type === 'register-frame') {
2634
parentOrigin = e.data.origin;
2735

36+
if (initializeResolved) {
37+
initializeResolved();
38+
}
2839
self.removeEventListener('message', parentOriginListener);
2940
}
3041
};

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@tippy.js/react": "^3.1.1",
5353
"babel-plugin-preval": "^3.0.1",
5454
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
55-
"codesandbox-api": "0.0.23",
55+
"codesandbox-api": "0.0.24",
5656
"color": "0.11.4",
5757
"date-fns": "^2.0.0",
5858
"dot-object": "1.9.0",

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@reach/auto-id": "^0.7.1",
3434
"@reach/visually-hidden": "^0.7.0",
3535
"@styled-system/css": "^5.1.4",
36-
"codesandbox-api": "0.0.23",
36+
"codesandbox-api": "0.0.24",
3737
"color": "3.1.2",
3838
"date-fns": "^2.8.1",
3939
"react-router-dom": "^5.1.2",

packages/executors/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"dependencies": {
2222
"@codesandbox/common": "^1.0.8",
23-
"codesandbox-api": "0.0.23",
23+
"codesandbox-api": "0.0.24",
2424
"debug": "^4.1.1",
2525
"socket.io-client": "^2.2.0"
2626
},

packages/sandbox-hooks/console/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { dispatch, listen } from 'codesandbox-api';
1+
import { dispatch, listen, intializedPromise } from 'codesandbox-api';
22
import Hook from 'console-feed/lib/Hook';
33
import { Encode } from 'console-feed/lib/Transform';
44

55
export default function setupConsole() {
6-
Hook(window.console, log => {
6+
Hook(window.console, async log => {
7+
await intializedPromise;
78
dispatch({
89
type: 'console',
910
log,

packages/sandbox-hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"dependencies": {
1414
"@codesandbox/common": "^1.0.8",
15-
"codesandbox-api": "0.0.23",
15+
"codesandbox-api": "0.0.24",
1616
"console-feed": "^2.8.5",
1717
"react-dev-utils": "3.1.1"
1818
},

packages/sse-hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"build:dev": "rollup -c rollup.config.js"
1414
},
1515
"dependencies": {
16-
"codesandbox-api": "0.0.23"
16+
"codesandbox-api": "0.0.24"
1717
},
1818
"devDependencies": {
1919
"@babel/core": "^7.5.5",

standalone-packages/react-sandpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"@types/shelljs": "^0.8.0",
7777
"classnames": "^2.2.5",
7878
"codemirror": "^5.35.0",
79-
"codesandbox-api": "0.0.23",
79+
"codesandbox-api": "0.0.24",
8080
"codesandbox-import-utils": "^1.2.3",
8181
"react-broadcast": "^0.6.2",
8282
"react-codemirror2": "^4.0.1",

standalone-packages/sandpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$"
6666
},
6767
"dependencies": {
68-
"codesandbox-api": "0.0.23",
68+
"codesandbox-api": "0.0.24",
6969
"codesandbox-import-utils": "^1.2.3",
7070
"lodash.isequal": "^4.5.0"
7171
},

0 commit comments

Comments
 (0)