Compare commits

...

1 commit

Author SHA1 Message Date
infiinte-persistence
393705f5b1
re-reselect proof of concept 2021-10-11 10:43:09 +08:00
7 changed files with 152 additions and 17 deletions

61
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;
@ -2616,6 +2655,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) {
@ -8147,6 +8198,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 +8210,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;

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,6 +189,7 @@ export {
} from 'redux/selectors/collections'; } from 'redux/selectors/collections';
export { export {
selectClaimForUri,
makeSelectClaimForUri, makeSelectClaimForUri,
makeSelectClaimIsMine, makeSelectClaimIsMine,
makeSelectFetchingChannelClaims, makeSelectFetchingChannelClaims,
@ -200,6 +201,7 @@ export {
makeSelectThumbnailForUri, makeSelectThumbnailForUri,
makeSelectCoverForUri, makeSelectCoverForUri,
makeSelectTitleForUri, makeSelectTitleForUri,
selectDateForUri,
makeSelectDateForUri, makeSelectDateForUri,
makeSelectAmountForUri, makeSelectAmountForUri,
makeSelectEffectiveAmountForUri, makeSelectEffectiveAmountForUri,

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,
@ -383,6 +424,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),

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"