scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
117 lines • 2.28 kB
JavaScript
import { AbsKeychain } from "scriptable-abstract";
const DEFAULT_STATE = {
items: /* @__PURE__ */ new Map(),
accessGroups: [],
lastAccess: null
};
class MockKeychain extends AbsKeychain {
static get instance() {
return super.instance;
}
constructor() {
super(DEFAULT_STATE);
}
/**
* @inheritdoc
*/
set(key, value) {
this.setState((state) => {
const items = new Map(state.items);
items.set(key, value);
return {
...state,
items,
lastAccess: /* @__PURE__ */ new Date()
};
});
}
/**
* @inheritdoc
*/
get(key) {
const value = this.state.items.get(key);
if (value === void 0) {
throw new Error(`No value found for key: ${key}`);
}
this.setState({ lastAccess: /* @__PURE__ */ new Date() });
return value;
}
/**
* @inheritdoc
*/
remove(key) {
this.setState((state) => {
const items = new Map(state.items);
items.delete(key);
return {
...state,
items,
lastAccess: /* @__PURE__ */ new Date()
};
});
}
/**
* @inheritdoc
*/
contains(key) {
return this.state.items.has(key);
}
/**
* @inheritdoc
*/
get accessGroup() {
return this.state.accessGroups[this.state.accessGroups.length - 1] ?? null;
}
/**
* @inheritdoc
*/
set accessGroup(group) {
if (group === null) {
this.setState((state) => ({
...state,
accessGroups: state.accessGroups.slice(0, -1)
}));
} else {
this.setState((state) => ({
...state,
accessGroups: [...state.accessGroups, group]
}));
}
}
/**
* @additional
* Get all stored keys
*/
getKeys() {
return Array.from(this.state.items.keys());
}
/**
* @additional
* Get all access groups
*/
getAccessGroups() {
return [...this.state.accessGroups];
}
/**
* @additional
* Get last access time
*/
getLastAccess() {
return this.state.lastAccess;
}
/**
* @additional
* Clear all stored items
*/
clear() {
this.setState({
items: /* @__PURE__ */ new Map(),
accessGroups: [],
lastAccess: /* @__PURE__ */ new Date()
});
}
}
export {
MockKeychain
};
//# sourceMappingURL=keychain.js.map