mirror of
https://github.com/FlipsideCrypto/near-bos-vm.git
synced 2026-02-06 11:18:24 +00:00
Merge pull request #113 from NearSocial/revert-106-no-stale
Reverts NearSocial/VM#106 Should be merged to `dev` instead
This commit is contained in:
commit
446e4a08ae
28
CHANGELOG.md
28
CHANGELOG.md
@ -1,33 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## Pending
|
||||
|
||||
- Add `cacheOptions` optional argument to the following methods:
|
||||
- `Social.get(keys, blockId|finality, options, cacheOptions)`
|
||||
- `Social.getr(keys, blockId|finality, options, cacheOptions)`
|
||||
- `Social.keys(keys, blockId|finality, options, cacheOptions)`
|
||||
- `Social.index(action, key, options, cacheOptions)`
|
||||
- `Near.view(contractName, methodName, args, blockId|finality, subscribe, cacheOptions)`
|
||||
- `Near.block(blockId|finality, subscribe, cacheOptions)`
|
||||
The `cacheOptions` object is optional and may contain the following property:
|
||||
- `ignoreCache` - boolean, if true, the method will ignore the cached value in the local DB and fetch the data from the API server. This will only happen once per session. Default is false.
|
||||
|
||||
This is useful to avoid loading stale objects that are likely to change often. For example, the index of posts for the main feed, or notifications.
|
||||
```jsx
|
||||
const index = Social.index(
|
||||
"post",
|
||||
"main",
|
||||
{
|
||||
limit: 10,
|
||||
order: "desc",
|
||||
},
|
||||
{
|
||||
ignoreCache: true,
|
||||
}
|
||||
);
|
||||
```
|
||||
- Replace `lodash` dependency with `lodash.clonedeep` to reduce bundle size.
|
||||
|
||||
## 2.3.2
|
||||
|
||||
- Nothing. Missed the package.json bump in the previous release.
|
||||
|
||||
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@ -48,7 +48,7 @@
|
||||
"idb": "^7.1.1",
|
||||
"iframe-resizer-react": "^1.1.0",
|
||||
"local-storage": "^2.0.0",
|
||||
"lodash.clonedeep": "^4.5.0",
|
||||
"lodash": "^4.17.21",
|
||||
"mdast-util-find-and-replace": "^2.0.0",
|
||||
"nanoid": "^4.0.1",
|
||||
"prettier": "^2.7.1",
|
||||
|
||||
@ -65,7 +65,7 @@ class Cache {
|
||||
return (await this.dbPromise).put(CacheDbObject, val, key);
|
||||
}
|
||||
|
||||
cachedPromise(key, promise, invalidate, cacheOptions) {
|
||||
cachedPromise(key, promise, invalidate, forceCachedValue) {
|
||||
key = JSON.stringify(key);
|
||||
const cached = this.cache[key] || {
|
||||
status: CacheStatus.NotStarted,
|
||||
@ -106,13 +106,10 @@ class Cache {
|
||||
) {
|
||||
return cached.result;
|
||||
}
|
||||
if (
|
||||
cached.status === CacheStatus.NotStarted &&
|
||||
!cacheOptions?.ignoreCache
|
||||
) {
|
||||
if (cached.status === CacheStatus.NotStarted) {
|
||||
this.innerGet(key).then((cachedResult) => {
|
||||
if (
|
||||
(cachedResult || cacheOptions?.forceCachedValue) &&
|
||||
(cachedResult || forceCachedValue) &&
|
||||
cached.status === CacheStatus.InProgress
|
||||
) {
|
||||
CacheDebug && console.log("Cached value", key, cachedResult);
|
||||
@ -203,27 +200,18 @@ class Cache {
|
||||
});
|
||||
}
|
||||
|
||||
cachedBlock(near, blockId, invalidate, cacheOptions) {
|
||||
cachedBlock(near, blockId, invalidate) {
|
||||
return this.cachedPromise(
|
||||
{
|
||||
action: Action.Block,
|
||||
blockId,
|
||||
},
|
||||
() => near.block(blockId),
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
}
|
||||
|
||||
cachedViewCall(
|
||||
near,
|
||||
contractId,
|
||||
methodName,
|
||||
args,
|
||||
blockId,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
) {
|
||||
cachedViewCall(near, contractId, methodName, args, blockId, invalidate) {
|
||||
return this.cachedPromise(
|
||||
{
|
||||
action: Action.ViewCall,
|
||||
@ -233,8 +221,7 @@ class Cache {
|
||||
blockId,
|
||||
},
|
||||
() => near.viewCall(contractId, methodName, args, blockId),
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
}
|
||||
|
||||
@ -279,7 +266,7 @@ class Cache {
|
||||
}
|
||||
}
|
||||
|
||||
cachedFetch(url, options, invalidate, cacheOptions) {
|
||||
cachedFetch(url, options, invalidate) {
|
||||
return this.cachedPromise(
|
||||
{
|
||||
action: Action.Fetch,
|
||||
@ -287,24 +274,22 @@ class Cache {
|
||||
options,
|
||||
},
|
||||
() => this.asyncFetch(url, options),
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
}
|
||||
|
||||
cachedCustomPromise(key, promise, invalidate, cacheOptions) {
|
||||
cachedCustomPromise(key, promise, invalidate) {
|
||||
return this.cachedPromise(
|
||||
{
|
||||
action: Action.CustomPromise,
|
||||
key,
|
||||
},
|
||||
() => promise(),
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
}
|
||||
|
||||
socialGet(near, keys, recursive, blockId, options, invalidate, cacheOptions) {
|
||||
socialGet(near, keys, recursive, blockId, options, invalidate) {
|
||||
if (!near) {
|
||||
return null;
|
||||
}
|
||||
@ -320,8 +305,7 @@ class Cache {
|
||||
"get",
|
||||
args,
|
||||
blockId,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
if (data === null) {
|
||||
return null;
|
||||
@ -341,7 +325,7 @@ class Cache {
|
||||
return data;
|
||||
}
|
||||
|
||||
socialIndex(near, action, key, options, invalidate, cacheOptions) {
|
||||
socialIndex(near, action, key, options, invalidate) {
|
||||
const res = this.cachedFetch(
|
||||
`${near.config.apiUrl}/index`,
|
||||
{
|
||||
@ -355,8 +339,7 @@ class Cache {
|
||||
options,
|
||||
}),
|
||||
},
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
|
||||
return res?.ok ? res.body : null;
|
||||
@ -371,9 +354,7 @@ class Cache {
|
||||
},
|
||||
undefined,
|
||||
invalidate,
|
||||
{
|
||||
forceCachedValue: true,
|
||||
}
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
@ -409,7 +390,7 @@ class Cache {
|
||||
}
|
||||
}
|
||||
|
||||
cachedEthersCall(ethersProvider, callee, args, invalidate, cacheOptions) {
|
||||
cachedEthersCall(ethersProvider, callee, args, invalidate) {
|
||||
if (!ethersProvider) {
|
||||
return null;
|
||||
}
|
||||
@ -420,8 +401,7 @@ class Cache {
|
||||
args,
|
||||
},
|
||||
() => ethersProvider[callee](...args),
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -437,14 +417,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;
|
||||
};
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ import BN from "bn.js";
|
||||
import * as nacl from "tweetnacl";
|
||||
import SecureIframe from "../components/SecureIframe";
|
||||
import { nanoid, customAlphabet } from "nanoid";
|
||||
import cloneDeep from "lodash.clonedeep";
|
||||
import _ from "lodash";
|
||||
import { Parser } from "acorn";
|
||||
import jsx from "acorn-jsx";
|
||||
|
||||
@ -738,13 +738,7 @@ class VmStack {
|
||||
if (args.length < 1) {
|
||||
throw new Error("Missing argument 'keys' for Social.getr");
|
||||
}
|
||||
return this.vm.cachedSocialGet(
|
||||
args[0],
|
||||
true,
|
||||
args[1],
|
||||
args[2],
|
||||
args[3]
|
||||
);
|
||||
return this.vm.cachedSocialGet(args[0], true, args[1], args[2]);
|
||||
} else if (
|
||||
(keyword === "Social" && callee === "get") ||
|
||||
callee === "socialGet"
|
||||
@ -752,25 +746,19 @@ class VmStack {
|
||||
if (args.length < 1) {
|
||||
throw new Error("Missing argument 'keys' for Social.get");
|
||||
}
|
||||
return this.vm.cachedSocialGet(
|
||||
args[0],
|
||||
false,
|
||||
args[1],
|
||||
args[2],
|
||||
args[3]
|
||||
);
|
||||
return this.vm.cachedSocialGet(args[0], false, args[1], args[2]);
|
||||
} else if (keyword === "Social" && callee === "keys") {
|
||||
if (args.length < 1) {
|
||||
throw new Error("Missing argument 'keys' for Social.keys");
|
||||
}
|
||||
return this.vm.cachedSocialKeys(...args);
|
||||
return this.vm.cachedSocialKeys(args[0], args[1], args[2]);
|
||||
} else if (keyword === "Social" && callee === "index") {
|
||||
if (args.length < 2) {
|
||||
throw new Error(
|
||||
"Missing argument 'action' and 'key` for Social.index"
|
||||
);
|
||||
}
|
||||
return this.vm.cachedIndex(...args);
|
||||
return this.vm.cachedIndex(args[0], args[1], args[2]);
|
||||
} else if (keyword === "Social" && callee === "set") {
|
||||
if (args.length < 1) {
|
||||
throw new Error("Missing argument 'data' for Social.set");
|
||||
@ -779,25 +767,17 @@ class VmStack {
|
||||
} else if (keyword === "Near" && callee === "view") {
|
||||
if (args.length < 2) {
|
||||
throw new Error(
|
||||
"Method: Near.view. Required arguments: 'contractName', 'methodName'. Optional: 'args', 'blockId/finality', 'subscribe', 'cacheOptions'"
|
||||
"Method: Near.view. Required arguments: 'contractName', 'methodName'. Optional: 'args', 'blockId/finality', 'subscribe'"
|
||||
);
|
||||
}
|
||||
const [
|
||||
contractName,
|
||||
methodName,
|
||||
viewArg,
|
||||
blockId,
|
||||
subscribe,
|
||||
cacheOptions,
|
||||
] = args;
|
||||
const [contractName, methodName, viewArg, blockId, subscribe] = args;
|
||||
|
||||
return this.vm.cachedNearView(
|
||||
contractName,
|
||||
methodName,
|
||||
viewArg,
|
||||
blockId,
|
||||
maybeSubscribe(subscribe, blockId),
|
||||
cacheOptions
|
||||
maybeSubscribe(subscribe, blockId)
|
||||
);
|
||||
} else if (keyword === "Near" && callee === "asyncView") {
|
||||
if (args.length < 2) {
|
||||
@ -807,11 +787,10 @@ class VmStack {
|
||||
}
|
||||
return this.vm.asyncNearView(...args);
|
||||
} else if (keyword === "Near" && callee === "block") {
|
||||
const [blockId, subscribe, cacheOptions] = args;
|
||||
const [blockId, subscribe] = args;
|
||||
return this.vm.cachedNearBlock(
|
||||
blockId,
|
||||
maybeSubscribe(subscribe, blockId),
|
||||
cacheOptions
|
||||
maybeSubscribe(subscribe, blockId)
|
||||
);
|
||||
} else if (keyword === "Near" && callee === "call") {
|
||||
if (args.length === 1) {
|
||||
@ -1828,7 +1807,7 @@ export default class VM {
|
||||
return deepCopy(promise(invalidate));
|
||||
}
|
||||
|
||||
cachedSocialGet(keys, recursive, blockId, options, cacheOptions) {
|
||||
cachedSocialGet(keys, recursive, blockId, options) {
|
||||
keys = Array.isArray(keys) ? keys : [keys];
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
@ -1838,8 +1817,7 @@ export default class VM {
|
||||
recursive,
|
||||
blockId,
|
||||
options,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
),
|
||||
options?.subscribe
|
||||
);
|
||||
@ -1855,7 +1833,7 @@ export default class VM {
|
||||
return this.cache.localStorageSet(domain, key, value);
|
||||
}
|
||||
|
||||
cachedSocialKeys(keys, blockId, options, cacheOptions) {
|
||||
cachedSocialKeys(keys, blockId, options) {
|
||||
keys = Array.isArray(keys) ? keys : [keys];
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
@ -1868,8 +1846,7 @@ export default class VM {
|
||||
options,
|
||||
},
|
||||
blockId,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
),
|
||||
options?.subscribe
|
||||
);
|
||||
@ -1879,28 +1856,20 @@ export default class VM {
|
||||
return this.near.viewCall(contractName, methodName, args, blockId);
|
||||
}
|
||||
|
||||
cachedEthersCall(callee, args, subscribe, cacheOptions) {
|
||||
cachedEthersCall(callee, args, subscribe) {
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
this.cache.cachedEthersCall(
|
||||
this.ethersProvider,
|
||||
callee,
|
||||
args,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
),
|
||||
subscribe
|
||||
);
|
||||
}
|
||||
|
||||
cachedNearView(
|
||||
contractName,
|
||||
methodName,
|
||||
args,
|
||||
blockId,
|
||||
subscribe,
|
||||
cacheOptions
|
||||
) {
|
||||
cachedNearView(contractName, methodName, args, blockId, subscribe) {
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
this.cache.cachedViewCall(
|
||||
@ -1909,17 +1878,15 @@ export default class VM {
|
||||
methodName,
|
||||
args,
|
||||
blockId,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
),
|
||||
subscribe
|
||||
);
|
||||
}
|
||||
|
||||
cachedNearBlock(blockId, subscribe, cacheOptions) {
|
||||
cachedNearBlock(blockId, subscribe) {
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
this.cache.cachedBlock(this.near, blockId, invalidate, cacheOptions),
|
||||
(invalidate) => this.cache.cachedBlock(this.near, blockId, invalidate),
|
||||
subscribe
|
||||
);
|
||||
}
|
||||
@ -1928,30 +1895,22 @@ export default class VM {
|
||||
return this.cache.asyncFetch(url, options);
|
||||
}
|
||||
|
||||
cachedFetch(url, options, cacheOptions) {
|
||||
cachedFetch(url, options) {
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
this.cache.cachedFetch(url, options, invalidate, cacheOptions),
|
||||
(invalidate) => this.cache.cachedFetch(url, options, invalidate),
|
||||
options?.subscribe
|
||||
);
|
||||
}
|
||||
|
||||
cachedIndex(action, key, options, cacheOptions) {
|
||||
cachedIndex(action, key, options) {
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
this.cache.socialIndex(
|
||||
this.near,
|
||||
action,
|
||||
key,
|
||||
options,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
),
|
||||
this.cache.socialIndex(this.near, action, key, options, invalidate),
|
||||
options?.subscribe
|
||||
);
|
||||
}
|
||||
|
||||
useCache(promiseGenerator, dataKey, options, cacheOptions) {
|
||||
useCache(promiseGenerator, dataKey, options) {
|
||||
return this.cachedPromise(
|
||||
(invalidate) =>
|
||||
this.cache.cachedCustomPromise(
|
||||
@ -1960,8 +1919,7 @@ export default class VM {
|
||||
dataKey,
|
||||
},
|
||||
promiseGenerator,
|
||||
invalidate,
|
||||
cacheOptions
|
||||
invalidate
|
||||
),
|
||||
options?.subscribe
|
||||
);
|
||||
@ -2061,7 +2019,7 @@ export default class VM {
|
||||
nacl: frozenNacl,
|
||||
get elliptic() {
|
||||
delete this.elliptic;
|
||||
this.elliptic = cloneDeep(elliptic);
|
||||
this.elliptic = _.cloneDeep(elliptic);
|
||||
return this.elliptic;
|
||||
},
|
||||
ethers: frozenEthers,
|
||||
|
||||
@ -5529,11 +5529,6 @@ locate-path@^5.0.0:
|
||||
dependencies:
|
||||
p-locate "^4.1.0"
|
||||
|
||||
lodash.clonedeep@^4.5.0:
|
||||
version "4.5.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
|
||||
integrity sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==
|
||||
|
||||
lodash.debounce@^4.0.8:
|
||||
version "4.0.8"
|
||||
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user