Skip to content

Instantly share code, notes, and snippets.

View claustres's full-sized avatar
🏠
Working from home

Luc Claustres claustres

🏠
Working from home
View GitHub Profile
@claustres
claustres / index.html
Last active October 12, 2017 14:45 — forked from VictorVelarde/index.html
Leaflet.CanvasLayer.Field issue #8 - weacast (Custom JSON)
<!DOCTYPE html>
<html>
<head>
<title>Weacast ScalarField | Issue #8 with 1.3.3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="//unpkg.com/[email protected]/dist/leaflet.css" />
<link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
<style>
@claustres
claustres / dynamic-hooks-configuration.js
Created November 15, 2017 10:36
Dynamic hooks configuration
function activateHooks (service, serviceHooks) {
// Iterate over hook types (before, after)
_.forOwn(serviceHooks, (hooksDefinition, stage) => {
// Iterate over hooks to create the hook pipeline
let pipeline = []
_.forOwn(hooksDefinition, (hookOptions, hookName) => {
// Jump from name/options to the real hook function
pipeline.push(hooks[hookName](hookOptions))
})
// Replace hooks in place so that we can use it directly with Feathers after
@claustres
claustres / generic-service-entities.js
Created November 24, 2017 10:06
Generic service entities
import _ from 'lodash'
class Service {
constructor (generators = {}) {
this.generators = {}
_.forOwn(generators, (value, key) => {
this.registerGenerator(key, value)
})
}
@claustres
claustres / create-generic-service-entity.js
Last active November 24, 2017 19:41
Create a generic service entity
class StoresService extends Service {
...
async create (data, params = {}) {
let { type, options } = data
return this.generate(type, options))
}
...
}
@claustres
claustres / job-file-with-hooks.js
Last active November 29, 2017 16:56
Job file with hooks definition
let job = {
// Options for job executor
options: {
workersLimit: 4
},
// Store to be used
store: {
id: 'job-store',
type: 'fs',
options: { path: './data' }
@claustres
claustres / async-job-execution.js
Last active November 24, 2017 19:43
Async job execution
class JobsService extends Service {
setup (app, path) {
this.tasksService = app.service('tasks')
}
async create (data, params = {}) {
let { type, options, store, tasks } = data
return this.generate(type, options, store, tasks)
}
@claustres
claustres / streamed-tasks.js
Last active November 24, 2017 13:08
Streamed tasks
class TasksService extends Service {
create (data, params) {
let { id, type, options } = data
let { store } = params
return new Promise((resolve, reject) => {
let taskStream = this.generate(type, options)
taskStream
.on('error', reject)
.pipe(store.createWriteStream({
@claustres
claustres / async_await.js
Last active February 20, 2018 06:39
Async/Await misconception
const delay = (duration) =>
new Promise(resolve => setTimeout(resolve, duration))
async function asyncWithAwait(prefix) {
console.log(prefix + ' before await')
await delay(1000)
console.log(prefix + ' after await')
}
function asyncWithPromise(prefix) {
@claustres
claustres / async_await_run_1.js
Last active February 20, 2018 06:40
Async/Await run 1
async function run() {
let prefix = '(1)'
console.log(prefix + ' with await')
asyncWithAwait(prefix)
console.log(prefix + ' with promise')
asyncWithPromise(prefix)
console.log(prefix + ' after all')
}
run()
@claustres
claustres / async_await_run_2.js
Last active February 20, 2018 06:40
Async/Await run 2
async function run() {
let prefix = '(2)'
console.log(prefix + ' with await')
await asyncWithAwait(prefix)
console.log(prefix + ' with promise')
asyncWithPromise(prefix).then(_ => console.log(prefix + ' after all'))
}
run()