Skip to content

Instantly share code, notes, and snippets.

View topicus's full-sized avatar

Mariano Carballal topicus

View GitHub Profile
import asyncio
from fastapi import FastAPI
app = FastAPI()
async def some_long_running_task(param):
await asyncio.sleep(10) # Simulating a long-running task
print(f"Task completed with param: {param}")
@app.get("/fire-and-forget/{param}")
@topicus
topicus / gist:9956cac3970b91bfa1be6665d64be1be
Last active July 20, 2022 17:33
Dataloader dynamic batch

Solution

Problem statement

GraphQL is great for exposing a single API that hides the complexity of the systems behind. As such, it is commonly use to aggregate data from different data sources. For example, databases, services, etc.

But most corporate web services are build around RESTful APIs, hence it is crucial to handle this use case efficiently.

Given the reference implementation for GraphQL is written in node and the close relationship between the frontend and the language, it is very popular to use node as the platform for the server.

@topicus
topicus / embed-script.html
Created April 12, 2017 21:04
How to automatically resize an iframe based on their content
<script type="text/javascript">
var node = document.scripts[document.scripts.length - 1];
var parent = node.parentElement;
var iframe = document.createElement('iframe');
iframe.setAttribute('src', 'http://example.com');
iframe.width = '100%';
iframe.frameBorder = 0;
parent.insertBefore(iframe, node.nextSibling);
window.addEventListener('message', function(e){ iframe.height = e.data }, false);
</script>
@topicus
topicus / bindCallApply.js
Created March 20, 2017 20:05
How bind, call and apply change the this.
function printName(id) {
console.log(this.name + id);
}
let person = {
name: 'Person Marian'
};
let human = {
name: 'Human Charles'
}
@topicus
topicus / objectMethods.js
Created March 20, 2017 19:35
Where this points in object methods
// From https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this
let o = {
prop: 37,
f: function() {
return this.prop;
}
};
console.log(o.f()); // logs 37
@topicus
topicus / arrow.js
Created March 20, 2017 17:37
How arrow functions works with this
function Animal() {
// this points to animal
document.addEventListener('click', (e) => {
// Unlike regular functions arrow functions takes this from the
// outer context so it points to animal as well.
console.log(this)
});
}
let animal = new Animal();
@topicus
topicus / bind.js
Last active March 20, 2017 20:43
How bind works
// This code doesn't make any sense.
// Just for pedagogical purposes.
class HelloComponent {
constructor(id) {
this.button = document.getElementById(id);
this.bindLog = this.log.bind(this);
// Use the bind version of log. It ties log to the object we are creating.
document.addEventListener('click', this.bindLog);
@topicus
topicus / constructors.js
Created March 20, 2017 17:06
Difference between function vs function constructors
function LikeButton(id) {
this.id = id;
}
// In this case "this" will point to btn. You can read the function like this:
// function LikeButton(id) {
// btn.id = id;
// return btn;
// }
let btn = new LikeButton('like-button');
@topicus
topicus / listeners.js
Last active March 20, 2017 16:47
How this is dynamically bind to DOM elements.
// Suppose you have an html like:
// <div id="like-button">
// <i class"icon-hand" />
// <div>Like</div>
// </div>
function LikeButton(id) {
this.buttonId = id;
this.subscribe = function() {
let btn = document.getElementById(this.buttonId);
@topicus
topicus / Web-Worker-HOS.js
Last active January 13, 2017 19:51
High Order Store for Web Workers
export const createSubscriber = (emmiter, store) => handler => {
emmiter.addEventListener('message', (message) => handler(message.data));
if (store) store.subscribe(handler);
};
export const createDispatcher = (emmiter, store) => action => {
emmiter.postMessage(action);
if (store) store.dispatch(action);
};