From 8e83be420638ad4c53709cde35a9a0f798c0bf7a Mon Sep 17 00:00:00 2001 From: Szymon Kaliski Date: Tue, 13 Nov 2018 11:22:57 +0100 Subject: [PATCH] updates --- src/apps/make-bot/bots/journaling.js | 82 ++++-------- src/apps/make-bot/bots/organizer.js | 65 ++++++++++ src/apps/make-bot/bots/pinterest-organizer.js | 71 ----------- src/apps/make-bot/make-bot.ts | 120 +++++++++--------- src/components/App.tsx | 2 + src/components/Bot.tsx | 4 +- src/components/Widget.tsx | 43 ++++--- 7 files changed, 180 insertions(+), 207 deletions(-) create mode 100644 src/apps/make-bot/bots/organizer.js delete mode 100644 src/apps/make-bot/bots/pinterest-organizer.js diff --git a/src/apps/make-bot/bots/journaling.js b/src/apps/make-bot/bots/journaling.js index 734edaf..cb49ddb 100644 --- a/src/apps/make-bot/bots/journaling.js +++ b/src/apps/make-bot/bots/journaling.js @@ -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 => { diff --git a/src/apps/make-bot/bots/organizer.js b/src/apps/make-bot/bots/organizer.js new file mode 100644 index 0000000..7eae157 --- /dev/null +++ b/src/apps/make-bot/bots/organizer.js @@ -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, + ) +}) diff --git a/src/apps/make-bot/bots/pinterest-organizer.js b/src/apps/make-bot/bots/pinterest-organizer.js deleted file mode 100644 index e57e0c3..0000000 --- a/src/apps/make-bot/bots/pinterest-organizer.js +++ /dev/null @@ -1,71 +0,0 @@ -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 organizeItems = () => { - const MARGIN = 20 - const WINDOW_HEIGHT = window.innerHeight - - Content.open(Content.workspaceUrl, workspace => { - const boardUrl = - workspace.navStack.length > 0 - ? last(workspace.navStack) - : workspace.rootUrl - - const changeBoard = Content.open(boardUrl, () => {}) - - changeBoard( - once(doc => { - const nonBotCards = Object.values(doc.cards).filter( - card => card.url.indexOf("Bot") < 0, - ) - - const avgWidth = - nonBotCards.reduce((memo, card) => card.width + memo, 0) / - nonBotCards.length - - let column = 0 - let topOffset = 0 - - nonBotCards.forEach(card => { - const aspect = card.width / card.height - - card.width = avgWidth - card.height = avgWidth / aspect - - if (topOffset + card.height + MARGIN * 2 > WINDOW_HEIGHT) { - column++ - topOffset = 0 - } - - card.x = column * (avgWidth + MARGIN) + MARGIN - card.y = topOffset + MARGIN - - topOffset = card.y + card.height - }) - }), - ) - }) -} - -// id here has to be the same as --id passed to cli, because of lazy engineering -makeBot("pinterest-organizer", bot => { - bot.autonomous("Board", () => { - organizeItems() - }) -}) diff --git a/src/apps/make-bot/make-bot.ts b/src/apps/make-bot/make-bot.ts index 4f92148..6f3b893 100644 --- a/src/apps/make-bot/make-bot.ts +++ b/src/apps/make-bot/make-bot.ts @@ -55,70 +55,64 @@ hm.joinSwarm( ) hm.ready.then(hm => { - Content.open(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).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(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(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() + }) }) diff --git a/src/components/App.tsx b/src/components/App.tsx index 271256b..238d608 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -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 diff --git a/src/components/Bot.tsx b/src/components/Bot.tsx index 4bc5508..2101272 100644 --- a/src/components/Bot.tsx +++ b/src/components/Bot.tsx @@ -75,6 +75,8 @@ class BotActor extends DocumentActor { 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]() } @@ -133,7 +135,7 @@ class Bot extends React.Component {

{this.props.doc.id}

-
{this.state.lastUpdate}
+ {/*
{this.state.lastUpdate}
*/} {isAutonomuos &&

(autonomous)

} diff --git a/src/components/Widget.tsx b/src/components/Widget.tsx index 12865e0..b9c9288 100644 --- a/src/components/Widget.tsx +++ b/src/components/Widget.tsx @@ -60,26 +60,35 @@ export function create( } componentDidMount() { - this.handle = Content.open(this.props.url).once((doc: any) => { - this.setState({ doc }, () => { - if (Link.parse(this.props.url).type === "Bot") return + this.handle = Content.open(this.props.url).subscribe((doc: any) => { + this.setState({ doc }) - const workspaceUrl = Content.store && Content.store.getWorkspace() - if (!workspaceUrl) return + if (Link.parse(this.props.url).type === "Bot") return - // send AnyChange to bot(s) on current board - Content.open(workspaceUrl).once(workspace => { - const boardUrl = - workspace.navStack.length > 0 - ? last(workspace.navStack) - : workspace.rootUrl + const workspaceUrl = Content.store && Content.store.getWorkspace() + if (!workspaceUrl) return - if (!boardUrl) return + // send AnyChange to bot(s) on current board + Content.open(workspaceUrl).once(workspace => { + const boardUrl = + workspace.navStack.length > 0 + ? last(workspace.navStack)!.url + : workspace.rootUrl - Content.open(boardUrl as string).once(board => { - Object.values(board.cards).forEach(card => { - if (!card || (card && card.url && card.url.indexOf("Bot") < 0)) - return + if (!boardUrl) return + + Content.open(boardUrl).once(board => { + Object.values(board.cards).forEach(card => { + if (!card || (card && card.url && card.url.indexOf("Bot") < 0)) + return + + setTimeout(() => { + console.log({ + type: "AnyChange", + body: doc, + from: this.props.url, + to: card.url, + }) Content.send({ type: "AnyChange", @@ -87,7 +96,7 @@ export function create( from: this.props.url, to: card.url, }) - }) + }, 200) }) }) })