Merge pull request #123 from NearSocial/missing-changes

## 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.
This commit is contained in:
Evgeny Kuzyakov 2023-09-20 09:36:38 -07:00 committed by GitHub
commit a6ed652ff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 129 additions and 56 deletions

View File

@ -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.
@ -61,6 +66,7 @@ const index = Social.index(
}
);
```
- Replace `lodash` dependency with `lodash.clonedeep` to reduce bundle size.
## 2.3.2

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -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": [
@ -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",

View File

@ -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,
@ -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);
@ -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,
@ -221,7 +233,8 @@ class Cache {
blockId,
},
() => near.viewCall(contractId, methodName, args, blockId),
invalidate
invalidate,
cacheOptions
);
}
@ -266,7 +279,7 @@ class Cache {
}
}
cachedFetch(url, options, invalidate) {
cachedFetch(url, options, invalidate, cacheOptions) {
return this.cachedPromise(
{
action: Action.Fetch,
@ -274,22 +287,24 @@ class Cache {
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;
}
@ -305,7 +320,8 @@ class Cache {
"get",
args,
blockId,
invalidate
invalidate,
cacheOptions
);
if (data === null) {
return null;
@ -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`,
{
@ -339,7 +355,8 @@ class Cache {
options,
}),
},
invalidate
invalidate,
cacheOptions
);
return res?.ok ? res.body : null;
@ -354,7 +371,9 @@ class Cache {
},
undefined,
invalidate,
true
{
forceCachedValue: true,
}
);
}
@ -390,7 +409,7 @@ class Cache {
}
}
cachedEthersCall(ethersProvider, callee, args, invalidate) {
cachedEthersCall(ethersProvider, callee, args, invalidate, cacheOptions) {
if (!ethersProvider) {
return null;
}
@ -401,7 +420,8 @@ class Cache {
args,
},
() => ethersProvider[callee](...args),
invalidate
invalidate,
cacheOptions
);
}
}
@ -417,7 +437,7 @@ const useSecondaryCache = singletonHook(secondaryCache, () => {
return secondaryCache;
});
export const useCache = networkId => {
export const useCache = (networkId) => {
const near = useNear();
const defaultCache = useDefaultCache();
const secondaryCache = useSecondaryCache();
@ -427,4 +447,4 @@ export const useCache = networkId => {
}
return secondaryCache;
}
};

View File

