bootstarted / midori Goto Github PK
View Code? Open in Web Editor NEWMinimalist, composable http middleware packs for http/express/hapi.
Minimalist, composable http middleware packs for http/express/hapi.
There are sometimes resources attached to a connection that need cleaning up โ e.g. database connection that's been checked out from a pool.
createSelector(getA, getB, getC, async (a, b, c) => {
return await dbPool.checkout(); // how to clean up this connection?
});
Possible option:
createSelector(getA, getB, getC, async (a, b, c) => {
return await dbPool.checkout();
}).withDisposer(async (a, b, c, con) => {
return await dbPool.release(con);
});
Currently there's a fairly large surface area for re-use here. Example: query
match and query
selector both generate the same value under the hood and it would be great if the latter could make use of the former somehow.
It seems flow
is strong enough now to reify a long chain of function composition, which means midori
should be able to benefit. Unfortunately it means the App
object has to be broken down into a bunch of ThunkObject
s since really the reified type of the request handler won't be the same as the error handler.
This will likely result in an internal refactor, but the outer API layer should stay totally the same. The request
function will become a wrapper to bind most likely.
// fn is the same request handler you see today.
const request = (fn) => bind(({req, res}) => {
return fn(req, res);
});
// fn is the same error handler you see today.
const error = (fn) => bind(({req, res, error}) => {
return fn(error, req, res);
});
Here's an example which you can play with at https://flow.org/try:
/* @flow */
type ThunkObject<T, U> = {
invoke: (a: T) => U;
}
type LiftThunkObject<T, U, V, W> = (out: ThunkObject<U, W>) => ThunkObject<T, V>;
const liftAssign = <T,U, V>(
obj: T
): LiftThunkObject<U, {...U, ...T}, V, V> => (
out: ThunkObject<{...U, ...T}, V>
): ThunkObject<U, V> => {
return {
invoke: (a): V => {
return out.invoke({...a, ...obj});
}
};
};
const bind = <T, U, V, W>(
fn: (a:T) => LiftThunkObject<T, U, V, W>
): LiftThunkObject<T, U, V, W> => {
return (out) => {
return {
invoke: (a) => {
const result = fn(a);
return result(out).invoke(a);
}
}
}
}
/**
* Instant return. No additional thunks will be processed.
*/
const pure = <T>(a: T): LiftThunkObject<*, *, T, *> => (): ThunkObject<*, T> => {
return {
invoke: () => a,
};
};
/**
* Force the next thunk to be called with argument `v`.
*/
const set = <T, V>(
v: T
): LiftThunkObject<*, T, V, V> => (
a: ThunkObject<T, V>
): ThunkObject<*, V> => {
return {
invoke: () => {
return a.invoke(v);
},
};
};
// Alternative `liftAssign` using the above:
// const liftAssign = <T,U, V>(obj: T): LiftThunkObject<U, {...U, ...T}, V, V> =>
// bind((x) => set({...x, ...obj}));
// TODO: Monad-ish? laws:
// compose(bind(set), f) === compose(identity, f);
// compose(set(a), bind(f)) === f(a)
// compose(bind(f), bind(g)) === bind((x) => compose(f(x), bind(g)))
declare var compose: $Compose;
const base = {
// Try changing a.foo to a.bar to a.baz
invoke: (a) => (a.bar + 4).toString(),
};
const example = compose(
liftAssign({bar: 3}),
bind((item) => {
return liftAssign({qux: item.bar + 2});
}),
)(base);
const result = example.invoke({foo: 1});
result * 3;
We could also do away with the object entirely:
/* @flow */
type Thunk<T, U> = (a: T) => U;
type LiftThunk<T, U, V, W> = (out: Thunk<U, W>) => Thunk<T, V>;
const bind = <T, U, V, W>(
fn: (a:T) => LiftThunk<T, U, V, W>
): LiftThunk<T, U, V, W> => {
return (out) => {
return (a) => {
return fn(a)(out)(a);
}
}
}
const pure = <T>(a: T): LiftThunk<*, *, T, *> => (): Thunk<*, T> => {
return () => a;
};
const set = <T, V>(
v: T
): LiftThunk<*, T, V, V> => (
a: Thunk<T, V>
): Thunk<*, V> => {
return () => {
return a(v);
};
};
const liftAssign = <T,U, V>(obj: T): LiftThunk<U, {...U, ...T}, V, V> => bind((x) => set({...x, ...obj}));
// const liftAssign = <T,U, V>(obj: T): LiftThunk<U, {...U, ...T}, V, V> => (out: Thunk<{...U, ...T}, V>): Thunk<U, V> => {
// return (a): V => {
// return out({...a, ...obj});
// };
//};
declare var compose: $Compose;
const base =
// Try change a.foo to a.bar to a.baz
(a) => (a.bar + 4).toString();
const example = compose(
liftAssign({bar: 3}),
bind((item) => {
return liftAssign({qux: item.bar + 2});
}),
)(base);
const result = example({foo: 1});
result * 3;
So a lot of times you'd like to have a resource constructed when the server starts listening, use it during the request lifecycle, and then dispose of it when the server shuts down. Being able to return new apps in listening may be able to allow this.
listening(() => {
const resource = new Thing();
return compose(
assign({resource}),
close(() => resource.dispose()),
);
});
The first argument of error
is always the error, but beyond that there have been some different conventions floating around. Best to standardize on one.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.