Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Convert sources reducer to ES modules (#2736)
Browse files Browse the repository at this point in the history
* Convert reducers/sources to es modules

* Convert tests for reducers/sources to es modules

* Remove type for sources reducer's state

* Set sources reducer's state argument's type to any

Without Flow complained about missing annotation. After setting it to
`SourcesState` it complained about unrecognized methods, which I suspect
come from Immutable and are not defined and `SourcesState`.

Since this PR isn't about type-checking, I left it at `any`.

* Use Record notation for sources reducer's argument type
  • Loading branch information
virzen authored and irfanhudda committed Apr 25, 2017
1 parent 5b651fe commit 0eaef81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
50 changes: 19 additions & 31 deletions src/reducers/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* @module reducers/sources
*/

const I = require("immutable");
const makeRecord = require("../utils/makeRecord");
const { getPrettySourceURL } = require("../utils/source");
const { prefs } = require("../utils/prefs");
import * as I from "immutable";
import makeRecord from "../utils/makeRecord";
import { getPrettySourceURL } from "../utils/source";
import { prefs } from "../utils/prefs";

import type { Source, Location } from "../types";
import type { Action } from "../actions/types";
Expand All @@ -34,7 +34,7 @@ export type SourcesState = {
tabs: I.List<any>
};

const State = makeRecord(
export const State = makeRecord(
({
sources: I.Map(),
selectedLocation: undefined,
Expand All @@ -44,7 +44,10 @@ const State = makeRecord(
}: SourcesState)
);

function update(state = State(), action: Action): Record<SourcesState> {
export function update(
state: Record<SourcesState> = State(),
action: Action
): Record<SourcesState> {
let availableTabs = null;
let location = null;

Expand Down Expand Up @@ -270,33 +273,33 @@ function getNewSelectedSourceId(state: SourcesState, availableTabs): string {
// (right now) to type those wrapped functions.
type OuterState = { sources: Record<SourcesState> };

function getSource(state: OuterState, id: string) {
export function getSource(state: OuterState, id: string) {
return state.sources.sources.get(id);
}

function getSourceByURL(state: OuterState, url: string) {
export function getSourceByURL(state: OuterState, url: string) {
return state.sources.sources.find(source => source.get("url") == url);
}

function getSourceById(state: OuterState, id: string) {
export function getSourceById(state: OuterState, id: string) {
return state.sources.sources.find(source => source.get("id") == id);
}

function getSources(state: OuterState) {
export function getSources(state: OuterState) {
return state.sources.sources;
}

function getSourceText(state: OuterState, id: ?string) {
export function getSourceText(state: OuterState, id: ?string) {
if (id) {
return state.sources.sourcesText.get(id);
}
}

function getSourceTabs(state: OuterState) {
export function getSourceTabs(state: OuterState) {
return state.sources.tabs.filter(tab => getSourceByURL(state, tab));
}

function getSelectedSource(state: OuterState) {
export function getSelectedSource(state: OuterState) {
const selectedLocation = state.sources.selectedLocation;
if (!selectedLocation) {
return;
Expand All @@ -307,34 +310,19 @@ function getSelectedSource(state: OuterState) {
);
}

function getSelectedLocation(state: OuterState) {
export function getSelectedLocation(state: OuterState) {
return state.sources.selectedLocation;
}

function getPendingSelectedLocation(state: OuterState) {
export function getPendingSelectedLocation(state: OuterState) {
return state.sources.pendingSelectedLocation;
}

function getPrettySource(state: OuterState, id: string) {
export function getPrettySource(state: OuterState, id: string) {
const source = getSource(state, id);
if (!source) {
return;
}

return getSourceByURL(state, getPrettySourceURL(source.get("url")));
}

module.exports = {
State,
update,
getSource,
getSourceByURL,
getSourceById,
getSources,
getSourceText,
getSourceTabs,
getSelectedSource,
getSelectedLocation,
getPendingSelectedLocation,
getPrettySource
};
6 changes: 3 additions & 3 deletions src/reducers/tests/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
declare var describe: (name: string, func: () => void) => void;
declare var it: (desc: string, func: () => void) => void;

const { State, update } = require("../sources");
const { foobar } = require("../../test/fixtures");
import { State, update } from "../sources";
import { foobar } from "../../test/fixtures";
const fakeSources = foobar.sources.sources;
const expect = require("expect.js");
import expect from "expect.js";

describe("sources reducer", () => {
it("should work", () => {
Expand Down

0 comments on commit 0eaef81

Please sign in to comment.