@tanstack/angular-db 0.0.1 → 0.1.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/LICENSE +21 -0
- package/dist/cjs/index.cjs +87 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +36 -0
- package/dist/esm/index.d.ts +36 -0
- package/dist/esm/index.js +87 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +9 -8
- package/src/index.ts +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kyle Mathews
|
|
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.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const core = require("@angular/core");
|
|
4
|
+
const db = require("@tanstack/db");
|
|
5
|
+
function injectLiveQuery(opts) {
|
|
6
|
+
core.assertInInjectionContext(injectLiveQuery);
|
|
7
|
+
const destroyRef = core.inject(core.DestroyRef);
|
|
8
|
+
const collection = core.computed(() => {
|
|
9
|
+
const isExistingCollection = opts && typeof opts === `object` && typeof opts.subscribeChanges === `function` && typeof opts.startSyncImmediate === `function` && typeof opts.id === `string`;
|
|
10
|
+
if (isExistingCollection) {
|
|
11
|
+
return opts;
|
|
12
|
+
}
|
|
13
|
+
if (typeof opts === `function`) {
|
|
14
|
+
return db.createLiveQueryCollection({
|
|
15
|
+
query: opts,
|
|
16
|
+
startSync: true,
|
|
17
|
+
gcTime: 0
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const isReactiveQueryOptions = opts && typeof opts === `object` && typeof opts.query === `function` && typeof opts.params === `function`;
|
|
21
|
+
if (isReactiveQueryOptions) {
|
|
22
|
+
const { params, query } = opts;
|
|
23
|
+
const currentParams = params();
|
|
24
|
+
return db.createLiveQueryCollection({
|
|
25
|
+
query: (q) => query({ params: currentParams, q }),
|
|
26
|
+
startSync: true,
|
|
27
|
+
gcTime: 0
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (opts && typeof opts === `object` && typeof opts.query === `function`) {
|
|
31
|
+
return db.createLiveQueryCollection(opts);
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Invalid options provided to injectLiveQuery`);
|
|
34
|
+
});
|
|
35
|
+
const state = core.signal(/* @__PURE__ */ new Map());
|
|
36
|
+
const data = core.signal([]);
|
|
37
|
+
const status = core.signal(`idle`);
|
|
38
|
+
const syncDataFromCollection = (currentCollection) => {
|
|
39
|
+
const newState = new Map(currentCollection.entries());
|
|
40
|
+
const newData = Array.from(currentCollection.values());
|
|
41
|
+
state.set(newState);
|
|
42
|
+
data.set(newData);
|
|
43
|
+
status.set(currentCollection.status);
|
|
44
|
+
};
|
|
45
|
+
let unsub = null;
|
|
46
|
+
const cleanup = () => {
|
|
47
|
+
unsub == null ? void 0 : unsub();
|
|
48
|
+
unsub = null;
|
|
49
|
+
};
|
|
50
|
+
core.effect((onCleanup) => {
|
|
51
|
+
const currentCollection = collection();
|
|
52
|
+
if (!currentCollection) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
cleanup();
|
|
56
|
+
syncDataFromCollection(currentCollection);
|
|
57
|
+
if (currentCollection.status === `idle`) {
|
|
58
|
+
currentCollection.startSyncImmediate();
|
|
59
|
+
status.set(currentCollection.status);
|
|
60
|
+
}
|
|
61
|
+
unsub = currentCollection.subscribeChanges(
|
|
62
|
+
(_) => {
|
|
63
|
+
syncDataFromCollection(currentCollection);
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
currentCollection.onFirstReady(() => {
|
|
67
|
+
status.set(currentCollection.status);
|
|
68
|
+
});
|
|
69
|
+
onCleanup(cleanup);
|
|
70
|
+
});
|
|
71
|
+
destroyRef.onDestroy(cleanup);
|
|
72
|
+
return {
|
|
73
|
+
state,
|
|
74
|
+
data,
|
|
75
|
+
collection,
|
|
76
|
+
status,
|
|
77
|
+
isLoading: core.computed(
|
|
78
|
+
() => status() === `loading` || status() === `initialCommit`
|
|
79
|
+
),
|
|
80
|
+
isReady: core.computed(() => status() === `ready`),
|
|
81
|
+
isIdle: core.computed(() => status() === `idle`),
|
|
82
|
+
isError: core.computed(() => status() === `error`),
|
|
83
|
+
isCleanedUp: core.computed(() => status() === `cleaned-up`)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
exports.injectLiveQuery = injectLiveQuery;
|
|
87
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import {\n DestroyRef,\n assertInInjectionContext,\n computed,\n effect,\n inject,\n signal,\n} from \"@angular/core\"\nimport { createLiveQueryCollection } from \"@tanstack/db\"\nimport type {\n ChangeMessage,\n Collection,\n CollectionStatus,\n Context,\n GetResult,\n InitialQueryBuilder,\n LiveQueryCollectionConfig,\n QueryBuilder,\n} from \"@tanstack/db\"\nimport type { Signal } from \"@angular/core\"\n\n/**\n * The result of calling `injectLiveQuery`.\n * Contains reactive signals for the query state and data.\n */\nexport interface InjectLiveQueryResult<\n TResult extends object = any,\n TKey extends string | number = string | number,\n TUtils extends Record<string, any> = {},\n> {\n /** A signal containing the complete state map of results keyed by their ID */\n state: Signal<Map<TKey, TResult>>\n /** A signal containing the results as an array */\n data: Signal<Array<TResult>>\n /** A signal containing the underlying collection instance */\n collection: Signal<Collection<TResult, TKey, TUtils>>\n /** A signal containing the current status of the collection */\n status: Signal<CollectionStatus>\n /** A signal indicating whether the collection is currently loading */\n isLoading: Signal<boolean>\n /** A signal indicating whether the collection is ready */\n isReady: Signal<boolean>\n /** A signal indicating whether the collection is idle */\n isIdle: Signal<boolean>\n /** A signal indicating whether the collection has an error */\n isError: Signal<boolean>\n /** A signal indicating whether the collection has been cleaned up */\n isCleanedUp: Signal<boolean>\n}\n\nexport function injectLiveQuery<\n TContext extends Context,\n TParams extends any,\n>(options: {\n params: () => TParams\n query: (args: {\n params: TParams\n q: InitialQueryBuilder\n }) => QueryBuilder<TContext>\n}): InjectLiveQueryResult<GetResult<TContext>>\nexport function injectLiveQuery<TContext extends Context>(\n queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>\n): InjectLiveQueryResult<GetResult<TContext>>\nexport function injectLiveQuery<TContext extends Context>(\n config: LiveQueryCollectionConfig<TContext>\n): InjectLiveQueryResult<GetResult<TContext>>\nexport function injectLiveQuery<\n TResult extends object,\n TKey extends string | number,\n TUtils extends Record<string, any>,\n>(\n liveQueryCollection: Collection<TResult, TKey, TUtils>\n): InjectLiveQueryResult<TResult, TKey, TUtils>\nexport function injectLiveQuery(opts: any) {\n assertInInjectionContext(injectLiveQuery)\n const destroyRef = inject(DestroyRef)\n\n const collection = computed(() => {\n // Check if it's an existing collection\n const isExistingCollection =\n opts &&\n typeof opts === `object` &&\n typeof opts.subscribeChanges === `function` &&\n typeof opts.startSyncImmediate === `function` &&\n typeof opts.id === `string`\n\n if (isExistingCollection) {\n return opts\n }\n\n if (typeof opts === `function`) {\n return createLiveQueryCollection({\n query: opts,\n startSync: true,\n gcTime: 0,\n })\n }\n\n // Check if it's reactive query options\n const isReactiveQueryOptions =\n opts &&\n typeof opts === `object` &&\n typeof opts.query === `function` &&\n typeof opts.params === `function`\n\n if (isReactiveQueryOptions) {\n const { params, query } = opts\n const currentParams = params()\n return createLiveQueryCollection({\n query: (q) => query({ params: currentParams, q }),\n startSync: true,\n gcTime: 0,\n })\n }\n\n // Handle LiveQueryCollectionConfig objects\n if (opts && typeof opts === `object` && typeof opts.query === `function`) {\n return createLiveQueryCollection(opts)\n }\n\n throw new Error(`Invalid options provided to injectLiveQuery`)\n })\n\n const state = signal(new Map<string | number, any>())\n const data = signal<Array<any>>([])\n const status = signal<CollectionStatus>(`idle`)\n\n const syncDataFromCollection = (\n currentCollection: Collection<any, any, any>\n ) => {\n const newState = new Map(currentCollection.entries())\n const newData = Array.from(currentCollection.values())\n\n state.set(newState)\n data.set(newData)\n status.set(currentCollection.status)\n }\n\n let unsub: (() => void) | null = null\n const cleanup = () => {\n unsub?.()\n unsub = null\n }\n\n effect((onCleanup) => {\n const currentCollection = collection()\n\n if (!currentCollection) {\n return\n }\n\n cleanup()\n\n // Initialize immediately with current state\n syncDataFromCollection(currentCollection)\n\n // Start sync if idle\n if (currentCollection.status === `idle`) {\n currentCollection.startSyncImmediate()\n // Update status after starting sync\n status.set(currentCollection.status)\n }\n\n // Subscribe to changes\n unsub = currentCollection.subscribeChanges(\n (_: Array<ChangeMessage<any>>) => {\n syncDataFromCollection(currentCollection)\n }\n )\n\n // Handle ready state\n currentCollection.onFirstReady(() => {\n status.set(currentCollection.status)\n })\n\n onCleanup(cleanup)\n })\n\n destroyRef.onDestroy(cleanup)\n\n return {\n state,\n data,\n collection,\n status,\n isLoading: computed(\n () => status() === `loading` || status() === `initialCommit`\n ),\n isReady: computed(() => status() === `ready`),\n isIdle: computed(() => status() === `idle`),\n isError: computed(() => status() === `error`),\n isCleanedUp: computed(() => status() === `cleaned-up`),\n }\n}\n"],"names":["assertInInjectionContext","inject","DestroyRef","computed","createLiveQueryCollection","signal","effect"],"mappings":";;;;AAyEO,SAAS,gBAAgB,MAAW;AACzCA,OAAAA,yBAAyB,eAAe;AACxC,QAAM,aAAaC,KAAAA,OAAOC,eAAU;AAEpC,QAAM,aAAaC,KAAAA,SAAS,MAAM;AAEhC,UAAM,uBACJ,QACA,OAAO,SAAS,YAChB,OAAO,KAAK,qBAAqB,cACjC,OAAO,KAAK,uBAAuB,cACnC,OAAO,KAAK,OAAO;AAErB,QAAI,sBAAsB;AACxB,aAAO;AAAA,IACT;AAEA,QAAI,OAAO,SAAS,YAAY;AAC9B,aAAOC,6BAA0B;AAAA,QAC/B,OAAO;AAAA,QACP,WAAW;AAAA,QACX,QAAQ;AAAA,MAAA,CACT;AAAA,IACH;AAGA,UAAM,yBACJ,QACA,OAAO,SAAS,YAChB,OAAO,KAAK,UAAU,cACtB,OAAO,KAAK,WAAW;AAEzB,QAAI,wBAAwB;AAC1B,YAAM,EAAE,QAAQ,MAAA,IAAU;AAC1B,YAAM,gBAAgB,OAAA;AACtB,aAAOA,6BAA0B;AAAA,QAC/B,OAAO,CAAC,MAAM,MAAM,EAAE,QAAQ,eAAe,GAAG;AAAA,QAChD,WAAW;AAAA,QACX,QAAQ;AAAA,MAAA,CACT;AAAA,IACH;AAGA,QAAI,QAAQ,OAAO,SAAS,YAAY,OAAO,KAAK,UAAU,YAAY;AACxE,aAAOA,GAAAA,0BAA0B,IAAI;AAAA,IACvC;AAEA,UAAM,IAAI,MAAM,6CAA6C;AAAA,EAC/D,CAAC;AAED,QAA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,YAAY;AAAA,EAAA;AAEzD;;"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Collection, CollectionStatus, Context, GetResult, InitialQueryBuilder, LiveQueryCollectionConfig, QueryBuilder } from '@tanstack/db';
|
|
2
|
+
import { Signal } from '@angular/core';
|
|
3
|
+
/**
|
|
4
|
+
* The result of calling `injectLiveQuery`.
|
|
5
|
+
* Contains reactive signals for the query state and data.
|
|
6
|
+
*/
|
|
7
|
+
export interface InjectLiveQueryResult<TResult extends object = any, TKey extends string | number = string | number, TUtils extends Record<string, any> = {}> {
|
|
8
|
+
/** A signal containing the complete state map of results keyed by their ID */
|
|
9
|
+
state: Signal<Map<TKey, TResult>>;
|
|
10
|
+
/** A signal containing the results as an array */
|
|
11
|
+
data: Signal<Array<TResult>>;
|
|
12
|
+
/** A signal containing the underlying collection instance */
|
|
13
|
+
collection: Signal<Collection<TResult, TKey, TUtils>>;
|
|
14
|
+
/** A signal containing the current status of the collection */
|
|
15
|
+
status: Signal<CollectionStatus>;
|
|
16
|
+
/** A signal indicating whether the collection is currently loading */
|
|
17
|
+
isLoading: Signal<boolean>;
|
|
18
|
+
/** A signal indicating whether the collection is ready */
|
|
19
|
+
isReady: Signal<boolean>;
|
|
20
|
+
/** A signal indicating whether the collection is idle */
|
|
21
|
+
isIdle: Signal<boolean>;
|
|
22
|
+
/** A signal indicating whether the collection has an error */
|
|
23
|
+
isError: Signal<boolean>;
|
|
24
|
+
/** A signal indicating whether the collection has been cleaned up */
|
|
25
|
+
isCleanedUp: Signal<boolean>;
|
|
26
|
+
}
|
|
27
|
+
export declare function injectLiveQuery<TContext extends Context, TParams extends any>(options: {
|
|
28
|
+
params: () => TParams;
|
|
29
|
+
query: (args: {
|
|
30
|
+
params: TParams;
|
|
31
|
+
q: InitialQueryBuilder;
|
|
32
|
+
}) => QueryBuilder<TContext>;
|
|
33
|
+
}): InjectLiveQueryResult<GetResult<TContext>>;
|
|
34
|
+
export declare function injectLiveQuery<TContext extends Context>(queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>): InjectLiveQueryResult<GetResult<TContext>>;
|
|
35
|
+
export declare function injectLiveQuery<TContext extends Context>(config: LiveQueryCollectionConfig<TContext>): InjectLiveQueryResult<GetResult<TContext>>;
|
|
36
|
+
export declare function injectLiveQuery<TResult extends object, TKey extends string | number, TUtils extends Record<string, any>>(liveQueryCollection: Collection<TResult, TKey, TUtils>): InjectLiveQueryResult<TResult, TKey, TUtils>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Collection, CollectionStatus, Context, GetResult, InitialQueryBuilder, LiveQueryCollectionConfig, QueryBuilder } from '@tanstack/db';
|
|
2
|
+
import { Signal } from '@angular/core';
|
|
3
|
+
/**
|
|
4
|
+
* The result of calling `injectLiveQuery`.
|
|
5
|
+
* Contains reactive signals for the query state and data.
|
|
6
|
+
*/
|
|
7
|
+
export interface InjectLiveQueryResult<TResult extends object = any, TKey extends string | number = string | number, TUtils extends Record<string, any> = {}> {
|
|
8
|
+
/** A signal containing the complete state map of results keyed by their ID */
|
|
9
|
+
state: Signal<Map<TKey, TResult>>;
|
|
10
|
+
/** A signal containing the results as an array */
|
|
11
|
+
data: Signal<Array<TResult>>;
|
|
12
|
+
/** A signal containing the underlying collection instance */
|
|
13
|
+
collection: Signal<Collection<TResult, TKey, TUtils>>;
|
|
14
|
+
/** A signal containing the current status of the collection */
|
|
15
|
+
status: Signal<CollectionStatus>;
|
|
16
|
+
/** A signal indicating whether the collection is currently loading */
|
|
17
|
+
isLoading: Signal<boolean>;
|
|
18
|
+
/** A signal indicating whether the collection is ready */
|
|
19
|
+
isReady: Signal<boolean>;
|
|
20
|
+
/** A signal indicating whether the collection is idle */
|
|
21
|
+
isIdle: Signal<boolean>;
|
|
22
|
+
/** A signal indicating whether the collection has an error */
|
|
23
|
+
isError: Signal<boolean>;
|
|
24
|
+
/** A signal indicating whether the collection has been cleaned up */
|
|
25
|
+
isCleanedUp: Signal<boolean>;
|
|
26
|
+
}
|
|
27
|
+
export declare function injectLiveQuery<TContext extends Context, TParams extends any>(options: {
|
|
28
|
+
params: () => TParams;
|
|
29
|
+
query: (args: {
|
|
30
|
+
params: TParams;
|
|
31
|
+
q: InitialQueryBuilder;
|
|
32
|
+
}) => QueryBuilder<TContext>;
|
|
33
|
+
}): InjectLiveQueryResult<GetResult<TContext>>;
|
|
34
|
+
export declare function injectLiveQuery<TContext extends Context>(queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>): InjectLiveQueryResult<GetResult<TContext>>;
|
|
35
|
+
export declare function injectLiveQuery<TContext extends Context>(config: LiveQueryCollectionConfig<TContext>): InjectLiveQueryResult<GetResult<TContext>>;
|
|
36
|
+
export declare function injectLiveQuery<TResult extends object, TKey extends string | number, TUtils extends Record<string, any>>(liveQueryCollection: Collection<TResult, TKey, TUtils>): InjectLiveQueryResult<TResult, TKey, TUtils>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, DestroyRef, computed, signal, effect } from "@angular/core";
|
|
2
|
+
import { createLiveQueryCollection } from "@tanstack/db";
|
|
3
|
+
function injectLiveQuery(opts) {
|
|
4
|
+
assertInInjectionContext(injectLiveQuery);
|
|
5
|
+
const destroyRef = inject(DestroyRef);
|
|
6
|
+
const collection = computed(() => {
|
|
7
|
+
const isExistingCollection = opts && typeof opts === `object` && typeof opts.subscribeChanges === `function` && typeof opts.startSyncImmediate === `function` && typeof opts.id === `string`;
|
|
8
|
+
if (isExistingCollection) {
|
|
9
|
+
return opts;
|
|
10
|
+
}
|
|
11
|
+
if (typeof opts === `function`) {
|
|
12
|
+
return createLiveQueryCollection({
|
|
13
|
+
query: opts,
|
|
14
|
+
startSync: true,
|
|
15
|
+
gcTime: 0
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
const isReactiveQueryOptions = opts && typeof opts === `object` && typeof opts.query === `function` && typeof opts.params === `function`;
|
|
19
|
+
if (isReactiveQueryOptions) {
|
|
20
|
+
const { params, query } = opts;
|
|
21
|
+
const currentParams = params();
|
|
22
|
+
return createLiveQueryCollection({
|
|
23
|
+
query: (q) => query({ params: currentParams, q }),
|
|
24
|
+
startSync: true,
|
|
25
|
+
gcTime: 0
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (opts && typeof opts === `object` && typeof opts.query === `function`) {
|
|
29
|
+
return createLiveQueryCollection(opts);
|
|
30
|
+
}
|
|
31
|
+
throw new Error(`Invalid options provided to injectLiveQuery`);
|
|
32
|
+
});
|
|
33
|
+
const state = signal(/* @__PURE__ */ new Map());
|
|
34
|
+
const data = signal([]);
|
|
35
|
+
const status = signal(`idle`);
|
|
36
|
+
const syncDataFromCollection = (currentCollection) => {
|
|
37
|
+
const newState = new Map(currentCollection.entries());
|
|
38
|
+
const newData = Array.from(currentCollection.values());
|
|
39
|
+
state.set(newState);
|
|
40
|
+
data.set(newData);
|
|
41
|
+
status.set(currentCollection.status);
|
|
42
|
+
};
|
|
43
|
+
let unsub = null;
|
|
44
|
+
const cleanup = () => {
|
|
45
|
+
unsub == null ? void 0 : unsub();
|
|
46
|
+
unsub = null;
|
|
47
|
+
};
|
|
48
|
+
effect((onCleanup) => {
|
|
49
|
+
const currentCollection = collection();
|
|
50
|
+
if (!currentCollection) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
cleanup();
|
|
54
|
+
syncDataFromCollection(currentCollection);
|
|
55
|
+
if (currentCollection.status === `idle`) {
|
|
56
|
+
currentCollection.startSyncImmediate();
|
|
57
|
+
status.set(currentCollection.status);
|
|
58
|
+
}
|
|
59
|
+
unsub = currentCollection.subscribeChanges(
|
|
60
|
+
(_) => {
|
|
61
|
+
syncDataFromCollection(currentCollection);
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
currentCollection.onFirstReady(() => {
|
|
65
|
+
status.set(currentCollection.status);
|
|
66
|
+
});
|
|
67
|
+
onCleanup(cleanup);
|
|
68
|
+
});
|
|
69
|
+
destroyRef.onDestroy(cleanup);
|
|
70
|
+
return {
|
|
71
|
+
state,
|
|
72
|
+
data,
|
|
73
|
+
collection,
|
|
74
|
+
status,
|
|
75
|
+
isLoading: computed(
|
|
76
|
+
() => status() === `loading` || status() === `initialCommit`
|
|
77
|
+
),
|
|
78
|
+
isReady: computed(() => status() === `ready`),
|
|
79
|
+
isIdle: computed(() => status() === `idle`),
|
|
80
|
+
isError: computed(() => status() === `error`),
|
|
81
|
+
isCleanedUp: computed(() => status() === `cleaned-up`)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
injectLiveQuery
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import {\n DestroyRef,\n assertInInjectionContext,\n computed,\n effect,\n inject,\n signal,\n} from \"@angular/core\"\nimport { createLiveQueryCollection } from \"@tanstack/db\"\nimport type {\n ChangeMessage,\n Collection,\n CollectionStatus,\n Context,\n GetResult,\n InitialQueryBuilder,\n LiveQueryCollectionConfig,\n QueryBuilder,\n} from \"@tanstack/db\"\nimport type { Signal } from \"@angular/core\"\n\n/**\n * The result of calling `injectLiveQuery`.\n * Contains reactive signals for the query state and data.\n */\nexport interface InjectLiveQueryResult<\n TResult extends object = any,\n TKey extends string | number = string | number,\n TUtils extends Record<string, any> = {},\n> {\n /** A signal containing the complete state map of results keyed by their ID */\n state: Signal<Map<TKey, TResult>>\n /** A signal containing the results as an array */\n data: Signal<Array<TResult>>\n /** A signal containing the underlying collection instance */\n collection: Signal<Collection<TResult, TKey, TUtils>>\n /** A signal containing the current status of the collection */\n status: Signal<CollectionStatus>\n /** A signal indicating whether the collection is currently loading */\n isLoading: Signal<boolean>\n /** A signal indicating whether the collection is ready */\n isReady: Signal<boolean>\n /** A signal indicating whether the collection is idle */\n isIdle: Signal<boolean>\n /** A signal indicating whether the collection has an error */\n isError: Signal<boolean>\n /** A signal indicating whether the collection has been cleaned up */\n isCleanedUp: Signal<boolean>\n}\n\nexport function injectLiveQuery<\n TContext extends Context,\n TParams extends any,\n>(options: {\n params: () => TParams\n query: (args: {\n params: TParams\n q: InitialQueryBuilder\n }) => QueryBuilder<TContext>\n}): InjectLiveQueryResult<GetResult<TContext>>\nexport function injectLiveQuery<TContext extends Context>(\n queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>\n): InjectLiveQueryResult<GetResult<TContext>>\nexport function injectLiveQuery<TContext extends Context>(\n config: LiveQueryCollectionConfig<TContext>\n): InjectLiveQueryResult<GetResult<TContext>>\nexport function injectLiveQuery<\n TResult extends object,\n TKey extends string | number,\n TUtils extends Record<string, any>,\n>(\n liveQueryCollection: Collection<TResult, TKey, TUtils>\n): InjectLiveQueryResult<TResult, TKey, TUtils>\nexport function injectLiveQuery(opts: any) {\n assertInInjectionContext(injectLiveQuery)\n const destroyRef = inject(DestroyRef)\n\n const collection = computed(() => {\n // Check if it's an existing collection\n const isExistingCollection =\n opts &&\n typeof opts === `object` &&\n typeof opts.subscribeChanges === `function` &&\n typeof opts.startSyncImmediate === `function` &&\n typeof opts.id === `string`\n\n if (isExistingCollection) {\n return opts\n }\n\n if (typeof opts === `function`) {\n return createLiveQueryCollection({\n query: opts,\n startSync: true,\n gcTime: 0,\n })\n }\n\n // Check if it's reactive query options\n const isReactiveQueryOptions =\n opts &&\n typeof opts === `object` &&\n typeof opts.query === `function` &&\n typeof opts.params === `function`\n\n if (isReactiveQueryOptions) {\n const { params, query } = opts\n const currentParams = params()\n return createLiveQueryCollection({\n query: (q) => query({ params: currentParams, q }),\n startSync: true,\n gcTime: 0,\n })\n }\n\n // Handle LiveQueryCollectionConfig objects\n if (opts && typeof opts === `object` && typeof opts.query === `function`) {\n return createLiveQueryCollection(opts)\n }\n\n throw new Error(`Invalid options provided to injectLiveQuery`)\n })\n\n const state = signal(new Map<string | number, any>())\n const data = signal<Array<any>>([])\n const status = signal<CollectionStatus>(`idle`)\n\n const syncDataFromCollection = (\n currentCollection: Collection<any, any, any>\n ) => {\n const newState = new Map(currentCollection.entries())\n const newData = Array.from(currentCollection.values())\n\n state.set(newState)\n data.set(newData)\n status.set(currentCollection.status)\n }\n\n let unsub: (() => void) | null = null\n const cleanup = () => {\n unsub?.()\n unsub = null\n }\n\n effect((onCleanup) => {\n const currentCollection = collection()\n\n if (!currentCollection) {\n return\n }\n\n cleanup()\n\n // Initialize immediately with current state\n syncDataFromCollection(currentCollection)\n\n // Start sync if idle\n if (currentCollection.status === `idle`) {\n currentCollection.startSyncImmediate()\n // Update status after starting sync\n status.set(currentCollection.status)\n }\n\n // Subscribe to changes\n unsub = currentCollection.subscribeChanges(\n (_: Array<ChangeMessage<any>>) => {\n syncDataFromCollection(currentCollection)\n }\n )\n\n // Handle ready state\n currentCollection.onFirstReady(() => {\n status.set(currentCollection.status)\n })\n\n onCleanup(cleanup)\n })\n\n destroyRef.onDestroy(cleanup)\n\n return {\n state,\n data,\n collection,\n status,\n isLoading: computed(\n () => status() === `loading` || status() === `initialCommit`\n ),\n isReady: computed(() => status() === `ready`),\n isIdle: computed(() => status() === `idle`),\n isError: computed(() => status() === `error`),\n isCleanedUp: computed(() => status() === `cleaned-up`),\n }\n}\n"],"names":[],"mappings":";;AAyEO,SAAS,gBAAgB,MAAW;AACzC,2BAAyB,eAAe;AACxC,QAAM,aAAa,OAAO,UAAU;AAEpC,QAAM,aAAa,SAAS,MAAM;AAEhC,UAAM,uBACJ,QACA,OAAO,SAAS,YAChB,OAAO,KAAK,qBAAqB,cACjC,OAAO,KAAK,uBAAuB,cACnC,OAAO,KAAK,OAAO;AAErB,QAAI,sBAAsB;AACxB,aAAO;AAAA,IACT;AAEA,QAAI,OAAO,SAAS,YAAY;AAC9B,aAAO,0BAA0B;AAAA,QAC/B,OAAO;AAAA,QACP,WAAW;AAAA,QACX,QAAQ;AAAA,MAAA,CACT;AAAA,IACH;AAGA,UAAM,yBACJ,QACA,OAAO,SAAS,YAChB,OAAO,KAAK,UA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ACnB,CAAC;AAED,aAAW,UAAU,OAAO;AAE5B,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW;AAAA,MACT,MAAM,OAAA,MAAa,aAAa,aAAa;AAAA,IAAA;AAAA,IAE/C,SAAS,SAAS,MAAM,OAAA,MAAa,OAAO;AAAA,IAC5C,QAAQ,SAAS,MAAM,OAAA,MAAa,MAAM;AAAA,IAC1C,SAAS,SAAS,MAAM,OAAA,MAAa,OAAO;AAAA,IAC5C,aAAa,SAAS,MAAM,OAAA,MAAa,YAAY;AAAA,EAAA;AAEzD;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/angular-db",
|
|
3
3
|
"description": "Angular integration for @tanstack/db",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"author": "Ethan McDaniel",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -15,13 +15,14 @@
|
|
|
15
15
|
"angular",
|
|
16
16
|
"typescript"
|
|
17
17
|
],
|
|
18
|
-
"packageManager": "pnpm@10.6.3",
|
|
19
18
|
"dependencies": {
|
|
20
|
-
"@tanstack/db": "
|
|
19
|
+
"@tanstack/db": "0.2.5"
|
|
21
20
|
},
|
|
22
21
|
"devDependencies": {
|
|
23
22
|
"@angular/common": "^19.0.0",
|
|
23
|
+
"@angular/compiler": "^19.0.0",
|
|
24
24
|
"@angular/core": "^19.0.0",
|
|
25
|
+
"@angular/platform-browser": "^19.0.0",
|
|
25
26
|
"@angular/platform-browser-dynamic": "^19.0.0",
|
|
26
27
|
"@vitest/coverage-istanbul": "^3.0.9",
|
|
27
28
|
"rxjs": "^7.8.0",
|
|
@@ -50,13 +51,13 @@
|
|
|
50
51
|
"@angular/core": ">=16.0.0",
|
|
51
52
|
"rxjs": ">=6.0.0"
|
|
52
53
|
},
|
|
54
|
+
"sideEffects": false,
|
|
55
|
+
"type": "module",
|
|
56
|
+
"types": "dist/esm/index.d.ts",
|
|
53
57
|
"scripts": {
|
|
54
58
|
"build": "vite build",
|
|
55
59
|
"dev": "vite build --watch",
|
|
56
60
|
"test": "npx vitest --run",
|
|
57
61
|
"lint": "eslint . --fix"
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
"type": "module",
|
|
61
|
-
"types": "dist/esm/index.d.ts"
|
|
62
|
-
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -24,9 +24,9 @@ import type { Signal } from "@angular/core"
|
|
|
24
24
|
* Contains reactive signals for the query state and data.
|
|
25
25
|
*/
|
|
26
26
|
export interface InjectLiveQueryResult<
|
|
27
|
-
TResult = any,
|
|
27
|
+
TResult extends object = any,
|
|
28
28
|
TKey extends string | number = string | number,
|
|
29
|
-
TUtils extends Record<string, any> =
|
|
29
|
+
TUtils extends Record<string, any> = {},
|
|
30
30
|
> {
|
|
31
31
|
/** A signal containing the complete state map of results keyed by their ID */
|
|
32
32
|
state: Signal<Map<TKey, TResult>>
|