Skip to content

Commit

Permalink
fix: flow typing of React API with Flow 0.92 (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
tricoder42 authored Feb 5, 2019
2 parents b289703 + a9bc0b7 commit 684bd70
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
29 changes: 21 additions & 8 deletions packages/react/src/I18nProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ export type I18nProviderProps = {
defaultRender: ?any
}

type LinguiPublisher = {|
i18n: I18n,
i18nHash: null,

getSubscribers(): Array<Function>,

subscribe(callback: Function): void,

unsubscribe(callback: Function): void,

update(?{ catalogs?: Catalogs, language?: string, locales?: string }): void
|}

/*
* I18nPublisher - Connects to lingui-i18n/I18n class
* Allows listeners to subscribe for changes
*/
export function LinguiPublisher(i18n: I18n) {
export function makeLinguiPublisher(i18n: I18n): LinguiPublisher {
let subscribers: Array<Function> = []

return {
Expand All @@ -40,11 +53,11 @@ export function LinguiPublisher(i18n: I18n) {
subscribers = subscribers.filter(cb => cb !== callback)
},

update({
catalogs,
language,
locales
}: { catalogs?: Catalogs, language?: string, locales?: string } = {}) {
update(
params: ?{ catalogs?: Catalogs, language?: string, locales?: string } = {}
) {
if (!params) return
const { catalogs, language, locales } = params
if (!catalogs && !language && !locales) return

if (catalogs) i18n.load(catalogs)
Expand Down Expand Up @@ -81,7 +94,7 @@ export default class I18nProvider extends React.Component<I18nProviderProps> {
locales,
catalogs
})
this.linguiPublisher = new LinguiPublisher(i18n)
this.linguiPublisher = makeLinguiPublisher(i18n)
this.linguiPublisher.i18n._missing = this.props.missing
}

Expand All @@ -106,6 +119,6 @@ export default class I18nProvider extends React.Component<I18nProviderProps> {
}

render() {
return this.props.children
return this.props.children || null
}
}
8 changes: 4 additions & 4 deletions packages/react/src/I18nProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { shallow, mount } from "enzyme"

import { setupI18n } from "@lingui/core"
import { I18nProvider, Trans } from "@lingui/react"
import { LinguiPublisher } from "./I18nProvider"
import { makeLinguiPublisher } from "./I18nProvider"
import { mockConsole } from "./mocks"

describe("I18nProvider", function() {
Expand Down Expand Up @@ -132,7 +132,7 @@ describe("I18nProvider", function() {

describe("I18nPublisher", function() {
it("should pass active language and messages to underlying I18n class", function() {
const linguiPublisher = new LinguiPublisher(
const linguiPublisher = makeLinguiPublisher(
setupI18n({
language: "en",
catalogs: {
Expand Down Expand Up @@ -169,7 +169,7 @@ describe("I18nPublisher", function() {
})

it("should subscribe/unsubscribe listeners for context changes", function() {
const linguiPublisher = new LinguiPublisher(
const linguiPublisher = makeLinguiPublisher(
setupI18n({
language: "en",
catalogs: { en: {} }
Expand All @@ -188,7 +188,7 @@ describe("I18nPublisher", function() {

it("should notify listeners only when relevant data changes", function() {
const listener = jest.fn()
const linguiPublisher = new LinguiPublisher(
const linguiPublisher = makeLinguiPublisher(
setupI18n({
language: "en",
catalogs: { en: {}, fr: {} }
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Render extends React.Component<RenderComponentProps> {
let render = this.props.render || this.context.linguiDefaultRender

if (render === null || render === undefined) {
return value
return value || null
} else if (typeof render === "string") {
// Built-in element: h1, p
return React.createElement(render, { className }, value)
Expand Down

0 comments on commit 684bd70

Please sign in to comment.