Compare commits

...

2 commits

Author SHA1 Message Date
infiinte-persistence
afa3272cc8
selectMetadataForUri -> selectTagsForUri 2021-10-11 10:43:09 +08:00
infiinte-persistence
393705f5b1
re-reselect proof of concept 2021-10-11 10:43:09 +08:00
7 changed files with 181 additions and 21 deletions

72
dist/bundle.es.js vendored
View file

@ -7,6 +7,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
require('proxy-polyfill'); require('proxy-polyfill');
var uuid = require('uuid'); var uuid = require('uuid');
var reselect = require('reselect'); var reselect = require('reselect');
var reReselect = require('re-reselect');
var fromEntries = _interopDefault(require('@ungap/from-entries')); var fromEntries = _interopDefault(require('@ungap/from-entries'));
const MINIMUM_PUBLISH_BID = 0.00000001; 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 selectState$1 = state => state.claims || {};
const selectById = reselect.createSelector(selectState$1, state => state.byId || {}); const selectById = state => selectState$1(state).byId || {};
const selectPendingClaimsById = state => selectState$1(state).pendingById || {};
const selectPendingClaimsById = reselect.createSelector(selectState$1, state => state.pendingById || {});
const selectClaimsById = reselect.createSelector(selectById, selectPendingClaimsById, (byId, pendingById) => { const selectClaimsById = reselect.createSelector(selectById, selectPendingClaimsById, (byId, pendingById) => {
return Object.assign(byId, pendingById); // do I need merged to keep metadata? 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); 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]); 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) => { const makeSelectClaimForUri = (uri, returnRepost = true) => reselect.createSelector(selectClaimIdsByUri, selectClaimsById, (byUri, byId) => {
let validUri; let validUri;
let channelClaimId; let channelClaimId;
@ -2605,6 +2644,11 @@ const makeSelectTotalPagesInChannelSearch = uri => reselect.createSelector(selec
return byChannel['pageCount']; return byChannel['pageCount'];
}); });
const selectMetadataForUri = reReselect.createCachedSelector(selectClaimForUri, (claim, uri) => {
const metadata = claim && claim.value;
return metadata || (claim === undefined ? undefined : null);
})((state, uri) => uri);
const makeSelectMetadataForUri = uri => reselect.createSelector(makeSelectClaimForUri(uri), claim => { const makeSelectMetadataForUri = uri => reselect.createSelector(makeSelectClaimForUri(uri), claim => {
const metadata = claim && claim.value; const metadata = claim && claim.value;
return metadata || (claim === undefined ? undefined : null); return metadata || (claim === undefined ? undefined : null);
@ -2616,6 +2660,18 @@ const makeSelectMetadataItemForUri = (uri, key) => reselect.createSelector(makeS
const makeSelectTitleForUri = uri => reselect.createSelector(makeSelectMetadataForUri(uri), metadata => metadata && metadata.title); 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 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); 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) { if (!timestamp) {
@ -2809,6 +2865,10 @@ const makeSelectMyChannelPermUrlForName = name => reselect.createSelector(select
return matchingClaim ? matchingClaim.permanent_url : null; return matchingClaim ? matchingClaim.permanent_url : null;
}); });
const selectTagsForUri = reReselect.createCachedSelector(selectMetadataForUri, metadata => {
return metadata && metadata.tags || [];
})((state, uri) => uri);
const makeSelectTagsForUri = uri => reselect.createSelector(makeSelectMetadataForUri(uri), metadata => { const makeSelectTagsForUri = uri => reselect.createSelector(makeSelectMetadataForUri(uri), metadata => {
return metadata && metadata.tags || []; return metadata && metadata.tags || [];
}); });
@ -8147,6 +8207,7 @@ exports.selectBlocks = selectBlocks;
exports.selectBuiltinCollections = selectBuiltinCollections; exports.selectBuiltinCollections = selectBuiltinCollections;
exports.selectChannelClaimCounts = selectChannelClaimCounts; exports.selectChannelClaimCounts = selectChannelClaimCounts;
exports.selectChannelImportPending = selectChannelImportPending; exports.selectChannelImportPending = selectChannelImportPending;
exports.selectClaimForUri = selectClaimForUri;
exports.selectClaimIdsByUri = selectClaimIdsByUri; exports.selectClaimIdsByUri = selectClaimIdsByUri;
exports.selectClaimSearchByQuery = selectClaimSearchByQuery; exports.selectClaimSearchByQuery = selectClaimSearchByQuery;
exports.selectClaimSearchByQueryLastPageReached = selectClaimSearchByQueryLastPageReached; exports.selectClaimSearchByQueryLastPageReached = selectClaimSearchByQueryLastPageReached;
@ -8158,6 +8219,7 @@ exports.selectCreateCollectionError = selectCreateCollectionError;
exports.selectCreatingChannel = selectCreatingChannel; exports.selectCreatingChannel = selectCreatingChannel;
exports.selectCreatingCollection = selectCreatingCollection; exports.selectCreatingCollection = selectCreatingCollection;
exports.selectCurrentChannelPage = selectCurrentChannelPage; exports.selectCurrentChannelPage = selectCurrentChannelPage;
exports.selectDateForUri = selectDateForUri;
exports.selectDownloadUrlsCount = selectDownloadUrlsCount; exports.selectDownloadUrlsCount = selectDownloadUrlsCount;
exports.selectDownloadedUris = selectDownloadedUris; exports.selectDownloadedUris = selectDownloadedUris;
exports.selectDownloadingByOutpoint = selectDownloadingByOutpoint; exports.selectDownloadingByOutpoint = selectDownloadingByOutpoint;
@ -8195,6 +8257,7 @@ exports.selectIsResolvingPublishUris = selectIsResolvingPublishUris;
exports.selectIsSendingSupport = selectIsSendingSupport; exports.selectIsSendingSupport = selectIsSendingSupport;
exports.selectIsStillEditing = selectIsStillEditing; exports.selectIsStillEditing = selectIsStillEditing;
exports.selectIsWalletReconnecting = selectIsWalletReconnecting; exports.selectIsWalletReconnecting = selectIsWalletReconnecting;
exports.selectMetadataForUri = selectMetadataForUri;
exports.selectMyActiveClaims = selectMyActiveClaims; exports.selectMyActiveClaims = selectMyActiveClaims;
exports.selectMyChannelClaims = selectMyChannelClaims; exports.selectMyChannelClaims = selectMyChannelClaims;
exports.selectMyChannelUrls = selectMyChannelUrls; exports.selectMyChannelUrls = selectMyChannelUrls;
@ -8236,6 +8299,7 @@ exports.selectResolvingUris = selectResolvingUris;
exports.selectSavedCollectionIds = selectSavedCollectionIds; exports.selectSavedCollectionIds = selectSavedCollectionIds;
exports.selectSupportsBalance = selectSupportsBalance; exports.selectSupportsBalance = selectSupportsBalance;
exports.selectSupportsByOutpoint = selectSupportsByOutpoint; exports.selectSupportsByOutpoint = selectSupportsByOutpoint;
exports.selectTagsForUri = selectTagsForUri;
exports.selectTakeOverAmount = selectTakeOverAmount; exports.selectTakeOverAmount = selectTakeOverAmount;
exports.selectTipsBalance = selectTipsBalance; exports.selectTipsBalance = selectTipsBalance;
exports.selectToast = selectToast; exports.selectToast = selectToast;

6
dist/flow-typed/rereselect.js vendored Normal file
View 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
View 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;
}

View file

@ -31,6 +31,7 @@
"dependencies": { "dependencies": {
"@ungap/from-entries": "^0.2.1", "@ungap/from-entries": "^0.2.1",
"proxy-polyfill": "0.1.6", "proxy-polyfill": "0.1.6",
"re-reselect": "^4.0.0",
"reselect": "^3.0.0", "reselect": "^3.0.0",
"uuid": "^8.3.1" "uuid": "^8.3.1"
}, },

View file

@ -189,20 +189,24 @@ export {
} from 'redux/selectors/collections'; } from 'redux/selectors/collections';
export { export {
selectClaimForUri,
makeSelectClaimForUri, makeSelectClaimForUri,
makeSelectClaimIsMine, makeSelectClaimIsMine,
makeSelectFetchingChannelClaims, makeSelectFetchingChannelClaims,
makeSelectClaimsInChannelForPage, makeSelectClaimsInChannelForPage,
makeSelectTotalPagesInChannelSearch, makeSelectTotalPagesInChannelSearch,
makeSelectTotalClaimsInChannelSearch, makeSelectTotalClaimsInChannelSearch,
selectMetadataForUri,
makeSelectMetadataForUri, makeSelectMetadataForUri,
makeSelectMetadataItemForUri, makeSelectMetadataItemForUri,
makeSelectThumbnailForUri, makeSelectThumbnailForUri,
makeSelectCoverForUri, makeSelectCoverForUri,
makeSelectTitleForUri, makeSelectTitleForUri,
selectDateForUri,
makeSelectDateForUri, makeSelectDateForUri,
makeSelectAmountForUri, makeSelectAmountForUri,
makeSelectEffectiveAmountForUri, makeSelectEffectiveAmountForUri,
selectTagsForUri,
makeSelectTagsForUri, makeSelectTagsForUri,
makeSelectTagInClaimOrChannelForUri, makeSelectTagInClaimOrChannelForUri,
makeSelectTotalStakedAmountForChannelUri, makeSelectTotalStakedAmountForChannelUri,

View file

@ -2,20 +2,16 @@
import { normalizeURI, parseURI } from 'lbryURI'; import { normalizeURI, parseURI } from 'lbryURI';
import { selectSupportsByOutpoint } from 'redux/selectors/wallet'; import { selectSupportsByOutpoint } from 'redux/selectors/wallet';
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { createCachedSelector } from 're-reselect';
import { isClaimNsfw, filterClaims } from 'util/claim'; import { isClaimNsfw, filterClaims } from 'util/claim';
import * as CLAIM from 'constants/claim'; import * as CLAIM from 'constants/claim';
type State = { claims: any };
const selectState = state => state.claims || {}; const selectState = state => state.claims || {};
export const selectById = createSelector( export const selectById = (state: State) => selectState(state).byId || {};
selectState, export const selectPendingClaimsById = (state: State) => selectState(state).pendingById || {};
state => state.byId || {}
);
export const selectPendingClaimsById = createSelector(
selectState,
state => state.pendingById || {}
);
export const selectClaimsById = createSelector( export const selectClaimsById = createSelector(
selectById, selectById,
@ -25,10 +21,7 @@ export const selectClaimsById = createSelector(
} }
); );
export const selectClaimIdsByUri = createSelector( export const selectClaimIdsByUri = (state: State) => selectState(state).claimsByUri || {};
selectState,
state => state.claimsByUri || {}
);
export const selectCurrentChannelPage = createSelector( export const selectCurrentChannelPage = createSelector(
selectState, selectState,
@ -132,6 +125,54 @@ export const makeSelectClaimForClaimId = (claimId: string) =>
byId => byId[claimId] 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) => export const makeSelectClaimForUri = (uri: string, returnRepost: boolean = true) =>
createSelector( createSelector(
selectClaimIdsByUri, selectClaimIdsByUri,
@ -299,8 +340,8 @@ export const makeSelectMyPurchasesForPage = (query: ?string, page: number = 1) =
const end = Number(page) * Number(CLAIM.PAGE_SIZE); const end = Number(page) * Number(CLAIM.PAGE_SIZE);
return matchingFileInfos && matchingFileInfos.length return matchingFileInfos && matchingFileInfos.length
? matchingFileInfos ? matchingFileInfos
.slice(start, end) .slice(start, end)
.map(fileInfo => fileInfo.canonical_url || fileInfo.permanent_url) .map(fileInfo => fileInfo.canonical_url || fileInfo.permanent_url)
: []; : [];
} }
); );
@ -360,6 +401,11 @@ export const makeSelectTotalPagesInChannelSearch = (uri: string) =>
} }
); );
export const selectMetadataForUri = createCachedSelector(selectClaimForUri, (claim, uri) => {
const metadata = claim && claim.value;
return metadata || (claim === undefined ? undefined : null);
})((state, uri) => uri);
export const makeSelectMetadataForUri = (uri: string) => export const makeSelectMetadataForUri = (uri: string) =>
createSelector( createSelector(
makeSelectClaimForUri(uri), makeSelectClaimForUri(uri),
@ -383,6 +429,27 @@ export const makeSelectTitleForUri = (uri: string) =>
metadata => metadata && metadata.title 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) => export const makeSelectDateForUri = (uri: string) =>
createSelector( createSelector(
makeSelectClaimForUri(uri), makeSelectClaimForUri(uri),
@ -393,8 +460,8 @@ export const makeSelectDateForUri = (uri: string) =>
(claim.value.release_time (claim.value.release_time
? claim.value.release_time * 1000 ? claim.value.release_time * 1000
: claim.meta && claim.meta.creation_timestamp : claim.meta && claim.meta.creation_timestamp
? claim.meta.creation_timestamp * 1000 ? claim.meta.creation_timestamp * 1000
: null); : null);
if (!timestamp) { if (!timestamp) {
return undefined; return undefined;
} }
@ -723,6 +790,13 @@ export const makeSelectMyChannelPermUrlForName = (name: string) =>
} }
); );
export const selectTagsForUri = createCachedSelector(
selectMetadataForUri,
(metadata: ?GenericMetadata) => {
return (metadata && metadata.tags) || [];
}
)((state, uri) => uri);
export const makeSelectTagsForUri = (uri: string) => export const makeSelectTagsForUri = (uri: string) =>
createSelector( createSelector(
makeSelectMetadataForUri(uri), makeSelectMetadataForUri(uri),

View file

@ -6755,6 +6755,11 @@ randomatic@^3.0.0:
kind-of "^6.0.0" kind-of "^6.0.0"
math-random "^1.0.1" 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: react-is@^16.12.0:
version "16.13.1" version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"