Skip to content

Instantly share code, notes, and snippets.

View colinbes's full-sized avatar

Colin Bester colinbes

  • Bester Designs LLC
  • Austin, Texas
View GitHub Profile
@colinbes
colinbes / Accordion.scala
Created July 3, 2013 16:20
A short Scala helper object to assist with creating Accordion functionality into LiftWeb using Twitter Bootstrap
package com.besterdesigns.bootstrap
/**
* Case Class defining header and body for accordion object.
*/
case class Collapsible(header: String, body: String)
object Accordion {
def apply(parentId: String) = new Accordion(parentId)
}
@colinbes
colinbes / question_json_field_ordering
Created September 16, 2014 17:15
lift-json custom serializer
/** Custom lift-json serializer
* Why does ordering of JFields matter - surely this is not desirable as JSON is unordered
*/
class MyUpdateSerializer extends Serializer[UpdateWrapper] {
private val UpdateClass = classOf[UpdateWrapper]
private val fmt = ISODateTimeFormat.dateTime()
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), UpdateWrapper] = {
case (TypeInfo(UpdateClass, _), json) => {
println("update serializer got json "+json)
@colinbes
colinbes / CheckDPI.scala
Created March 18, 2015 12:39
Scala, jpeg image using java awt to set and check compression and dpi
/*
* Simple DPI check of file
*/
import javax.imageio.ImageIO
import org.w3c.dom.Element
import java.io.File
object CheckDPI extends App {
val file = new File("test.jpg")
@colinbes
colinbes / loaner.scala
Created June 17, 2015 20:46
Scala loan pattern
//Loaner pattern to ensure closing of bufferedsource
...
def using[T](r: BufferedSource)(handler: BufferedSource => T): T = {
try {
handler(r)
} finally {
r.close()
}
}
@colinbes
colinbes / setupstream.js
Created January 6, 2020 15:27
Initiate EventSource
setupStream () {
let evtSource = new EventSource("http://localhost:8082/events")
evtSource.addEventListener('myEvent', event => {
let data = JSON.parse(event.data)
this.now = data.msg
}, false)
evtSource.addEventListener('error', event => {
if (event.readyState == EventSource.CLOSED) {
@colinbes
colinbes / index.html
Created January 6, 2020 15:34
Vuejs using EventSource
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue/Akka/SSE</title>
</head>
<body>
<div id="app">
<h1>{{ message }}</h1>
<span>SSE Date: {{ now }}</span>
@colinbes
colinbes / myScript.js
Created January 6, 2020 15:35
Vuejs file for EventSource
const app = new Vue({
el: '#app',
data: {
message: 'Vue/SSE Example!',
now: 'wait for it ...'
},
created () {
this.setupStream()
},
methods: {
@colinbes
colinbes / Akka-Source-SSE.scala
Created January 6, 2020 15:44
Akka Source.queue for SSE
lazy val (sourceQueue, eventsSource) = Source.queue[String](Int.MaxValue, OverflowStrategy.backpressure)
.delay(1.seconds, DelayOverflowStrategy.backpressure)
.map(message => ServerSentEvent(message, Some("myEvent")))
.keepAlive(1.second, () => ServerSentEvent.heartbeat)
.toMat(BroadcastHub.sink[ServerSentEvent])(Keep.both)
.run()
@colinbes
colinbes / sseevent.scala
Created January 6, 2020 16:47
SSE Rest Endpoint
package com.besterdesigns.rest.services
import akka.actor.{ActorRef, ActorSystem}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.sse.ServerSentEvent
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.MethodDirectives.{get, post}
import akka.http.scaladsl.server.directives.PathDirectives.path
import akka.http.scaladsl.server.directives.RouteDirectives.complete
@colinbes
colinbes / StreamActor.scala
Last active January 6, 2020 19:07
SSE Streaming Actor
package com.besterdesigns.actors
import java.util.Calendar
import _root_.akka.actor.{Actor, Props}
import _root_.akka.stream.scaladsl.SourceQueueWithComplete
import com.besterdesigns.models.Json4sFormat
import scala.concurrent.duration._
case object StartClock