Skip to content

Instantly share code, notes, and snippets.

View mnylen's full-sized avatar

Mikko Nylén mnylen

  • Mikko Nylén Oy
  • Toijala
  • X @nylemi
View GitHub Profile
@mnylen
mnylen / _.md
Last active April 23, 2021 21:17
Debounced fetching to reduce number of requests when doing API proxying through GraphQL

Simple implementation of debounced fetching in GraphQL to allow merging of multiple rest / database requests into one. Although this example uses GraphQL, the debouncedFetch / fetchProgramPlaycount implementations could probably be used in any context to achieve the same result.

This approach was first described by @leebyron at graphql/graphql-js#19 (comment)

For example this allows turning ten requests for playcounts from this GraphQL query into just one:

{
  latestPrograms(first: 10) {
    name,

playcount

@mnylen
mnylen / gist:8c4010ab353f4886f89c
Last active March 12, 2021 16:33
Using the new Safari View Controller from React Native application

Using the new Safari View Controller from React Native application

  • Safari View Controller is iOS 9 only. You need Xcode 7 (beta 2 currently) to use it

  • React Native 0.6.0 is the minimum I got this working on

You might need to add this to your Info.plist in order for iOS 9 to load your application correctly:

<key>NSAppTransportSecurity</key>
wait4(-1, 0x7fffe0ce8fd4, WNOHANG|WSTOPPED|WCONTINUED, 0x7fffe0ce8ff0) = 0
rt_sigreturn({mask=[]}) = -1 EINTR (Interrupted system call)
kill(8741, SIGCONT) = -1 ESRCH (No such process)
kill(8744, SIGCONT) = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [INT CHLD WINCH], 8) = 0
rt_sigsuspend([], 8) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_CONTINUED, si_pid=8744, si_uid=1000, si_status=SIGCONT, si_utime=0, si_stime=0} ---
rt_sigprocmask(SIG_BLOCK, ~[RTMIN RT_1], [CHLD], 8) = 0
rt_sigprocmask(SIG_SETMASK, [CHLD], ~[KILL STOP RTMIN RT_1], 8) = 0
wait4(-1, [{WIFCONTINUED(s)}], WNOHANG|WSTOPPED|WCONTINUED, {ru_utime={tv_sec=0, tv_usec=0}, ru_stime={tv_sec=0, tv_usec=15625}, ...}) = 8744
@mnylen
mnylen / docker-login.sh
Last active September 13, 2018 06:07
Get JWT token to access private repositories on hub.docker.com
function echoerr {
echo "$@" 1>&2;
}
docker_username=${DOCKER_USER:-}
docker_password=${DOCKER_PASSWORD:-}
if [ -z "$docker_username" ]; then
if command -v docker-credential-osxkeychain > /dev/null 2>&1; then
readonly registry_url="https://index.docker.io/v1/"

S3/CloudFront problems

  • CloudFront: updates are very slow, taking about 20 - 30 minutes each time you try to change something to fix some of the things listed here
  • CloudFront: no support for Cache-Control: stale-while-revalidate (would be nice for index.html etc. that can't be hashed)
  • CloudFront: does not forward the original host name to a custom origin (would be useful for creating a S3 proxy origin to fix some of the issues here, instead of trying to implement them w/ lambda@edge)
  • S3: Vary: Origin header is not added for non-CORS requests    * this is needed to fix a browser caching issue where if you load an image via , the browser will cache the response to that. Then, when you do cross origin XHR to the same object to receive metadata, the browsers will use the cached response and fail, because it didn't contain Access-Control-Allow-Origin header for the original request made by - and because there was no Vary: Origin, browser doesn't think it could get a di
@mnylen
mnylen / ScalatraFileUploadTests.scala
Created April 6, 2012 19:11
postMulti() helper for scalatra-test for making multipart/form-data requests. Useful when testing file uploads with scalatra-fileupload
import org.apache.commons.io.IOUtils
import org.eclipse.jetty.http.HttpHeaders
import org.eclipse.jetty.testing.HttpTester
import org.eclipse.jetty.io.ByteArrayBuffer
import util.DynamicVariable
import org.scalatra.test.ScalatraTests
import java.net.HttpCookie
import java.io.{ByteArrayOutputStream, FileInputStream, File}
trait ScalatraFileUploadTests extends ScalatraTests {
import org.scalatra._
import org.scalatra.test.specs2._
class TestTest extends MutableScalatraSpec {
override def start() {
println("/// STARTING JETTY")
super.start()
}
override def stop() {
$ m2search "logback"
"de.huxhorn.lilith" % "logback" % "0.9.41"
"ch.qos.logback" % "logback" % "0.5"
"ch.qos.logback" % "logback-parent" % "1.0.6"
"ch.qos.logback" % "logback-site" % "1.0.6"
"ch.qos.logback" % "logback-skin" % "0.9"
"org.wicketstuff" % "wicketstuff-logback-parent" % "6.0.0-beta1.1"
"ch.qos.logback" % "audit-parent" % "0.5"
"ch.qos.logback" % "logback-examples" % "1.0.6"
"ch.qos.logback" % "logback-access" % "1.0.6"
(def x (transient {}))
(conj! x [\A 1]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\B 2]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\C 3]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\D 4]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\E 5]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\F 6]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\G 7]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\H 8]) ;; => #<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@646dee9f>
(conj! x [\I 9]) ;; => #<TransientHashMap clojure.lang.PersistentHashMap$TransientHashMap@77aa13c5>
val resp = client
.prepareSearch(IndexName)
.setTypes(TypeName)
.setQuery(qb)
.addSort("id", SortOrder.ASC)
.execute()
.actionGet()
resp.getHits.getHits.map(hit =>
parse(hit.sourceAsString()).asInstanceOf[JObject]