re-reselect proof of concept
This commit is contained in:
parent
0f930c4a7b
commit
393705f5b1
7 changed files with 152 additions and 17 deletions
61
dist/bundle.es.js
vendored
61
dist/bundle.es.js
vendored
|
@ -7,6 +7,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
|
|||
require('proxy-polyfill');
|
||||
var uuid = require('uuid');
|
||||
var reselect = require('reselect');
|
||||
var reReselect = require('re-reselect');
|
||||
var fromEntries = _interopDefault(require('@ungap/from-entries'));
|
||||
|
||||
const MINIMUM_PUBLISH_BID = 0.00000001;
|
||||
|
@ -2405,15 +2406,14 @@ var _extends$3 = Object.assign || function (target) { for (var i = 1; i < argume
|
|||
|
||||
const selectState$1 = state => state.claims || {};
|
||||
|
||||
const selectById = reselect.createSelector(selectState$1, state => state.byId || {});
|
||||
|
||||
const selectPendingClaimsById = reselect.createSelector(selectState$1, state => state.pendingById || {});
|
||||
const selectById = state => selectState$1(state).byId || {};
|
||||
const selectPendingClaimsById = state => selectState$1(state).pendingById || {};
|
||||
|
||||
const selectClaimsById = reselect.createSelector(selectById, selectPendingClaimsById, (byId, pendingById) => {
|
||||
return Object.assign(byId, pendingById); // do I need merged to keep metadata?
|
||||
});
|
||||
|
||||
const selectClaimIdsByUri = reselect.createSelector(selectState$1, state => state.claimsByUri || {});
|
||||
const selectClaimIdsByUri = state => selectState$1(state).claimsByUri || {};
|
||||
|
||||
const selectCurrentChannelPage = reselect.createSelector(selectState$1, state => state.currentChannelPage || 1);
|
||||
|
||||
|
@ -2469,6 +2469,45 @@ const selectReflectingById = reselect.createSelector(selectState$1, state => sta
|
|||
|
||||
const makeSelectClaimForClaimId = claimId => reselect.createSelector(selectClaimsById, byId => byId[claimId]);
|
||||
|
||||
// Compare with makeSelectClaimForUri just down below
|
||||
const selectClaimForUri = reReselect.createCachedSelector(selectClaimIdsByUri, selectClaimsById, (state, uri) => uri, (state, uri, returnRepost = true) => returnRepost, (byUri, byId, uri, returnRepost) => {
|
||||
console.log(' selectClaimForUri');
|
||||
|
||||
let validUri;
|
||||
let channelClaimId;
|
||||
let streamClaimId;
|
||||
let isChannel;
|
||||
try {
|
||||
({ isChannel, channelClaimId, streamClaimId } = parseURI(uri));
|
||||
validUri = true;
|
||||
} catch (e) {}
|
||||
|
||||
if (validUri && byUri) {
|
||||
const claimId = uri && byUri[normalizeURI(uri)];
|
||||
const claim = byId[claimId];
|
||||
|
||||
// Make sure to return the claim as is so apps can check if it's been resolved before (null) or still needs to be resolved (undefined)
|
||||
if (claimId === null) {
|
||||
return null;
|
||||
} else if (claimId === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const repostedClaim = claim && claim.reposted_claim;
|
||||
if (repostedClaim && returnRepost) {
|
||||
const channelUrl = claim.signing_channel && (claim.signing_channel.canonical_url || claim.signing_channel.permanent_url);
|
||||
|
||||
return _extends$3({}, repostedClaim, {
|
||||
repost_url: normalizeURI(uri),
|
||||
repost_channel_url: channelUrl,
|
||||
repost_bid_amount: claim && claim.meta && claim.meta.effective_amount
|
||||
});
|
||||
} else {
|
||||
return claim;
|
||||
}
|
||||
}
|
||||
})((state, uri, returnRepost = true) => `${uri}:${returnRepost ? '1' : '0'}`);
|
||||
|
||||
const makeSelectClaimForUri = (uri, returnRepost = true) => reselect.createSelector(selectClaimIdsByUri, selectClaimsById, (byUri, byId) => {
|
||||
let validUri;
|
||||
let channelClaimId;
|
||||
|
@ -2616,6 +2655,18 @@ const makeSelectMetadataItemForUri = (uri, key) => reselect.createSelector(makeS
|
|||
|
||||
const makeSelectTitleForUri = uri => reselect.createSelector(makeSelectMetadataForUri(uri), metadata => metadata && metadata.title);
|
||||
|
||||
// Compare with makeSelectDateForUri just down below
|
||||
const selectDateForUri = reReselect.createCachedSelector(selectClaimForUri, // (state, uri, ?returnRepost)
|
||||
claim => {
|
||||
console.log(' selectDateForUri');
|
||||
const timestamp = claim && claim.value && (claim.value.release_time ? claim.value.release_time * 1000 : claim.meta && claim.meta.creation_timestamp ? claim.meta.creation_timestamp * 1000 : null);
|
||||
if (!timestamp) {
|
||||
return undefined;
|
||||
}
|
||||
const dateObj = new Date(timestamp);
|
||||
return dateObj;
|
||||
})((state, uri) => uri);
|
||||
|
||||
const makeSelectDateForUri = uri => reselect.createSelector(makeSelectClaimForUri(uri), claim => {
|
||||
const timestamp = claim && claim.value && (claim.value.release_time ? claim.value.release_time * 1000 : claim.meta && claim.meta.creation_timestamp ? claim.meta.creation_timestamp * 1000 : null);
|
||||
if (!timestamp) {
|
||||
|
@ -8147,6 +8198,7 @@ exports.selectBlocks = selectBlocks;
|
|||
exports.selectBuiltinCollections = selectBuiltinCollections;
|
||||
exports.selectChannelClaimCounts = selectChannelClaimCounts;
|
||||
exports.selectChannelImportPending = selectChannelImportPending;
|
||||
exports.selectClaimForUri = selectClaimForUri;
|
||||
exports.selectClaimIdsByUri = selectClaimIdsByUri;
|
||||
exports.selectClaimSearchByQuery = selectClaimSearchByQuery;
|
||||
exports.selectClaimSearchByQueryLastPageReached = selectClaimSearchByQueryLastPageReached;
|
||||
|
@ -8158,6 +8210,7 @@ exports.selectCreateCollectionError = selectCreateCollectionError;
|
|||
exports.selectCreatingChannel = selectCreatingChannel;
|
||||
exports.selectCreatingCollection = selectCreatingCollection;
|
||||
exports.selectCurrentChannelPage = selectCurrentChannelPage;
|
||||
exports.selectDateForUri = selectDateForUri;
|
||||
exports.selectDownloadUrlsCount = selectDownloadUrlsCount;
|
||||
exports.selectDownloadedUris = selectDownloadedUris;
|
||||
exports.selectDownloadingByOutpoint = selectDownloadingByOutpoint;
|
||||
|
|
6
dist/flow-typed/rereselect.js
vendored
Normal file
6
dist/flow-typed/rereselect.js
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
// @flow
|
||||
// We should be using the `reselect` that comes with flow-typed but it's going to take a ton of work to get that working
|
||||
// without any errors. For now it's any type
|
||||
declare module 're-reselect' {
|
||||
declare module.exports: any;
|
||||
}
|
6
flow-typed/rereselect.js
vendored
Normal file
6
flow-typed/rereselect.js
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
// @flow
|
||||
// We should be using the `reselect` that comes with flow-typed but it's going to take a ton of work to get that working
|
||||
// without any errors. For now it's any type
|
||||
declare module 're-reselect' {
|
||||
declare module.exports: any;
|
||||
}
|
|
@ -31,6 +31,7 @@
|
|||
"dependencies": {
|
||||
"@ungap/from-entries": "^0.2.1",
|
||||
"proxy-polyfill": "0.1.6",
|
||||
"re-reselect": "^4.0.0",
|
||||
"reselect": "^3.0.0",
|
||||
"uuid": "^8.3.1"
|
||||
},
|
||||
|
|
|
@ -189,6 +189,7 @@ export {
|
|||
} from 'redux/selectors/collections';
|
||||
|
||||
export {
|
||||
selectClaimForUri,
|
||||
makeSelectClaimForUri,
|
||||
makeSelectClaimIsMine,
|
||||
makeSelectFetchingChannelClaims,
|
||||
|
@ -200,6 +201,7 @@ export {
|
|||
makeSelectThumbnailForUri,
|
||||
makeSelectCoverForUri,
|
||||
makeSelectTitleForUri,
|
||||
selectDateForUri,
|
||||
makeSelectDateForUri,
|
||||
makeSelectAmountForUri,
|
||||
makeSelectEffectiveAmountForUri,
|
||||
|
|
|
@ -2,20 +2,16 @@
|
|||
import { normalizeURI, parseURI } from 'lbryURI';
|
||||
import { selectSupportsByOutpoint } from 'redux/selectors/wallet';
|
||||
import { createSelector } from 'reselect';
|
||||
import { createCachedSelector } from 're-reselect';
|
||||
import { isClaimNsfw, filterClaims } from 'util/claim';
|
||||
import * as CLAIM from 'constants/claim';
|
||||
|
||||
type State = { claims: any };
|
||||
|
||||
const selectState = state => state.claims || {};
|
||||
|
||||
export const selectById = createSelector(
|
||||
selectState,
|
||||
state => state.byId || {}
|
||||
);
|
||||
|
||||
export const selectPendingClaimsById = createSelector(
|
||||
selectState,
|
||||
state => state.pendingById || {}
|
||||
);
|
||||
export const selectById = (state: State) => selectState(state).byId || {};
|
||||
export const selectPendingClaimsById = (state: State) => selectState(state).pendingById || {};
|
||||
|
||||
export const selectClaimsById = createSelector(
|
||||
selectById,
|
||||
|
@ -25,10 +21,7 @@ export const selectClaimsById = createSelector(
|
|||
}
|
||||
);
|
||||
|
||||
export const selectClaimIdsByUri = createSelector(
|
||||
selectState,
|
||||
state => state.claimsByUri || {}
|
||||
);
|
||||
export const selectClaimIdsByUri = (state: State) => selectState(state).claimsByUri || {};
|
||||
|
||||
export const selectCurrentChannelPage = createSelector(
|
||||
selectState,
|
||||
|
@ -132,6 +125,54 @@ export const makeSelectClaimForClaimId = (claimId: string) =>
|
|||
byId => byId[claimId]
|
||||
);
|
||||
|
||||
// Compare with makeSelectClaimForUri just down below
|
||||
export const selectClaimForUri = createCachedSelector(
|
||||
selectClaimIdsByUri,
|
||||
selectClaimsById,
|
||||
(state, uri) => uri,
|
||||
(state, uri, returnRepost = true) => returnRepost,
|
||||
(byUri, byId, uri, returnRepost) => {
|
||||
console.log(' selectClaimForUri');
|
||||
|
||||
let validUri;
|
||||
let channelClaimId;
|
||||
let streamClaimId;
|
||||
let isChannel;
|
||||
try {
|
||||
({ isChannel, channelClaimId, streamClaimId } = parseURI(uri));
|
||||
validUri = true;
|
||||
} catch (e) {}
|
||||
|
||||
if (validUri && byUri) {
|
||||
const claimId = uri && byUri[normalizeURI(uri)];
|
||||
const claim = byId[claimId];
|
||||
|
||||
// Make sure to return the claim as is so apps can check if it's been resolved before (null) or still needs to be resolved (undefined)
|
||||
if (claimId === null) {
|
||||
return null;
|
||||
} else if (claimId === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const repostedClaim = claim && claim.reposted_claim;
|
||||
if (repostedClaim && returnRepost) {
|
||||
const channelUrl =
|
||||
claim.signing_channel &&
|
||||
(claim.signing_channel.canonical_url || claim.signing_channel.permanent_url);
|
||||
|
||||
return {
|
||||
...repostedClaim,
|
||||
repost_url: normalizeURI(uri),
|
||||
repost_channel_url: channelUrl,
|
||||
repost_bid_amount: claim && claim.meta && claim.meta.effective_amount,
|
||||
};
|
||||
} else {
|
||||
return claim;
|
||||
}
|
||||
}
|
||||
}
|
||||
)((state, uri, returnRepost = true) => `${uri}:${returnRepost ? '1' : '0'}`);
|
||||
|
||||
export const makeSelectClaimForUri = (uri: string, returnRepost: boolean = true) =>
|
||||
createSelector(
|
||||
selectClaimIdsByUri,
|
||||
|
@ -383,6 +424,27 @@ export const makeSelectTitleForUri = (uri: string) =>
|
|||
metadata => metadata && metadata.title
|
||||
);
|
||||
|
||||
// Compare with makeSelectDateForUri just down below
|
||||
export const selectDateForUri = createCachedSelector(
|
||||
selectClaimForUri, // (state, uri, ?returnRepost)
|
||||
claim => {
|
||||
console.log(' selectDateForUri');
|
||||
const timestamp =
|
||||
claim &&
|
||||
claim.value &&
|
||||
(claim.value.release_time
|
||||
? claim.value.release_time * 1000
|
||||
: claim.meta && claim.meta.creation_timestamp
|
||||
? claim.meta.creation_timestamp * 1000
|
||||
: null);
|
||||
if (!timestamp) {
|
||||
return undefined;
|
||||
}
|
||||
const dateObj = new Date(timestamp);
|
||||
return dateObj;
|
||||
}
|
||||
)((state, uri) => uri);
|
||||
|
||||
export const makeSelectDateForUri = (uri: string) =>
|
||||
createSelector(
|
||||
makeSelectClaimForUri(uri),
|
||||
|
|
|
@ -6755,6 +6755,11 @@ randomatic@^3.0.0:
|
|||
kind-of "^6.0.0"
|
||||
math-random "^1.0.1"
|
||||
|
||||
re-reselect@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/re-reselect/-/re-reselect-4.0.0.tgz#9ddec4c72c4d952f68caa5aa4b76a9ed38b75cac"
|
||||
integrity sha512-wuygyq8TXUlSdVXv2kigXxQNOgdb9m7LbIjwfTNGSpaY1riLd5e+VeQjlQMyUtrk0oiyhi1AqIVynworl3qxHA==
|
||||
|
||||
react-is@^16.12.0:
|
||||
version "16.13.1"
|
||||
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
|
||||
|
|
Loading…
Reference in a new issue