@effect-atom/atom-livestore 0.1.0

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.
@@ -0,0 +1,6 @@
1
+ {
2
+ "main": "../dist/cjs/AtomLivestore.js",
3
+ "module": "../dist/esm/AtomLivestore.js",
4
+ "types": "../dist/dts/AtomLivestore.d.ts",
5
+ "sideEffects": []
6
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present The Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ WIP.
2
+
3
+ Docs: https://effect-ts.github.io/rx
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.make = void 0;
7
+ var Atom = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("@effect-atom/atom/Atom"));
8
+ var Result = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("@effect-atom/atom/Result"));
9
+ var _livestore = /*#__PURE__*/require("@livestore/livestore");
10
+ var Context = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("effect/Context"));
11
+ var Effect = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("effect/Effect"));
12
+ var _Function = /*#__PURE__*/require("effect/Function");
13
+ var Layer = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("effect/Layer"));
14
+ function _interopRequireWildcard(e, t) {
15
+ if ("function" == typeof WeakMap) var r = new WeakMap(),
16
+ n = new WeakMap();
17
+ return (_interopRequireWildcard = function (e, t) {
18
+ if (!t && e && e.__esModule) return e;
19
+ var o,
20
+ i,
21
+ f = {
22
+ __proto__: null,
23
+ default: e
24
+ };
25
+ if (null === e || "object" != typeof e && "function" != typeof e) return f;
26
+ if (o = t ? n : r) {
27
+ if (o.has(e)) return o.get(e);
28
+ o.set(e, f);
29
+ }
30
+ for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]);
31
+ return f;
32
+ })(e, t);
33
+ }
34
+ /**
35
+ * @since 1.0.0
36
+ */
37
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
38
+
39
+ /**
40
+ * @since 1.0.0
41
+ * @category Constructors
42
+ */
43
+ const make = options => {
44
+ const StoreService = Context.GenericTag("@effect-atom/atom-livestore/StoreService");
45
+ const layer = Layer.scoped(StoreService, (0, _livestore.createStore)(options).pipe((0, _livestore.provideOtel)({
46
+ parentSpanContext: options?.otelOptions?.rootSpanContext,
47
+ otelTracer: options?.otelOptions?.tracer
48
+ }), Effect.orDie));
49
+ const runtimeAtom = Atom.runtime(layer);
50
+ const storeAtom = runtimeAtom.atom(StoreService);
51
+ const storeAtomUnsafe = Atom.readable(get => {
52
+ const result = get(storeAtom);
53
+ return Result.getOrElse(result, _Function.constUndefined);
54
+ });
55
+ const makeQueryAtom = query => Atom.readable(get => {
56
+ const store = get(storeAtom);
57
+ return Result.map(store, store => {
58
+ const result = store.query(query);
59
+ get.addFinalizer(store.subscribe(query, {
60
+ onUpdate(value) {
61
+ get.setSelf(Result.success(value));
62
+ }
63
+ }));
64
+ return result;
65
+ });
66
+ });
67
+ const makeQueryAtomUnsafe = query => Atom.readable(get => {
68
+ const store = get(storeAtomUnsafe);
69
+ if (store === undefined) {
70
+ return undefined;
71
+ }
72
+ get.addFinalizer(store.subscribe(query, {
73
+ onUpdate(value) {
74
+ get.setSelf(Result.success(value));
75
+ }
76
+ }));
77
+ return store.query(query);
78
+ });
79
+ const commitAtom = Atom.writable(get => {
80
+ get(storeAtomUnsafe);
81
+ }, (ctx, value) => {
82
+ ctx.get(storeAtomUnsafe)?.commit(value);
83
+ });
84
+ return {
85
+ StoreService,
86
+ layer,
87
+ runtimeAtom,
88
+ storeAtom,
89
+ storeAtomUnsafe,
90
+ makeQueryAtom,
91
+ makeQueryAtomUnsafe,
92
+ commitAtom
93
+ };
94
+ };
95
+ exports.make = make;
96
+ //# sourceMappingURL=AtomLivestore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AtomLivestore.js","names":["Atom","_interopRequireWildcard","require","Result","_livestore","Context","Effect","_Function","Layer","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","make","options","StoreService","GenericTag","layer","scoped","createStore","pipe","provideOtel","parentSpanContext","otelOptions","rootSpanContext","otelTracer","tracer","orDie","runtimeAtom","runtime","storeAtom","atom","storeAtomUnsafe","readable","result","getOrElse","constUndefined","makeQueryAtom","query","store","map","addFinalizer","subscribe","onUpdate","value","setSelf","success","makeQueryAtomUnsafe","undefined","commitAtom","writable","ctx","commit","exports"],"sources":["../../src/AtomLivestore.ts"],"sourcesContent":[null],"mappings":";;;;;;A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ACrBK,QAAQA,CAACC,KAAK;UACZtC,GAAG,CAACuC,OAAO,CAACzD,MAAM,CAAC0D,OAAO,CAACF,KAAK,CAAC,CAAC;QACpC;OACD,CAAC,CACH;MACD,OAAOV,MAAM;IACf,CAAC,CAAC;EACJ,CAAC,CAAC;EACJ,MAAMa,mBAAmB,GAAOT,KAAsB,IACpDrD,IAAI,CAACgD,QAAQ,CAAE3B,GAAG,IAAI;IACpB,MAAMiC,KAAK,GAAGjC,GAAG,CAAC0B,eAAe,CAAC;IAClC,IAAIO,KAAK,KAAKS,SAAS,EAAE;MACvB,OAAOA,SAAS;IAClB;IACA1C,GAAG,CAACmC,YAAY,CACdF,KAAK,CAACG,SAAS,CAACJ,KAAK,EAAE;MACrBK,QAAQA,CAACC,KAAK;QACZtC,GAAG,CAACuC,OAAO,CAACzD,MAAM,CAAC0D,OAAO,CAACF,KAAK,CAAC,CAAC;MACpC;KACD,CAAC,CACH;IACD,OAAOL,KAAK,CAACD,KAAK,CAACA,KAAK,CAAC;EAC3B,CAAC,CAAC;EACJ,MAAMW,UAAU,GAAGhE,IAAI,CAACiE,QAAQ,CAAE5C,GAAG,IAAI;IACvCA,GAAG,CAAC0B,eAAe,CAAC;EACtB,CAAC,EAAE,CAACmB,GAAG,EAAEP,KAAS,KAAI;IACpBO,GAAG,CAAC7C,GAAG,CAAC0B,eAAe,CAAC,EAAEoB,MAAM,CAACR,KAAK,CAAC;EACzC,CAAC,CAAC;EACF,OAAO;IACL7B,YAAY;IACZE,KAAK;IACLW,WAAW;IACXE,SAAS;IACTE,eAAe;IACfK,aAAa;IACbU,mBAAmB;IACnBE;GACQ;AACZ,CAAC;AAAAI,OAAA,CAAAxC,IAAA,GAAAA,IAAA","ignoreList":[]}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.AtomLivestore = void 0;
7
+ var _AtomLivestore = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("./AtomLivestore.js"));
8
+ exports.AtomLivestore = _AtomLivestore;
9
+ function _interopRequireWildcard(e, t) {
10
+ if ("function" == typeof WeakMap) var r = new WeakMap(),
11
+ n = new WeakMap();
12
+ return (_interopRequireWildcard = function (e, t) {
13
+ if (!t && e && e.__esModule) return e;
14
+ var o,
15
+ i,
16
+ f = {
17
+ __proto__: null,
18
+ default: e
19
+ };
20
+ if (null === e || "object" != typeof e && "function" != typeof e) return f;
21
+ if (o = t ? n : r) {
22
+ if (o.has(e)) return o.get(e);
23
+ o.set(e, f);
24
+ }
25
+ for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]);
26
+ return f;
27
+ })(e, t);
28
+ }
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"","ignoreList":[]}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import * as Atom from "@effect-atom/atom/Atom";
5
+ import * as Result from "@effect-atom/atom/Result";
6
+ import type { CreateStoreOptions, LiveQueryDef, LiveStoreSchema, OtelOptions, Store } from "@livestore/livestore";
7
+ import * as Context from "effect/Context";
8
+ import * as Layer from "effect/Layer";
9
+ /**
10
+ * @since 1.0.0
11
+ * @category Store
12
+ */
13
+ export interface StoreService {
14
+ readonly _: unique symbol;
15
+ }
16
+ /**
17
+ * @since 1.0.0
18
+ * @category Constructors
19
+ */
20
+ export declare const make: <S extends LiveStoreSchema, Context = {}>(options: CreateStoreOptions<S, Context> & {
21
+ readonly otelOptions?: Partial<OtelOptions> | undefined;
22
+ }) => {
23
+ /**
24
+ * A context tag for the Store.
25
+ */
26
+ readonly StoreService: Context.Tag<StoreService, Store<S, Context>>;
27
+ /**
28
+ * The Layer that builds the Store
29
+ */
30
+ readonly layer: Layer.Layer<StoreService>;
31
+ /**
32
+ * A Atom.runtime that contains the Store.
33
+ */
34
+ readonly runtimeAtom: Atom.AtomRuntime<StoreService, never>;
35
+ /**
36
+ * A Atom that allows you to access the Store. It will emit a `Result` that
37
+ * contains the Store or an error if it could not be created.
38
+ */
39
+ readonly storeAtom: Atom.Atom<Result.Result<Store<S, Context>>>;
40
+ /**
41
+ * A Atom that allows you to access the Store. It will emit the Store or
42
+ * `undefined` if has not been created yet.
43
+ */
44
+ readonly storeAtomUnsafe: Atom.Atom<Store<S, Context> | undefined>;
45
+ /**
46
+ * Creates a Atom that allows you to resolve a LiveQueryDef. It embeds the loading
47
+ * of the Store and will emit a `Result` that contains the result of the query
48
+ */
49
+ readonly makeQueryAtom: <A>(query: LiveQueryDef<A>) => Atom.Atom<Result.Result<A>>;
50
+ /**
51
+ * Creates a Atom that allows you to resolve a LiveQueryDef. If the Store has
52
+ * not been created yet, it will return `undefined`.
53
+ */
54
+ readonly makeQueryAtomUnsafe: <A>(query: LiveQueryDef<A>) => Atom.Atom<A | undefined>;
55
+ /**
56
+ * A Atom.Writable that allows you to commit an event to the Store.
57
+ */
58
+ readonly commitAtom: Atom.Writable<void, {}>;
59
+ };
60
+ //# sourceMappingURL=AtomLivestore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AtomLivestore.d.ts","sourceRoot":"","sources":["../../src/AtomLivestore.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAA;AAC9C,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAA;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,eAAe,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;AAEjH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AAGzC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,C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AAU,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;CAgE7C,CAAA"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ export * as AtomLivestore from "./AtomLivestore.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
5
+ import * as Atom from "@effect-atom/atom/Atom";
6
+ import * as Result from "@effect-atom/atom/Result";
7
+ import { createStore, provideOtel } from "@livestore/livestore";
8
+ import * as Context from "effect/Context";
9
+ import * as Effect from "effect/Effect";
10
+ import { constUndefined } from "effect/Function";
11
+ import * as Layer from "effect/Layer";
12
+ /**
13
+ * @since 1.0.0
14
+ * @category Constructors
15
+ */
16
+ export const make = options => {
17
+ const StoreService = Context.GenericTag("@effect-atom/atom-livestore/StoreService");
18
+ const layer = Layer.scoped(StoreService, createStore(options).pipe(provideOtel({
19
+ parentSpanContext: options?.otelOptions?.rootSpanContext,
20
+ otelTracer: options?.otelOptions?.tracer
21
+ }), Effect.orDie));
22
+ const runtimeAtom = Atom.runtime(layer);
23
+ const storeAtom = runtimeAtom.atom(StoreService);
24
+ const storeAtomUnsafe = Atom.readable(get => {
25
+ const result = get(storeAtom);
26
+ return Result.getOrElse(result, constUndefined);
27
+ });
28
+ const makeQueryAtom = query => Atom.readable(get => {
29
+ const store = get(storeAtom);
30
+ return Result.map(store, store => {
31
+ const result = store.query(query);
32
+ get.addFinalizer(store.subscribe(query, {
33
+ onUpdate(value) {
34
+ get.setSelf(Result.success(value));
35
+ }
36
+ }));
37
+ return result;
38
+ });
39
+ });
40
+ const makeQueryAtomUnsafe = query => Atom.readable(get => {
41
+ const store = get(storeAtomUnsafe);
42
+ if (store === undefined) {
43
+ return undefined;
44
+ }
45
+ get.addFinalizer(store.subscribe(query, {
46
+ onUpdate(value) {
47
+ get.setSelf(Result.success(value));
48
+ }
49
+ }));
50
+ return store.query(query);
51
+ });
52
+ const commitAtom = Atom.writable(get => {
53
+ get(storeAtomUnsafe);
54
+ }, (ctx, value) => {
55
+ ctx.get(storeAtomUnsafe)?.commit(value);
56
+ });
57
+ return {
58
+ StoreService,
59
+ layer,
60
+ runtimeAtom,
61
+ storeAtom,
62
+ storeAtomUnsafe,
63
+ makeQueryAtom,
64
+ makeQueryAtomUnsafe,
65
+ commitAtom
66
+ };
67
+ };
68
+ //# sourceMappingURL=AtomLivestore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AtomLivestore.js","names":["Atom","Result","createStore","provideOtel","Context","Effect","constUndefined","Layer","make","options","StoreService","GenericTag","layer","scoped","pipe","parentSpanContext","otelOptions","rootSpanContext","otelTracer","tracer","orDie","runtimeAtom","runtime","storeAtom","atom","storeAtomUnsafe","readable","get","result","getOrElse","makeQueryAtom","query","store","map","addFinalizer","subscribe","onUpdate","value","setSelf","success","makeQueryAtomUnsafe","undefined","commitAtom","writable","ctx","commit"],"sources":["../../src/AtomLivestore.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA;AACA,OAAO,KAAKA,IAAI,MAAM,wBAAwB;AAC9C,OAAO,KAAKC,MAAM,MAAM,0BAA0B;AAElD,SAASC,WAAW,EAAEC,WAAW,QAAQ,sBAAsB;AAC/D,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,SAASC,cAAc,QAAQ,iBAAiB;AAChD,OAAO,KAAKC,KAAK,MAAM,cAAc;AAUrC;;;;AAIA,OAAO,MAAMC,IAAI,GACfC,OAEC,IAsCC;EACF,MAAMC,YAAY,GAAGN,OAAO,CAACO,UAAU,CAAkC,0CAA0C,CAAC;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F,eAAe,CAAC;IAClC,IAAIO,KAAK,KAAKS,SAAS,EAAE;MACvB,OAAOA,SAAS;IAClB;IACAd,GAAG,CAACO,YAAY,CACdF,KAAK,CAACG,SAAS,CAACJ,KAAK,EAAE;MACrBK,QAAQA,CAACC,KAAK;QACZV,GAAG,CAACW,OAAO,CAACrC,MAAM,CAACsC,OAAO,CAACF,KAAK,CAAC,CAAC;MACpC;KACD,CAAC,CACH;IACD,OAAOL,KAAK,CAACD,KAAK,CAACA,KAAK,CAAC;EAC3B,CAAC,CAAC;EACJ,MAAMW,UAAU,GAAG1C,IAAI,CAAC2C,QAAQ,CAAEhB,GAAG,IAAI;IACvCA,GAAG,CAACF,eAAe,CAAC;EACtB,CAAC,EAAE,CAACmB,GAAG,EAAEP,KAAS,KAAI;IACpBO,GAAG,CAACjB,GAAG,CAACF,eAAe,CAAC,EAAEoB,MAAM,CAACR,KAAK,CAAC;EACzC,CAAC,CAAC;EACF,OAAO;IACL3B,YAAY;IACZE,KAAK;IACLS,WAAW;IACXE,SAAS;IACTE,eAAe;IACfK,aAAa;IACbU,mBAAmB;IACnBE;GACQ;AACZ,CAAC","ignoreList":[]}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ export * as AtomLivestore from "./AtomLivestore.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["AtomLivestore"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,aAAa,MAAM,oBAAoB","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ {
2
+ "type": "module",
3
+ "sideEffects": []
4
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@effect-atom/atom-livestore",
3
+ "version": "0.1.0",
4
+ "description": "effect-atom bindings to livestore",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/tim-smart/effect-atom.git"
9
+ },
10
+ "sideEffects": [],
11
+ "author": "Effect contributors",
12
+ "homepage": "https://github.com/tim-smart/effect-atom",
13
+ "dependencies": {
14
+ "@effect-atom/atom": "^0.1.0"
15
+ },
16
+ "peerDependencies": {
17
+ "@livestore/livestore": "*",
18
+ "effect": "^3.17"
19
+ },
20
+ "main": "./dist/cjs/index.js",
21
+ "module": "./dist/esm/index.js",
22
+ "types": "./dist/dts/index.d.ts",
23
+ "exports": {
24
+ "./package.json": "./package.json",
25
+ ".": {
26
+ "types": "./dist/dts/index.d.ts",
27
+ "import": "./dist/esm/index.js",
28
+ "default": "./dist/cjs/index.js"
29
+ },
30
+ "./AtomLivestore": {
31
+ "types": "./dist/dts/AtomLivestore.d.ts",
32
+ "import": "./dist/esm/AtomLivestore.js",
33
+ "default": "./dist/cjs/AtomLivestore.js"
34
+ }
35
+ },
36
+ "typesVersions": {
37
+ "*": {
38
+ "AtomLivestore": [
39
+ "./dist/dts/AtomLivestore.d.ts"
40
+ ]
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
5
+ import * as Atom from "@effect-atom/atom/Atom"
6
+ import * as Result from "@effect-atom/atom/Result"
7
+ import type { CreateStoreOptions, LiveQueryDef, LiveStoreSchema, OtelOptions, Store } from "@livestore/livestore"
8
+ import { createStore, provideOtel } from "@livestore/livestore"
9
+ import * as Context from "effect/Context"
10
+ import * as Effect from "effect/Effect"
11
+ import { constUndefined } from "effect/Function"
12
+ import * as Layer from "effect/Layer"
13
+
14
+ /**
15
+ * @since 1.0.0
16
+ * @category Store
17
+ */
18
+ export interface StoreService {
19
+ readonly _: unique symbol
20
+ }
21
+
22
+ /**
23
+ * @since 1.0.0
24
+ * @category Constructors
25
+ */
26
+ export const make = <S extends LiveStoreSchema, Context = {}>(
27
+ options: CreateStoreOptions<S, Context> & {
28
+ readonly otelOptions?: Partial<OtelOptions> | undefined
29
+ }
30
+ ): {
31
+ /**
32
+ * A context tag for the Store.
33
+ */
34
+ readonly StoreService: Context.Tag<StoreService, Store<S, Context>>
35
+ /**
36
+ * The Layer that builds the Store
37
+ */
38
+ readonly layer: Layer.Layer<StoreService>
39
+ /**
40
+ * A Atom.runtime that contains the Store.
41
+ */
42
+ readonly runtimeAtom: Atom.AtomRuntime<StoreService, never>
43
+ /**
44
+ * A Atom that allows you to access the Store. It will emit a `Result` that
45
+ * contains the Store or an error if it could not be created.
46
+ */
47
+ readonly storeAtom: Atom.Atom<Result.Result<Store<S, Context>>>
48
+ /**
49
+ * A Atom that allows you to access the Store. It will emit the Store or
50
+ * `undefined` if has not been created yet.
51
+ */
52
+ readonly storeAtomUnsafe: Atom.Atom<Store<S, Context> | undefined>
53
+ /**
54
+ * Creates a Atom that allows you to resolve a LiveQueryDef. It embeds the loading
55
+ * of the Store and will emit a `Result` that contains the result of the query
56
+ */
57
+ readonly makeQueryAtom: <A>(query: LiveQueryDef<A>) => Atom.Atom<Result.Result<A>>
58
+ /**
59
+ * Creates a Atom that allows you to resolve a LiveQueryDef. If the Store has
60
+ * not been created yet, it will return `undefined`.
61
+ */
62
+ readonly makeQueryAtomUnsafe: <A>(query: LiveQueryDef<A>) => Atom.Atom<A | undefined>
63
+ /**
64
+ * A Atom.Writable that allows you to commit an event to the Store.
65
+ */
66
+ readonly commitAtom: Atom.Writable<void, {}>
67
+ } => {
68
+ const StoreService = Context.GenericTag<StoreService, Store<S, Context>>("@effect-atom/atom-livestore/StoreService")
69
+ const layer = Layer.scoped(
70
+ StoreService,
71
+ createStore(options).pipe(
72
+ provideOtel({
73
+ parentSpanContext: options?.otelOptions?.rootSpanContext,
74
+ otelTracer: options?.otelOptions?.tracer
75
+ }),
76
+ Effect.orDie
77
+ )
78
+ )
79
+ const runtimeAtom = Atom.runtime(layer)
80
+ const storeAtom = runtimeAtom.atom(StoreService)
81
+ const storeAtomUnsafe = Atom.readable((get) => {
82
+ const result = get(storeAtom)
83
+ return Result.getOrElse(result, constUndefined)
84
+ })
85
+ const makeQueryAtom = <A>(query: LiveQueryDef<A>) =>
86
+ Atom.readable((get) => {
87
+ const store = get(storeAtom)
88
+ return Result.map(store, (store) => {
89
+ const result = store.query(query)
90
+ get.addFinalizer(
91
+ store.subscribe(query, {
92
+ onUpdate(value) {
93
+ get.setSelf(Result.success(value))
94
+ }
95
+ })
96
+ )
97
+ return result
98
+ })
99
+ })
100
+ const makeQueryAtomUnsafe = <A>(query: LiveQueryDef<A>) =>
101
+ Atom.readable((get) => {
102
+ const store = get(storeAtomUnsafe)
103
+ if (store === undefined) {
104
+ return undefined
105
+ }
106
+ get.addFinalizer(
107
+ store.subscribe(query, {
108
+ onUpdate(value) {
109
+ get.setSelf(Result.success(value))
110
+ }
111
+ })
112
+ )
113
+ return store.query(query)
114
+ })
115
+ const commitAtom = Atom.writable((get) => {
116
+ get(storeAtomUnsafe)
117
+ }, (ctx, value: {}) => {
118
+ ctx.get(storeAtomUnsafe)?.commit(value)
119
+ })
120
+ return {
121
+ StoreService,
122
+ layer,
123
+ runtimeAtom,
124
+ storeAtom,
125
+ storeAtomUnsafe,
126
+ makeQueryAtom,
127
+ makeQueryAtomUnsafe,
128
+ commitAtom
129
+ } as const
130
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ export * as AtomLivestore from "./AtomLivestore.js"