Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski committed Nov 13, 2018
1 parent a3ebc6c commit 8e83be4
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 207 deletions.
82 changes: 27 additions & 55 deletions src/apps/make-bot/bots/journaling.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,37 @@
const last = arr => arr[arr.length - 1]

const once = fn => {
let fired = false

return (...args) => {
if (!fired) {
const result = fn(...args)

fired = true
fn = undefined

return result
}

return undefined
}
}

const addTimestamp = type => {
const url = Content.create("Text")

const change = Content.open(url, () => {})

change(
once(doc => {
doc.content =
type === "date"
? new Date().toDateString().split("")
: new Date().toTimeString().split("")
}),
)

Content.open(
Content.workspaceUrl,
once(workspace => {
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack)
: workspace.rootUrl

const changeBoard = Content.open(boardUrl, () => {})

const id = UUID.create()
Content.change(url, doc => {
doc.content =
type === "date"
? new Date().toDateString().split("")
: new Date().toTimeString().split("")
})

const card = {
id,
x: 50,
y: 50,
z: 100, // lazy, should take board.topZ
width: type === "date" ? 200 : 400,
height: 40,
url,
}
Content.open(Content.store.getWorkspace()).once(workspace => {
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack).url
: workspace.rootUrl

const id = UUID.create()

const card = {
id,
x: 50,
y: 50,
z: 100,
width: type === "date" ? 200 : 400,
height: 40,
url,
}

changeBoard(
once(doc => {
doc.cards[id] = card
}),
)
}),
)
Content.open(boardUrl).change(doc => {
doc.cards[id] = card
})
})
}

makeBot("journaling", bot => {
Expand Down
65 changes: 65 additions & 0 deletions src/apps/make-bot/bots/organizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const last = arr => arr[arr.length - 1]

const cleanUp = () => {
// constants for gallery
const MARGIN = 20
const WINDOW_HEIGHT = window.innerHeight

// open workspace
Content.open(Content.store.getWorkspace())
.once(workspace => {
// grab a board
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack).url
: workspace.rootUrl

Content.change(boardUrl, board => {
// all cards that are not a bot
const nonBotCards = Object.values(board.cards).filter(
card => card.url.indexOf("Bot") < 0,
)

// calculate average width of a card
const avgWidth =
nonBotCards.reduce((memo, card) => card.width + memo, 0) /
nonBotCards.length

// some imperative code to arrange in columns
let column = 0
let topOffset = 0

nonBotCards.forEach(card => {
// update card aspect ratio
const aspect = card.width / card.height
card.width = avgWidth
card.height = avgWidth / aspect

// move to new column if needed
if (topOffset + card.height + MARGIN * 2 > WINDOW_HEIGHT) {
column++
topOffset = 0
}

// update x & y pos
card.x = column * (avgWidth + MARGIN) + MARGIN
card.y = topOffset + MARGIN

// store offset
topOffset = card.y + card.height
})
}).close()
})
.close()
}

makeBot("organizer", bot => {
// and now, instead of working as a button-based action
// bot.action("Clean Up!", cleanUp)

// we can make it autonomous!
bot.autonomous(
"Board", // act on any change on board
cleanUp,
)
})
71 changes: 0 additions & 71 deletions src/apps/make-bot/bots/pinterest-organizer.js

This file was deleted.

120 changes: 57 additions & 63 deletions src/apps/make-bot/make-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,70 +55,64 @@ hm.joinSwarm(
)

hm.ready.then(hm => {
Content.open<Workspace.Model>(workspace)
.once(workspace => {
const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack)
: workspace.rootUrl

if (!boardUrl) {
console.log("Can't find a board, exiting...")
return
}
console.log("Ready!")

Content.open<Workspace.Model>(workspace).once(workspace => {
console.log("Opened workspace", workspace)

const boardUrl =
workspace.navStack.length > 0
? last(workspace.navStack)!.url
: workspace.rootUrl

if (!boardUrl) {
console.log("Can't find a board, exiting...")
return
}

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

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

// console.log("board doc", doc)

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

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

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(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)
.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()
const botUrl = doc.cards[botId]!.url

if (!botUrl) return

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

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

const botHandle = Content.open(botUrl).change(doc => {
doc.id = botId
doc.code = code
})

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

board.cards[botId] = card
})
}
})
.close()
})
})
2 changes: 2 additions & 0 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as Workspace from "./Workspace"

const log = Debug("component:app")

require("events").EventEmitter.prototype._maxListeners = 1000

type State = {
url?: string
shouldHideFPSCounter?: boolean
Expand Down
4 changes: 3 additions & 1 deletion src/components/Bot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class BotActor extends DocumentActor<Model, AnyChange> {
if (!bot || !bot.autonomous) return
if (!message.from) return

console.log("GOT ANYCHANGE", message)

const { type } = Link.parse(message.from)
bot.autonomous[type] && bot.autonomous[type]()
}
Expand Down Expand Up @@ -133,7 +135,7 @@ class Bot extends React.Component<Props, State> {
<div className={css.Bot}>
<h3>{this.props.doc.id}</h3>

<pre>{this.state.lastUpdate}</pre>
{/*<pre>{this.state.lastUpdate}</pre>*/}

{isAutonomuos && <h4>(autonomous)</h4>}

Expand Down
Loading

0 comments on commit 8e83be4

Please sign in to comment.