Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use subscribe to implement PubSub like postbox #14

Open
nmocruz opened this issue Sep 28, 2018 · 2 comments
Open

use subscribe to implement PubSub like postbox #14

nmocruz opened this issue Sep 28, 2018 · 2 comments
Labels

Comments

@nmocruz
Copy link

nmocruz commented Sep 28, 2018

I was wondering is was a good idea extend the subscribe to subscribe to topics strings like PubSub, and add a publish method that could trigger the callback.

@gnaeus
Copy link
Owner

gnaeus commented Oct 5, 2018

knockout-decorators already have same concept — @event decorator. It also uses ko.subscribable() under the hood. The key difference from knockout postbox that events are strongly typed:

class Events {
  @event first: () => void;
  @event second: (word: string, index: number) => void;
}

export default new Events();
import events from "./events";

subscribe(events.first, () => {
  // do something
});

subscribe(events.second, (word, index) => {
  // TypeScript infers that typeof word === "sting"
  // and typeof index === number;
  console.log(word.charAt(index));
})

@nmocruz
Copy link
Author

nmocruz commented Feb 25, 2019

cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants