damus.io

damus.io website
git clone git://jb55.com/damus.io
Log | Files | Refs | README | LICENSE

index.js (2251B)


      1 
      2 async function damus_init()
      3 {
      4 	const relay = await Relay("wss://relay.damus.io")
      5 	const now = (new Date().getTime()) / 1000
      6 	const el = document.querySelector("#content")
      7 	const model = {events: []}
      8 
      9 	el.innerHTML = render_initial_content()
     10 	model.el = el.querySelector("#home")
     11 
     12 	relay.subscribe("test_sub_id", {kinds: [1], limit: 20})
     13 	relay.event = (sub_id, ev) => {
     14 		insert_event_sorted(model.events, ev)
     15 		if (model.realtime)
     16 			render_home_view(model)
     17 	}
     18 
     19 	relay.eose = () => {
     20 		model.realtime = true
     21 		render_home_view(model)
     22 	}
     23 
     24 	return relay
     25 }
     26 
     27 function render_home_view(model) {
     28 	model.el.innerHTML = render_events(model.events)
     29 }
     30 
     31 function render_initial_content() {
     32 	return `<ul id="home"> </ul>`
     33 }
     34 
     35 function insert_event_sorted(evs, new_ev) {
     36 	for (let i = 0; i < evs.length; i++) {
     37 		const ev = evs[i]
     38 
     39 		if (new_ev.id === ev.id) {
     40 			return false
     41 		}
     42 
     43 		if (new_ev.created_at > ev.created_at) {
     44 			evs.splice(i, 0, new_ev)
     45 			return true
     46 		}
     47 	}
     48 
     49 	evs.push(new_ev)
     50 	return true
     51 }
     52 
     53 function render_events(evs) {
     54 	return evs.map(render_event).join("\n")
     55 }
     56 
     57 function render_event(ev) {
     58 	return `<li>${ev.content}</li>`
     59 }
     60 
     61 function Relay(relay, opts={})
     62 {
     63 	if (!(this instanceof Relay))
     64 		return new Relay(relay, opts)
     65 
     66 	this.relay = relay
     67 	this.opts = opts
     68 
     69 	const me = this
     70 	return new Promise((resolve, reject) => {
     71 		const ws = me.ws = new WebSocket(relay);
     72 		let resolved = false
     73 		ws.onmessage = (m) => { handle_message(me, m) }
     74 		ws.onclose = () => { me.close && me.close() }
     75 		ws.onerror = () => { me.error && me.error() }
     76 		ws.onopen = () => {
     77 			if (resolved) {
     78 				me.open.bind(me)
     79 				return
     80 			}
     81 
     82 			resolved = true
     83 			resolve(me)
     84 		}
     85 	})
     86 }
     87 
     88 Relay.prototype.subscribe = function relay_subscribe(sub_id, ...filters) {
     89 	const tosend = ["REQ", sub_id, ...filters]
     90 	console.log("sending", tosend)
     91 	this.ws.send(JSON.stringify(tosend))
     92 }
     93 
     94 function handle_message(relay, msg)
     95 {
     96 	const data = JSON.parse(msg.data)
     97 	if (data.length >= 2) {
     98 		switch (data[0]) {
     99 		case "EVENT":
    100 			if (data.length < 3)
    101 				return
    102 			return relay.event && relay.event(data[1], data[2])
    103 		case "EOSE":
    104 			return relay.eose && relay.eose(data[1])
    105 		case "NOTICE":
    106 			return relay.note && relay.note(...data.slice(1))
    107 		}
    108 	}
    109 }
    110 
    111 const relay = damus_init()