Skip to content

Commit f3f9c60

Browse files
some changes
1 parent b46ca5b commit f3f9c60

File tree

10 files changed

+54
-47
lines changed

10 files changed

+54
-47
lines changed

esbuild.cjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ Promise.all([
2121
// Browser builds:
2222
// CommonJS build
2323
esbuild.build({
24-
entryPoints: ["src/sessions/WebSocketSession/browser.ts"],
24+
entryPoints: ["src/browser.ts"],
2525
bundle: true,
2626
format: "cjs",
2727
// .cjs extension is required because "type": "module" is set in package.json
2828
outfile: "dist/cjs/browser.cjs",
2929
platform: "browser",
3030
// pitcher-common currently requires this, but breaks the first experience
3131
banner: {
32-
js: `if (!window.process) {
32+
js: `if (typeof window !== "undefined" && !window.process) {
3333
window.process = {
3434
env: {},
3535
};
@@ -41,14 +41,14 @@ Promise.all([
4141

4242
// ESM build
4343
esbuild.build({
44-
entryPoints: ["src/sessions/WebSocketSession/browser.ts"],
44+
entryPoints: ["src/browser.ts"],
4545
bundle: true,
4646
format: "esm",
4747
outdir: "dist/esm",
4848
platform: "browser",
4949
// pitcher-common currently requires this, but breaks the first experience
5050
banner: {
51-
js: `if (!window.process) {
51+
js: `if (typeof window !== "undefined" && !window.process) {
5252
window.process = {
5353
env: {},
5454
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"default": "./dist/cjs/index.cjs"
4444
},
4545
"./browser": {
46-
"types": "./dist/esm/sessions/WebSocketSession/browser.d.ts",
46+
"types": "./dist/esm/browser.d.ts",
4747
"import": "./dist/esm/browser.js",
4848
"require": "./dist/cjs/browser.cjs",
4949
"default": "./dist/cjs/browser.cjs"

src/sessions/WebSocketSession/browser.ts renamed to src/browser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { initPitcherClient, protocol } from "@codesandbox/pitcher-client";
2-
import { DEFAULT_SUBSCRIPTIONS, SandboxBrowserSession } from "../../types";
3-
import { WebSocketSession } from ".";
2+
import { DEFAULT_SUBSCRIPTIONS, SandboxBrowserSession } from "./types";
3+
import { WebSocketSession } from "./sessions/WebSocketSession";
44

5-
export * from ".";
5+
export * from "./sessions/WebSocketSession";
6+
7+
export { createPreview } from "./previews";
68

79
/**
810
* With this function you can connect to a sandbox from the browser.

src/sessions/WebSocketSession/previews/Preview.ts renamed to src/previews/Preview.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
InjectMessage,
77
Message,
88
} from "./types";
9-
import { Disposable } from "../../../utils/disposable";
109
import { injectAndInvokeInsidePreview } from "./preview-script";
1110

1211
type PreviewStatus = "DISCONNECTED" | "CONNECTED";
@@ -34,10 +33,7 @@ export class Preview<
3433
onStatusChange = this.onStatusChangeEmitter.event;
3534
iframe: HTMLIFrameElement;
3635

37-
constructor(sessionDisposable: Disposable, src: string) {
38-
sessionDisposable.onWillDispose(() => {
39-
this.dispose();
40-
});
36+
constructor(src: string) {
4137
this.origin = new URL(src).origin;
4238
this.iframe = this.createIframe(src);
4339
this.windowListener = (event: MessageEvent) => {
@@ -65,7 +61,6 @@ export class Preview<
6561

6662
this.onPreviewLoad = () => {
6763
this.status = "CONNECTED";
68-
console.log("INJECT SCRIPT");
6964
this._injectAndInvoke(injectAndInvokeInsidePreview, {});
7065
};
7166

@@ -111,10 +106,21 @@ export class Preview<
111106
}
112107
}
113108

114-
injectAndInvoke<Scope extends Record<string, unknown>>(
109+
async injectAndInvoke<Scope extends Record<string, unknown>>(
115110
func: InjectFunction<MessageToPreview, MessageFromPreview, Scope>,
116111
scope: Scope
117112
) {
113+
if (this.status !== "CONNECTED") {
114+
return new Promise(() => {
115+
const listener = this.onStatusChange((status) => {
116+
if (status === "CONNECTED") {
117+
this._injectAndInvoke(func, scope);
118+
listener.dispose();
119+
}
120+
});
121+
});
122+
}
123+
118124
this._injectAndInvoke(func, scope);
119125
}
120126

src/previews/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Preview } from "./Preview";
2+
import { InjectFunction } from "./types";
3+
4+
export { Preview, InjectFunction };
5+
6+
export function createPreview(src: string) {
7+
return new Preview(src);
8+
}
File renamed without changes.
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import type { Id, IPitcherClient } from "@codesandbox/pitcher-client";
2-
import { listenOnce } from "@codesandbox/pitcher-common/dist/event";
32

4-
import { Disposable } from "../../utils/disposable";
5-
import { Emitter } from "../../utils/event";
6-
7-
export class Git extends Disposable {
8-
constructor(private pitcherClient: IPitcherClient) {
9-
super();
3+
export class Git {
4+
onStatusChange = this.pitcherClient.clients.git.onStatusUpdated;
5+
constructor(private pitcherClient: IPitcherClient) {}
6+
pull(force?: boolean) {
7+
return this.pitcherClient.clients.git.pull(undefined, force);
8+
}
9+
commit(message: string, paths?: string[]) {
10+
return this.pitcherClient.clients.git.commit(message, paths);
11+
}
12+
push() {
13+
return this.pitcherClient.clients.git.push();
14+
}
15+
status() {
16+
return this.pitcherClient.clients.git.getStatus();
17+
}
18+
discard(path?: string[]) {
19+
return this.pitcherClient.clients.git.discard(path);
20+
}
21+
resetToRemote() {
22+
return this.pitcherClient.clients.git.resetLocalWithRemote();
1023
}
11-
pull() {
12-
return this.pitcherClient.clients.git.pull();
24+
// TODO: Expose git checkout
25+
checkout(branch: string) {
26+
return this.pitcherClient.clients.task.runCommand(`git checkout ${branch}`);
1327
}
1428
}

src/sessions/WebSocketSession/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { Shells } from "./shells";
1212
import { Tasks } from "./tasks";
1313
import { DEFAULT_SUBSCRIPTIONS, SandboxSession } from "../../types";
1414
import { Client } from "@hey-api/client-fetch";
15-
import { Previews } from "./previews";
1615
import { Interpreters } from "./interpreters";
1716
import { Terminals } from "./terminals";
1817
import { Commands } from "./commands";
@@ -22,7 +21,6 @@ export * from "./ports";
2221
export * from "./setup";
2322
export * from "./shells";
2423
export * from "./tasks";
25-
export * from "./previews";
2624
export * from "./terminals";
2725
export * from "./commands";
2826
export * from "./interpreters";
@@ -82,11 +80,6 @@ export class WebSocketSession {
8280
*/
8381
public readonly fs = new FileSystem(this.disposable, this.pitcherClient);
8482

85-
/**
86-
* Namespace for previews
87-
*/
88-
public readonly previews = new Previews(this.disposable);
89-
9083
/**
9184
* Namespace for running shell commands on this sandbox.
9285
*/

src/sessions/WebSocketSession/previews/index.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)