Skip to content

Commit

Permalink
implement #22 useMap & useId
Browse files Browse the repository at this point in the history
  • Loading branch information
dancerphil committed Dec 11, 2019
1 parent bf42c4f commit b811a2b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/createCombinedRegion/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types="react" />
import { LegacyKey, LoadOption, ConnectOption, AnyObject, PropsKey } from '../types';
import { LegacyKey, LoadOption, ConnectOption, AnyObject } from '../types';
declare const createCombinedRegion: () => {
private_store: {
getAttribute: (key: string, attribute: PropsKey) => any;
getAttribute: (key: string, attribute: import("../types").PropsKey) => any;
private_setState: (value: import("../types").State) => void;
load: (payload: import("../types").LoadPayload) => import("../types").State;
set: (payload: import("../types").Payload) => import("../types").State;
Expand All @@ -19,6 +19,8 @@ declare const createCombinedRegion: () => {
fetchTime: number | undefined;
error: Error | undefined;
} & AnyObject;
getMap: (key: string | string[]) => any;
getId: (key: string | string[]) => any;
getValue: (key: string | string[]) => any;
getLoading: (key: string | string[]) => boolean;
getError: (key: string | string[]) => Error | undefined;
Expand All @@ -27,6 +29,8 @@ declare const createCombinedRegion: () => {
connect: (key: LegacyKey, { Loading, Error: ErrorComponent }?: ConnectOption) => (Display?: any) => (ownProps: AnyObject) => JSX.Element;
useProps: (key: string | string[]) => AnyObject;
useValue: (key: string | string[]) => any;
useMap: (key: string | string[]) => any;
useId: (key: string | string[]) => any;
useLoading: (key: string | string[]) => boolean;
useError: (key: string | string[]) => Error | undefined;
useFetchTime: (key: string | string[]) => number | undefined;
Expand Down
18 changes: 18 additions & 0 deletions lib/createCombinedRegion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ var createCombinedRegion = function () {
});
}); };
};
var getMap = function (key) {
if (Array.isArray(key)) {
return key.map(function (k) { return private_store.getAttribute(k, 'results'); });
}
return private_store.getAttribute(key, 'results');
};
var getId = function (key) {
if (Array.isArray(key)) {
return key.map(function (k) { return private_store.getAttribute(k, 'id'); });
}
return private_store.getAttribute(key, 'id');
};
var getValue = function (key) {
if (Array.isArray(key)) {
return key.map(function (k) { return private_store.getAttribute(k, 'result'); });
Expand Down Expand Up @@ -184,6 +196,8 @@ var createCombinedRegion = function () {
};
};
var useProps = util_1.createHooks({ getFn: getProps, equalityFn: shallowEqual, store: private_store });
var useMap = util_1.createHooks({ getFn: getMap, equalityFn: strictEqual, store: private_store });
var useId = util_1.createHooks({ getFn: getId, equalityFn: strictEqual, store: private_store });
var useValue = util_1.createHooks({ getFn: getValue, equalityFn: strictEqual, store: private_store });
var useLoading = util_1.createHooks({ getFn: getLoading, equalityFn: strictEqual, store: private_store });
var useError = util_1.createHooks({ getFn: getError, equalityFn: strictEqual, store: private_store });
Expand All @@ -195,6 +209,8 @@ var createCombinedRegion = function () {
load: load,
loadBy: loadBy,
getProps: getProps,
getMap: getMap,
getId: getId,
getValue: getValue,
getLoading: getLoading,
getError: getError,
Expand All @@ -203,6 +219,8 @@ var createCombinedRegion = function () {
connect: connect,
useProps: useProps,
useValue: useValue,
useMap: useMap,
useId: useId,
useLoading: useLoading,
useError: useError,
useFetchTime: useFetchTime,
Expand Down
4 changes: 4 additions & 0 deletions lib/createRegion/createLocalStorageRegion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ declare const createLocalStorageRegion: (key: string, fallbackValue: any) => {
fetchTime: number | undefined;
error: Error | undefined;
} & import("../types").AnyObject;
getMap: () => any;
getId: () => any;
getValue: () => any;
getLoading: () => boolean;
getError: () => Error | undefined;
getFetchTime: () => number | undefined;
useProps: () => import("../types").AnyObject;
useMap: () => any;
useId: () => any;
useValue: () => any;
useLoading: () => boolean;
useError: () => Error | undefined;
Expand Down
4 changes: 4 additions & 0 deletions lib/createRegion/createRegion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ export declare const createRegion: (initialValue?: any) => {
fetchTime: number | undefined;
error: Error | undefined;
} & import("../types").AnyObject;
getMap: () => any;
getId: () => any;
getValue: () => any;
getLoading: () => boolean;
getError: () => Error | undefined;
getFetchTime: () => number | undefined;
useProps: () => import("../types").AnyObject;
useMap: () => any;
useId: () => any;
useValue: () => any;
useLoading: () => boolean;
useError: () => Error | undefined;
Expand Down
16 changes: 16 additions & 0 deletions lib/createRegion/createRegion.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ exports.createRegion = function (initialValue) {
var getProps = function () {
return region.getProps('value');
};
var getMap = function () {
return region.getMap('value');
};
var getId = function () {
return region.getId('value');
};
var getValue = function () {
return region.getValue('value');
};
Expand All @@ -33,6 +39,12 @@ exports.createRegion = function (initialValue) {
var useProps = function () {
return region.useProps('value');
};
var useMap = function () {
return region.useMap('value');
};
var useId = function () {
return region.useId('value');
};
var useValue = function () {
return region.useValue('value');
};
Expand All @@ -57,11 +69,15 @@ exports.createRegion = function (initialValue) {
load: load,
loadBy: loadBy,
getProps: getProps,
getMap: getMap,
getId: getId,
getValue: getValue,
getLoading: getLoading,
getError: getError,
getFetchTime: getFetchTime,
useProps: useProps,
useMap: useMap,
useId: useId,
useValue: useValue,
useLoading: useLoading,
useError: useError,
Expand Down
16 changes: 12 additions & 4 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ describe('export api', () => {
const { createRegion } = api;
const {
set, load, loadBy,
getProps, getValue, getLoading, getError, getFetchTime,
useProps, useValue, useLoading, useError, useFetchTime,
getProps, getMap, getId, getValue, getLoading, getError, getFetchTime,
useProps, useMap, useId, useValue, useLoading, useError, useFetchTime,
} = createRegion();
expect(typeof set).toBe('function');
expect(typeof load).toBe('function');
expect(typeof loadBy).toBe('function');
expect(typeof getProps).toBe('function');
expect(typeof getMap).toBe('function');
expect(typeof getId).toBe('function');
expect(typeof getValue).toBe('function');
expect(typeof getLoading).toBe('function');
expect(typeof getError).toBe('function');
expect(typeof getFetchTime).toBe('function');
expect(typeof useProps).toBe('function');
expect(typeof useMap).toBe('function');
expect(typeof useId).toBe('function');
expect(typeof useValue).toBe('function');
expect(typeof useLoading).toBe('function');
expect(typeof useError).toBe('function');
Expand All @@ -43,8 +47,8 @@ describe('export api', () => {
const { createCombinedRegion } = api;
const {
reset, set, load, loadBy, connect, connectWith,
getProps, getValue, getLoading, getError, getFetchTime,
useProps, useValue, useLoading, useError, useFetchTime,
getProps, getMap, getId, getValue, getLoading, getError, getFetchTime,
useProps, useMap, useId, useValue, useLoading, useError, useFetchTime,
private_store,
...rest
} = createCombinedRegion();
Expand All @@ -55,11 +59,15 @@ describe('export api', () => {
expect(typeof connect).toBe('function');
expect(typeof connectWith).toBe('function');
expect(typeof getProps).toBe('function');
expect(typeof getMap).toBe('function');
expect(typeof getId).toBe('function');
expect(typeof getValue).toBe('function');
expect(typeof getLoading).toBe('function');
expect(typeof getError).toBe('function');
expect(typeof getFetchTime).toBe('function');
expect(typeof useProps).toBe('function');
expect(typeof useMap).toBe('function');
expect(typeof useId).toBe('function');
expect(typeof useValue).toBe('function');
expect(typeof useLoading).toBe('function');
expect(typeof useError).toBe('function');
Expand Down
24 changes: 22 additions & 2 deletions src/createCombinedRegion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
DisplayType,
ConnectOption,
AnyObject,
PropsKey,
} from '../types';

interface ToPromiseParams {
Expand Down Expand Up @@ -108,8 +107,21 @@ const createCombinedRegion = () => {
};
};

const getValue = (key: Key) => {
const getMap = (key: Key) => {
if (Array.isArray(key)) {
return key.map(k => private_store.getAttribute(k, 'results'));
}
return private_store.getAttribute(key, 'results');
};

const getId = (key: Key) => {
if (Array.isArray(key)) {
return key.map(k => private_store.getAttribute(k, 'id'));
}
return private_store.getAttribute(key, 'id');
};

const getValue = (key: Key) => {
if (Array.isArray(key)) {
return key.map(k => private_store.getAttribute(k, 'result'));
}
Expand Down Expand Up @@ -167,6 +179,10 @@ const createCombinedRegion = () => {

const useProps: (key: Key) => AnyObject = createHooks({ getFn: getProps, equalityFn: shallowEqual, store: private_store });

const useMap = createHooks({ getFn: getMap, equalityFn: strictEqual, store: private_store });

const useId = createHooks({ getFn: getId, equalityFn: strictEqual, store: private_store });

const useValue = createHooks({ getFn: getValue, equalityFn: strictEqual, store: private_store });

const useLoading = createHooks({ getFn: getLoading, equalityFn: strictEqual, store: private_store });
Expand All @@ -182,6 +198,8 @@ const createCombinedRegion = () => {
load,
loadBy,
getProps,
getMap,
getId,
getValue,
getLoading,
getError,
Expand All @@ -190,6 +208,8 @@ const createCombinedRegion = () => {
connect,
useProps,
useValue,
useMap,
useId,
useLoading,
useError,
useFetchTime,
Expand Down
20 changes: 20 additions & 0 deletions src/createRegion/createRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export const createRegion = (initialValue?: any) => {
return region.getProps('value');
};

const getMap = () => {
return region.getMap('value');
};

const getId = () => {
return region.getId('value');
};

const getValue = () => {
return region.getValue('value');
};
Expand All @@ -41,6 +49,14 @@ export const createRegion = (initialValue?: any) => {
return region.useProps('value');
};

const useMap = () => {
return region.useMap('value');
};

const useId = () => {
return region.useId('value');
};

const useValue = () => {
return region.useValue('value');
};
Expand Down Expand Up @@ -70,11 +86,15 @@ export const createRegion = (initialValue?: any) => {
load,
loadBy,
getProps,
getMap,
getId,
getValue,
getLoading,
getError,
getFetchTime,
useProps,
useMap,
useId,
useValue,
useLoading,
useError,
Expand Down

0 comments on commit b811a2b

Please sign in to comment.