@yaasl/core 0.14.0-alpha.0 → 0.14.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,4 +14,6 @@ export interface IndexedDbOptions {
14
14
  *
15
15
  * @returns The effect to be used on atoms.
16
16
  **/
17
- export declare const indexedDb: (...[optionsArg]: [] | [undefined] | [IndexedDbOptions]) => import("./create-effect").EffectAtomCallback<IndexedDbOptions | undefined, unknown>;
17
+ export declare const indexedDb: ((...[optionsArg]: [] | [undefined] | [IndexedDbOptions]) => import("./create-effect").EffectAtomCallback<IndexedDbOptions | undefined, unknown>) & {
18
+ getAllKeys: () => Promise<string[]>;
19
+ };
@@ -1,8 +1,9 @@
1
- export declare class IdbStore<T> {
1
+ export declare class IdbStore<T = unknown> {
2
2
  private name;
3
3
  private database?;
4
4
  constructor(name: string);
5
5
  private getStore;
6
+ getAllKeys(): Promise<string[]>;
6
7
  get(key: string): Promise<T | undefined>;
7
8
  set(key: string, value: T): Promise<IDBValidKey | undefined>;
8
9
  delete(key: string): Promise<undefined>;
@@ -14,6 +14,13 @@ const create_effect_1 = require("./create-effect");
14
14
  const base_1 = require("../base");
15
15
  const idb_store_1 = require("../utils/idb-store");
16
16
  let atomDb = null;
17
+ const getAtomDb = () => {
18
+ var _a;
19
+ if (!atomDb) {
20
+ atomDb = new idb_store_1.IdbStore((_a = base_1.CONFIG.name) !== null && _a !== void 0 ? _a : "yaasl");
21
+ }
22
+ return atomDb;
23
+ };
17
24
  /** Middleware to save and load atom values to an indexedDb.
18
25
  *
19
26
  * Will use one database and store for all atoms with your `CONFIG.name`
@@ -26,16 +33,13 @@ let atomDb = null;
26
33
  *
27
34
  * @returns The effect to be used on atoms.
28
35
  **/
29
- exports.indexedDb = (0, create_effect_1.createEffect)(({ atom, options }) => {
36
+ exports.indexedDb = Object.assign((0, create_effect_1.createEffect)(({ atom, options }) => {
30
37
  var _a;
31
38
  const key = (_a = options === null || options === void 0 ? void 0 : options.key) !== null && _a !== void 0 ? _a : atom.name;
32
39
  return {
33
40
  sort: "pre",
34
41
  init: (_a) => __awaiter(void 0, [_a], void 0, function* ({ atom, set }) {
35
- var _b;
36
- if (!atomDb) {
37
- atomDb = new idb_store_1.IdbStore((_b = base_1.CONFIG.name) !== null && _b !== void 0 ? _b : "yaasl");
38
- }
42
+ const atomDb = getAtomDb();
39
43
  const existing = yield atomDb.get(key);
40
44
  if (existing != null) {
41
45
  set(existing);
@@ -54,4 +58,6 @@ exports.indexedDb = (0, create_effect_1.createEffect)(({ atom, options }) => {
54
58
  }
55
59
  },
56
60
  };
61
+ }), {
62
+ getAllKeys: () => getAtomDb().getAllKeys(),
57
63
  });
@@ -37,6 +37,11 @@ class IdbStore {
37
37
  return database === null || database === void 0 ? void 0 : database.transaction(this.name, mode).objectStore(this.name);
38
38
  });
39
39
  }
40
+ getAllKeys() {
41
+ return this.getStore("readonly").then(store => !store
42
+ ? []
43
+ : promisifyRequest(store.getAllKeys()).then(keys => keys.map(String)));
44
+ }
40
45
  get(key) {
41
46
  return __awaiter(this, void 0, void 0, function* () {
42
47
  return this.getStore("readonly").then(store => !store ? undefined : promisifyRequest(store.get(key)));
@@ -2,6 +2,12 @@ import { createEffect } from "./create-effect";
2
2
  import { CONFIG } from "../base";
3
3
  import { IdbStore } from "../utils/idb-store";
4
4
  let atomDb = null;
5
+ const getAtomDb = () => {
6
+ if (!atomDb) {
7
+ atomDb = new IdbStore(CONFIG.name ?? "yaasl");
8
+ }
9
+ return atomDb;
10
+ };
5
11
  /** Middleware to save and load atom values to an indexedDb.
6
12
  *
7
13
  * Will use one database and store for all atoms with your `CONFIG.name`
@@ -14,14 +20,12 @@ let atomDb = null;
14
20
  *
15
21
  * @returns The effect to be used on atoms.
16
22
  **/
17
- export const indexedDb = createEffect(({ atom, options }) => {
23
+ export const indexedDb = Object.assign(createEffect(({ atom, options }) => {
18
24
  const key = options?.key ?? atom.name;
19
25
  return {
20
26
  sort: "pre",
21
27
  init: async ({ atom, set }) => {
22
- if (!atomDb) {
23
- atomDb = new IdbStore(CONFIG.name ?? "yaasl");
24
- }
28
+ const atomDb = getAtomDb();
25
29
  const existing = await atomDb.get(key);
26
30
  if (existing != null) {
27
31
  set(existing);
@@ -40,4 +44,6 @@ export const indexedDb = createEffect(({ atom, options }) => {
40
44
  }
41
45
  },
42
46
  };
47
+ }), {
48
+ getAllKeys: () => getAtomDb().getAllKeys(),
43
49
  });
@@ -24,6 +24,11 @@ export class IdbStore {
24
24
  const database = await this.database;
25
25
  return database?.transaction(this.name, mode).objectStore(this.name);
26
26
  }
27
+ getAllKeys() {
28
+ return this.getStore("readonly").then(store => !store
29
+ ? []
30
+ : promisifyRequest(store.getAllKeys()).then(keys => keys.map(String)));
31
+ }
27
32
  async get(key) {
28
33
  return this.getStore("readonly").then(store => !store ? undefined : promisifyRequest(store.get(key)));
29
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaasl/core",
3
- "version": "0.14.0-alpha.0",
3
+ "version": "0.14.0-alpha.1",
4
4
  "description": "yet another atomic store library (vanilla-js)",
5
5
  "author": "PrettyCoffee",
6
6
  "license": "MIT",