Git Product home page Git Product logo

Comments (13)

eddyystop avatar eddyystop commented on August 29, 2024 1

Thanks for this comment.

Work is being done on hooks for event filters and permissions. We'll address this particular issue when that's stabilized.

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

from feathers-hooks-common.

bedeoverend avatar bedeoverend commented on August 29, 2024

Looking back at this - I wonder if this would be an anti-pattern, and instead hooks that need to operate like this should instead attach to resource events e.g. app.on('user:created')? I'm not sure...there is also the benefit of being able to reuse hooks, rather than transform them to use events.

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

Why not setTimeout such an action, binding it to the scope you want?

from feathers-hooks-common.

bedeoverend avatar bedeoverend commented on August 29, 2024

@eddyystop that's essentially what I've done in the demo code at the top of the function. I could definitely just do that each time, but it's just common enough that I'd want to turn it into a reusable function. I understand though if it's consider out of scope for this repo.

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

I'll leave this open.

from feathers-hooks-common.

bedeoverend avatar bedeoverend commented on August 29, 2024

I'll put together a pull request (not much code, and I use it for my own project anyway). That way if you feel it's appropriate, can just merge otherwise close, and the onus is off you guys to do initially implementation

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

The repo has been heavily refactored. Wait till v3 has been published. I suggest defer is too ambiguous a name.

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

Can this be done with a separate Promise.then chain that is not returned?

from feathers-hooks-common.

bedeoverend avatar bedeoverend commented on August 29, 2024

Like:

function defer(handler) {
  return (hook) => {
    Promise.resolve().then(handler);
    return hook;
  }
}

app.service('users').after({
  create: [ defer(notify('User Created')) ]
});

I don't see why not - unless there's a perf consideration with creating a Promise vs running setImmediate. Also a Promise will set it to run on the next tick, whereas setImmediate will set it to run at the start of the next event loop. I'm not familiar enough with the event loop to know how big of a difference that is, but certainly a difference.

In general, I'm having second thoughts as to whether this should be something to include - is it an anti-pattern? Is there a case where this should be used over feathers event system? The only one I can think of is so you can simply re-use hooks, rather than having to rewrite them for event handling.

As for a more specific name - what about, runAsync or onNextTick or something along those lines?

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

The issue with SetImmediate is https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate:

Non-standard
This feature is non-standard and is not on a standards track.
Do not use it on production sites facing the Web: it will not work for every user.
There may also be large incompatibilities between implementations
and the behavior may change in the future.

This method is not expected to become standard,
and is only implemented by recent builds of Internet Explorer and Node.js 0.10+.
It meets resistance both from Gecko (Firefox) and Webkit (Google/Apple).

I believe nextTick is Node-only.

So we can't use either setImmediate or nextTick as hooks can run on the client as well as on the server.

A Promise chain would handle defered async processing without extra code. You'd have to use something like setImmediate or nextTick inside the Promise is the defered processing is all sync. But then we've offloaded that decision onto the developer who knows what platforms the code runs on.

from feathers-hooks-common.

bedeoverend avatar bedeoverend commented on August 29, 2024

Ah sorry, yeah I don't use feathers client side, forgot this should be universally available. Yeah sure, could use setTimeout(callback, 0), but I think a Promise chain would be better; a microtask timing would make more sense I think.

Anyway - definitely happy to run with a Promise chain.

from feathers-hooks-common.

eddyystop avatar eddyystop commented on August 29, 2024

@bedeoverend I took the liberty of adding your (modified) defer hook as the first addition to the new feathers-community-hooks repo. I renamed it to runParallel. Feel free to modify it based on your experience.

I'd be delighted if you were able to contribute more hooks.

Thanks!

from feathers-hooks-common.

Related Issues (20)

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.