Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] end-user-programming "REPL" exploration #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"webpack-cli": "^3.1.0"
},
"dependencies": {
"@types/node-localstorage": "^1.3.0",
"automerge": "^0.9.1",
"browser-process-hrtime": "^0.1.2",
"bs58": "^4.0.1",
Expand All @@ -68,8 +69,10 @@
"hypercore-archiver": "^4.6.0",
"hyperdiscovery": "^8.0.0",
"js-crc": "^0.2.0",
"json-fn": "^1.1.1",
"lodash": "^4.17.10",
"micromatch": "^3.1.10",
"node-localstorage": "^1.3.1",
"random-access-chrome-file": "^1.1.1",
"react": "^16.5.2",
"react-dom": "^16.5.2",
Expand Down
2 changes: 2 additions & 0 deletions src/apps/repl/.gitignore
@@ -0,0 +1,2 @@
.cache
default
3 changes: 3 additions & 0 deletions src/apps/repl/repl
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

../../../node_modules/.bin/cross-env TS_NODE_FILES=1 TS_NODE_CACHE_DIRECTORY=.cache TS_NODE_SKIP_IGNORE=1 node -r ts-node/register ./repl.ts $@
105 changes: 105 additions & 0 deletions src/apps/repl/repl.ts
@@ -0,0 +1,105 @@
import { LocalStorage } from "node-localstorage"

interface Global {
localStorage: LocalStorage
}

declare var global: Global

global.localStorage = new LocalStorage("./localstorage")

const raf = require("random-access-file")
import * as Link from "capstone/Link"
import * as Peek from "../../data/Peek"
import * as Workspace from "../../plugins/Workspace"
import * as repl from "repl"
import CloudClient from "discovery-cloud/Client"
import Content from "capstone/Content"
import Handle from "capstone/Handle"
import { Model as REPLModel } from "capstone/REPL"
import Store from "capstone/Store"
import StoreBackend from "capstone/StoreBackend"
import { Doc } from "automerge/frontend"
import { Hypermerge } from "hypermerge"
import { last } from "lodash"
import { parse } from "json-fn"

const workspaceUrl = process.argv[2]

const DEBUG = false

const hm = new Hypermerge({ storage: raf })
const storeBackend = new StoreBackend(hm)
Content.store = new Store()

storeBackend.sendQueue.subscribe(msg => {
if (DEBUG) {
console.log("backend msg", msg)
}
Content.store.onMessage(msg)
})

Content.store.sendQueue.subscribe(msg => {
if (DEBUG) {
console.log("frontend msg", msg)
}
storeBackend.onMessage(msg)
})

hm.joinSwarm(
new CloudClient({
url: "wss://discovery-cloud.herokuapp.com",
// url: "ws://localhost:8080",
id: hm.id,
stream: hm.stream,
}),
)

const startRepl = (replUrl: string) => {
repl.start({
prompt: ">>> ",
eval: (cmd: string, context: any, filename: string, callback: Function) => {
if (!cmd || !cmd.length) {
callback()
}

const handle = Content.open<REPLModel>(replUrl)

handle.change(doc => {
if (!doc.commands) {
doc.commands = [{ code: cmd }]
} else {
doc.commands.push({ code: cmd })
}

return doc
})

handle.subscribe(doc => {
const lastCmd = last(doc.commands)
if (!lastCmd) return

const { result } = lastCmd as { result: string }
if (!result) return

const parsed = parse(result)

handle.close()

callback(parsed.error, parsed.result)
})
},
})
}

hm.ready.then(hm => {
if (DEBUG) {
Peek.enable()
}

Content.once<Workspace.Model>(workspaceUrl, workspace => {
console.log(`Welcome to Capstone REPL! (${workspace.replUrl})`)

setTimeout(() => startRepl(workspace.replUrl), 10)
})
})
22 changes: 22 additions & 0 deletions src/components/App.tsx
Expand Up @@ -5,11 +5,15 @@ import { Content } from "capstone"
import Stats from "./Stats"
import * as Debug from "debug"
import * as css from "./css/App.css"
import * as Board from "../plugins/Board"
import * as UUID from "capstone/UUID"

import "../plugins"

const log = Debug("component:app")

window.hooks = {}

type State = {
url?: string
shouldHideFPSCounter?: boolean
Expand All @@ -26,6 +30,24 @@ export default class App extends React.Component<State> {
shouldHideFPSCounter: Content.store.shouldHideFPSCounter(),
})

Content.once(url, workspace => {
if (!workspace.rootUrl) return

Content.change<Board.Model>(workspace.rootUrl as string, rootBoard => {
const id = UUID.create()

rootBoard.cards[id] = {
id,
url: workspace.replUrl as string,
x: 100,
y: 100,
z: 1,
width: 300,
height: 500,
}
})
})

setTimeout(() => {
// This fn is triggered by the same observable setWorkspace writes to.
// Without the timeout, some receive the workspace in the wrong order
Expand Down
16 changes: 14 additions & 2 deletions src/node_modules/capstone/Env.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions src/node_modules/capstone/REPL.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/node_modules/capstone/Store.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/node_modules/capstone/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/node_modules/gps/Interactable.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.