Skip to content

Instantly share code, notes, and snippets.

@idrisadetunmbi
idrisadetunmbi / Resource.kt
Created March 8, 2024 11:55
Resource class from Old Android Guide to App Architecture
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
data class Resource<out T>(val status: Status, val data: T?, val exception: Throwable?) {
operator fun invoke(): T? {
interface ApiClient {
suspend fun getTodos(): List<Todo>
}
class Repo(private val apiClient: ApiClient) {
suspend fun getTodos(): Result<List<Todo>> = runApiRequest {
apiClient.getTodos()
}
}
@idrisadetunmbi
idrisadetunmbi / useResizeEffect.js
Last active July 23, 2020 10:18
Hook that runs when dependencies changes and reruns on window resize
import { useLayoutEffect } from "react"
export const useResizeEffect = (effect, deps = []) => {
// might need to wrap effect in a useCallback
useLayoutEffect(() => {
effect()
window.addEventListener('resize', effect)
return () => window.removeEventListener('resize', effect)
}, deps)
}
@idrisadetunmbi
idrisadetunmbi / swagger-ui-express.js
Last active December 27, 2017 12:36
Serving API Docs Through Swagger-UI-Express
const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
// './swagger.json' refers to the api documentation file generated through postman and converted through Apimatic
// it could be named anything else e.g. converted.json or api-docs.json
const swaggerDocument = require('./swagger.json');
// '/api-docs' refers to the url or route path through which the documentation is to be served
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));