Giter Club home page Giter Club logo

midori's People

Contributors

10xjs avatar izaakschroeder avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

tchon shahmalav

midori's Issues

Support selector `finally` function.

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);
});

Allow matchers to use selectors.

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.

Investigate flow reified types.

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 ThunkObjects 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;

Support "resources" in `listening`.

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()),
  );
});

Standardize `error` arguments.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.