Skip to content

Commit

Permalink
make bots compile again
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski committed Nov 6, 2018
1 parent 2c0d729 commit a3ebc6c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/apps/make-bot/make-bot
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

../../../node_modules/.bin/cross-env TS_NODE_FILES=1 TS_NODE_CACHE_DIRECTORY=.cache node -r ts-node/register ./make-bot.ts $@
../../../node_modules/.bin/cross-env TS_NODE_FILES=1 TS_NODE_CACHE_DIRECTORY=.cache TS_NODE_SKIP_IGNORE=1 node -r ts-node/register ./make-bot.ts $@
88 changes: 40 additions & 48 deletions src/apps/make-bot/make-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import * as Link from "../../data/Link"
import Store from "../../data/Store"
import StoreBackend from "../../data/StoreBackend"

import CloudClient from "../../modules/discovery-cloud/Client"
import { FrontendHandle } from "../../modules/hypermerge/frontend"
import { Hypermerge } from "../../modules/hypermerge"
import CloudClient from "discovery-cloud/Client"
import { Hypermerge, FrontendManager } from "hypermerge"

import "./Bot" // we have local bot implementation since the Capstone one uses css imports
import * as Board from "../../components/Board"
Expand All @@ -50,16 +49,14 @@ Content.store.sendQueue.subscribe(msg => storeBackend.onMessage(msg))
hm.joinSwarm(
new CloudClient({
url: "wss://discovery-cloud.herokuapp.com",
// url: "ws://localhost:8080",
id: hm.id,
stream: hm.stream,
}),
)

hm.ready.then(hm => {
Content.open(
workspace,
once((workspace: Doc<Workspace.Model>) => {
Content.open<Workspace.Model>(workspace)
.once(workspace => {
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack)
Expand All @@ -72,61 +69,56 @@ hm.ready.then(hm => {

console.log(`Using board: ${boardUrl}`)

const boardHandle = Content.open(
boardUrl,
once((doc: any) => {
const boardHandle = Content.open<Board.Model>(boardUrl as string).once(
doc => {
const botExists = !!doc.cards[botId]

console.log("board doc", doc)

if (botExists) {
console.log(`Updating bot ${botId}`)

const botUrl = doc.cards[botId]!.url

if (!botUrl) return

// update
const botHandle = Content.open(
doc.cards[botId].url,
once((doc: any) => {
console.log("bot doc", doc)
}),
)

botHandle(bot => {
bot.code = code
})
const botHandle = Content.open(botUrl)
.change(bot => {
bot.code = code
})
.close()
} else {
console.log(`creating new bot: ${botId}`)

// create
const botUrl = Content.create("Bot")

const botHandle = Content.open(
botUrl,
once((doc: any) => {
console.log("bot doc", doc)
}),
)

botHandle(doc => {
doc.id = botId
doc.code = code
})

boardHandle((board: Doc<Board.Model>) => {
const card = {
id: botId,
z: 0,
x: 0,
y: 0,
width: 200,
height: 200,
url: botUrl,
}

board.cards[botId] = card
})
const botHandle = Content.open(botUrl)
.change(doc => {
doc.id = botId
doc.code = code
})
.close()

boardHandle
.change(board => {
const card = {
id: botId,
z: 0,
x: 0,
y: 0,
width: 200,
height: 200,
url: botUrl,
}

board.cards[botId] = card
})
.close()
}
}),
},
)
}),
)
})
.close()
})
2 changes: 0 additions & 2 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import "./Workspace"
import "./HTML"
import "./Bot"

import * as Feedback from "./CommandFeedback"
import * as Workspace from "./Workspace"
import GlobalKeyboard from "./GlobalKeyboard"

const log = Debug("component:app")

Expand Down
57 changes: 27 additions & 30 deletions src/components/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Content, {
import Handle from "../data/Handle"
import { once, last } from "lodash"
import { Model as BoardModel } from "./Board"
import { Model as WorkspaceModel } from "./Workspace"
import * as Link from "../data/Link"

export interface Props<T = {}, M = never> {
Expand Down Expand Up @@ -59,40 +60,36 @@ export function create<T, M extends Message = never>(
}

componentDidMount() {
this.handle = Content.open<T>(this.props.url, (doc: any) => {
this.handle = Content.open<T>(this.props.url).once((doc: any) => {
this.setState({ doc }, () => {
if (Link.parse(this.props.url).type === "Bot") return

const workspaceUrl = Content.store && Content.store.getWorkspace()
if (!workspaceUrl) return

// send AnyChange to bot(s) on current board
Content.open(
Content.workspaceUrl,
once(workspace => {
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack)
: workspace.rootUrl

Content.open(
boardUrl,
once((board: BoardModel) => {
Object.values(board.cards).forEach(card => {
if (
!card ||
(card && card.url && card.url.indexOf("Bot") < 0)
)
return

Content.send({
type: "AnyChange",
body: doc,
from: this.props.url,
to: card.url,
})
})
}),
)
}),
)
Content.open<WorkspaceModel>(workspaceUrl).once(workspace => {
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack)
: workspace.rootUrl

if (!boardUrl) return

Content.open<BoardModel>(boardUrl as string).once(board => {
Object.values(board.cards).forEach(card => {
if (!card || (card && card.url && card.url.indexOf("Bot") < 0))
return

Content.send({
type: "AnyChange",
body: doc,
from: this.props.url,
to: card.url,
})
})
})
})
})
})
}
Expand Down
16 changes: 14 additions & 2 deletions src/data/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ export interface Env {
device: "capstone" | "sidecar"
}

declare global {
namespace NodeJS {
interface Global {
navigator: any
}
}
}

export function defaultEnv(): Env {
return {
isTouchscreen: navigator.maxTouchPoints > 0,
device: navigator.userAgent.includes("CrOS") ? "capstone" : "sidecar",
isTouchscreen: global.navigator ? navigator.maxTouchPoints > 0 : false,
device: global.navigator
? navigator.userAgent.includes("CrOS")
? "capstone"
: "sidecar"
: "sidecar",
}
}

Expand Down

0 comments on commit a3ebc6c

Please sign in to comment.