Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rematch/select

Package Overview
Dependencies
Maintainers
3
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rematch/select - npm Package Compare versions

Comparing version 3.0.0-next.6 to 3.0.0-next.7

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

# [3.0.0-next.7](https://github.com/rematch/rematch/compare/@rematch/select@3.0.0-next.6...@rematch/select@3.0.0-next.7) (2020-11-30)
### Bug Fixes
* @rematch/select typescript plugin compatibility ([#828](https://github.com/rematch/rematch/issues/828)) ([61890ca](https://github.com/rematch/rematch/commit/61890ca645ca1aa44bb375b819ee1d4e4316f9e1))
# [3.0.0-next.6](https://github.com/rematch/rematch/compare/@rematch/select@3.0.0-next.5...@rematch/select@3.0.0-next.6) (2020-10-08)

@@ -8,0 +19,0 @@

4

dist/index.d.ts

@@ -1,6 +0,6 @@

import { Plugin } from '@rematch/core';
import { Models, Plugin } from '@rematch/core';
import { SelectConfig } from './types';
declare const createSelectPlugin: (config?: SelectConfig) => Plugin;
declare const createSelectPlugin: <TModels extends Models<TModels>, TExtraModels extends Models<TModels> = Record<string, any>>(config?: SelectConfig<TModels>) => Plugin<TModels, TExtraModels, Partial<TExtraModels>>;
export default createSelectPlugin;
export * from './types';
//# sourceMappingURL=index.d.ts.map

@@ -67,3 +67,3 @@ 'use strict';

{
if (typeof selectorFactories[selectorName] !== 'function') {
if (typeof (selectorFactories === null || selectorFactories === void 0 ? void 0 : selectorFactories[selectorName]) !== 'function') {
throw new Error("Selector (" + model.name + "/" + selectorName + ") must be a function");

@@ -82,3 +82,3 @@ }

var sliceState = config.sliceState || function (state, model) {
return state[model.name];
return state[model.name || ''];
};

@@ -129,3 +129,4 @@

delete select[model.name][selectorName];
return select[model.name][selectorName] = selectorFactories[selectorName].call(select[model.name], select);
select[model.name][selectorName] = selectorFactories[selectorName].call(select[model.name], select);
return select[model.name][selectorName];
};

@@ -132,0 +133,0 @@

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("reselect");Object.defineProperty(exports,"createSelector",{enumerable:!0,get:function(){return e.createSelector}}),Object.defineProperty(exports,"createStructuredSelector",{enumerable:!0,get:function(){return e.createStructuredSelector}}),exports.default=function(t){void 0===t&&(t={});var r,n,c=t.sliceState||function(e,t){return e[t.name]},u=t.selectorCreator||e.createSelector,o=function(e){return function(t){var r=this;return u((function(e){return e}),(function(n){return e.call(r,t,n)}))}},i=(r=!1,n=new Set,{add:function(e){e.forEach(r?function(e){return e()}:function(e){return n.add(e)})},finish:function(e){n.delete(e)},startBuilding:function(){r=!0,n.forEach((function(e){return e()}))}}),f=function t(r,n){void 0===n&&(n=e.createStructuredSelector);var c=function(e,u){return(c=n(r(t)))(e,u)};return function(e,t){return c(e,t)}};return{exposed:{select:f,sliceState:c,selectorCreator:u},onModel:function(e){f[e.name]={};var t="function"==typeof e.selectors?e.selectors(function(e){return function(t){return"function"==typeof t?u((function(t){return c(t,e)}),t):c(t,e)}}(e),u,o):e.selectors;i.add(Object.keys(t||{}).map((function(r){var n=function n(){return i.finish(n),delete f[e.name][r],f[e.name][r]=t[r].call(f[e.name],f)};return Object.defineProperty(f[e.name],r,{configurable:!0,get:function(){return n()}}),n})))},onStoreCreated:function(e){i.startBuilding(),e.select=f}}};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("reselect");Object.defineProperty(exports,"createSelector",{enumerable:!0,get:function(){return e.createSelector}}),Object.defineProperty(exports,"createStructuredSelector",{enumerable:!0,get:function(){return e.createStructuredSelector}}),exports.default=function(t){void 0===t&&(t={});var r,n,c=t.sliceState||function(e,t){return e[t.name||""]},u=t.selectorCreator||e.createSelector,o=function(e){return function(t){var r=this;return u((function(e){return e}),(function(n){return e.call(r,t,n)}))}},i=(r=!1,n=new Set,{add:function(e){e.forEach(r?function(e){return e()}:function(e){return n.add(e)})},finish:function(e){n.delete(e)},startBuilding:function(){r=!0,n.forEach((function(e){return e()}))}}),f=function t(r,n){void 0===n&&(n=e.createStructuredSelector);var c=function(e,u){return(c=n(r(t)))(e,u)};return function(e,t){return c(e,t)}};return{exposed:{select:f,sliceState:c,selectorCreator:u},onModel:function(e){f[e.name]={};var t="function"==typeof e.selectors?e.selectors(function(e){return function(t){return"function"==typeof t?u((function(t){return c(t,e)}),t):c(t,e)}}(e),u,o):e.selectors;i.add(Object.keys(t||{}).map((function(r){var n=function n(){return i.finish(n),delete f[e.name][r],f[e.name][r]=t[r].call(f[e.name],f)};return Object.defineProperty(f[e.name],r,{configurable:!0,get:function(){return n()}}),n})))},onStoreCreated:function(e){i.startBuilding(),e.select=f}}};
//# sourceMappingURL=select.cjs.production.min.js.map

@@ -64,3 +64,3 @@ import { createSelector, createStructuredSelector } from 'reselect';

if (process.env.NODE_ENV !== 'production') {
if (typeof selectorFactories[selectorName] !== 'function') {
if (typeof (selectorFactories === null || selectorFactories === void 0 ? void 0 : selectorFactories[selectorName]) !== 'function') {
throw new Error("Selector (" + model.name + "/" + selectorName + ") must be a function");

@@ -79,3 +79,3 @@ }

var sliceState = config.sliceState || function (state, model) {
return state[model.name];
return state[model.name || ''];
};

@@ -126,3 +126,4 @@

delete select[model.name][selectorName];
return select[model.name][selectorName] = selectorFactories[selectorName].call(select[model.name], select);
select[model.name][selectorName] = selectorFactories[selectorName].call(select[model.name], select);
return select[model.name][selectorName];
};

@@ -129,0 +130,0 @@

@@ -67,3 +67,3 @@ (function (global, factory) {

{
if (typeof selectorFactories[selectorName] !== 'function') {
if (typeof (selectorFactories === null || selectorFactories === void 0 ? void 0 : selectorFactories[selectorName]) !== 'function') {
throw new Error("Selector (" + model.name + "/" + selectorName + ") must be a function");

@@ -82,3 +82,3 @@ }

var sliceState = config.sliceState || function (state, model) {
return state[model.name];
return state[model.name || ''];
};

@@ -129,3 +129,4 @@

delete select[model.name][selectorName];
return select[model.name][selectorName] = selectorFactories[selectorName].call(select[model.name], select);
select[model.name][selectorName] = selectorFactories[selectorName].call(select[model.name], select);
return select[model.name][selectorName];
};

@@ -132,0 +133,0 @@

@@ -1,2 +0,2 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("reselect")):"function"==typeof define&&define.amd?define(["exports","reselect"],t):t((e=e||self)["@rematch/select"]={},e.reselect)}(this,(function(e,t){"use strict";Object.defineProperty(e,"createSelector",{enumerable:!0,get:function(){return t.createSelector}}),Object.defineProperty(e,"createStructuredSelector",{enumerable:!0,get:function(){return t.createStructuredSelector}}),e.default=function(e){void 0===e&&(e={});var n,r,c=e.sliceState||function(e,t){return e[t.name]},o=e.selectorCreator||t.createSelector,u=function(e){return function(t){var n=this;return o((function(e){return e}),(function(r){return e.call(n,t,r)}))}},i=(n=!1,r=new Set,{add:function(e){e.forEach(n?function(e){return e()}:function(e){return r.add(e)})},finish:function(e){r.delete(e)},startBuilding:function(){n=!0,r.forEach((function(e){return e()}))}}),f=function e(n,r){void 0===r&&(r=t.createStructuredSelector);var c=function(t,o){return(c=r(n(e)))(t,o)};return function(e,t){return c(e,t)}};return{exposed:{select:f,sliceState:c,selectorCreator:o},onModel:function(e){f[e.name]={};var t="function"==typeof e.selectors?e.selectors(function(e){return function(t){return"function"==typeof t?o((function(t){return c(t,e)}),t):c(t,e)}}(e),o,u):e.selectors;i.add(Object.keys(t||{}).map((function(n){var r=function r(){return i.finish(r),delete f[e.name][n],f[e.name][n]=t[n].call(f[e.name],f)};return Object.defineProperty(f[e.name],n,{configurable:!0,get:function(){return r()}}),r})))},onStoreCreated:function(e){i.startBuilding(),e.select=f}}},Object.defineProperty(e,"__esModule",{value:!0})}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("reselect")):"function"==typeof define&&define.amd?define(["exports","reselect"],t):t((e=e||self)["@rematch/select"]={},e.reselect)}(this,(function(e,t){"use strict";Object.defineProperty(e,"createSelector",{enumerable:!0,get:function(){return t.createSelector}}),Object.defineProperty(e,"createStructuredSelector",{enumerable:!0,get:function(){return t.createStructuredSelector}}),e.default=function(e){void 0===e&&(e={});var n,r,c=e.sliceState||function(e,t){return e[t.name||""]},o=e.selectorCreator||t.createSelector,u=function(e){return function(t){var n=this;return o((function(e){return e}),(function(r){return e.call(n,t,r)}))}},i=(n=!1,r=new Set,{add:function(e){e.forEach(n?function(e){return e()}:function(e){return r.add(e)})},finish:function(e){r.delete(e)},startBuilding:function(){n=!0,r.forEach((function(e){return e()}))}}),f=function e(n,r){void 0===r&&(r=t.createStructuredSelector);var c=function(t,o){return(c=r(n(e)))(t,o)};return function(e,t){return c(e,t)}};return{exposed:{select:f,sliceState:c,selectorCreator:o},onModel:function(e){f[e.name]={};var t="function"==typeof e.selectors?e.selectors(function(e){return function(t){return"function"==typeof t?o((function(t){return c(t,e)}),t):c(t,e)}}(e),o,u):e.selectors;i.add(Object.keys(t||{}).map((function(n){var r=function r(){return i.finish(r),delete f[e.name][n],f[e.name][n]=t[n].call(f[e.name],f)};return Object.defineProperty(f[e.name],n,{configurable:!0,get:function(){return r()}}),r})))},onStoreCreated:function(e){i.startBuilding(),e.select=f}}},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=select.umd.production.min.js.map

@@ -1,34 +0,54 @@

import { Models, RematchRootState } from '@rematch/core';
import { ExtractRematchStateFromModels, Models, Model, Action, RematchRootState, ModelReducers, ModelEffects, ModelEffectsCreator } from '@rematch/core';
import * as Reselect from 'reselect';
import { Store as ReduxStore, Reducer as ReduxReducer } from 'redux';
export { createSelector, createStructuredSelector } from 'reselect';
export interface SelectConfig {
sliceState?: any;
export declare type Selector<TState, TReturns, TProps = void> = TProps extends void ? (state: TState) => TReturns : (state: TState, props: TProps) => TReturns;
export declare type ExtractSelectorsFromModel<TModels extends Models<TModels> = Record<string, any>, TModel extends Model<TModels> = Model<TModels>> = TModel['selectors'] extends (...args: any[]) => infer TReturnObj ? TReturnObj : TModel['selectors'] extends Record<string, any> ? TModel['selectors'] : never;
export declare type ExtractSelectorsSignatureFromSelectorsModel<TRootState, TSelectorsConfigObject, TKey extends keyof TSelectorsConfigObject> = TSelectorsConfigObject[TKey] extends (...args: any[]) => infer TSelector ? TSelector extends (props: infer TProps) => Selector<any, infer TReturns> ? (props: TProps) => Selector<TRootState, TReturns> : TSelector extends Selector<any, infer TReturns> ? Selector<TRootState, TReturns> : TSelector extends Selector<any, infer TReturns, infer TProps> ? Selector<TRootState, TReturns, TProps> : never : never;
export interface SelectConfig<TModels extends Models<TModels> = Record<string, any>> {
sliceState?: (state: ExtractRematchStateFromModels<TModels>, model: Model<TModels>) => Record<string, any> | undefined;
selectorCreator?: any;
}
export declare type Selector<S, P = any, R = any> = Reselect.Selector<S, R> & Reselect.ParametricSelector<S, P, R>;
export interface ModelSelectors<S> {
[key: string]: Selector<S>;
export declare type ModelSelectors<TModels extends Models<TModels> = Record<string, any>, TModel extends Model<TModels> = Model<TModels>> = {
[key in keyof ExtractSelectorsFromModel<TModels, TModel>]: ExtractSelectorsSignatureFromSelectorsModel<RematchRootState<TModels>, ExtractSelectorsFromModel<TModels, TModel>, key>;
};
export declare type Slicer<TSliceState, TRootState> = (<TReturns>(resultFn: (slice: TSliceState) => TReturns) => Selector<TRootState, TReturns>) & Selector<TRootState, TSliceState>;
export interface ModelSelectorFactories<TModels extends Models<TModels> = Record<string, any>, TSliceState = any> {
[key: string]: SelectorFactory<TModels, TSliceState> | SelectorParametricFactory<TModels, TSliceState>;
}
export interface StoreSelectors<S> {
[key: string]: ModelSelectors<S>;
}
export declare type SelectorFactory<S, P = any, R = any> = (this: ModelSelectors<S>, models: StoreSelectors<S>) => Selector<S, P, R>;
export declare type SelectorParametricFactory<S, P = any> = (this: ModelSelectors<S>, models: StoreSelectors<S>, props: P) => Selector<S>;
export declare type Slicer<S, M = any, R = any> = Selector<S, void, M> & ((resultFn: (slice: M) => R) => Selector<S, void, R>);
export declare type SelectorCreator = typeof Reselect.createSelector;
export declare type Parameterizer<S, P = any> = (factory: SelectorParametricFactory<S, P>) => SelectorFactory<P, void, Selector<S>>;
export interface ModelSelectorFactories<S> {
[key: string]: SelectorFactory<S> | SelectorParametricFactory<S>;
}
export declare type ModelSelectorsFactory<S> = (slice: Slicer<S>, createSelector: SelectorCreator, hasProps: Parameterizer<S>) => ModelSelectorFactories<S>;
export declare type ModelSelectorsConfig<S> = ModelSelectorsFactory<S> | ModelSelectorFactories<S>;
export declare type RematchSelect<M extends object = Models, RootState = any> = ((mapSelectToProps: (select: RematchSelect<M, RootState>) => object) => Reselect.OutputParametricSelector<RootState, any, object, Reselect.Selector<RootState, object>>) & StoreSelectors<RootState>;
export declare type StoreSelectors<TModels extends Models<TModels> = Record<string, any>> = {
[TModelKey in keyof TModels]: ModelSelectors<TModels, TModels[TModelKey]>;
};
export declare type SelectorFactory<TModels extends Models<TModels> = Record<string, any>, TSliceState = any> = <TReturns>(this: ModelSelectorFactories<TModels, TSliceState>, models: StoreSelectors<TModels>) => Selector<RematchRootState<TModels>, TReturns>;
export declare type SelectorParametricFactory<TModels extends Models<TModels> = Record<string, any>, TSliceState = any, TProps = any, TReturns = any> = (this: ModelSelectorFactories<TModels, TSliceState>, models: StoreSelectors<TModels>, props: TProps) => Selector<RematchRootState<TModels>, TReturns, TProps>;
export declare type ParameterizerSelectorFactory<TModels extends Models<TModels> = Record<string, any>, TSliceState = any, TProps = any, TReturns = any> = (this: ModelSelectorFactories<TModels, TSliceState>, models: StoreSelectors<TModels>, props: TProps) => (props: TProps) => Selector<RematchRootState<TModels>, TReturns>;
export declare type Parameterizer<TModels extends Models<TModels> = Record<string, any>, TSliceState = any> = <TProps, TReturns>(factory: SelectorParametricFactory<TModels, TSliceState, TProps, TReturns>) => ParameterizerSelectorFactory<TModels, TSliceState, TProps, TReturns>;
export declare type ModelSelectorsFactory<TModels extends Models<TModels> = Record<string, any>, TSliceState = any> = (slice: Slicer<TSliceState, RematchRootState<TModels>>, createSelector: typeof Reselect.createSelector, hasProps: Parameterizer<TModels, TSliceState>) => ModelSelectorFactories<TModels, TSliceState>;
export declare type ModelSelectorsConfig<TModels extends Models<TModels> = Record<string, any>, TSliceState = any> = ModelSelectorsFactory<TModels, TSliceState> | ModelSelectorFactories<TModels, TSliceState>;
export declare type RematchSelect<TModels extends Models<TModels>, TRootState = RematchRootState<TModels>> = (<TReturn>(mapSelectToProps: (select: RematchSelect<TModels>) => TReturn) => Reselect.OutputParametricSelector<TRootState, any, TReturn, Reselect.Selector<TRootState, Record<string, any>>> & Reselect.OutputSelector<TRootState, TReturn, Reselect.Selector<TRootState, Record<string, any>>>) & StoreSelectors<TModels>;
declare module '@rematch/core' {
interface Model<TModels extends Models<TModels> = Record<string, any>, TState = any, TBaseState = TState> {
selectors?: ModelSelectorsConfig<TState>;
interface RematchStore<TModels extends Models<TModels> = Record<string, any>, TExtraModels extends Models<TModels> = {}> extends ReduxStore<RematchRootState<TModels, TExtraModels>, Action> {
select: RematchSelect<TModels>;
}
interface RematchStore<TModels> {
select: RematchSelect<TModels, RematchRootState<TModels>>;
interface Model<TModels extends Models<TModels> = Record<string, any>, TState = any> {
selectors?: ModelSelectorsConfig<TModels, TState>;
}
interface ModelCreator {
<RM extends Models<RM>>(): <R extends ModelReducers<S>, BR extends ReduxReducer<BS>, E extends ModelEffects<RM> | ModelEffectsCreator<RM>, SE extends ModelSelectorsConfig<RM, S>, S, BS = S>(mo: {
name?: string;
state: S;
selectors?: SE;
reducers?: R;
baseReducer?: BR;
effects?: E;
}) => {
name?: string;
state: S;
selectors: SE;
reducers: R;
baseReducer: BR;
effects: E;
};
}
}
//# sourceMappingURL=types.d.ts.map
{
"name": "@rematch/select",
"version": "3.0.0-next.6",
"version": "3.0.0-next.7",
"description": "Selectors plugin for Rematch",

@@ -30,6 +30,8 @@ "homepage": "https://rematch.github.io/rematch",

"devDependencies": {
"@rematch/core": "^2.0.0-next.6"
"@rematch/core": "^2.0.0-next.7",
"redux": "^4.0.0"
},
"peerDependencies": {
"@rematch/core": ">=2.0.0-next.0"
"@rematch/core": ">=2.0.0-next.0",
"redux": "^4.0.0"
},

@@ -57,3 +59,3 @@ "husky": {

},
"gitHead": "b4abe68e0c1b244b551cc5a093dfb3419337e882"
"gitHead": "8012cdf311bddfb69d9ba46a20c98d89f5442e4c"
}

@@ -1,3 +0,8 @@

// @ts-nocheck
import { NamedModel, Plugin } from '@rematch/core'
import {
ExtractRematchStateFromModels,
Model,
Models,
Plugin,
RematchStore,
} from '@rematch/core'
import { createSelector, createStructuredSelector } from 'reselect'

@@ -18,3 +23,3 @@ import { SelectConfig } from './types'

) {
let func = (state, props) => {
let func = (state: any, props: any): any => {
func = structuredSelectorCreator(mapSelectToStructure(select))

@@ -24,3 +29,3 @@ return func(state, props)

return (state, props) => func(state, props)
return (state: any, props: any) => func(state, props)
}

@@ -35,10 +40,10 @@

return {
add(added) {
add(added: any) {
if (!ready) {
added.forEach((factory) => factories.add(factory))
added.forEach((factory: any) => factories.add(factory))
} else {
added.forEach((factory) => factory())
added.forEach((factory: any) => factory())
}
},
finish(factory) {
finish(factory: any) {
factories.delete(factory)

@@ -48,3 +53,3 @@ },

ready = true
factories.forEach((factory) => factory())
factories.forEach((factory: any) => factory())
},

@@ -54,3 +59,5 @@ }

const validateConfig = (config: SelectConfig): void => {
const validateConfig = <TModels extends Models<TModels>>(
config: SelectConfig<TModels>
): void => {
if (process.env.NODE_ENV !== 'production') {

@@ -70,5 +77,9 @@ if (config.sliceState && typeof config.sliceState !== 'function') {

const validateSelector = (selectorFactories, selectorName, model): void => {
const validateSelector = (
selectorFactories: any,
selectorName: any,
model: any
): void => {
if (process.env.NODE_ENV !== 'production') {
if (typeof selectorFactories![selectorName] !== 'function') {
if (typeof selectorFactories?.[selectorName] !== 'function') {
throw new Error(

@@ -81,11 +92,23 @@ `Selector (${model.name}/${selectorName}) must be a function`

const createSelectPlugin = (config: SelectConfig = {}): Plugin => {
const createSelectPlugin = <
TModels extends Models<TModels>,
TExtraModels extends Models<TModels> = Record<string, any>
>(
config: SelectConfig<TModels> = {}
): Plugin<TModels, TExtraModels> => {
validateConfig(config)
const sliceState = config.sliceState || ((state, model) => state[model.name])
const sliceState: SelectConfig<TModels>['sliceState'] =
config.sliceState || ((state, model) => state[model.name || ''])
const selectorCreator = config.selectorCreator || createSelector
const slice = (model) => (stateOrNext) => {
const slice = (model: Model<TModels>) => (
stateOrNext: ExtractRematchStateFromModels<TModels>
) => {
if (typeof stateOrNext === 'function') {
return selectorCreator((state) => sliceState(state, model), stateOrNext)
return selectorCreator(
(state: ExtractRematchStateFromModels<TModels>) =>
sliceState(state, model),
stateOrNext
)
}

@@ -95,7 +118,7 @@ return sliceState(stateOrNext, model)

const hasProps = (inner) =>
function (models) {
const hasProps = (inner: any) =>
function (this: any, models: any) {
return selectorCreator(
(props) => props,
(props) => inner.call(this, models, props)
(props: any) => props,
(props: any) => inner.call(this, models, props)
)

@@ -111,6 +134,8 @@ }

select,
// @ts-ignore
sliceState,
selectorCreator,
},
onModel(model: NamedModel): void {
onModel(model: Model<TModels>) {
// @ts-ignore
select[model.name] = {}

@@ -120,3 +145,4 @@

typeof model.selectors === 'function'
? model.selectors(slice(model), selectorCreator, hasProps)
? // @ts-ignore
model.selectors(slice(model), selectorCreator, hasProps)
: model.selectors

@@ -130,10 +156,15 @@

factoryGroup.finish(factory)
// @ts-ignore
delete select[model.name][selectorName]
// eslint-disable-next-line no-return-assign
return (select[model.name][selectorName] = selectorFactories[
// @ts-ignore
select[model.name][selectorName] = selectorFactories[
selectorName
].call(select[model.name], select))
// @ts-ignore
].call(select[model.name], select)
// @ts-ignore
return select[model.name][selectorName]
}
// Define a getter for early constructing
// @ts-ignore
Object.defineProperty(select[model.name], selectorName, {

@@ -150,4 +181,6 @@ configurable: true,

},
onStoreCreated(store): void {
// @ts-ignore
onStoreCreated(store: RematchStore) {
factoryGroup.startBuilding()
// @ts-ignore
store.select = select

@@ -154,0 +187,0 @@ },

@@ -1,80 +0,212 @@

// Type definitions for @rematch/select 3.0.0
// Type definitions for @rematch/select 2.0.0
// Definitions by: Sam Richard <https://github.com/d3dc>
import { Models, RematchRootState } from '@rematch/core'
import {
ExtractRematchStateFromModels,
Models,
Model,
Action,
RematchRootState,
ModelReducers,
ModelEffects,
ModelEffectsCreator,
} from '@rematch/core'
import * as Reselect from 'reselect'
import { Store as ReduxStore, Reducer as ReduxReducer } from 'redux'
export { createSelector, createStructuredSelector } from 'reselect'
export interface SelectConfig {
sliceState?: any
export type Selector<TState, TReturns, TProps = void> = TProps extends void
? (state: TState) => TReturns
: (state: TState, props: TProps) => TReturns
export type ExtractSelectorsFromModel<
TModels extends Models<TModels> = Record<string, any>,
TModel extends Model<TModels> = Model<TModels>
> =
// thunk case: (models) => ({...})
TModel['selectors'] extends (...args: any[]) => infer TReturnObj
? TReturnObj
: // normal object case
TModel['selectors'] extends Record<string, any>
? TModel['selectors']
: never
export type ExtractSelectorsSignatureFromSelectorsModel<
TRootState,
TSelectorsConfigObject,
TKey extends keyof TSelectorsConfigObject
> = TSelectorsConfigObject[TKey] extends (...args: any[]) => infer TSelector
? // hasProps case
TSelector extends (props: infer TProps) => Selector<any, infer TReturns>
? (props: TProps) => Selector<TRootState, TReturns>
: // selector without props case
TSelector extends Selector<any, infer TReturns>
? Selector<TRootState, TReturns>
: // selector with props case
TSelector extends Selector<any, infer TReturns, infer TProps>
? Selector<TRootState, TReturns, TProps>
: never
: never
export interface SelectConfig<
TModels extends Models<TModels> = Record<string, any>
> {
sliceState?: (
state: ExtractRematchStateFromModels<TModels>,
model: Model<TModels>
) => Record<string, any> | undefined
selectorCreator?: any
}
export type Selector<S, P = any, R = any> = Reselect.Selector<S, R> &
Reselect.ParametricSelector<S, P, R>
export type ModelSelectors<
TModels extends Models<TModels> = Record<string, any>,
TModel extends Model<TModels> = Model<TModels>
> = {
[key in keyof ExtractSelectorsFromModel<
TModels,
TModel
>]: ExtractSelectorsSignatureFromSelectorsModel<
RematchRootState<TModels>,
ExtractSelectorsFromModel<TModels, TModel>,
key
>
}
export interface ModelSelectors<S> {
[key: string]: Selector<S>
export type Slicer<TSliceState, TRootState> = (<TReturns>(
resultFn: (slice: TSliceState) => TReturns
) => Selector<TRootState, TReturns>) &
Selector<TRootState, TSliceState>
export interface ModelSelectorFactories<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any
> {
[key: string]:
| SelectorFactory<TModels, TSliceState>
| SelectorParametricFactory<TModels, TSliceState>
}
export interface StoreSelectors<S> {
[key: string]: ModelSelectors<S>
export type StoreSelectors<
TModels extends Models<TModels> = Record<string, any>
> = {
[TModelKey in keyof TModels]: ModelSelectors<TModels, TModels[TModelKey]>
}
export type SelectorFactory<S, P = any, R = any> = (
this: ModelSelectors<S>,
models: StoreSelectors<S>
) => Selector<S, P, R>
export type SelectorFactory<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any
> = <TReturns>(
// FIXME: https://github.com/Microsoft/TypeScript/issues/27862
this: ModelSelectorFactories<TModels, TSliceState>,
models: StoreSelectors<TModels>
) => Selector<RematchRootState<TModels>, TReturns>
export type SelectorParametricFactory<S, P = any> = (
this: ModelSelectors<S>,
models: StoreSelectors<S>,
props: P
) => Selector<S>
export type SelectorParametricFactory<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any,
TProps = any,
TReturns = any
> = (
// FIXME: https://github.com/Microsoft/TypeScript/issues/27862
this: ModelSelectorFactories<TModels, TSliceState>,
models: StoreSelectors<TModels>,
props: TProps
) => Selector<RematchRootState<TModels>, TReturns, TProps>
export type Slicer<S, M = any, R = any> = Selector<S, void, M> &
((resultFn: (slice: M) => R) => Selector<S, void, R>)
// the same as `SelectorParametricFactory` but with different return signature
export type ParameterizerSelectorFactory<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any,
TProps = any,
TReturns = any
> = (
// FIXME: https://github.com/Microsoft/TypeScript/issues/27862
this: ModelSelectorFactories<TModels, TSliceState>,
models: StoreSelectors<TModels>,
props: TProps
) => (props: TProps) => Selector<RematchRootState<TModels>, TReturns>
export type SelectorCreator = typeof Reselect.createSelector
export type Parameterizer<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any
> = <TProps, TReturns>(
factory: SelectorParametricFactory<TModels, TSliceState, TProps, TReturns>
) => ParameterizerSelectorFactory<TModels, TSliceState, TProps, TReturns>
export type Parameterizer<S, P = any> = (
factory: SelectorParametricFactory<S, P>
) => SelectorFactory<P, void, Selector<S>>
export type ModelSelectorsFactory<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any
> = (
slice: Slicer<TSliceState, RematchRootState<TModels>>,
createSelector: typeof Reselect.createSelector,
hasProps: Parameterizer<TModels, TSliceState>
) => ModelSelectorFactories<TModels, TSliceState>
export interface ModelSelectorFactories<S> {
[key: string]: SelectorFactory<S> | SelectorParametricFactory<S>
}
export type ModelSelectorsConfig<
TModels extends Models<TModels> = Record<string, any>,
TSliceState = any
> =
| ModelSelectorsFactory<TModels, TSliceState>
| ModelSelectorFactories<TModels, TSliceState>
export type ModelSelectorsFactory<S> = (
slice: Slicer<S>,
createSelector: SelectorCreator,
hasProps: Parameterizer<S>
) => ModelSelectorFactories<S>
export type ModelSelectorsConfig<S> =
| ModelSelectorsFactory<S>
| ModelSelectorFactories<S>
export type RematchSelect<M extends object = Models, RootState = any> = ((
mapSelectToProps: (select: RematchSelect<M, RootState>) => object
export type RematchSelect<
TModels extends Models<TModels>,
TRootState = RematchRootState<TModels>
> = (<TReturn>(
mapSelectToProps: (select: RematchSelect<TModels>) => TReturn
) => Reselect.OutputParametricSelector<
RootState,
TRootState,
any,
object,
Reselect.Selector<RootState, object>
>) &
StoreSelectors<RootState>
TReturn,
Reselect.Selector<TRootState, Record<string, any>>
> &
Reselect.OutputSelector<
TRootState,
TReturn,
Reselect.Selector<TRootState, Record<string, any>>
>) &
StoreSelectors<TModels>
declare module '@rematch/core' {
// Add overloads for store to add select
interface RematchStore<
TModels extends Models<TModels> = Record<string, any>,
TExtraModels extends Models<TModels> = {}
> extends ReduxStore<RematchRootState<TModels, TExtraModels>, Action> {
select: RematchSelect<TModels>
}
// add overloads for Model here.
interface Model<
TModels extends Models<TModels> = Record<string, any>,
TState = any,
TBaseState = TState
TState = any
> {
selectors?: ModelSelectorsConfig<TState>
selectors?: ModelSelectorsConfig<TModels, TState>
}
interface RematchStore<TModels> {
select: RematchSelect<TModels, RematchRootState<TModels>>
// add overloads for ModelCreator here.
interface ModelCreator {
<RM extends Models<RM>>(): <
R extends ModelReducers<S>,
BR extends ReduxReducer<BS>,
E extends ModelEffects<RM> | ModelEffectsCreator<RM>,
SE extends ModelSelectorsConfig<RM, S>,
S,
BS = S
>(mo: {
name?: string
state: S
selectors?: SE
reducers?: R
baseReducer?: BR
effects?: E
}) => {
name?: string
state: S
selectors: SE
reducers: R
baseReducer: BR
effects: E
}
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc