From f36a4a213c10bd80e6d109f21e2d2cb765adeb58 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Fri, 20 May 2022 13:55:29 +0800 Subject: [PATCH] add lookup set --- src/collections/index.js | 4 +++- src/collections/lookup-set.js | 34 ++++++++++++++++++++++++++++++++++ src/index.js | 10 ++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/collections/lookup-set.js diff --git a/src/collections/index.js b/src/collections/index.js index b7ffe51d5..258a34850 100644 --- a/src/collections/index.js +++ b/src/collections/index.js @@ -1,7 +1,9 @@ import { LookupMap } from "./lookup-map"; import { Vector } from "./vector"; +import { LookupSet } from "./lookup-set"; export { LookupMap, - Vector + Vector, + LookupSet } \ No newline at end of file diff --git a/src/collections/lookup-set.js b/src/collections/lookup-set.js new file mode 100644 index 000000000..746a5878d --- /dev/null +++ b/src/collections/lookup-set.js @@ -0,0 +1,34 @@ +import * as near from '../api' + +export class LookupSet { + constructor(keyPrefix) { + this.keyPrefix = keyPrefix + } + + contains(key) { + let storageKey = this.keyPrefix + key + return near.jsvmStorageHasKey(storageKey) + } + + remove(key) { + let storageKey = this.keyPrefix + key + if (near.jsvmStorageRemove(storageKey)) { + return near.storageGetEvicted() + } + return null + } + + set(key) { + let storageKey = this.keyPrefix + key + if (near.jsvmStorageWrite(storageKey, '')) { + return near.storageGetEvicted() + } + return null + } + + extend(kvs) { + for(let kv of kvs) { + this.set(kv[0]) + } + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 458f2c66f..fd5aa4655 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,11 @@ import { } from './near-contract' import * as near from './api' -import { LookupMap } from './collections/lookup-map' +import { + LookupMap, + Vector, + LookupSet +} from './collections' export { call, @@ -17,5 +21,7 @@ export { NearBindgen, NearContract, near, - LookupMap + LookupMap, + Vector, + LookupSet } \ No newline at end of file