Git Product home page Git Product logo

Comments (10)

chrisguttandin avatar chrisguttandin commented on July 4, 2024 1

Thanks a lot for your suggestion. I just updated the README. 41983a3 It looks much better now.

I'm not sure about adding a .map() function. If we do so then someone might ask for a .filter() function next and at some point we end up with a lot of operators which will bloat the bundle for everyone.

Currently the map() operator isn't even public. It's just a test helper for now. https://github.com/chrisguttandin/subscribable-things/blob/41983a368f1936fbd349595d632126f442ea0d77/test/helpers/map.js

I wonder if there is already a collection of operators that could be reused somehow.

from subscribable-things.

chrisguttandin avatar chrisguttandin commented on July 4, 2024 1

Thanks for all the links. It looks like it's a solved problem. ;-) Should we then close this issue? Or am I missing something?

from subscribable-things.

chrisguttandin avatar chrisguttandin commented on July 4, 2024

Hi Dmitry,

there is already a generic function for listening to events.

Here is the part in the README that talks about it: https://github.com/chrisguttandin/subscribable-things#ontarget-eventtarget-type-string-options-boolean--addeventlisteneroptions-subscribablethingevent.

Here is the integration test which probably documents it better:

it('should work with hyperf', async () => {
const test = h`<div id="test">${map(on(htmlElement, 'click'), ({ target }) => target.nodeName)}</div>`;
document.body.appendChild(test);
finalizationRegistry.register(test);
while (true) {
try {
expect(document.getElementById('test').textContent).to.equal('A');
break;
} catch {
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
}
}
document.body.removeChild(test);
});

Does that help with your use case?

from subscribable-things.

dy avatar dy commented on July 4, 2024

Right

import { on, map } from 'subscribable-things'
map(on(window, 'hashchange'), e => window.location.hash)

A couple of off-topics, if you don't mind.

  1. Zen-observable defines .map method for observables, so that it's not necessary to require map. Wdyt?
  2. Now titles in docs are a bit heavy:

on(target: EventTarget, type: string, options?: boolean | AddEventListenerOptions): SubscribableThing

The keywords get lost in type definitions, makes it a bit hard to find.

Would it make sense to making it more lightweight as:

on(target, type, options?)

on(target: EventTarget, type: string, options?: boolean | AddEventListenerOptions): SubscribableThing<Event>

from subscribable-things.

dy avatar dy commented on July 4, 2024

Looks nice! Yeah, I also doubt about map. Observable proposal discussion converges to minimal possible implementation, which makes the most sense.

As for the operators library - good idea. In value-ref I came to Ref.from(anyReactiveSource) method, mimicking Array.from for observables.

But there indeed seems to be missing a small lib for observables with minimal set of operators and reactive sources (value/object/list). Like tiny rxjs, or reactive lodash. Sort of less proprietary vue reactivity.

from subscribable-things.

chrisguttandin avatar chrisguttandin commented on July 4, 2024

Maybe we could create a package called pipeable-operators (the name is still available) which draws some inspiration from RxJS.

It could provide some operators and a pipe() function which takes a SubscribableThing (or any other standards compliant Observable) as a first parameter and a list of operators to apply. The return value would be another SubscribableThing.

const newObservable = pipe(
    oldObservable,
    map((value) => value + 2),
    filter((value) => value % 2 === 0)
);

Like in RxJS an operator would be a function which takes an Observable and returns another one.

But it almost feels unrealistic to me that something like this doesn't exist yet. 🤷‍♂️

from subscribable-things.

dy avatar dy commented on July 4, 2024

I like the idea, but I think that is rxjs/operators we're talking about, unless there's a point for a separate portable lib with minimal set of indispensable ones. map, filter, take, ... That reminds mutant for actual Observables.

Yeah, it can also possibly subscribe to asyncIterables as well? Or that's a separate concern though.

from subscribable-things.

dy avatar dy commented on July 4, 2024

To the point there is:

from subscribable-things.

dy avatar dy commented on July 4, 2024

I guess, yes.

from subscribable-things.

chrisguttandin avatar chrisguttandin commented on July 4, 2024

Alright, I'm going to close it. Please feel free to open a new issue if you have another idea how this library could be extended or improved.

from subscribable-things.

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.