Skip to content

Commit

Permalink
Merge pull request #123 from NearSocial/missing-changes
Browse files Browse the repository at this point in the history
## 2.4.2

- Add missing code changes (`cacheOptions` and `lodash`) from 2.4.0.
> This happened due to revert from master that later cleaned changes from dev at merge conflict.
  • Loading branch information
Evgeny Kuzyakov committed Sep 20, 2023
2 parents 8cdb319 + 120e0d6 commit a6ed652
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 51 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## 2.4.2

- Add missing code changes (`cacheOptions` and `lodash`) from 2.4.0.
> This happened due to revert from master that later cleaned changes from dev at merge conflict.
## 2.4.1

- FIX: Resolve bug with `VM.require` affected by the introduction of `useState` and `useEffect` hooks.
Expand Down Expand Up @@ -61,6 +66,7 @@ const index = Social.index(
}
);
```

- Replace `lodash` dependency with `lodash.clonedeep` to reduce bundle size.

## 2.3.2
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "near-social-vm",
"version": "2.4.1",
"version": "2.4.2",
"description": "Near Social VM",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -48,7 +48,7 @@
"idb": "^7.1.1",
"iframe-resizer-react": "^1.1.0",
"local-storage": "^2.0.0",
"lodash": "^4.17.21",
"lodash.clonedeep": "^4.5.0",
"mdast-util-find-and-replace": "^2.0.0",
"nanoid": "^4.0.1",
"prettier": "^2.7.1",
Expand Down
62 changes: 41 additions & 21 deletions src/lib/data/cache.js
Expand Up @@ -65,7 +65,7 @@ class Cache {
return (await this.dbPromise).put(CacheDbObject, val, key);
}

cachedPromise(key, promise, invalidate, forceCachedValue) {
cachedPromise(key, promise, invalidate, cacheOptions) {
key = JSON.stringify(key);
const cached = this.cache[key] || {
status: CacheStatus.NotStarted,
Expand Down Expand Up @@ -106,10 +106,13 @@ class Cache {
) {
return cached.result;
}
if (cached.status === CacheStatus.NotStarted) {
if (
cached.status === CacheStatus.NotStarted &&
!cacheOptions?.ignoreCache
) {
this.innerGet(key).then((cachedResult) => {
if (
(cachedResult || forceCachedValue) &&
(cachedResult || cacheOptions?.forceCachedValue) &&
cached.status === CacheStatus.InProgress
) {
CacheDebug && console.log("Cached value", key, cachedResult);
Expand Down Expand Up @@ -200,18 +203,27 @@ class Cache {
});
}

cachedBlock(near, blockId, invalidate) {
cachedBlock(near, blockId, invalidate, cacheOptions) {
return this.cachedPromise(
{
action: Action.Block,
blockId,
},
() => near.block(blockId),
invalidate
invalidate,
cacheOptions
);
}

cachedViewCall(near, contractId, methodName, args, blockId, invalidate) {
cachedViewCall(
near,
contractId,
methodName,
args,
blockId,
invalidate,
cacheOptions
) {
return this.cachedPromise(
{
action: Action.ViewCall,
Expand All @@ -221,7 +233,8 @@ class Cache {
blockId,
},
() => near.viewCall(contractId, methodName, args, blockId),
invalidate
invalidate,
cacheOptions
);
}

Expand Down Expand Up @@ -266,30 +279,32 @@ class Cache {
}
}

cachedFetch(url, options, invalidate) {
cachedFetch(url, options, invalidate, cacheOptions) {
return this.cachedPromise(
{
action: Action.Fetch,
url,
options,
},
() => this.asyncFetch(url, options),
invalidate
invalidate,
cacheOptions
);
}

cachedCustomPromise(key, promise, invalidate) {
cachedCustomPromise(key, promise, invalidate, cacheOptions) {
return this.cachedPromise(
{
action: Action.CustomPromise,
key,
},
() => promise(),
invalidate
invalidate,
cacheOptions
);
}

socialGet(near, keys, recursive, blockId, options, invalidate) {
socialGet(near, keys, recursive, blockId, options, invalidate, cacheOptions) {
if (!near) {
return null;
}
Expand All @@ -305,7 +320,8 @@ class Cache {
"get",
args,
blockId,
invalidate
invalidate,
cacheOptions
);
if (data === null) {
return null;
Expand All @@ -325,7 +341,7 @@ class Cache {
return data;
}

socialIndex(near, action, key, options, invalidate) {
socialIndex(near, action, key, options, invalidate, cacheOptions) {
const res = this.cachedFetch(
`${near.config.apiUrl}/index`,
{
Expand All @@ -339,7 +355,8 @@ class Cache {
options,
}),
},
invalidate
invalidate,
cacheOptions
);

return res?.ok ? res.body : null;
Expand All @@ -354,7 +371,9 @@ class Cache {
},
undefined,
invalidate,
true
{
forceCachedValue: true,
}
);
}

Expand Down Expand Up @@ -390,7 +409,7 @@ class Cache {
}
}

cachedEthersCall(ethersProvider, callee, args, invalidate) {
cachedEthersCall(ethersProvider, callee, args, invalidate, cacheOptions) {
if (!ethersProvider) {
return null;
}
Expand All @@ -401,7 +420,8 @@ class Cache {
args,
},
() => ethersProvider[callee](...args),
invalidate
invalidate,
cacheOptions
);
}
}
Expand All @@ -417,14 +437,14 @@ const useSecondaryCache = singletonHook(secondaryCache, () => {
return secondaryCache;
});

export const useCache = networkId => {
export const useCache = (networkId) => {
const near = useNear();
const defaultCache = useDefaultCache();
const secondaryCache = useSecondaryCache();

if(!networkId || networkId === near.config.networkId) {
if (!networkId || networkId === near.config.networkId) {
return defaultCache;
}

return secondaryCache;
}
};

0 comments on commit a6ed652

Please sign in to comment.