@welshman/store 0.0.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.
- package/README.md +3 -0
- package/build/src/index.cjs +138 -0
- package/build/src/index.cjs.map +1 -0
- package/build/src/index.d.ts +47 -0
- package/build/src/index.mjs +126 -0
- package/build/src/index.mjs.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deriveIsDeletedByAddress = exports.deriveEvent = exports.deriveEvents = exports.deriveEventsMapped = exports.createEventStore = exports.throttled = exports.withGetter = exports.custom = exports.getter = 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 util_1 = require("@welshman/util");
|
|
8
|
+
const getter = (store) => {
|
|
9
|
+
let value;
|
|
10
|
+
store.subscribe((newValue) => {
|
|
11
|
+
value = newValue;
|
|
12
|
+
});
|
|
13
|
+
return () => value;
|
|
14
|
+
};
|
|
15
|
+
exports.getter = getter;
|
|
16
|
+
const custom = (start, opts = {}) => {
|
|
17
|
+
const subs = [];
|
|
18
|
+
let value;
|
|
19
|
+
let stop;
|
|
20
|
+
return {
|
|
21
|
+
subscribe: (sub) => {
|
|
22
|
+
if (opts.throttle) {
|
|
23
|
+
sub = (0, throttle_debounce_1.throttle)(opts.throttle, sub);
|
|
24
|
+
}
|
|
25
|
+
if (subs.length === 0) {
|
|
26
|
+
stop = start((newValue) => {
|
|
27
|
+
for (const sub of subs) {
|
|
28
|
+
sub(newValue);
|
|
29
|
+
}
|
|
30
|
+
value = newValue;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
subs.push(sub);
|
|
34
|
+
sub(value);
|
|
35
|
+
return () => {
|
|
36
|
+
subs.splice(subs.findIndex(s => s === sub), 1);
|
|
37
|
+
if (subs.length === 0) {
|
|
38
|
+
stop();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
exports.custom = custom;
|
|
45
|
+
function withGetter(store) {
|
|
46
|
+
return { ...store, get: (0, exports.getter)(store) };
|
|
47
|
+
}
|
|
48
|
+
exports.withGetter = withGetter;
|
|
49
|
+
const throttled = (delay, store) => (0, exports.custom)(set => store.subscribe((0, throttle_debounce_1.throttle)(delay, set)));
|
|
50
|
+
exports.throttled = throttled;
|
|
51
|
+
const createEventStore = (repository) => {
|
|
52
|
+
let subs = [];
|
|
53
|
+
const onUpdate = (0, throttle_debounce_1.throttle)(300, () => {
|
|
54
|
+
const $events = repository.dump();
|
|
55
|
+
for (const sub of subs) {
|
|
56
|
+
sub($events);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
get: () => repository.dump(),
|
|
61
|
+
set: (events) => repository.load(events),
|
|
62
|
+
subscribe: (f) => {
|
|
63
|
+
f(repository.dump());
|
|
64
|
+
subs.push(f);
|
|
65
|
+
if (subs.length === 1) {
|
|
66
|
+
repository.on("update", onUpdate);
|
|
67
|
+
}
|
|
68
|
+
return () => {
|
|
69
|
+
subs = subs.filter(x => x !== f);
|
|
70
|
+
if (subs.length === 0) {
|
|
71
|
+
repository.off("update", onUpdate);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
exports.createEventStore = createEventStore;
|
|
78
|
+
const deriveEventsMapped = ({ filters, repository, eventToItem, itemToEvent, includeDeleted = false, }) => (0, exports.custom)(setter => {
|
|
79
|
+
let data = repository.query(filters, { includeDeleted }).map(eventToItem).filter(lib_1.identity);
|
|
80
|
+
setter(data);
|
|
81
|
+
const onUpdate = (0, lib_1.batch)(300, (updates) => {
|
|
82
|
+
const removed = new Set();
|
|
83
|
+
const added = new Map();
|
|
84
|
+
// Apply updates in order
|
|
85
|
+
for (const update of updates) {
|
|
86
|
+
for (const event of update.added.values()) {
|
|
87
|
+
added.set(event.id, event);
|
|
88
|
+
}
|
|
89
|
+
for (const id of update.removed) {
|
|
90
|
+
removed.add(id);
|
|
91
|
+
added.delete(id);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
let dirty = false;
|
|
95
|
+
for (const event of added.values()) {
|
|
96
|
+
if ((0, util_1.matchFilters)(filters, event)) {
|
|
97
|
+
const item = eventToItem(event);
|
|
98
|
+
if (item) {
|
|
99
|
+
dirty = true;
|
|
100
|
+
data.push(item);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!includeDeleted && removed.size > 0) {
|
|
105
|
+
const [deleted, ok] = (0, lib_1.partition)((item) => (0, util_1.getIdAndAddress)(itemToEvent(item)).some(id => removed.has(id)), data);
|
|
106
|
+
if (deleted.length > 0) {
|
|
107
|
+
dirty = true;
|
|
108
|
+
data = ok;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (dirty) {
|
|
112
|
+
setter(data);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
repository.on("update", onUpdate);
|
|
116
|
+
return () => repository.off("update", onUpdate);
|
|
117
|
+
});
|
|
118
|
+
exports.deriveEventsMapped = deriveEventsMapped;
|
|
119
|
+
const deriveEvents = (repository, opts) => (0, exports.deriveEventsMapped)({
|
|
120
|
+
...opts,
|
|
121
|
+
repository,
|
|
122
|
+
eventToItem: lib_1.identity,
|
|
123
|
+
itemToEvent: lib_1.identity,
|
|
124
|
+
});
|
|
125
|
+
exports.deriveEvents = deriveEvents;
|
|
126
|
+
const deriveEvent = (repository, idOrAddress) => (0, store_1.derived)((0, exports.deriveEvents)(repository, {
|
|
127
|
+
filters: (0, util_1.getIdFilters)([idOrAddress]),
|
|
128
|
+
includeDeleted: true,
|
|
129
|
+
}), lib_1.first);
|
|
130
|
+
exports.deriveEvent = deriveEvent;
|
|
131
|
+
const deriveIsDeletedByAddress = (repository, event) => (0, exports.custom)(setter => {
|
|
132
|
+
setter(repository.isDeletedByAddress(event));
|
|
133
|
+
const onUpdate = (0, lib_1.batch)(300, () => setter(repository.isDeletedByAddress(event)));
|
|
134
|
+
repository.on("update", onUpdate);
|
|
135
|
+
return () => repository.off("update", onUpdate);
|
|
136
|
+
});
|
|
137
|
+
exports.deriveIsDeletedByAddress = deriveIsDeletedByAddress;
|
|
138
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAA0C;AAC1C,wCAAoC;AAEpC,uCAA+D;AAE/D,yCAA0E;AAGnE,MAAM,MAAM,GAAG,CAAI,KAAkB,EAAE,EAAE;IAC9C,IAAI,KAAQ,CAAA;IAEZ,KAAK,CAAC,SAAS,CAAC,CAAC,QAAW,EAAE,EAAE;QAC9B,KAAK,GAAG,QAAQ,CAAA;IAClB,CAAC,CAAC,CAAA;IAEF,OAAO,GAAG,EAAE,CAAC,KAAK,CAAA;AACpB,CAAC,CAAA;AARY,QAAA,MAAM,UAQlB;AAMM,MAAM,MAAM,GAAG,CAAI,KAAe,EAAE,OAA4B,EAAE,EAAE,EAAE;IAC3E,MAAM,IAAI,GAAa,EAAE,CAAA;IAEzB,IAAI,KAAQ,CAAA;IACZ,IAAI,IAAgB,CAAA;IAEpB,OAAO;QACL,SAAS,EAAE,CAAC,GAAW,EAAE,EAAE;YACzB,IAAI,IAAI,CAAC,QAAQ,EAAE;gBACjB,GAAG,GAAG,IAAA,4BAAQ,EAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;aACnC;YAED,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;gBACrB,IAAI,GAAG,KAAK,CAAC,CAAC,QAAW,EAAE,EAAE;oBAC3B,KAAK,MAAM,GAAG,IAAI,IAA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 @@
|
|
|
1
|
+
import { throttle } from "throttle-debounce";
|
|
2
|
+
import type { Readable, Writable } from "svelte/store";
|
|
3
|
+
import type { Repository } from "@welshman/util";
|
|
4
|
+
import type { Filter, TrustedEvent } from "@welshman/util";
|
|
5
|
+
export declare const getter: <T>(store: Readable<T>) => () => T;
|
|
6
|
+
type Stop = () => void;
|
|
7
|
+
type Sub<T> = (x: T) => void;
|
|
8
|
+
type Start<T> = (set: Sub<T>) => Stop;
|
|
9
|
+
export declare const custom: <T>(start: Start<T>, opts?: {
|
|
10
|
+
throttle?: number;
|
|
11
|
+
}) => {
|
|
12
|
+
subscribe: (sub: Sub<T>) => () => void;
|
|
13
|
+
};
|
|
14
|
+
export declare function withGetter<T>(store: Writable<T>): Writable<T> & {
|
|
15
|
+
get: () => T;
|
|
16
|
+
};
|
|
17
|
+
export declare function withGetter<T>(store: Readable<T>): Readable<T> & {
|
|
18
|
+
get: () => T;
|
|
19
|
+
};
|
|
20
|
+
export declare const throttled: <T>(delay: number, store: Readable<T>) => {
|
|
21
|
+
subscribe: (sub: Sub<unknown>) => () => void;
|
|
22
|
+
};
|
|
23
|
+
export declare const createEventStore: (repository: Repository) => {
|
|
24
|
+
get: () => TrustedEvent[];
|
|
25
|
+
set: (events: TrustedEvent[]) => Promise<void>;
|
|
26
|
+
subscribe: (f: Sub<TrustedEvent[]>) => () => void;
|
|
27
|
+
};
|
|
28
|
+
export declare const deriveEventsMapped: <T>({ filters, repository, eventToItem, itemToEvent, includeDeleted, }: {
|
|
29
|
+
filters: Filter[];
|
|
30
|
+
repository: Repository;
|
|
31
|
+
eventToItem: (event: TrustedEvent) => T;
|
|
32
|
+
itemToEvent: (item: T) => TrustedEvent;
|
|
33
|
+
includeDeleted?: boolean | undefined;
|
|
34
|
+
}) => {
|
|
35
|
+
subscribe: (sub: Sub<T[]>) => () => void;
|
|
36
|
+
};
|
|
37
|
+
export declare const deriveEvents: (repository: Repository, opts: {
|
|
38
|
+
filters: Filter[];
|
|
39
|
+
includeDeleted?: boolean;
|
|
40
|
+
}) => {
|
|
41
|
+
subscribe: (sub: Sub<TrustedEvent[]>) => () => void;
|
|
42
|
+
};
|
|
43
|
+
export declare const deriveEvent: (repository: Repository, idOrAddress: string) => Readable<TrustedEvent>;
|
|
44
|
+
export declare const deriveIsDeletedByAddress: (repository: Repository, event: TrustedEvent) => {
|
|
45
|
+
subscribe: (sub: Sub<boolean>) => () => void;
|
|
46
|
+
};
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { throttle } from "throttle-debounce";
|
|
2
|
+
import { derived } from "svelte/store";
|
|
3
|
+
import { identity, batch, partition, first } from "@welshman/lib";
|
|
4
|
+
import { matchFilters, getIdAndAddress, getIdFilters } from "@welshman/util";
|
|
5
|
+
export const getter = (store) => {
|
|
6
|
+
let value;
|
|
7
|
+
store.subscribe((newValue) => {
|
|
8
|
+
value = newValue;
|
|
9
|
+
});
|
|
10
|
+
return () => value;
|
|
11
|
+
};
|
|
12
|
+
export const custom = (start, opts = {}) => {
|
|
13
|
+
const subs = [];
|
|
14
|
+
let value;
|
|
15
|
+
let stop;
|
|
16
|
+
return {
|
|
17
|
+
subscribe: (sub) => {
|
|
18
|
+
if (opts.throttle) {
|
|
19
|
+
sub = throttle(opts.throttle, sub);
|
|
20
|
+
}
|
|
21
|
+
if (subs.length === 0) {
|
|
22
|
+
stop = start((newValue) => {
|
|
23
|
+
for (const sub of subs) {
|
|
24
|
+
sub(newValue);
|
|
25
|
+
}
|
|
26
|
+
value = newValue;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
subs.push(sub);
|
|
30
|
+
sub(value);
|
|
31
|
+
return () => {
|
|
32
|
+
subs.splice(subs.findIndex(s => s === sub), 1);
|
|
33
|
+
if (subs.length === 0) {
|
|
34
|
+
stop();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export function withGetter(store) {
|
|
41
|
+
return { ...store, get: getter(store) };
|
|
42
|
+
}
|
|
43
|
+
export const throttled = (delay, store) => custom(set => store.subscribe(throttle(delay, set)));
|
|
44
|
+
export const createEventStore = (repository) => {
|
|
45
|
+
let subs = [];
|
|
46
|
+
const onUpdate = throttle(300, () => {
|
|
47
|
+
const $events = repository.dump();
|
|
48
|
+
for (const sub of subs) {
|
|
49
|
+
sub($events);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
get: () => repository.dump(),
|
|
54
|
+
set: (events) => repository.load(events),
|
|
55
|
+
subscribe: (f) => {
|
|
56
|
+
f(repository.dump());
|
|
57
|
+
subs.push(f);
|
|
58
|
+
if (subs.length === 1) {
|
|
59
|
+
repository.on("update", onUpdate);
|
|
60
|
+
}
|
|
61
|
+
return () => {
|
|
62
|
+
subs = subs.filter(x => x !== f);
|
|
63
|
+
if (subs.length === 0) {
|
|
64
|
+
repository.off("update", onUpdate);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
export const deriveEventsMapped = ({ filters, repository, eventToItem, itemToEvent, includeDeleted = false, }) => custom(setter => {
|
|
71
|
+
let data = repository.query(filters, { includeDeleted }).map(eventToItem).filter(identity);
|
|
72
|
+
setter(data);
|
|
73
|
+
const onUpdate = batch(300, (updates) => {
|
|
74
|
+
const removed = new Set();
|
|
75
|
+
const added = new Map();
|
|
76
|
+
// Apply updates in order
|
|
77
|
+
for (const update of updates) {
|
|
78
|
+
for (const event of update.added.values()) {
|
|
79
|
+
added.set(event.id, event);
|
|
80
|
+
}
|
|
81
|
+
for (const id of update.removed) {
|
|
82
|
+
removed.add(id);
|
|
83
|
+
added.delete(id);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
let dirty = false;
|
|
87
|
+
for (const event of added.values()) {
|
|
88
|
+
if (matchFilters(filters, event)) {
|
|
89
|
+
const item = eventToItem(event);
|
|
90
|
+
if (item) {
|
|
91
|
+
dirty = true;
|
|
92
|
+
data.push(item);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!includeDeleted && removed.size > 0) {
|
|
97
|
+
const [deleted, ok] = partition((item) => getIdAndAddress(itemToEvent(item)).some(id => removed.has(id)), data);
|
|
98
|
+
if (deleted.length > 0) {
|
|
99
|
+
dirty = true;
|
|
100
|
+
data = ok;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (dirty) {
|
|
104
|
+
setter(data);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
repository.on("update", onUpdate);
|
|
108
|
+
return () => repository.off("update", onUpdate);
|
|
109
|
+
});
|
|
110
|
+
export const deriveEvents = (repository, opts) => deriveEventsMapped({
|
|
111
|
+
...opts,
|
|
112
|
+
repository,
|
|
113
|
+
eventToItem: identity,
|
|
114
|
+
itemToEvent: identity,
|
|
115
|
+
});
|
|
116
|
+
export const deriveEvent = (repository, idOrAddress) => derived(deriveEvents(repository, {
|
|
117
|
+
filters: getIdFilters([idOrAddress]),
|
|
118
|
+
includeDeleted: true,
|
|
119
|
+
}), first);
|
|
120
|
+
export const deriveIsDeletedByAddress = (repository, event) => custom(setter => {
|
|
121
|
+
setter(repository.isDeletedByAddress(event));
|
|
122
|
+
const onUpdate = batch(300, () => setter(repository.isDeletedByAddress(event)));
|
|
123
|
+
repository.on("update", onUpdate);
|
|
124
|
+
return () => repository.off("update", onUpdate);
|
|
125
|
+
});
|
|
126
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"OAAO,EAAC,QAAQ,EAAC,MAAM,mBAAmB;OACnC,EAAC,OAAO,EAAC,MAAM,cAAc;OAE7B,EAAC,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAC,MAAM,eAAe;OAExD,EAAC,YAAY,EAAE,eA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IAAI;CACrB,CAAC,EACF,KAAK,CACN,CAAA;AAEH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,UAAsB,EAAE,KAAmB,EAAE,EAAE,CACtF,MAAM,CAAU,MAAM,CAAC,EAAE;IACvB,MAAM,CAAC,UAAU,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAA;IAE5C,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAE/E,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;IAEjC,OAAO,GAAG,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;AACjD,CAAC,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@welshman/store",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "hodlbod",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "A collection of utilities based on svelte/store for use with welshman",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"build"
|
|
13
|
+
],
|
|
14
|
+
"types": "./build/src/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./build/src/index.d.ts",
|
|
18
|
+
"import": "./build/src/index.mjs",
|
|
19
|
+
"require": "./build/src/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"pub": "npm run lint && npm run build && npm publish",
|
|
24
|
+
"build": "gts clean && tsc-multi",
|
|
25
|
+
"lint": "gts lint",
|
|
26
|
+
"fix": "gts fix"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"gts": "^5.0.1",
|
|
30
|
+
"tsc-multi": "^1.1.0",
|
|
31
|
+
"typescript": "~5.1.6"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"svelte": "^4.2.18"
|
|
35
|
+
}
|
|
36
|
+
}
|