From 4162a1c169550a1987ec03ebe6246761f2b84374 Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Tue, 22 Jan 2019 11:27:54 -0200 Subject: [PATCH] chore(docz): remove callbags for state subcribe --- core/docz/src/utils/createState.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/docz/src/utils/createState.tsx b/core/docz/src/utils/createState.tsx index f2fcb3e17..fc4040583 100644 --- a/core/docz/src/utils/createState.tsx +++ b/core/docz/src/utils/createState.tsx @@ -1,8 +1,6 @@ import * as React from 'react' import { createContext, Component } from 'react' import equal from 'fast-deep-equal' -import observe from 'callbag-observe' -import makeSubject from 'callbag-subject' export interface ProviderProps { initial?: T @@ -10,29 +8,35 @@ export interface ProviderProps { export type PrevState = (prevState: T) => T export type GetFn = (state: T) => React.ReactNode +export type Dispatch = T | PrevState export interface State { get: (fn: GetFn) => React.ReactNode - set: (param: T | PrevState) => void + set: (param: Dispatch) => void Provider: React.ComponentType> } -const subject = makeSubject() export function create(initial: T = {} as T): State { const { Provider, Consumer }: any = createContext(initial) + const listeners = new Set() + Consumer.displayName = 'StateConsumer' + const dispatch = (fn: Dispatch) => { + listeners.forEach(listener => listener(fn)) + } + return { get: fn => {fn}, - set: fn => subject(1, fn), + set: fn => dispatch(fn), Provider: class CustomProvider extends Component, T> { public static displayName = 'StateProvider' public state: T = this.props.initial || initial public componentDidMount(): void { - observe((fn: T) => this.setState(fn))(subject) + listeners.add((fn: Dispatch) => this.setState(fn)) } public componentWillUnmount(): void { - subject(2) + listeners.clear() } public shouldComponentUpdate(nextProps: any, nextState: any): boolean { return !equal(this.state, nextState)