Skip to content

Instantly share code, notes, and snippets.

View radzserg's full-sized avatar

Sergey Radzishevskii radzserg

View GitHub Profile
@withRouter()
class HomePage extends PureComponent<RouteComponentProps> {
render() {
return (
<a onClick={this.props.history.push("/catalog")}>Catalog</a>
);
}
}
// now I wanna make a snapshot testing
export default function withLogger<T>(
WrappedComponent: React.ComponentType<T>
): React.ComponentType<T> {
const name = WrappedComponent.displayName || WrappedComponent.name;
class InjectionWrapper extends React.Component<any, any> {
static contextType = LoggerContext;
static displayName = `withLogger(${name})`;
static WrappedComponent = WrappedComponent;
<ErrorBoundary>
<ApolloProvider client={apolloClient}>
<Provider store={reduxStore}>
<Router history={browserHistory}>
<Translation>
<CookiesProvider>
...
...
...
<App />
class App extends PureComponent {
render() {
const browserHistory = createBrowserHistory();
return (
<Router history={browserHistory}>
<Switch>
<Route path="/" component={HomePage} exact={true} />
</Switch>
</Router>
);
import * as React from "react";
import { PureComponent } from "react";
interface IProps {
label: string;
doSomeMagic: () => void;
}
export default class Foo extends PureComponent<IProps> {
render() {
defmodule App.EventDispatcher do
use Rsed.EventDispatcher, name: :my_app_ed
def configure() do
%{}
|> Rsed.ListenersBag.add_subscriber(App.Users.UserRegistrationSubscriber)
|> Rsed.ListenersBag.add_listener(:user_sign_up, {App.Users.RegistrationEmail, :send_email}, 0)
end
end
defmodule Rsed.EventDispatcher do
defmacro __using__(opts) do
name = Keyword.get(opts, :name)
quote do
use GenServer
# rest of our functions
defp my_name() do
unquote(name)
defmodule App.Application do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(App.Repo, [])
supervisor(App.Web.Endpoint, []),
worker(Rsed.EventDispatcher, []) # adds our event dispatcher
@radzserg
radzserg / event_dispatcher_3.ex
Created January 15, 2019 12:03
dispatch events
defmodule Rsed.Event do
@enforce_keys [:name]
defstruct [:name, :data]
@type t :: %Rsed.Event{
name: String.t(),
data: any(),
}
end
expected_listeners = %{
test_event_2: [
{Rsed.Test.TestSubscriber2, :handler_2, 0},
{Rsed.Test.TestSubscriber, :handler_2, 100}
],
test_event_5: [{Rsed.Test.TestSubscriber, :handler_5, 100}],
test_event_3: [{Rsed.Test.TestSubscriber2, :test_event_3_handler, 0}],
test_event_4: [
{Rsed.Test.TestSubscriber2, :handler_5, 100},
{Rsed.Test.TestSubscriber2, :handler_6, 200}