@novasamatech/storage-adapter 0.5.0-9
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.
- package/README.md +11 -0
- package/dist/fieldView.d.ts +26 -0
- package/dist/fieldView.js +53 -0
- package/dist/helpers.d.ts +2 -0
- package/dist/helpers.js +12 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/localStorage.d.ts +2 -0
- package/dist/localStorage.js +36 -0
- package/dist/memory.d.ts +2 -0
- package/dist/memory.js +22 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ResultAsync } from 'neverthrow';
|
|
2
|
+
import type { StorageAdapter } from './types.js';
|
|
3
|
+
type Params<T> = {
|
|
4
|
+
storage: StorageAdapter;
|
|
5
|
+
key: string;
|
|
6
|
+
initial: T;
|
|
7
|
+
autosync?: boolean;
|
|
8
|
+
from(value: string): T;
|
|
9
|
+
to(value: T): string | null;
|
|
10
|
+
};
|
|
11
|
+
export declare function fieldView<T>({ storage, initial, key, from, to, autosync }: Params<T>): {
|
|
12
|
+
read(): ResultAsync<T, Error>;
|
|
13
|
+
write(value: T): ResultAsync<T, Error> | ResultAsync<null, Error>;
|
|
14
|
+
clear(): ResultAsync<void, Error>;
|
|
15
|
+
subscribe(fn: (value: T) => void): VoidFunction;
|
|
16
|
+
};
|
|
17
|
+
export declare function fieldListView<T>(params: Params<T[]>): {
|
|
18
|
+
add(value: T): ResultAsync<T, Error>;
|
|
19
|
+
filter(fn: (value: T) => boolean): ResultAsync<T[], Error>;
|
|
20
|
+
mutate(fn: (value: T[]) => T[]): ResultAsync<T[], Error>;
|
|
21
|
+
read(): ResultAsync<T[], Error>;
|
|
22
|
+
write(value: T[]): ResultAsync<T[], Error> | ResultAsync<null, Error>;
|
|
23
|
+
clear(): ResultAsync<void, Error>;
|
|
24
|
+
subscribe(fn: (value: T[]) => void): VoidFunction;
|
|
25
|
+
};
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { okAsync } from 'neverthrow';
|
|
2
|
+
import { nonNullable } from './helpers.js';
|
|
3
|
+
export function fieldView({ storage, initial, key, from, to, autosync = true }) {
|
|
4
|
+
const enhancedStorage = {
|
|
5
|
+
read() {
|
|
6
|
+
return storage.read(key).map(x => (nonNullable(x) ? from(x) : initial));
|
|
7
|
+
},
|
|
8
|
+
write(value) {
|
|
9
|
+
const data = to(value);
|
|
10
|
+
if (data !== null) {
|
|
11
|
+
return storage.write(key, data).map(() => value);
|
|
12
|
+
}
|
|
13
|
+
return okAsync(null);
|
|
14
|
+
},
|
|
15
|
+
clear() {
|
|
16
|
+
return storage.clear(key);
|
|
17
|
+
},
|
|
18
|
+
subscribe(fn) {
|
|
19
|
+
if (autosync) {
|
|
20
|
+
enhancedStorage.read().andTee(fn);
|
|
21
|
+
}
|
|
22
|
+
return storage.subscribe(key, x => fn(nonNullable(x) ? from(x) : initial));
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
if (autosync) {
|
|
26
|
+
enhancedStorage.read();
|
|
27
|
+
}
|
|
28
|
+
return enhancedStorage;
|
|
29
|
+
}
|
|
30
|
+
export function fieldListView(params) {
|
|
31
|
+
const view = fieldView(params);
|
|
32
|
+
const listView = {
|
|
33
|
+
...view,
|
|
34
|
+
add(value) {
|
|
35
|
+
return listView.mutate(list => list.concat(value)).map(() => value);
|
|
36
|
+
},
|
|
37
|
+
filter(fn) {
|
|
38
|
+
return listView.mutate(list => {
|
|
39
|
+
const filtered = list.filter(fn);
|
|
40
|
+
return filtered.length === list.length ? list : filtered;
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
mutate(fn) {
|
|
44
|
+
return listView.read().andThen(list => {
|
|
45
|
+
const result = fn(list);
|
|
46
|
+
if (result === list)
|
|
47
|
+
return okAsync(result);
|
|
48
|
+
return listView.write(result).map(() => result);
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
return listView;
|
|
53
|
+
}
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function toError(err) {
|
|
2
|
+
if (err instanceof Error) {
|
|
3
|
+
return err;
|
|
4
|
+
}
|
|
5
|
+
if (err) {
|
|
6
|
+
return new Error(err.toString());
|
|
7
|
+
}
|
|
8
|
+
return new Error('Unknown error occurred.');
|
|
9
|
+
}
|
|
10
|
+
export function nonNullable(value) {
|
|
11
|
+
return value !== null && value !== undefined;
|
|
12
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createNanoEvents } from 'nanoevents';
|
|
2
|
+
import { fromAsyncThrowable } from 'neverthrow';
|
|
3
|
+
import { toError } from './helpers.js';
|
|
4
|
+
export function createLocalStorageAdapter(prefix) {
|
|
5
|
+
const events = createNanoEvents();
|
|
6
|
+
const withPrefix = (key) => `PAPP_${prefix}_${key}`;
|
|
7
|
+
return {
|
|
8
|
+
write: fromAsyncThrowable(async (key, value) => {
|
|
9
|
+
const prefixedKey = withPrefix(key);
|
|
10
|
+
localStorage.setItem(prefixedKey, value);
|
|
11
|
+
events.emit(prefixedKey, value);
|
|
12
|
+
}, toError),
|
|
13
|
+
read: fromAsyncThrowable(async (key) => {
|
|
14
|
+
return localStorage.getItem(withPrefix(key));
|
|
15
|
+
}, toError),
|
|
16
|
+
clear: fromAsyncThrowable(async (key) => {
|
|
17
|
+
const prefixedKey = withPrefix(key);
|
|
18
|
+
localStorage.removeItem(prefixedKey);
|
|
19
|
+
events.emit(prefixedKey, null);
|
|
20
|
+
}, toError),
|
|
21
|
+
subscribe(key, callback) {
|
|
22
|
+
const prefixedKey = withPrefix(key);
|
|
23
|
+
const unsubscribeLocalListener = events.on(prefixedKey, callback);
|
|
24
|
+
const externalListener = (event) => {
|
|
25
|
+
if (event.storageArea === localStorage && event.key === prefixedKey) {
|
|
26
|
+
callback(event.newValue);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
window.addEventListener('storage', externalListener);
|
|
30
|
+
return () => {
|
|
31
|
+
unsubscribeLocalListener();
|
|
32
|
+
window.removeEventListener('storage', externalListener);
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
package/dist/memory.d.ts
ADDED
package/dist/memory.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createNanoEvents } from 'nanoevents';
|
|
2
|
+
import { fromAsyncThrowable } from 'neverthrow';
|
|
3
|
+
export function createMemoryAdapter(external) {
|
|
4
|
+
const events = createNanoEvents();
|
|
5
|
+
const storage = external ? { ...external } : {};
|
|
6
|
+
return {
|
|
7
|
+
write: fromAsyncThrowable(async (key, value) => {
|
|
8
|
+
storage[key] = value;
|
|
9
|
+
events.emit(key, value);
|
|
10
|
+
}),
|
|
11
|
+
read: fromAsyncThrowable(async (key) => {
|
|
12
|
+
return storage[key] ?? null;
|
|
13
|
+
}),
|
|
14
|
+
clear: fromAsyncThrowable(async (key) => {
|
|
15
|
+
delete storage[key];
|
|
16
|
+
events.emit(key, null);
|
|
17
|
+
}),
|
|
18
|
+
subscribe(key, callback) {
|
|
19
|
+
return events.on(key, callback);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ResultAsync } from 'neverthrow';
|
|
2
|
+
export type StorageAdapter = {
|
|
3
|
+
write(key: string, value: string): ResultAsync<void, Error>;
|
|
4
|
+
read(key: string): ResultAsync<string | null, Error>;
|
|
5
|
+
clear(key: string): ResultAsync<void, Error>;
|
|
6
|
+
subscribe(key: string, callback: (value: string | null) => unknown): VoidFunction;
|
|
7
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { ResultAsync } from 'neverthrow';
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@novasamatech/storage-adapter",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.5.0-9",
|
|
5
|
+
"description": "Statement store integration",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/novasamatech/spektr-sdk.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"polkadot"
|
|
13
|
+
],
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"nanoevents": "^9.1.0",
|
|
28
|
+
"neverthrow": "^8.2.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
}
|
|
33
|
+
}
|