-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
export const CREATE_CONNECTION = "CREATE_CONNECTION"; | ||
export const UPDATE_CONNECTION = "UPDATE_CONNECTION"; | ||
|
||
export const IMAGINE_CONNECTION = "IMAGINE_CONNECTION"; | ||
export const REALIZE_CONNECTION = "REALIZE_CONNECTION"; | ||
export const ABANDON_CONNECTION = "ABANDON_CONNECTION"; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
export function createConnection(connection) { | ||
return { | ||
type: CREATE_CONNECTION, | ||
|
@@ -14,3 +18,31 @@ export function updateConnection(connection) { | |
connection | ||
}; | ||
} | ||
|
||
export function imagineConnection({ | ||
connectionId, | ||
lineId, | ||
sourceId, | ||
terminalId | ||
}) { | ||
return { | ||
type: IMAGINE_CONNECTION, | ||
connection: { id: connectionId }, | ||
line: { id: lineId }, | ||
source: { id: sourceId }, | ||
terminal: { id: terminalId } | ||
}; | ||
This comment has been minimized.
Sorry, something went wrong.
henrycatalinismith
Author
Owner
|
||
} | ||
|
||
export function realizeConnection({ destinationId }) { | ||
return { | ||
type: REALIZE_CONNECTION, | ||
destination: { id: destinationId } | ||
}; | ||
} | ||
|
||
export function abandonConnection() { | ||
return { | ||
type: ABANDON_CONNECTION | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
// keep | ||
export const DRAGON_DROP = "DRAGON_DROP"; | ||
export const DRAGON_GRAB = "DRAGON_GRAB"; | ||
export const DRAGON_MOVE = "DRAGON_MOVE"; | ||
|
||
// deprecate | ||
export const DRAGON_DRAG_TERMINAL = "DRAGON_DRAG_TERMINAL"; | ||
export const DRAGON_GRAB_TERMINAL = "DRAGON_GRAB_TERMINAL"; | ||
export const DRAGON_DROP_TERMINAL = "DRAGON_DROP_TERMINAL"; | ||
export const DRAGON_CREATE_CONNECTION = "DRAGON_CREATE_CONNECTION"; | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
henrycatalinismith
Author
Owner
|
||
export function dragonDrop(entity, id) { | ||
return { | ||
type: DRAGON_DROP, | ||
|
@@ -29,33 +22,3 @@ export function dragonMove(x, y) { | |
dragon: { x, y } | ||
}; | ||
} | ||
|
||
export function dragonGrabTerminal(id) { | ||
return { | ||
type: DRAGON_GRAB_TERMINAL, | ||
terminal: { id } | ||
}; | ||
} | ||
|
||
export function dragonMoveTerminal(x, y, id) { | ||
return { | ||
type: DRAGON_DRAG_TERMINAL, | ||
x, | ||
y, | ||
id | ||
}; | ||
} | ||
|
||
export function dragonDropTerminal(id) { | ||
return { | ||
type: DRAGON_DROP_TERMINAL, | ||
terminal: { id } | ||
}; | ||
} | ||
|
||
export function dragonCreateConnection(destinationId) { | ||
return { | ||
type: DRAGON_CREATE_CONNECTION, | ||
connection: { destinationId } | ||
}; | ||
This comment has been minimized.
Sorry, something went wrong.
henrycatalinismith
Author
Owner
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { createMiddleware } from "signalbox"; | ||
import uuid from "uuid/v1"; | ||
import actions from "../actions"; | ||
import { select } from "../reducers"; | ||
|
||
export const middleware = createMiddleware((before, after, cancel) => ({ | ||
[before(actions.CREATE_CONNECTION)]: function inferConnectionProperties( | ||
|
@@ -10,5 +11,66 @@ export const middleware = createMiddleware((before, after, cancel) => ({ | |
if (!action.connection.id) { | ||
action.connection.id = uuid(); | ||
} | ||
}, | ||
|
||
[before(actions.IMAGINE_CONNECTION)]: function inferConnectionProperties( | ||
This comment has been minimized.
Sorry, something went wrong.
henrycatalinismith
Author
Owner
|
||
store, | ||
action | ||
) { | ||
if (!action.connection.id) { | ||
action.connection.id = uuid(); | ||
} | ||
|
||
action.sourceTerminal = { | ||
id: uuid(), | ||
connectionId: action.connection.id, | ||
stationId: action.source.id, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
This comment has been minimized.
Sorry, something went wrong. |
||
}; | ||
|
||
action.destinationTerminal = { | ||
id: uuid(), | ||
connectionId: action.connection.id, | ||
lineId: "Riverside" | ||
}; | ||
}, | ||
|
||
[before(actions.REALIZE_CONNECTION)]: function hydrateNewlyRealizedConnection( | ||
store, | ||
action | ||
) { | ||
const state = store.getState(); | ||
const connection = select("connections") | ||
.from(state) | ||
.imaginary() | ||
.toJS(); | ||
action.connection = { id: connection.id }; | ||
action.line = { id: connection.lineId }; | ||
action.source = { id: connection.sourceId }; | ||
}, | ||
|
||
[after(actions.REALIZE_CONNECTION)]: function imagineNextConnection( | ||
store, | ||
action | ||
) { | ||
const imagineConnection = actions.imagineConnection({ | ||
sourceId: action.destination.id, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
}); | ||
store.dispatch(imagineConnection); | ||
}, | ||
|
||
[before(actions.ABANDON_CONNECTION)]: function hydrateNewlyRealizedConnection( | ||
store, | ||
action | ||
) { | ||
const state = store.getState(); | ||
const connection = select("connections") | ||
.from(state) | ||
.imaginary() | ||
.toJS(); | ||
action.connection = { id: connection.id }; | ||
action.line = { id: connection.lineId }; | ||
action.source = { id: connection.sourceId }; | ||
} | ||
})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ export const middleware = createMiddleware((before, after, cancel) => ({ | |
action | ||
) { | ||
if (action.dragon.entity === "terminal" && !!action.dragon.id) { | ||
store.dispatch(actions.dragonDropTerminal(action.dragon.id)); | ||
store.dispatch(actions.abandonConnection()); | ||
return true; | ||
} | ||
}, | ||
|
@@ -24,7 +24,7 @@ export const middleware = createMiddleware((before, after, cancel) => ({ | |
|
||
if (entity === "terminal" && !!id) { | ||
store.dispatch( | ||
actions.dragonMoveTerminal(action.dragon.x, action.dragon.y, id) | ||
actions.dragTerminal(id, action.dragon.x, action.dragon.y) | ||
); | ||
return true; | ||
} | ||
|
@@ -37,93 +37,12 @@ export const middleware = createMiddleware((before, after, cancel) => ({ | |
action | ||
) { | ||
const stationId = action.station.id; | ||
const connectionId = uuid(); | ||
const terminalId = uuid(); | ||
|
||
store.dispatch( | ||
actions.createConnection({ | ||
id: connectionId, | ||
sourceId: stationId, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
}) | ||
); | ||
|
||
store.dispatch( | ||
actions.createTerminal({ | ||
id: uuid(), | ||
connectionId, | ||
stationId, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
}) | ||
); | ||
|
||
store.dispatch( | ||
actions.createTerminal({ | ||
id: terminalId, | ||
connectionId, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
}) | ||
); | ||
|
||
store.dispatch(actions.dragonGrabTerminal(terminalId)); | ||
|
||
return true; | ||
}, | ||
|
||
[cancel(actions.DRAGON_CREATE_CONNECTION)]: function finishCreatingConnection( | ||
store, | ||
action | ||
) { | ||
const state = store.getState(); | ||
|
||
const dragon = store | ||
.getState() | ||
.get("dragon") | ||
.toJS(); | ||
|
||
const terminal = select("terminals") | ||
.from(state) | ||
.byId(dragon.id) | ||
.toJS(); | ||
|
||
const connection = select("connections") | ||
.from(state) | ||
.byId(terminal.connectionId) | ||
.toJS(); | ||
|
||
const lineId = connection.lineId; | ||
const sourceId = connection.sourceId; | ||
const destinationId = action.connection.destinationId; | ||
|
||
if (sourceId === destinationId) { | ||
// this has only fired because the mouse has re-entered the origin | ||
// station and obviously we don't want to connect the station to itself | ||
// so just cancel this completely | ||
return true; | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
henrycatalinismith
Author
Owner
|
||
|
||
store.dispatch( | ||
actions.updateConnection({ | ||
id: connection.id, | ||
destinationId | ||
}) | ||
); | ||
|
||
const newId = uuid(); | ||
store.dispatch( | ||
actions.createConnection({ | ||
id: newId, | ||
sourceId: destinationId, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
}) | ||
); | ||
|
||
store.dispatch( | ||
actions.updateTerminal({ | ||
id: terminal.id, | ||
connectionId: newId | ||
}) | ||
); | ||
const imagineConnection = actions.imagineConnection({ | ||
sourceId: action.station.id, | ||
lineId: "Riverside" // todo: pick 1st empty line for this | ||
}); | ||
store.dispatch(imagineConnection); | ||
This comment has been minimized.
Sorry, something went wrong.
henrycatalinismith
Author
Owner
|
||
|
||
return true; | ||
} | ||
|
Okay so I think Redux work is shitloads more fun and vivid if you work super hard on the action type naming. I like making little repeatable clusters of cool descriptive names like these cos it feels like a really nice lightweight kind of architecture work.
IMAGINE_CONNECTION
This is when you start dragging a terminal around and you're kind of plotting a hypothetical new connection. The normal zero-imagination way of doing this would be like
dispatch(action.createConnection())
with a bunch of props that implicitly mark out the new connection object as kind of imaginary in the sense that it lacks a solid destinationId yet. I think it's loads nicer to have that just be a separate action type altogether.REALIZE_CONNECTION
This is when you drag the terminal to a new station, thus indicating which two stations you want to connect. It "makes the connection real". I'm a bit of a weirdo about the action type names having the same length as their little siblings in the cluster lol.
ABANDON_CONNECTION
This is if you drop the terminal before connecting to a destination station. It triggers all the cleanup i.e. deleting the connection and the tracks.