-
-
Save Raynos/4846f985289eeac51e11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fold = require("reducers/fold") | |
var expand = require("reducers/expand") | |
var merge = require("reducers/merge") | |
var send = require("event/send") | |
var Emitter = require("./emitter-reduce") | |
var console = require("console") | |
var SignalChannel = require("signal-channel") | |
var FullyConnected = require("topology/fully") | |
var Model = require("scuttlebutt/model") | |
module.exports = communication | |
function communication(appState, identity) { | |
var channel = SignalChannel("Colingo~colingo-feed~feed-demo") | |
var model = Model() | |
var seen = {} | |
identity = identity || defaultIdentity | |
FullyConnected(channel, function (conn, opener) { | |
var peerId = conn.peerId | |
console.log("connected to peer", peerId) | |
if (opener) { | |
hookUp(conn.createStream("model")) | |
} else { | |
conn.on("connection", function (stream) { | |
if (stream.meta === "model") { | |
hookUp(stream) | |
} | |
}) | |
} | |
}) | |
return merge([ | |
expand(Emitter(model, "change"), function (tuple) { | |
var key = tuple[0] | |
var item = tuple[1] | |
var id = identity(item) | |
if (!id) { | |
throw new Error("got item that does not have id from Model") | |
} | |
if (seen[id]) { | |
return | |
} | |
console.log("got a value", key, item) | |
seen[id] = true | |
return item | |
}) | |
, expand(appState, function (item) { | |
var id = identity(item) | |
if (!id) { | |
throw new Error("got item that does not have id from feed") | |
} | |
if (seen[id]) { | |
return | |
} | |
console.log("setting a value (item)", item) | |
seen[id] = true | |
model.set(id, item) | |
}) | |
]) | |
function hookUp(stream) { | |
console.log("hookedUp", stream.meta) | |
stream. | |
pipe(model.createStream()). | |
pipe(stream) | |
} | |
} | |
function defaultIdentity(item) { | |
return item.value.id | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment