@welshman/app 0.0.1 → 0.0.2
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/build/src/collection.cjs +40 -0
- package/build/src/collection.cjs.map +1 -0
- package/build/src/collection.d.ts +15 -0
- package/build/src/collection.mjs +36 -0
- package/build/src/collection.mjs.map +1 -0
- package/build/src/core.cjs +32 -0
- package/build/src/core.cjs.map +1 -0
- package/build/src/core.d.ts +16 -0
- package/build/src/core.mjs +26 -0
- package/build/src/core.mjs.map +1 -0
- package/build/src/follows.cjs +31 -0
- package/build/src/follows.cjs.map +1 -0
- package/build/src/follows.d.ts +9 -0
- package/build/src/follows.mjs +27 -0
- package/build/src/follows.mjs.map +1 -0
- package/build/src/freshness.cjs +21 -0
- package/build/src/freshness.cjs.map +1 -0
- package/build/src/freshness.d.ts +8 -0
- package/build/src/freshness.mjs +14 -0
- package/build/src/freshness.mjs.map +1 -0
- package/build/src/handles.cjs +43 -0
- package/build/src/handles.cjs.map +1 -0
- package/build/src/handles.d.ts +16 -0
- package/build/src/handles.mjs +37 -0
- package/build/src/handles.mjs.map +1 -0
- package/build/src/index.cjs +23 -2
- package/build/src/index.cjs.map +1 -1
- package/build/src/index.d.ts +15 -2
- package/build/src/index.mjs +23 -2
- package/build/src/index.mjs.map +1 -1
- package/build/src/mutes.cjs +31 -0
- package/build/src/mutes.cjs.map +1 -0
- package/build/src/mutes.d.ts +9 -0
- package/build/src/mutes.mjs +27 -0
- package/build/src/mutes.mjs.map +1 -0
- package/build/src/plaintext.cjs +24 -0
- package/build/src/plaintext.cjs.map +1 -0
- package/build/src/plaintext.d.ts +8 -0
- package/build/src/plaintext.mjs +18 -0
- package/build/src/plaintext.mjs.map +1 -0
- package/build/src/profiles.cjs +40 -0
- package/build/src/profiles.cjs.map +1 -0
- package/build/src/profiles.d.ts +13 -0
- package/build/src/profiles.mjs +34 -0
- package/build/src/profiles.mjs.map +1 -0
- package/build/src/relaySelections.cjs +28 -0
- package/build/src/relaySelections.cjs.map +1 -0
- package/build/src/relaySelections.d.ts +12 -0
- package/build/src/relaySelections.mjs +22 -0
- package/build/src/relaySelections.mjs.map +1 -0
- package/build/src/relays.cjs +122 -0
- package/build/src/relays.cjs.map +1 -0
- package/build/src/relays.d.ts +28 -0
- package/build/src/relays.mjs +115 -0
- package/build/src/relays.mjs.map +1 -0
- package/build/src/session.cjs +27 -18
- package/build/src/session.cjs.map +1 -1
- package/build/src/session.d.ts +13 -1
- package/build/src/session.mjs +23 -18
- package/build/src/session.mjs.map +1 -1
- package/build/src/storage.cjs +91 -0
- package/build/src/storage.cjs.map +1 -0
- package/build/src/storage.d.ts +21 -0
- package/build/src/storage.mjs +82 -0
- package/build/src/storage.mjs.map +1 -0
- package/build/src/thunk.cjs +44 -0
- package/build/src/thunk.cjs.map +1 -0
- package/build/src/thunk.d.ts +37 -0
- package/build/src/thunk.mjs +39 -0
- package/build/src/thunk.mjs.map +1 -0
- package/build/src/topics.cjs +30 -0
- package/build/src/topics.cjs.map +1 -0
- package/build/src/topics.d.ts +10 -0
- package/build/src/topics.mjs +27 -0
- package/build/src/topics.mjs.map +1 -0
- package/build/src/util.cjs +62 -0
- package/build/src/util.cjs.map +1 -0
- package/build/src/util.d.ts +22 -0
- package/build/src/util.mjs +47 -0
- package/build/src/util.mjs.map +1 -0
- package/build/src/zappers.cjs +43 -0
- package/build/src/zappers.cjs.map +1 -0
- package/build/src/zappers.d.ts +10 -0
- package/build/src/zappers.mjs +38 -0
- package/build/src/zappers.mjs.map +1 -0
- package/package.json +10 -4
- package/build/src/relay.cjs +0 -13
- package/build/src/relay.cjs.map +0 -1
- package/build/src/relay.d.ts +0 -6
- package/build/src/relay.mjs +0 -10
- package/build/src/relay.mjs.map +0 -1
- package/build/src/types.cjs +0 -3
- package/build/src/types.cjs.map +0 -1
- package/build/src/types.d.ts +0 -15
- package/build/src/types.mjs +0 -2
- package/build/src/types.mjs.map +0 -1
package/build/src/session.d.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
/// <reference types="svelte" />
|
|
2
|
+
import { type Nip46Handler } from "@welshman/signer";
|
|
2
3
|
import { Nip46Signer, Nip07Signer, Nip01Signer } from "@welshman/signer";
|
|
3
|
-
|
|
4
|
+
export type Session = {
|
|
5
|
+
method: string;
|
|
6
|
+
pubkey: string;
|
|
7
|
+
token?: string;
|
|
8
|
+
secret?: string;
|
|
9
|
+
handler?: Nip46Handler;
|
|
10
|
+
};
|
|
4
11
|
export declare const pubkey: import("svelte/store").Writable<string | null> & {
|
|
5
12
|
get: () => string | null;
|
|
6
13
|
};
|
|
@@ -12,8 +19,13 @@ export declare const session: import("svelte/store").Readable<Session | null> &
|
|
|
12
19
|
};
|
|
13
20
|
export declare const getSession: (pubkey: string) => Session;
|
|
14
21
|
export declare const addSession: (session: Session) => void;
|
|
22
|
+
export declare const putSession: (session: Session) => void;
|
|
23
|
+
export declare const updateSession: (pubkey: string, f: (session: Session) => Session) => void;
|
|
24
|
+
export declare const dropSession: (pubkey: string) => void;
|
|
15
25
|
export declare const nip46Perms = "sign_event:22242,nip04_encrypt,nip04_decrypt,nip44_encrypt,nip44_decrypt";
|
|
16
26
|
export declare const getSigner: (...args: any[]) => Nip07Signer | Nip01Signer | Nip46Signer | null;
|
|
17
27
|
export declare const signer: import("svelte/store").Readable<Nip07Signer | Nip01Signer | Nip46Signer | null> & {
|
|
18
28
|
get: () => Nip07Signer | Nip01Signer | Nip46Signer | null;
|
|
19
29
|
};
|
|
30
|
+
export declare const authChallenges: Set<unknown>;
|
|
31
|
+
export declare const onAuth: (url: string, challenge: string) => Promise<any>;
|
package/build/src/session.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { derived } from "svelte/store";
|
|
2
|
-
import { memoize, assoc } from "@welshman/lib";
|
|
2
|
+
import { memoize, omit, equals, assoc } from "@welshman/lib";
|
|
3
3
|
import { createEvent } from "@welshman/util";
|
|
4
4
|
import { withGetter, synced } from "@welshman/store";
|
|
5
5
|
import { NetworkContext } from "@welshman/net";
|
|
@@ -12,6 +12,13 @@ export const addSession = (session) => {
|
|
|
12
12
|
sessions.update(assoc(session.pubkey, session));
|
|
13
13
|
pubkey.set(session.pubkey);
|
|
14
14
|
};
|
|
15
|
+
export const putSession = (session) => {
|
|
16
|
+
if (!equals(getSession(session.pubkey), session)) {
|
|
17
|
+
sessions.update(assoc(session.pubkey, session));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
export const updateSession = (pubkey, f) => putSession(f(getSession(pubkey)));
|
|
21
|
+
export const dropSession = (pubkey) => sessions.update($sessions => omit([pubkey], $sessions));
|
|
15
22
|
export const nip46Perms = "sign_event:22242,nip04_encrypt,nip04_decrypt,nip44_encrypt,nip44_decrypt";
|
|
16
23
|
export const getSigner = memoize((session) => {
|
|
17
24
|
switch (session === null || session === void 0 ? void 0 : session.method) {
|
|
@@ -26,21 +33,19 @@ export const getSigner = memoize((session) => {
|
|
|
26
33
|
}
|
|
27
34
|
});
|
|
28
35
|
export const signer = withGetter(derived(session, getSigner));
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
});
|
|
36
|
+
export const authChallenges = new Set();
|
|
37
|
+
export const onAuth = async (url, challenge) => {
|
|
38
|
+
if (authChallenges.has(challenge) || !signer.get()) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
authChallenges.add(challenge);
|
|
42
|
+
const event = await signer.get().sign(createEvent(22242, {
|
|
43
|
+
tags: [
|
|
44
|
+
["relay", url],
|
|
45
|
+
["challenge", challenge],
|
|
46
|
+
],
|
|
47
|
+
}));
|
|
48
|
+
NetworkContext.pool.get(url).send(["AUTH", event]);
|
|
49
|
+
return event;
|
|
50
|
+
};
|
|
46
51
|
//# sourceMappingURL=session.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.mjs","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"OAAO,EAAC,OAAO,EAAC,MAAM,cAAc;OAC7B,EAAC,OAAO,EAAE,KAAK,EAAC,MAAM,eAAe;
|
|
1
|
+
{"version":3,"file":"session.mjs","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"OAAO,EAAC,OAAO,EAAC,MAAM,cAAc;OAC7B,EAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAC,MAAM,eAAe;OACnD,EAAC,WAAW,EAAC,MAAM,gBAAgB;OACnC,EAAC,UAAU,EAAE,MAAM,EAAC,MAAM,iBAAiB;OAE3C,EAAC,cAAc,EAAC,MAAM,eAAe;OACrC,EAAC,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAC,MAAM,kBAAkB;AAUnF,MAAM,CAAC,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAgB,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAA;AAEvE,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC,MAAM,CAA0B,UAAU,EAAE,EAAE,CAAC,CAAC,CAAA;AAEnF,MAAM,CAAC,MAAM,OAAO,GAAG,UAAU,CAC/B,OAAO,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAC7F,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,CAAA;AAEpE,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,OAAgB,EA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AAC,CAAC,CAAA;QAC5F;YACE,OAAO,IAAI,CAAA;KACd;AACH,CAAC,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,CAAA;AAE7D,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,GAAG,EAAE,CAAA;AAEvC,MAAM,CAAC,MAAM,MAAM,GAAG,KAAK,EAAE,GAAW,EAAE,SAAiB,EAAE,EAAE;IAC7D,IAAI,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,EAAE;QAClD,OAAM;KACP;IAED,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;IAE7B,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,GAAG,EAAG,CAAC,IAAI,CACpC,WAAW,CAAC,KAAK,EAAE;QACjB,IAAI,EAAE;YACJ,CAAC,OAAO,EAAE,GAAG,CAAC;YACd,CAAC,WAAW,EAAE,SAAS,CAAC;SACzB;KACF,CAAC,CACH,CAAA;IAED,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAA;IAElD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clearStorage = exports.closeStorage = exports.initStorage = exports.initIndexedDbAdapter = exports.bulkDelete = exports.bulkPut = exports.getAll = exports.DB_NAME = exports.subs = exports.dead = exports.db = void 0;
|
|
4
|
+
const idb_1 = require("idb");
|
|
5
|
+
const throttle_debounce_1 = require("throttle-debounce");
|
|
6
|
+
const store_1 = require("svelte/store");
|
|
7
|
+
const lib_1 = require("@welshman/lib");
|
|
8
|
+
const store_2 = require("@welshman/store");
|
|
9
|
+
exports.dead = (0, store_2.withGetter)((0, store_1.writable)(false));
|
|
10
|
+
exports.subs = [];
|
|
11
|
+
exports.DB_NAME = "flotilla";
|
|
12
|
+
const getAll = async (name) => {
|
|
13
|
+
const tx = exports.db.transaction(name, "readwrite");
|
|
14
|
+
const store = tx.objectStore(name);
|
|
15
|
+
const result = await store.getAll();
|
|
16
|
+
await tx.done;
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
exports.getAll = getAll;
|
|
20
|
+
const bulkPut = async (name, data) => {
|
|
21
|
+
const tx = exports.db.transaction(name, "readwrite");
|
|
22
|
+
const store = tx.objectStore(name);
|
|
23
|
+
await Promise.all(data.map(item => store.put(item)));
|
|
24
|
+
await tx.done;
|
|
25
|
+
};
|
|
26
|
+
exports.bulkPut = bulkPut;
|
|
27
|
+
const bulkDelete = async (name, ids) => {
|
|
28
|
+
const tx = exports.db.transaction(name, "readwrite");
|
|
29
|
+
const store = tx.objectStore(name);
|
|
30
|
+
await Promise.all(ids.map(id => store.delete(id)));
|
|
31
|
+
await tx.done;
|
|
32
|
+
};
|
|
33
|
+
exports.bulkDelete = bulkDelete;
|
|
34
|
+
const initIndexedDbAdapter = async (name, adapter) => {
|
|
35
|
+
let copy = await (0, exports.getAll)(name);
|
|
36
|
+
adapter.store.set(copy);
|
|
37
|
+
adapter.store.subscribe((0, throttle_debounce_1.throttle)((0, lib_1.randomInt)(3000, 5000), async (data) => {
|
|
38
|
+
if (exports.dead.get()) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const prevIds = new Set(copy.map(item => item[adapter.keyPath]));
|
|
42
|
+
const currentIds = new Set(data.map(item => item[adapter.keyPath]));
|
|
43
|
+
const newRecords = data.filter(r => !prevIds.has(r[adapter.keyPath]));
|
|
44
|
+
const removedRecords = copy.filter(r => !currentIds.has(r[adapter.keyPath]));
|
|
45
|
+
copy = data;
|
|
46
|
+
if (newRecords.length > 0) {
|
|
47
|
+
await (0, exports.bulkPut)(name, newRecords);
|
|
48
|
+
}
|
|
49
|
+
if (removedRecords.length > 0) {
|
|
50
|
+
await (0, exports.bulkDelete)(name, removedRecords.map(item => item[adapter.keyPath]));
|
|
51
|
+
}
|
|
52
|
+
}));
|
|
53
|
+
};
|
|
54
|
+
exports.initIndexedDbAdapter = initIndexedDbAdapter;
|
|
55
|
+
const initStorage = async (version, adapters) => {
|
|
56
|
+
if (!window.indexedDB)
|
|
57
|
+
return;
|
|
58
|
+
window.addEventListener("beforeunload", () => (0, exports.closeStorage)());
|
|
59
|
+
exports.db = await (0, idb_1.openDB)(exports.DB_NAME, version, {
|
|
60
|
+
upgrade(db) {
|
|
61
|
+
const names = Object.keys(adapters);
|
|
62
|
+
for (const name of db.objectStoreNames) {
|
|
63
|
+
if (!names.includes(name)) {
|
|
64
|
+
db.deleteObjectStore(name);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const [name, { keyPath }] of Object.entries(adapters)) {
|
|
68
|
+
try {
|
|
69
|
+
db.createObjectStore(name, { keyPath });
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
console.warn(e);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
await Promise.all(Object.entries(adapters).map(([name, config]) => (0, exports.initIndexedDbAdapter)(name, config)));
|
|
78
|
+
};
|
|
79
|
+
exports.initStorage = initStorage;
|
|
80
|
+
const closeStorage = async () => {
|
|
81
|
+
exports.dead.set(true);
|
|
82
|
+
exports.subs.forEach(unsub => unsub());
|
|
83
|
+
await (exports.db === null || exports.db === void 0 ? void 0 : exports.db.close());
|
|
84
|
+
};
|
|
85
|
+
exports.closeStorage = closeStorage;
|
|
86
|
+
const clearStorage = async () => {
|
|
87
|
+
await (0, exports.closeStorage)();
|
|
88
|
+
await (0, idb_1.deleteDB)(exports.DB_NAME);
|
|
89
|
+
};
|
|
90
|
+
exports.clearStorage = clearStorage;
|
|
91
|
+
//# sourceMappingURL=storage.cjs.map
|
|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AAC,OAAO,EAAC,CAAC,CAAA;iBACtC;gBAAC,OAAO,CAAC,EAAE;oBACV,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;iBAChB;aACF;QACH,CAAC;KACF,CAAC,CAAA;IAEF,MAAM,OAAO,CAAC,GAAG,CACf,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,IAAA,4BAAoB,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CACrF,CAAA;AACH,CAAC,CAAA;AA5BY,QAAA,WAAW,eA4BvB;AAEM,MAAM,YAAY,GAAG,KAAK,IAAI,EAAE;IACrC,YAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACd,YAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,CAAA;IAC9B,MAAM,CAAA,UAAE,aAAF,UAAE,uBAAF,UAAE,CAAE,KAAK,EAAE,CAAA,CAAA;AACnB,CAAC,CAAA;AAJY,QAAA,YAAY,gBAIxB;AAEM,MAAM,YAAY,GAAG,KAAK,IAAI,EAAE;IACrC,MAAM,IAAA,oBAAY,GAAE,CAAA;IACpB,MAAM,IAAA,cAAQ,EAAC,eAAO,CAAC,CAAA;AACzB,CAAC,CAAA;AAHY,QAAA,YAAY,gBAGxB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="svelte" />
|
|
2
|
+
import type { IDBPDatabase } from "idb";
|
|
3
|
+
import type { Unsubscriber, Writable } from "svelte/store";
|
|
4
|
+
export type Item = Record<string, any>;
|
|
5
|
+
export type IndexedDbAdapter = {
|
|
6
|
+
keyPath: string;
|
|
7
|
+
store: Writable<Item[]>;
|
|
8
|
+
};
|
|
9
|
+
export declare let db: IDBPDatabase;
|
|
10
|
+
export declare const dead: Writable<boolean> & {
|
|
11
|
+
get: () => boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const subs: Unsubscriber[];
|
|
14
|
+
export declare const DB_NAME = "flotilla";
|
|
15
|
+
export declare const getAll: (name: string) => Promise<any[]>;
|
|
16
|
+
export declare const bulkPut: (name: string, data: any[]) => Promise<void>;
|
|
17
|
+
export declare const bulkDelete: (name: string, ids: string[]) => Promise<void>;
|
|
18
|
+
export declare const initIndexedDbAdapter: (name: string, adapter: IndexedDbAdapter) => Promise<void>;
|
|
19
|
+
export declare const initStorage: (version: number, adapters: Record<string, IndexedDbAdapter>) => Promise<void>;
|
|
20
|
+
export declare const closeStorage: () => Promise<void>;
|
|
21
|
+
export declare const clearStorage: () => Promise<void>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { openDB, deleteDB } from "idb";
|
|
2
|
+
import { throttle } from "throttle-debounce";
|
|
3
|
+
import { writable } from "svelte/store";
|
|
4
|
+
import { randomInt } from "@welshman/lib";
|
|
5
|
+
import { withGetter } from "@welshman/store";
|
|
6
|
+
export let db;
|
|
7
|
+
export const dead = withGetter(writable(false));
|
|
8
|
+
export const subs = [];
|
|
9
|
+
export const DB_NAME = "flotilla";
|
|
10
|
+
export const getAll = async (name) => {
|
|
11
|
+
const tx = db.transaction(name, "readwrite");
|
|
12
|
+
const store = tx.objectStore(name);
|
|
13
|
+
const result = await store.getAll();
|
|
14
|
+
await tx.done;
|
|
15
|
+
return result;
|
|
16
|
+
};
|
|
17
|
+
export const bulkPut = async (name, data) => {
|
|
18
|
+
const tx = db.transaction(name, "readwrite");
|
|
19
|
+
const store = tx.objectStore(name);
|
|
20
|
+
await Promise.all(data.map(item => store.put(item)));
|
|
21
|
+
await tx.done;
|
|
22
|
+
};
|
|
23
|
+
export const bulkDelete = async (name, ids) => {
|
|
24
|
+
const tx = db.transaction(name, "readwrite");
|
|
25
|
+
const store = tx.objectStore(name);
|
|
26
|
+
await Promise.all(ids.map(id => store.delete(id)));
|
|
27
|
+
await tx.done;
|
|
28
|
+
};
|
|
29
|
+
export const initIndexedDbAdapter = async (name, adapter) => {
|
|
30
|
+
let copy = await getAll(name);
|
|
31
|
+
adapter.store.set(copy);
|
|
32
|
+
adapter.store.subscribe(throttle(randomInt(3000, 5000), async (data) => {
|
|
33
|
+
if (dead.get()) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const prevIds = new Set(copy.map(item => item[adapter.keyPath]));
|
|
37
|
+
const currentIds = new Set(data.map(item => item[adapter.keyPath]));
|
|
38
|
+
const newRecords = data.filter(r => !prevIds.has(r[adapter.keyPath]));
|
|
39
|
+
const removedRecords = copy.filter(r => !currentIds.has(r[adapter.keyPath]));
|
|
40
|
+
copy = data;
|
|
41
|
+
if (newRecords.length > 0) {
|
|
42
|
+
await bulkPut(name, newRecords);
|
|
43
|
+
}
|
|
44
|
+
if (removedRecords.length > 0) {
|
|
45
|
+
await bulkDelete(name, removedRecords.map(item => item[adapter.keyPath]));
|
|
46
|
+
}
|
|
47
|
+
}));
|
|
48
|
+
};
|
|
49
|
+
export const initStorage = async (version, adapters) => {
|
|
50
|
+
if (!window.indexedDB)
|
|
51
|
+
return;
|
|
52
|
+
window.addEventListener("beforeunload", () => closeStorage());
|
|
53
|
+
db = await openDB(DB_NAME, version, {
|
|
54
|
+
upgrade(db) {
|
|
55
|
+
const names = Object.keys(adapters);
|
|
56
|
+
for (const name of db.objectStoreNames) {
|
|
57
|
+
if (!names.includes(name)) {
|
|
58
|
+
db.deleteObjectStore(name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const [name, { keyPath }] of Object.entries(adapters)) {
|
|
62
|
+
try {
|
|
63
|
+
db.createObjectStore(name, { keyPath });
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.warn(e);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
await Promise.all(Object.entries(adapters).map(([name, config]) => initIndexedDbAdapter(name, config)));
|
|
72
|
+
};
|
|
73
|
+
export const closeStorage = async () => {
|
|
74
|
+
dead.set(true);
|
|
75
|
+
subs.forEach(unsub => unsub());
|
|
76
|
+
await (db === null || db === void 0 ? void 0 : db.close());
|
|
77
|
+
};
|
|
78
|
+
export const clearStorage = async () => {
|
|
79
|
+
await closeStorage();
|
|
80
|
+
await deleteDB(DB_NAME);
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=storage.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.mjs","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":"OAAO,EAAC,MAAM,EAAE,QAAQ,EAAC,MAAM,KAAK;OAE7B,EAAC,QAAQ,EAAC,MAAM,mBAAmB;OACnC,EAAC,QAAQ,EAAC,MAAM,cAAc;OAE9B,EAAC,SAAS,EAAC,MAAM,eAAe;OAChC,EAAC,UAAU,EAAC,MAAM,iBAAiB;AAS1C,MAAM,CAAC,IAAI,EAAgB,CAAA;AAE3B,MAAM,CAAC,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;AAE/C,MAAM,CAAC,MAAM,IAAI,GAAmB,EAAE,CAAA;AAEtC,MAAM,CAAC,MAAM,OAAO,GAAG,UAAU,CAAA;AAEjC,MAAM,CAAC,MAAM,MAAM,GAAG,KAAK,EAAE,IAAY,EAAE,EAAE;IAC3C,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC,CAAA;IAC5C,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;IAClC,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,MAAM,EAAE,CAAA;IAEnC,MAAM,EAAE,CAAC,IAAI,CAAA;IAEb,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAAE,IAAY,EAAE,IAAW,EAAE,EAAE;IACzD,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,C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.makeThunk = exports.thunkWorker = exports.publishStatusData = void 0;
|
|
4
|
+
const store_1 = require("svelte/store");
|
|
5
|
+
const lib_1 = require("@welshman/lib");
|
|
6
|
+
const signer_1 = require("@welshman/signer");
|
|
7
|
+
const net_1 = require("@welshman/net");
|
|
8
|
+
const core_1 = require("./core.cjs");
|
|
9
|
+
const session_1 = require("./session.cjs");
|
|
10
|
+
exports.publishStatusData = (0, store_1.writable)({});
|
|
11
|
+
exports.thunkWorker = new lib_1.Worker();
|
|
12
|
+
exports.thunkWorker.addGlobalHandler(async ({ event, relays, resolve }) => {
|
|
13
|
+
const session = (0, session_1.getSession)(event.pubkey);
|
|
14
|
+
if (!session) {
|
|
15
|
+
return console.warn(`No session found for ${event.pubkey}`);
|
|
16
|
+
}
|
|
17
|
+
const signedEvent = await (0, session_1.getSigner)(session).sign(event);
|
|
18
|
+
const pub = (0, net_1.publish)({ event: signedEvent, relays });
|
|
19
|
+
core_1.repository.getEvent(signedEvent.id).sig = signedEvent.sig;
|
|
20
|
+
// Track publish success
|
|
21
|
+
const { id } = event;
|
|
22
|
+
const statusByUrl = {};
|
|
23
|
+
pub.emitter.on("*", (status, url, message) => {
|
|
24
|
+
exports.publishStatusData.update((0, lib_1.assoc)(id, Object.assign(statusByUrl, { [url]: { id, url, status, message } })));
|
|
25
|
+
if (Object.values(statusByUrl).filter(s => s.status !== net_1.PublishStatus.Pending).length ===
|
|
26
|
+
relays.length) {
|
|
27
|
+
resolve(statusByUrl);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
const makeThunk = ({ event, relays }) => {
|
|
32
|
+
const $pubkey = (0, store_1.get)(session_1.pubkey);
|
|
33
|
+
if (!$pubkey) {
|
|
34
|
+
throw new Error("Unable to make thunk if no user is logged in");
|
|
35
|
+
}
|
|
36
|
+
return { event: (0, signer_1.hash)((0, signer_1.own)((0, signer_1.stamp)(event), $pubkey)), relays };
|
|
37
|
+
};
|
|
38
|
+
exports.makeThunk = makeThunk;
|
|
39
|
+
const publishThunk = (thunk) => new Promise(resolve => {
|
|
40
|
+
exports.thunkWorker.push({ ...thunk, resolve });
|
|
41
|
+
core_1.repository.publish(thunk.event);
|
|
42
|
+
});
|
|
43
|
+
exports.publishThunk = publishThunk;
|
|
44
|
+
//# sourceMappingURL=thunk.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thunk.cjs","sourceRoot":"","sources":["../../src/thunk.ts"],"names":[],"mappings":";;;AAAA,wCAA0C;AAC1C,uCAA2C;AAC3C,6CAAiD;AAEjD,uCAAoD;AACpD,qCAAiC;AACjC,2CAAuD;AAa1C,QAAA,iBAAiB,GAAG,IAAA,gBAAQ,EAA6B,EAAE,CAAC,CAAA;AAW5D,QAAA,WAAW,GAAG,IAAI,YAAM,EAAoB,CAAA;AAEzD,mBAAW,CAAC,gBAAgB,CAAC,KAAK,EAAE,EAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAmB,EAAE,EAAE;IAChF,MAAM,OAAO,GAAG,IAAA,oBAAU,EAAC,KAAK,CAAC,MAAM,CAAC,CAAA;IAExC,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,OAAO,CAAC,IAAI,CAAC,wBAAwB,KAAK,CAAC,MAAM,EAAE,CAAC,CAAA;KAC5D;IAED,MAAM,WAAW,GAAG,MAAM,IAAA,mBAAS,EAAC,OAAO,CAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACzD,MAAM,GAAG,GAAG,IAAA,aAAO,EAAC,EAAC,KAAK,EAAE,WAAW,EAAE,MAAM,EAAC,CAAC,CAGhD;IAAC,iBAAU,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAiB,CAAC,GAAG,GAAG,WAAW,CAAC,GAAG,CAAA;IAE3E,wBAAwB;IACxB,MAAM,EAAC,EAAE,EAAC,GAAG,KAAK,CAAA;IAClB,MAAM,WAAW,GAA2B,EAAE,CAAA;IAE9C,GAAG,CAAC,OAAO,CAAC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A,YAAY,gBAIrB"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="svelte" />
|
|
2
|
+
import { Worker } from '@welshman/lib';
|
|
3
|
+
import type { HashedEvent, EventTemplate } from '@welshman/util';
|
|
4
|
+
import { PublishStatus } from "@welshman/net";
|
|
5
|
+
export type PublishStatusData = {
|
|
6
|
+
id: string;
|
|
7
|
+
url: string;
|
|
8
|
+
message: string;
|
|
9
|
+
status: PublishStatus;
|
|
10
|
+
};
|
|
11
|
+
export type PublishStatusDataByUrl = Record<string, PublishStatusData>;
|
|
12
|
+
export type PublishStatusDataByUrlById = Record<string, PublishStatusDataByUrl>;
|
|
13
|
+
export declare const publishStatusData: import("svelte/store").Writable<PublishStatusDataByUrlById>;
|
|
14
|
+
export type Thunk = {
|
|
15
|
+
event: HashedEvent;
|
|
16
|
+
relays: string[];
|
|
17
|
+
};
|
|
18
|
+
export type ThunkWithResolve = Thunk & {
|
|
19
|
+
resolve: (data: PublishStatusDataByUrl) => void;
|
|
20
|
+
};
|
|
21
|
+
export declare const thunkWorker: Worker<ThunkWithResolve>;
|
|
22
|
+
export type ThunkParams = {
|
|
23
|
+
event: EventTemplate;
|
|
24
|
+
relays: string[];
|
|
25
|
+
};
|
|
26
|
+
export declare const makeThunk: ({ event, relays }: ThunkParams) => {
|
|
27
|
+
event: {
|
|
28
|
+
id: string;
|
|
29
|
+
tags: string[][];
|
|
30
|
+
content: string;
|
|
31
|
+
kind: number;
|
|
32
|
+
created_at: number;
|
|
33
|
+
pubkey: string;
|
|
34
|
+
};
|
|
35
|
+
relays: string[];
|
|
36
|
+
};
|
|
37
|
+
export declare const publishThunk: (thunk: Thunk) => Promise<PublishStatusDataByUrl>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { writable, get } from 'svelte/store';
|
|
2
|
+
import { Worker, assoc } from '@welshman/lib';
|
|
3
|
+
import { stamp, own, hash } from "@welshman/signer";
|
|
4
|
+
import { publish, PublishStatus } from "@welshman/net";
|
|
5
|
+
import { repository } from "./core.mjs";
|
|
6
|
+
import { pubkey, getSession, getSigner } from "./session.mjs";
|
|
7
|
+
export const publishStatusData = writable({});
|
|
8
|
+
export const thunkWorker = new Worker();
|
|
9
|
+
thunkWorker.addGlobalHandler(async ({ event, relays, resolve }) => {
|
|
10
|
+
const session = getSession(event.pubkey);
|
|
11
|
+
if (!session) {
|
|
12
|
+
return console.warn(`No session found for ${event.pubkey}`);
|
|
13
|
+
}
|
|
14
|
+
const signedEvent = await getSigner(session).sign(event);
|
|
15
|
+
const pub = publish({ event: signedEvent, relays });
|
|
16
|
+
repository.getEvent(signedEvent.id).sig = signedEvent.sig;
|
|
17
|
+
// Track publish success
|
|
18
|
+
const { id } = event;
|
|
19
|
+
const statusByUrl = {};
|
|
20
|
+
pub.emitter.on("*", (status, url, message) => {
|
|
21
|
+
publishStatusData.update(assoc(id, Object.assign(statusByUrl, { [url]: { id, url, status, message } })));
|
|
22
|
+
if (Object.values(statusByUrl).filter(s => s.status !== PublishStatus.Pending).length ===
|
|
23
|
+
relays.length) {
|
|
24
|
+
resolve(statusByUrl);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
export const makeThunk = ({ event, relays }) => {
|
|
29
|
+
const $pubkey = get(pubkey);
|
|
30
|
+
if (!$pubkey) {
|
|
31
|
+
throw new Error("Unable to make thunk if no user is logged in");
|
|
32
|
+
}
|
|
33
|
+
return { event: hash(own(stamp(event), $pubkey)), relays };
|
|
34
|
+
};
|
|
35
|
+
export const publishThunk = (thunk) => new Promise(resolve => {
|
|
36
|
+
thunkWorker.push({ ...thunk, resolve });
|
|
37
|
+
repository.publish(thunk.event);
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=thunk.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thunk.mjs","sourceRoot":"","sources":["../../src/thunk.ts"],"names":[],"mappings":"OAAO,EAAC,QAAQ,EAAE,GAAG,EAAC,M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AW,EAAE,EAAC,CAAC,GAAG,CAAC,EAAE,EAAC,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAC,EAAC,CAAC,CAAC,CAC3E,CAAA;QAED,IACE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,aAAa,CAAC,OAAO,CAAC,CAAC,MAAM;YACjF,MAAM,CAAC,MAAM,EACb;YACA,OAAO,CAAC,WAAW,CAAC,CAAA;SACrB;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA;AAOF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,EAAC,KAAK,EAAE,MAAM,EAAc,EAAE,EAAE;IACxD,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,CAAA;IAE3B,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAA;KAChE;IAED,OAAO,EAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,MAAM,EAAC,CAAA;AAC1D,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAY,EAAE,EAAE,CAC3C,IAAI,OAAO,CAAyB,OAAO,CAAC,EAAE;IAC5C,WAAW,CAAC,IAAI,CAAC,EAAC,GAAG,KAAK,EAAE,OAAO,EAAC,CAAC,CAAA;IACrC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AACjC,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.topicSearch = exports.topics = void 0;
|
|
4
|
+
const throttle_debounce_1 = require("throttle-debounce");
|
|
5
|
+
const store_1 = require("svelte/store");
|
|
6
|
+
const lib_1 = require("@welshman/lib");
|
|
7
|
+
const store_2 = require("@welshman/store");
|
|
8
|
+
const util_1 = require("./util.cjs");
|
|
9
|
+
const core_1 = require("./core.cjs");
|
|
10
|
+
exports.topics = (0, store_2.custom)(setter => {
|
|
11
|
+
const getTopics = () => {
|
|
12
|
+
const topics = new Map();
|
|
13
|
+
for (const tagString of core_1.repository.eventsByTag.keys()) {
|
|
14
|
+
if (tagString.startsWith("t:")) {
|
|
15
|
+
const topic = tagString.slice(2).toLowerCase();
|
|
16
|
+
topics.set(topic, (0, lib_1.inc)(topics.get(topic)));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return Array.from(topics.entries()).map(([name, count]) => ({ name, count }));
|
|
20
|
+
};
|
|
21
|
+
setter(getTopics());
|
|
22
|
+
const onUpdate = (0, throttle_debounce_1.throttle)(3000, () => setter(getTopics()));
|
|
23
|
+
core_1.repository.on("update", onUpdate);
|
|
24
|
+
return () => core_1.repository.off("update", onUpdate);
|
|
25
|
+
});
|
|
26
|
+
exports.topicSearch = (0, store_1.derived)(exports.topics, $topics => (0, util_1.createSearch)($topics, {
|
|
27
|
+
getValue: (topic) => topic.name,
|
|
28
|
+
fuseOptions: { keys: ["name"] },
|
|
29
|
+
}));
|
|
30
|
+
//# sourceMappingURL=topics.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topics.cjs","sourceRoot":"","sources":["../../src/topics.ts"],"names":[],"mappings":";;;AAAA,yDAA0C;AAC1C,wCAAoC;AACpC,uCAAiC;AACjC,2CAAsC;AACtC,qCAAmC;AACnC,qCAAiC;AAOpB,QAAA,MAAM,GAAG,IAAA,cAAM,EAAU,MAAM,CAAC,EAAE;IAC7C,MAAM,SAAS,GAAG,GAAG,EAAE;QACrB,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAA;QACxC,KAAK,MAAM,SAAS,IAAI,iBAAU,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE;YACrD,IAA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
set: (newValue: Topic[]) => void;
|
|
8
|
+
subscribe: (sub: import("svelte/store").Subscriber<Topic[]>) => () => void;
|
|
9
|
+
};
|
|
10
|
+
export declare const topicSearch: import("svelte/store").Readable<import("./util").Search<string, Topic>>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { throttle } from 'throttle-debounce';
|
|
2
|
+
import { derived } from 'svelte/store';
|
|
3
|
+
import { inc } from '@welshman/lib';
|
|
4
|
+
import { custom } from '@welshman/store';
|
|
5
|
+
import { createSearch } from "./util.mjs";
|
|
6
|
+
import { repository } from "./core.mjs";
|
|
7
|
+
export const topics = custom(setter => {
|
|
8
|
+
const getTopics = () => {
|
|
9
|
+
const topics = new Map();
|
|
10
|
+
for (const tagString of repository.eventsByTag.keys()) {
|
|
11
|
+
if (tagString.startsWith("t:")) {
|
|
12
|
+
const topic = tagString.slice(2).toLowerCase();
|
|
13
|
+
topics.set(topic, inc(topics.get(topic)));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return Array.from(topics.entries()).map(([name, count]) => ({ name, count }));
|
|
17
|
+
};
|
|
18
|
+
setter(getTopics());
|
|
19
|
+
const onUpdate = throttle(3000, () => setter(getTopics()));
|
|
20
|
+
repository.on("update", onUpdate);
|
|
21
|
+
return () => repository.off("update", onUpdate);
|
|
22
|
+
});
|
|
23
|
+
export const topicSearch = derived(topics, $topics => createSearch($topics, {
|
|
24
|
+
getValue: (topic) => topic.name,
|
|
25
|
+
fuseOptions: { keys: ["name"] },
|
|
26
|
+
}));
|
|
27
|
+
//# sourceMappingURL=topics.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"topics.mjs","sourceRoot":"","sources":["../../src/topics.ts"],"names":[],"mappings":"OAAO,EAAC,QAAQ,EAAC,MAAM,mBAAmB;OACnC,EAAC,OAAO,EAAC,MAAM,cAAc;OAC7B,EAAC,GAAG,EAAC,MAAM,eAAe;OAC1B,EAAC,MAAM,EAAC,MAAM,iBAAiB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AC,KAAK,CAAC,IAAI;IACtC,WAAW,EAAE,EAAC,IAAI,EAAE,CAAC,MAAM,CAAC,EAAC;CAC9B,CAAC,CACH,CAAA"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.formatTimestampAsTime = exports.formatTimestampAsDate = exports.formatTimestamp = exports.getLocale = exports.createLocalDate = exports.getTimeZone = exports.dateToSeconds = exports.secondsToDate = exports.createSearch = void 0;
|
|
7
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
8
|
+
const lib_1 = require("@welshman/lib");
|
|
9
|
+
const createSearch = (options, opts) => {
|
|
10
|
+
const fuse = new fuse_js_1.default(options, { ...opts.fuseOptions, includeScore: true });
|
|
11
|
+
const map = new Map(options.map(item => [opts.getValue(item), item]));
|
|
12
|
+
const search = (term) => {
|
|
13
|
+
let results = term ? fuse.search(term) : options.map(item => ({ item, score: 1 }));
|
|
14
|
+
if (opts.sortFn) {
|
|
15
|
+
results = (0, lib_1.sortBy)(opts.sortFn, results);
|
|
16
|
+
}
|
|
17
|
+
return results.map(result => result.item);
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
options,
|
|
21
|
+
getValue: opts.getValue,
|
|
22
|
+
getOption: (value) => map.get(value),
|
|
23
|
+
searchOptions: (term) => search(term),
|
|
24
|
+
searchValues: (term) => search(term).map(opts.getValue),
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
exports.createSearch = createSearch;
|
|
28
|
+
const secondsToDate = (ts) => new Date(ts * 1000);
|
|
29
|
+
exports.secondsToDate = secondsToDate;
|
|
30
|
+
const dateToSeconds = (date) => Math.round(date.valueOf() / 1000);
|
|
31
|
+
exports.dateToSeconds = dateToSeconds;
|
|
32
|
+
const getTimeZone = () => new Date().toString().match(/GMT[^\s]+/);
|
|
33
|
+
exports.getTimeZone = getTimeZone;
|
|
34
|
+
const createLocalDate = (dateString) => new Date(`${dateString} ${(0, exports.getTimeZone)()}`);
|
|
35
|
+
exports.createLocalDate = createLocalDate;
|
|
36
|
+
const getLocale = () => new Intl.DateTimeFormat().resolvedOptions().locale;
|
|
37
|
+
exports.getLocale = getLocale;
|
|
38
|
+
const formatTimestamp = (ts) => {
|
|
39
|
+
const formatter = new Intl.DateTimeFormat((0, exports.getLocale)(), {
|
|
40
|
+
dateStyle: "short",
|
|
41
|
+
timeStyle: "short",
|
|
42
|
+
});
|
|
43
|
+
return formatter.format((0, exports.secondsToDate)(ts));
|
|
44
|
+
};
|
|
45
|
+
exports.formatTimestamp = formatTimestamp;
|
|
46
|
+
const formatTimestampAsDate = (ts) => {
|
|
47
|
+
const formatter = new Intl.DateTimeFormat((0, exports.getLocale)(), {
|
|
48
|
+
year: "numeric",
|
|
49
|
+
month: "long",
|
|
50
|
+
day: "numeric",
|
|
51
|
+
});
|
|
52
|
+
return formatter.format((0, exports.secondsToDate)(ts));
|
|
53
|
+
};
|
|
54
|
+
exports.formatTimestampAsDate = formatTimestampAsDate;
|
|
55
|
+
const formatTimestampAsTime = (ts) => {
|
|
56
|
+
const formatter = new Intl.DateTimeFormat((0, exports.getLocale)(), {
|
|
57
|
+
timeStyle: "short",
|
|
58
|
+
});
|
|
59
|
+
return formatter.format((0, exports.secondsToDate)(ts));
|
|
60
|
+
};
|
|
61
|
+
exports.formatTimestampAsTime = formatTimestampAsTime;
|
|
62
|
+
//# sourceMappingURL=util.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.cjs","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA0B;AAE1B,uCAAoC;AAgB7B,MAAM,YAAY,GAAG,CAAO,OAAY,EAAE,IAAyB,EAAgB,EAAE;IAC1F,MAAM,IAAI,GAAG,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,EAAC,EAAE,CAAC,CAAC,CAAA;AAC5C,CAAC,CAAA;AARY,QAAA,qBAAqB,yBAQjC;AAEM,MAAM,qBAAqB,GAAG,CAAC,EAAU,EAAE,EAAE;IAClD,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,cAAc,CAAC,IAAA,iBAAS,GAAE,EAAE;QACrD,SAAS,EAAE,OAAO;KACnB,CAAC,CAAA;IAEF,OAAO,SAAS,CAAC,MAAM,CAAC,IAAA,qBAAa,EAAC,EAAE,CAAC,CAAC,CAAA;AAC5C,CAAC,CAAA;AANY,QAAA,qBAAqB,yBAMjC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IFuseOptions, FuseResult } from "fuse.js";
|
|
2
|
+
export type SearchOptions<V, T> = {
|
|
3
|
+
getValue: (item: T) => V;
|
|
4
|
+
fuseOptions?: IFuseOptions<T>;
|
|
5
|
+
sortFn?: (items: FuseResult<T>) => any;
|
|
6
|
+
};
|
|
7
|
+
export type Search<V, T> = {
|
|
8
|
+
options: T[];
|
|
9
|
+
getValue: (item: T) => V;
|
|
10
|
+
getOption: (value: V) => T | undefined;
|
|
11
|
+
searchOptions: (term: string) => T[];
|
|
12
|
+
searchValues: (term: string) => V[];
|
|
13
|
+
};
|
|
14
|
+
export declare const createSearch: <V, T>(options: T[], opts: SearchOptions<V, T>) => Search<V, T>;
|
|
15
|
+
export declare const secondsToDate: (ts: number) => Date;
|
|
16
|
+
export declare const dateToSeconds: (date: Date) => number;
|
|
17
|
+
export declare const getTimeZone: () => RegExpMatchArray | null;
|
|
18
|
+
export declare const createLocalDate: (dateString: any) => Date;
|
|
19
|
+
export declare const getLocale: () => string;
|
|
20
|
+
export declare const formatTimestamp: (ts: number) => string;
|
|
21
|
+
export declare const formatTimestampAsDate: (ts: number) => string;
|
|
22
|
+
export declare const formatTimestampAsTime: (ts: number) => string;
|