@openctx/client
Advanced tools
Comparing version 0.0.24 to 0.0.25
import type { AnnotationsParams, ItemsParams, ItemsResult, MentionsParams, MentionsResult, MetaParams, MetaResult, ProviderSettings } from '@openctx/protocol'; | ||
import type { Annotation as AnnotationWithPlainRange, Range } from '@openctx/schema'; | ||
import { type Observable, type ObservableInput } from 'rxjs'; | ||
import { Observable, type ObservableLike } from 'observable-fns'; | ||
import type { ClientEnv, ProviderMethodOptions } from './client/client.js'; | ||
@@ -17,3 +17,3 @@ import type { ProviderClient } from './providerClient/createProviderClient.js'; | ||
export type ObservableProviderClient = { | ||
[M in keyof ProviderClient]: (...args: Parameters<Required<ProviderClient>[M]>) => ObservableInput<Awaited<ReturnType<Required<ProviderClient>[M]>>>; | ||
[M in keyof ProviderClient]: (...args: Parameters<Required<ProviderClient>[M]>) => ObservableLike<Awaited<ReturnType<Required<ProviderClient>[M]>>>; | ||
}; | ||
@@ -20,0 +20,0 @@ export interface ProviderClientWithSettings { |
@@ -1,2 +0,3 @@ | ||
import { catchError, combineLatest, defer, distinctUntilChanged, filter, from, map, mergeMap, of, startWith, tap, } from 'rxjs'; | ||
import { Observable, filter, map } from 'observable-fns'; | ||
import { catchError, combineLatest, defer, distinctUntilChanged, mergeMap, promiseOrObservableToObservable, startWith, tap, } from './misc/observable.js'; | ||
function observeProviderCall(providerClients, fn, { emitPartial, errorHook, logger }) { | ||
@@ -20,3 +21,3 @@ // This sentinel value lets us avoid emitting a "fake" partial result when `emitPartial` is | ||
} | ||
return of(null); | ||
return Observable.of(null); | ||
})) | ||
@@ -26,3 +27,3 @@ .pipe(map(items => items === EMIT_PARTIAL_SENTINEL | ||
: (items || []).map(item => ({ ...item, providerUri: uri })))))) | ||
: of([])), filter(result => !emitPartial || result.length === 0 || result.some(v => v !== EMIT_PARTIAL_SENTINEL)), map(result => result | ||
: Observable.of([])), filter(result => !emitPartial || result.length === 0 || result.some(v => v !== EMIT_PARTIAL_SENTINEL)), map(result => result | ||
.filter((v) => v !== null && v !== EMIT_PARTIAL_SENTINEL) | ||
@@ -39,3 +40,3 @@ .flat()), distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)), tap(items => { | ||
export function observeMeta(providerClients, params, opts) { | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => from(providerClient.meta(params, settings)).pipe(map(result => [result])), opts); | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => promiseOrObservableToObservable(providerClient.meta(params, settings)).pipe(map(result => [result])), opts); | ||
} | ||
@@ -46,3 +47,3 @@ /** | ||
export function observeMentions(providerClients, params, opts) { | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => from(providerClient.mentions(params, settings)), opts); | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => promiseOrObservableToObservable(providerClient.mentions(params, settings)), opts); | ||
} | ||
@@ -53,3 +54,3 @@ /** | ||
export function observeItems(providerClients, params, opts) { | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => from(providerClient.items(params, settings)), opts); | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => promiseOrObservableToObservable(providerClient.items(params, settings)), opts); | ||
} | ||
@@ -60,3 +61,3 @@ /** | ||
export function observeAnnotations(providerClients, params, { logger, makeRange, emitPartial, errorHook, }) { | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => from(providerClient.annotations(params, settings)).pipe(map(anns => anns | ||
return observeProviderCall(providerClients, ({ providerClient, settings }) => promiseOrObservableToObservable(providerClient.annotations(params, settings)).pipe(map(anns => anns | ||
? anns | ||
@@ -63,0 +64,0 @@ .map(ann => ({ |
import type { AnnotationsParams, ItemsParams, ItemsResult, MentionsParams, MentionsResult, MetaParams, MetaResult } from '@openctx/protocol'; | ||
import type { Provider } from '@openctx/provider'; | ||
import type { Range } from '@openctx/schema'; | ||
import { type Observable, type ObservableInput } from 'rxjs'; | ||
import { Observable, type ObservableLike } from 'observable-fns'; | ||
import { type Annotation, type EachWithProviderUri, type ObservableProviderClient } from '../api.js'; | ||
@@ -26,3 +26,3 @@ import { type ConfigurationUserInput, type ImportedProviderConfiguration } from '../configuration.js'; | ||
*/ | ||
configuration(resource?: string): ObservableInput<ConfigurationUserInput>; | ||
configuration(resource?: string): ObservableLike<ConfigurationUserInput> | Promise<ConfigurationUserInput>; | ||
/** | ||
@@ -35,3 +35,3 @@ * The base URI to use when resolving configured provider URIs. | ||
*/ | ||
providers?: ObservableInput<ImportedProviderConfiguration[]>; | ||
providers?: ObservableLike<ImportedProviderConfiguration[]> | Promise<ImportedProviderConfiguration[]>; | ||
/** | ||
@@ -42,3 +42,3 @@ * The authentication info for the provider. | ||
*/ | ||
authInfo?(provider: string): ObservableInput<AuthInfo | null>; | ||
authInfo?(provider: string): ObservableLike<AuthInfo | null> | Promise<AuthInfo | null>; | ||
/** | ||
@@ -115,5 +115,4 @@ * Called to print a log message. | ||
* It does not continue to listen for changes, as {@link Client.metaChanges} does. Using | ||
* {@link Client.meta} is simpler and does not require use of observables (with a library like | ||
* RxJS), but it means that the client needs to manually poll for updated item kinds if | ||
* freshness is important. | ||
* {@link Client.meta} is simpler and does not require use of observables, but it means that the | ||
* client needs to manually poll for updated item kinds if freshness is important. | ||
*/ | ||
@@ -132,5 +131,4 @@ meta(params: MetaParams, opts?: ProviderMethodOptions): Promise<EachWithProviderUri<MetaResult[]>>; | ||
* It does not continue to listen for changes, as {@link Client.mentionsChanges} does. Using | ||
* {@link Client.Mentions} is simpler and does not require use of observables (with a library | ||
* like RxJS), but it means that the client needs to manually poll for updated mentions if | ||
* freshness is important. | ||
* {@link Client.Mentions} is simpler and does not require use of observables, but it means that | ||
* the client needs to manually poll for updated mentions if freshness is important. | ||
*/ | ||
@@ -149,5 +147,4 @@ mentions(params: MentionsParams, opts?: ProviderMethodOptions): Promise<EachWithProviderUri<MentionsResult>>; | ||
* It does not continue to listen for changes, as {@link Client.itemsChanges} does. Using | ||
* {@link Client.items} is simpler and does not require use of observables (with a library like | ||
* RxJS), but it means that the client needs to manually poll for updated items if freshness is | ||
* important. | ||
* {@link Client.items} is simpler and does not require use of observables, but it means that | ||
* the client needs to manually poll for updated items if freshness is important. | ||
*/ | ||
@@ -166,5 +163,4 @@ items(params: ItemsParams, opts?: ProviderMethodOptions): Promise<EachWithProviderUri<ItemsResult>>; | ||
* It does not continue to listen for changes, as {@link Client.annotationsChanges} does. Using | ||
* {@link Client.annotations} is simpler and does not require use of observables (with a library | ||
* like RxJS), but it means that the client needs to manually poll for updated annotations if | ||
* freshness is important. | ||
* {@link Client.annotations} is simpler and does not require use of observables, but it means | ||
* that the client needs to manually poll for updated annotations if freshness is important. | ||
*/ | ||
@@ -171,0 +167,0 @@ annotations(params: AnnotationsParams, opts?: ProviderMethodOptions): Promise<EachWithProviderUri<Annotation<R>[]>>; |
import { LRUCache } from 'lru-cache'; | ||
import { catchError, combineLatest, concatMap, distinctUntilChanged, firstValueFrom, from, map, mergeMap, of, shareReplay, take, timer, } from 'rxjs'; | ||
import { Observable, map } from 'observable-fns'; | ||
import { observeAnnotations, observeItems, observeMentions, observeMeta, } from '../api.js'; | ||
import { configurationFromUserInput, } from '../configuration.js'; | ||
import { catchError, combineLatest, concatMap, distinctUntilChanged, firstValueFrom, mergeMap, promiseOrObservableToObservable, shareReplay, take, timer, } from '../misc/observable.js'; | ||
import { createProviderClient } from '../providerClient/createProviderClient.js'; | ||
@@ -15,4 +16,4 @@ /** | ||
// Enable/disable logger based on the `debug` config. | ||
const debug = from(env.configuration()).pipe(map(config => configurationFromUserInput(config).debug), distinctUntilChanged(), shareReplay(1)); | ||
subscriptions.push(debug.subscribe()); | ||
const debug = promiseOrObservableToObservable(env.configuration()).pipe(map(config => configurationFromUserInput(config).debug), distinctUntilChanged(), shareReplay()); | ||
subscriptions.push(debug.subscribe({})); | ||
const logger = message => { | ||
@@ -29,4 +30,4 @@ firstValueFrom(debug) | ||
return combineLatest([ | ||
env.configuration(resource), | ||
env.providers ? env.providers : of(undefined), | ||
promiseOrObservableToObservable(env.configuration(resource)), | ||
env.providers ? promiseOrObservableToObservable(env.providers) : Observable.of(undefined), | ||
]) | ||
@@ -40,3 +41,5 @@ .pipe(map(([config, providers]) => { | ||
.pipe(mergeMap(([configuration, providers]) => configuration.providers.length > 0 | ||
? combineLatest(configuration.providers.map(({ providerUri, settings }) => (env.authInfo ? from(env.authInfo(providerUri)) : of(null)).pipe(map(authInfo => ({ | ||
? combineLatest(configuration.providers.map(({ providerUri, settings }) => (env.authInfo | ||
? promiseOrObservableToObservable(env.authInfo(providerUri)) | ||
: Observable.of(null)).pipe(map(authInfo => ({ | ||
uri: providerUri, | ||
@@ -53,5 +56,5 @@ providerClient: env.__mock__?.getProviderClient | ||
logger(`Error creating provider client for ${providerUri}: ${error}`); | ||
return of(null); | ||
return Observable.of(null); | ||
})))) | ||
: of([])), | ||
: Observable.of([])), | ||
// Filter out null clients. | ||
@@ -96,3 +99,3 @@ map(providerClients => providerClients.filter((providerClient) => providerClient !== null))); | ||
.pipe(take(1)); | ||
subscriptions.push(preload.subscribe()); | ||
subscriptions.push(preload.subscribe({})); | ||
} | ||
@@ -99,0 +102,0 @@ return { |
@@ -1,5 +0,3 @@ | ||
import { Observable } from 'rxjs'; | ||
import type { Observable } from 'rxjs'; | ||
export declare function observableToAsyncGenerator<T>(observable: Observable<T>, signal?: AbortSignal): AsyncGenerator<T>; | ||
export declare function asyncGeneratorToObservable<T>(asyncGenerator: AsyncGenerator<T, void>): Observable<T>; | ||
export declare function isAsyncGenerator(value: any): value is AsyncGenerator<any, any, any>; | ||
//# sourceMappingURL=util.d.ts.map |
@@ -1,2 +0,1 @@ | ||
import { Observable } from 'rxjs'; | ||
export async function* observableToAsyncGenerator(observable, signal) { | ||
@@ -61,34 +60,2 @@ const queue = []; | ||
} | ||
export function asyncGeneratorToObservable(asyncGenerator) { | ||
return new Observable(observer => { | ||
; | ||
(async () => { | ||
try { | ||
for await (const value of asyncGenerator) { | ||
observer.next(value); | ||
} | ||
observer.complete(); | ||
} | ||
catch (error) { | ||
observer.error(error); | ||
} | ||
})(); | ||
return () => { | ||
// If the AsyncGenerator has a return method, call it to clean up | ||
if (asyncGenerator.return) { | ||
asyncGenerator.return(); | ||
} | ||
}; | ||
}); | ||
} | ||
export function isAsyncGenerator(value) { | ||
if (value === null || typeof value !== 'object') { | ||
return false; | ||
} | ||
return (typeof value.next === 'function' && | ||
typeof value.throw === 'function' && | ||
typeof value.return === 'function' && | ||
typeof value[Symbol.asyncIterator] === 'function' && | ||
value[Symbol.asyncIterator]() === value); | ||
} | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@openctx/client", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"description": "OpenCtx client library", | ||
@@ -20,10 +20,24 @@ "license": "Apache-2.0", | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"./src/index": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"./observable": { | ||
"types": "./dist/misc/observable.d.ts", | ||
"default": "./dist/misc/observable.js" | ||
} | ||
}, | ||
"sideEffects": false, | ||
"dependencies": { | ||
"lru-cache": "^10.1.0", | ||
"observable-fns": "^0.6.1", | ||
"picomatch": "^3.0.1", | ||
"rxjs": "^7.8.1", | ||
"@openctx/protocol": "0.0.16", | ||
"@openctx/schema": "0.0.12", | ||
"@openctx/provider": "0.0.15" | ||
"@openctx/provider": "0.0.15", | ||
"@openctx/schema": "0.0.12" | ||
}, | ||
@@ -30,0 +44,0 @@ "devDependencies": { |
@@ -12,5 +12,5 @@ import type { | ||
import type { Annotation as AnnotationWithPlainRange, Range } from '@openctx/schema' | ||
import { Observable, type ObservableLike, filter, map } from 'observable-fns' | ||
import type { ClientEnv, ProviderMethodOptions } from './client/client.js' | ||
import { | ||
type Observable, | ||
type ObservableInput, | ||
catchError, | ||
@@ -20,11 +20,7 @@ combineLatest, | ||
distinctUntilChanged, | ||
filter, | ||
from, | ||
map, | ||
mergeMap, | ||
of, | ||
promiseOrObservableToObservable, | ||
startWith, | ||
tap, | ||
} from 'rxjs' | ||
import type { ClientEnv, ProviderMethodOptions } from './client/client.js' | ||
} from './misc/observable.js' | ||
import type { ProviderClient } from './providerClient/createProviderClient.js' | ||
@@ -46,3 +42,3 @@ | ||
...args: Parameters<Required<ProviderClient>[M]> | ||
) => ObservableInput<Awaited<ReturnType<Required<ProviderClient>[M]>>> | ||
) => ObservableLike<Awaited<ReturnType<Required<ProviderClient>[M]>>> | ||
} | ||
@@ -102,3 +98,3 @@ | ||
} | ||
return of(null) | ||
return Observable.of(null) | ||
}), | ||
@@ -115,5 +111,8 @@ ) | ||
) | ||
: of([]), | ||
: Observable.of([]), | ||
), | ||
filter( | ||
filter< | ||
(typeof EMIT_PARTIAL_SENTINEL | EachWithProviderUri<R[]>)[], | ||
(typeof EMIT_PARTIAL_SENTINEL | EachWithProviderUri<R[]>)[] | ||
>( | ||
result => | ||
@@ -147,3 +146,5 @@ !emitPartial || result.length === 0 || result.some(v => v !== EMIT_PARTIAL_SENTINEL), | ||
({ providerClient, settings }) => | ||
from(providerClient.meta(params, settings)).pipe(map(result => [result])), | ||
promiseOrObservableToObservable(providerClient.meta(params, settings)).pipe( | ||
map(result => [result]), | ||
), | ||
opts, | ||
@@ -163,3 +164,4 @@ ) | ||
providerClients, | ||
({ providerClient, settings }) => from(providerClient.mentions(params, settings)), | ||
({ providerClient, settings }) => | ||
promiseOrObservableToObservable(providerClient.mentions(params, settings)), | ||
opts, | ||
@@ -179,3 +181,4 @@ ) | ||
providerClients, | ||
({ providerClient, settings }) => from(providerClient.items(params, settings)), | ||
({ providerClient, settings }) => | ||
promiseOrObservableToObservable(providerClient.items(params, settings)), | ||
opts, | ||
@@ -201,3 +204,3 @@ ) | ||
({ providerClient, settings }) => | ||
from(providerClient.annotations(params, settings)).pipe( | ||
promiseOrObservableToObservable(providerClient.annotations(params, settings)).pipe( | ||
map(anns => | ||
@@ -204,0 +207,0 @@ anns |
@@ -13,20 +13,4 @@ import type { | ||
import { LRUCache } from 'lru-cache' | ||
import { Observable, type ObservableLike, map } from 'observable-fns' | ||
import { | ||
type Observable, | ||
type ObservableInput, | ||
type Unsubscribable, | ||
catchError, | ||
combineLatest, | ||
concatMap, | ||
distinctUntilChanged, | ||
firstValueFrom, | ||
from, | ||
map, | ||
mergeMap, | ||
of, | ||
shareReplay, | ||
take, | ||
timer, | ||
} from 'rxjs' | ||
import { | ||
type Annotation, | ||
@@ -48,2 +32,15 @@ type EachWithProviderUri, | ||
import type { Logger } from '../logger.js' | ||
import { | ||
type Unsubscribable, | ||
catchError, | ||
combineLatest, | ||
concatMap, | ||
distinctUntilChanged, | ||
firstValueFrom, | ||
mergeMap, | ||
promiseOrObservableToObservable, | ||
shareReplay, | ||
take, | ||
timer, | ||
} from '../misc/observable.js' | ||
import { type ProviderClient, createProviderClient } from '../providerClient/createProviderClient.js' | ||
@@ -69,3 +66,5 @@ | ||
*/ | ||
configuration(resource?: string): ObservableInput<ConfigurationUserInput> | ||
configuration( | ||
resource?: string, | ||
): ObservableLike<ConfigurationUserInput> | Promise<ConfigurationUserInput> | ||
@@ -80,3 +79,5 @@ /** | ||
*/ | ||
providers?: ObservableInput<ImportedProviderConfiguration[]> | ||
providers?: | ||
| ObservableLike<ImportedProviderConfiguration[]> | ||
| Promise<ImportedProviderConfiguration[]> | ||
@@ -88,3 +89,3 @@ /** | ||
*/ | ||
authInfo?(provider: string): ObservableInput<AuthInfo | null> | ||
authInfo?(provider: string): ObservableLike<AuthInfo | null> | Promise<AuthInfo | null> | ||
@@ -166,5 +167,4 @@ /** | ||
* It does not continue to listen for changes, as {@link Client.metaChanges} does. Using | ||
* {@link Client.meta} is simpler and does not require use of observables (with a library like | ||
* RxJS), but it means that the client needs to manually poll for updated item kinds if | ||
* freshness is important. | ||
* {@link Client.meta} is simpler and does not require use of observables, but it means that the | ||
* client needs to manually poll for updated item kinds if freshness is important. | ||
*/ | ||
@@ -188,5 +188,4 @@ meta(params: MetaParams, opts?: ProviderMethodOptions): Promise<EachWithProviderUri<MetaResult[]>> | ||
* It does not continue to listen for changes, as {@link Client.mentionsChanges} does. Using | ||
* {@link Client.Mentions} is simpler and does not require use of observables (with a library | ||
* like RxJS), but it means that the client needs to manually poll for updated mentions if | ||
* freshness is important. | ||
* {@link Client.Mentions} is simpler and does not require use of observables, but it means that | ||
* the client needs to manually poll for updated mentions if freshness is important. | ||
*/ | ||
@@ -213,5 +212,4 @@ mentions( | ||
* It does not continue to listen for changes, as {@link Client.itemsChanges} does. Using | ||
* {@link Client.items} is simpler and does not require use of observables (with a library like | ||
* RxJS), but it means that the client needs to manually poll for updated items if freshness is | ||
* important. | ||
* {@link Client.items} is simpler and does not require use of observables, but it means that | ||
* the client needs to manually poll for updated items if freshness is important. | ||
*/ | ||
@@ -235,5 +233,4 @@ items(params: ItemsParams, opts?: ProviderMethodOptions): Promise<EachWithProviderUri<ItemsResult>> | ||
* It does not continue to listen for changes, as {@link Client.annotationsChanges} does. Using | ||
* {@link Client.annotations} is simpler and does not require use of observables (with a library | ||
* like RxJS), but it means that the client needs to manually poll for updated annotations if | ||
* freshness is important. | ||
* {@link Client.annotations} is simpler and does not require use of observables, but it means | ||
* that the client needs to manually poll for updated annotations if freshness is important. | ||
*/ | ||
@@ -273,8 +270,8 @@ annotations( | ||
// Enable/disable logger based on the `debug` config. | ||
const debug = from(env.configuration()).pipe( | ||
const debug = promiseOrObservableToObservable(env.configuration()).pipe( | ||
map(config => configurationFromUserInput(config).debug), | ||
distinctUntilChanged(), | ||
shareReplay(1), | ||
shareReplay(), | ||
) | ||
subscriptions.push(debug.subscribe()) | ||
subscriptions.push(debug.subscribe({})) | ||
const logger: Logger = message => { | ||
@@ -292,4 +289,4 @@ firstValueFrom(debug) | ||
return combineLatest([ | ||
env.configuration(resource), | ||
env.providers ? env.providers : of(undefined), | ||
promiseOrObservableToObservable(env.configuration(resource)), | ||
env.providers ? promiseOrObservableToObservable(env.providers) : Observable.of(undefined), | ||
]) | ||
@@ -309,3 +306,6 @@ .pipe( | ||
configuration.providers.map(({ providerUri, settings }) => | ||
(env.authInfo ? from(env.authInfo(providerUri)) : of(null)).pipe( | ||
(env.authInfo | ||
? promiseOrObservableToObservable(env.authInfo(providerUri)) | ||
: Observable.of(null) | ||
).pipe( | ||
map(authInfo => ({ | ||
@@ -332,3 +332,3 @@ uri: providerUri, | ||
) | ||
return of(null) | ||
return Observable.of(null) | ||
}), | ||
@@ -338,3 +338,3 @@ ), | ||
) | ||
: of([]), | ||
: Observable.of([]), | ||
), | ||
@@ -417,3 +417,3 @@ | ||
.pipe(take(1)) | ||
subscriptions.push(preload.subscribe()) | ||
subscriptions.push(preload.subscribe({})) | ||
} | ||
@@ -420,0 +420,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
222132
71
3833
+ Addedobservable-fns@^0.6.1
+ Addedobservable-fns@0.6.1(transitive)
- Removedrxjs@^7.8.1
- Removedrxjs@7.8.1(transitive)
- Removedtslib@2.8.1(transitive)