@ -28,7 +28,7 @@ import BN from "bn.js";
import * as nacl from "tweetnacl";
import SecureIframe from "../components/SecureIframe";
import { nanoid, customAlphabet } from "nanoid";
import _ from "lodash";
import cloneDeep from "lodash.clonedeep";
import { Parser } from "acorn";
import jsx from "acorn-jsx";
import { ethers } from "ethers";
@ -741,7 +741,13 @@ 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]);
return this.vm.cachedSocialGet(
args[0],
true,
args[1],
args[2],
args[3]
);
} else if (
(keyword === "Social" && callee === "get") ||
callee === "socialGet"
@ -749,19 +755,25 @@ 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]);
return this.vm.cachedSocialGet(
args[0],
false,
args[1],
args[2],
args[3]
);
} else if (keyword === "Social" && callee === "keys") {
if (args.length < 1) {
throw new Error("Missing argument 'keys' for Social.keys");
}
return this.vm.cachedSocialKeys(args[0], args[1], args[2]);
return this.vm.cachedSocialKeys(...args);
} 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[0], args[1], args[2]);
return this.vm.cachedIndex(...args);
} else if (keyword === "Social" && callee === "set") {
if (args.length < 1) {
throw new Error("Missing argument 'data' for Social.set");
@ -770,17 +782,25 @@ 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'"
"Method: Near.view. Required arguments: 'contractName', 'methodName'. Optional: 'args', 'blockId/finality', 'subscribe', 'cacheOptions'"
);
}
const [contractName, methodName, viewArg, blockId, subscribe] = args;
const [
contractName,
methodName,
viewArg,
blockId,
subscribe,
cacheOptions,
] = args;
return this.vm.cachedNearView(
contractName,
methodName,
viewArg,
blockId,
maybeSubscribe(subscribe, blockId)
maybeSubscribe(subscribe, blockId),
cacheOptions
);
} else if (keyword === "Near" && callee === "asyncView") {
if (args.length < 2) {
@ -790,10 +810,11 @@ class VmStack {
}
return this.vm.asyncNearView(...args);
} else if (keyword === "Near" && callee === "block") {
const [blockId, subscribe] = args;
const [blockId, subscribe, cacheOptions] = args;
return this.vm.cachedNearBlock(
blockId,
maybeSubscribe(subscribe, blockId)
maybeSubscribe(subscribe, blockId),
cacheOptions
);
} else if (keyword === "Near" && callee === "call") {
if (args.length === 1) {
@ -1899,7 +1920,7 @@ export default class VM {
return deepCopy(promise(invalidate));
}
cachedSocialGet(keys, recursive, blockId, options) {
cachedSocialGet(keys, recursive, blockId, options, cacheOptions) {
keys = Array.isArray(keys) ? keys : [keys];
return this.cachedPromise(
(invalidate) =>
@ -1909,7 +1930,8 @@ export default class VM {
recursive,
blockId,
options,
invalidate
invalidate,
cacheOptions
),
options?.subscribe
);
@ -1925,7 +1947,7 @@ export default class VM {
return this.cache.localStorageSet(domain, key, value);
}
cachedSocialKeys(keys, blockId, options) {
cachedSocialKeys(keys, blockId, options, cacheOptions) {
keys = Array.isArray(keys) ? keys : [keys];
return this.cachedPromise(
(invalidate) =>
@ -1938,7 +1960,8 @@ export default class VM {
options,
},
blockId,
invalidate
invalidate,
cacheOptions
),
options?.subscribe
);
@ -1948,20 +1971,28 @@ export default class VM {
return this.near.viewCall(contractName, methodName, args, blockId);
}
cachedEthersCall(callee, args, subscribe) {
cachedEthersCall(callee, args, subscribe, cacheOptions) {
return this.cachedPromise(
(invalidate) =>
this.cache.cachedEthersCall(
this.ethersProvider,
callee,
args,
invalidate
invalidate,
cacheOptions
),
subscribe
);
}
cachedNearView(contractName, methodName, args, blockId, subscribe) {
cachedNearView(
contractName,
methodName,
args,
blockId,
subscribe,
cacheOptions
) {
return this.cachedPromise(
(invalidate) =>
this.cache.cachedViewCall(
@ -1970,15 +2001,17 @@ export default class VM {
methodName,
args,
blockId,
invalidate
invalidate,
cacheOptions
),
subscribe
);
}
cachedNearBlock(blockId, subscribe) {
cachedNearBlock(blockId, subscribe, cacheOptions) {
return this.cachedPromise(
(invalidate) => this.cache.cachedBlock(this.near, blockId, invalidate),
(invalidate) =>
this.cache.cachedBlock(this.near, blockId, invalidate, cacheOptions),
subscribe
);
}
@ -1987,22 +2020,30 @@ export default class VM {
return this.cache.asyncFetch(url, options);
}
cachedFetch(url, options) {
return this.cachedPromise(
(invalidate) => this.cache.cachedFetch(url, options, invalidate),
options?.subscribe
);
}
cachedIndex(action, key, options) {
cachedFetch(url, options, cacheOptions) {
return this.cachedPromise(
(invalidate) =>
this.cache.socialIndex(this.near, action, key, options, invalidate),
this.cache.cachedFetch(url, options, invalidate, cacheOptions),
options?.subscribe
);
}
useCache(promiseGenerator, dataKey, options) {
cachedIndex(action, key, options, cacheOptions) {
return this.cachedPromise(
(invalidate) =>
this.cache.socialIndex(
this.near,
action,
key,
options,
invalidate,
cacheOptions
),
options?.subscribe
);
}
useCache(promiseGenerator, dataKey, options, cacheOptions) {
return this.cachedPromise(
(invalidate) =>
this.cache.cachedCustomPromise(
@ -2011,7 +2052,8 @@ export default class VM {
dataKey,
},
promiseGenerator,
invalidate
invalidate,
cacheOptions
),
options?.subscribe
);
@ -2113,7 +2155,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,

View File

@ -5529,6 +5529,11 @@ 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"