Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Last active October 6, 2017 16:49
Show Gist options
  • Save DScheglov/9506a2b45f6069349d98550bfad9ab37 to your computer and use it in GitHub Desktop.
Save DScheglov/9506a2b45f6069349d98550bfad9ab37 to your computer and use it in GitHub Desktop.
export const shalowCompareArgs = (args, _args) => (
_args == null ||
args.length !== _args.length ||
args.some((a, index) => a !== _args[index])
);
export const memorize = (func, compare = shalowCompareArgs) => {
let _args;
let _res;
return (...args) => {
if (compare(args, _args)) {
_args = args;
_res = func(..._args);
}
return _res;
};
};
export const combineSelectors = (entrySelectors, selector, memorizer = memorize) => {
if (typeof memorizer === 'function') selector = memorizer(selector);
return (...args) => selector(...entrySelectors.map(apply(args)));
};
export const getUsers = combineSelectors(
[getUserIds, getUserEntities], mapIds, memorize2
);
export const getUsers = combineSelectors([getUserIds, getUserEntities], mapIds);
export const memorize1 = f => ((_a, res) =>
a => ( // eslint-disable-line no-return-assign
_a !== a ? res = f(_a = a) : res
)
)();
export const memorize2 = f => ((_a, _b, res) =>
(a, b) => ( // eslint-disable-line no-return-assign
_a !== a || _b !== b ? res = f(_a = a, _b = b) : res
)
)();
export const memorize3 = f => ((_a, _b, _c, res) =>
(a, b, c) => ( // eslint-disable-line no-return-assign
_a !== a || _b !== b || _c !== c
? res = f(_a = a, _b = b, _c = c)
: res
)
)();
state = {
users: [
{ id: 1, ... },
{ id: 2, ... },
{ id: 3, ... },
{ id: 4, ... },
...
],
modals: {
succcess: false,
error: false,
},
loading: false,
};
state = {
users: {
allIds: [1, 2, 3, 4, ...],
byId: {
1: { id: 1, ... },
2: { id: 2, ... },
3: { id: 3, ... },
4: { id: 4, ... },
...
},
modals: {
succcess: false,
error: false,
},
loading: false,
};
const apply = args => f => f(...args);
const shalowCompareArgs = (args, pArgs) => (
args == null || pArgs == null ||
args.length !== pArgs.length ||
args.some((arg, index) => arg !== pArgs[index])
);
const memorizeDecorator = (f, compare = shalowCompareArgs) => (
(_pArgs, _pRes) => (...args) => ( // eslint-disable-line no-return-assign
_pRes = compare(args, _pArgs) ? f(...(_pArgs = args)) : _pRes
)
)();
const createSelector = (memorize = memorizeDecorator) => (...selectors) => {
const selector = memorize(selectors.pop());
return (...args) => (
selector(...selectors.map(apply(args)))
);
};
const getIds = (state, props) => state.ids[props.listId] || [];
const getEntities = state => state.entities;
const byId = entities => id => entities[id];
const mapIds = (ids, entities) => ids.map(byId(entities));
const listSelector = ({ listId }) => ( // eslint-disable-line no-return-assign
listSelector[listId] ||
(listSelector[listId] = createSelector()(getIds, getEntities, mapIds))
);
const getAll = (state, props) => listSelector(props)(state, props);
export const getUserEntities = state => state.users.byId;
export const getUserIds = state => state.users.allIds;
export const userById = state => id => state[id];
export const mapUserIds = memorize2(
(ids, userEntities) => ids.map(userById(userEntities))
);
export const getUsers = state => mapUserIds(
getUserIds(state), getUserEntities(state)
);
export const getUsersCount = state => getUserIds(state).length;
state = {
users: {
lists: {
firstUserList: [1, 2, 3, 4, ...],
secondUserList: [1, 2, 3, 4, ...],
},
byId: {
1: { id: 1, ... },
2: { id: 2, ... },
3: { id: 3, ... },
4: { id: 4, ... },
...
},
modals: {
succcess: false,
error: false,
},
loading: false,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment