Skip to content

Commit

Permalink
refactor(typealias): add Readonly modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
protoman92 committed Sep 7, 2018
1 parent 5ae57b8 commit 9956c60
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 67 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-base-utilities-js",
"version": "1.4.1",
"version": "1.4.2",
"description": "Utilities for React applications.",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
Expand Down
10 changes: 5 additions & 5 deletions src/hoc.distinctprop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as React from 'react';
import {Component, ComponentType} from 'react';
import {getComponentDisplayName} from './util';

export type DistinctPropsHOCOptions<Props> = {
export type DistinctPropsHOCOptions<Props> = Readonly<{
/**
* If this is not specified, use all keys of the component's props, but
* beware that some keys may be optional.
*/
readonly propKeysForComparison?: (keyof Props)[];
readonly propKeysToIgnore?: (keyof Props)[];
readonly checkEquality: (obj1: unknown, obj2: unknown) => boolean;
};
propKeysForComparison?: (keyof Props)[];
propKeysToIgnore?: (keyof Props)[];
checkEquality: (obj1: unknown, obj2: unknown) => boolean;
}>;

/**
* Filter out duplicate props for some keys.
Expand Down
18 changes: 9 additions & 9 deletions src/hoc.lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as React from 'react';
import {Component, ComponentType, StatelessComponent} from 'react';
import {getComponentDisplayName} from './util';

export type LifecycleHOCHooks = {
readonly onConstruction?: () => void;
readonly componentDidMount?: () => void;
readonly componentWillUnmount?: () => void;
};

export type LifecycleHooksHOCOptions = {
readonly lifecycleHooks: LifecycleHOCHooks;
};
export type LifecycleHOCHooks = Readonly<{
onConstruction?: () => void;
componentDidMount?: () => void;
componentWillUnmount?: () => void;
}>;

export type LifecycleHooksHOCOptions = Readonly<{
lifecycleHooks: LifecycleHOCHooks;
}>;

/**
* Set up hooks for component lifecycle.
Expand Down
15 changes: 8 additions & 7 deletions src/hoc.loadable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import {ComponentType} from 'react';
import * as Loadable from 'react-loadable';
import {Options, OptionsWithoutRender} from 'react-loadable';

export type LoadableHOCHooks = {
readonly beforeComponentLoaded?: () => void;
};
export type LoadableHOCHooks = Readonly<{
beforeComponentLoaded?: () => void;
}>;

export type LoadableHOCOptions<Props, Exports extends object> = {
readonly hooks?: LoadableHOCHooks;
readonly displayName?: string;
} & Options<Props, Exports>;
export type LoadableHOCOptions<Props, Exports extends object> = Readonly<{
hooks?: LoadableHOCHooks;
displayName?: string;
}> &
Options<Props, Exports>;

/**
* Convert a component to Loadable.
Expand Down
27 changes: 16 additions & 11 deletions src/hoc.viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import {NeverProp, Objects, Omit, Types} from 'javascriptutilities';
import * as React from 'react';
import {Component, ComponentType, StatelessComponent} from 'react';
import {getComponentDisplayName} from './util';
import {ReduxType, RootType} from './viewmodel';
import {ReduxViewModel, RootViewModel} from './viewmodel';
export type ViewModelHOCProps<VM> = {readonly viewModel: VM};
export type ViewModelFactoryHOCProps = {readonly viewModelFactory: unknown};

export type FactorifiedViewModelHOCProps<
Props extends ViewModelHOCProps<any>
> = Omit<Props, 'viewModel'> & ViewModelFactoryHOCProps;

export type ViewModelHOCHooks = {
readonly beforeViewModelCreated?: () => void;
};
export type ViewModelHOCHooks = Readonly<{
beforeViewModelCreated?: () => void;
}>;

export type ViewModelHOCOptions<VM, Props extends ViewModelHOCProps<VM>> = {
readonly viewModelHooks?: ViewModelHOCHooks;
readonly createViewModel: (
export type ViewModelHOCOptions<
VM,
Props extends ViewModelHOCProps<VM>
> = Readonly<{
viewModelHooks?: ViewModelHOCHooks;
createViewModel: (
props: Omit<Props, 'viewModel'> & ViewModelFactoryHOCProps
) => VM;
};
}>;

export type TargetViewModelHOCComponent<
VM,
Expand Down Expand Up @@ -69,17 +72,19 @@ export function withViewModel<VM, Props extends ViewModelHOCProps<VM>, State>(
public componentDidMount() {
let {viewModel} = this;

if (Types.isInstance<RootType>(viewModel, 'initialize')) {
if (Types.isInstance<RootViewModel>(viewModel, 'initialize')) {
viewModel.initialize();
}

if (Types.isInstance<ReduxType<State>>(viewModel, 'setUpStateCallback')) {
if (
Types.isInstance<ReduxViewModel<State>>(viewModel, 'setUpStateCallback')
) {
viewModel.setUpStateCallback(state => this.setState(state));
}
}

public componentWillUnmount() {
if (Types.isInstance<RootType>(this.viewModel, 'deinitialize')) {
if (Types.isInstance<RootViewModel>(this.viewModel, 'deinitialize')) {
this.viewModel.deinitialize();
}
}
Expand Down
38 changes: 7 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
export {
CompleteSetupHOCOptions,
withCompleteSetup,
withTestCompleteSetup,
} from './hoc.all';
export {DistinctPropsHOCOptions, withDistinctProps} from './hoc.distinctprop';
export {
LifecycleHOCHooks,
LifecycleHooksHOCOptions,
withLifecycleHooks,
} from './hoc.lifecycle';
export {
LoadableHOCOptions,
LoadableHOCHooks,
withLoadable,
withTestLoadable,
} from './hoc.loadable';
export {
FactorifiedViewModelHOCProps,
TargetViewModelHOCComponent,
ViewModelFactoryHOCProps,
ViewModelHOCOptions,
ViewModelHOCProps,
ViewModelHOCHooks,
withViewModel,
} from './hoc.viewmodel';
export {getComponentDisplayName} from './util';
export {
ReduxType as ReduxViewModel,
RootType as RootViewModel,
} from './viewmodel';
export * from './hoc.all';
export * from './hoc.distinctprop';
export * from './hoc.lifecycle';
export * from './hoc.loadable';
export * from './hoc.viewmodel';
export * from './util';
export * from './viewmodel';
6 changes: 3 additions & 3 deletions src/viewmodel.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Root view model type.
*/
export interface RootType {
export interface RootViewModel {
initialize(): void;
deinitialize(): void;
}

/**
* Provide the relevant state stream for the component state.
* @extends {RootType} Root type extension.
* @extends {RootViewModel} Root type extension.
* @template State State generics.
*/
export interface ReduxType<State> extends RootType {
export interface ReduxViewModel<State> extends RootViewModel {
/**
* Listen to state changes.
* @param {(state: State) => void} callback A state callback.
Expand Down

0 comments on commit 9956c60

Please sign in to comment.