UNPKG

jinaga

Version:

Data management for web and mobile applications.

146 lines 6.21 kB
import { Authentication } from "./authentication/authentication"; import { HashMap } from './fact/hydrate'; import { SyncStatus, SyncStatusNotifier } from './http/web-client'; import { FactManager } from './managers/factManager'; import { User } from './model/user'; import { ObservableCollection, Observer, ResultAddedFunc } from './observer/observer'; import { SpecificationOf } from './specification/model'; export interface Profile { displayName: string; } export declare type MakeObservable<T> = T extends Array<infer U> ? ObservableCollection<MakeObservable<U>> : T extends { [key: string]: unknown; } ? { [K in keyof T]: MakeObservable<T[K]>; } : T; declare type WatchArgs<T extends unknown[], U> = [...T, ResultAddedFunc<MakeObservable<U>>]; export declare type Fact = { type: string; } & HashMap; export declare class Jinaga { private authentication; private factManager; private syncStatusNotifier; private errorHandlers; private loadingHandlers; private progressHandlers; constructor(authentication: Authentication, factManager: FactManager, syncStatusNotifier: SyncStatusNotifier | null); /** * Register an callback to receive error messages. * * @param handler A function to receive error messages */ onError(handler: (message: string) => void): void; /** * Register a callback to receive loading state notifications. * * @param handler A function to receive loading state */ onLoading(handler: (loading: boolean) => void): void; /** * Register a callback to receive outgoing fact count. * A count greater than 0 is an indication to the user that the application is saving. * * @param handler A function to receive the number of facts in the queue */ onProgress(handler: (queueCount: number) => void): void; onSyncStatus(handler: (status: SyncStatus) => void): void; /** * Log the user in and return a fact that represents their identity. * This method is only valid in the browser. * * @returns A promise that resolves to a fact that represents the user's identity, and the user's profile as reported by the configured Passport strategy */ login<U extends Fact>(): Promise<{ userFact: U; profile: Profile; }>; /** * Access the identity of the local machine. * This method is only valid for the server and clients with local storage. * The local machine's identity is not shared with remote machines. * * @returns A promise that resolves to the local machine's identity */ local<D extends Fact>(): Promise<D>; /** * Creates a new fact. * This method is asynchronous. * It will be resolved when the fact has been persisted. * * @param prototype The fact to save and share * @returns The fact that was just created */ fact<T extends Fact>(prototype: T): Promise<T>; /** * Execute a query for facts matching a specification. * * @param specification Use Model.given().match() to create a specification * @param given The fact or facts from which to begin the query * @returns A promise that resolves to an array of results */ query<T extends unknown[], U>(specification: SpecificationOf<T, U>, ...given: T): Promise<U[]>; /** * Receive notification when a projection changes. * The notification function will initially receive all matching results. * It will then subsequently receive new results as they are created. * Return a function to be called when the result is removed. * * @param specification Use Model.given().match() to create a specification * @param given The fact or facts from which to begin the query * @param resultAdded A function to receive the initial and new results * @returns An observer to control notifications */ watch<T extends unknown[], U>(specification: SpecificationOf<T, U>, ...args: WatchArgs<T, U>): Observer<U>; /** * Request server-sent events when a fact affects query results. * While the subscription is active, the server will push matching facts * to the client. Call Subscription.stop() to stop receiving events. * * @param specification Use Model.given().match() to create a specification * @param given The fact or facts from which to begin the subscription * @returns A subscription, which remains running until you call stop */ subscribe<T extends unknown[], U>(specification: SpecificationOf<T, U>, ...args: WatchArgs<T, U>): Observer<U>; /** * Compute the SHA-256 hash of a fact. * This is a deterministic hash that can be used to identify the fact. * @param fact The fact to hash * @returns The SHA-256 hash of the fact as a base-64 string */ static hash<T extends Fact>(fact: T): string; /** * Compute the SHA-256 hash of a fact. * This is a deterministic hash that can be used to identify the fact. * @param fact The fact to hash * @returns The SHA-256 hash of the fact as a base-64 string */ hash<T extends Fact>(fact: T): string; /** * Purge the data store of all descendants of purge roots. * A purge root is a fact that satisfies a purge condition. * @returns Resolves when the data store has been purged. */ purge(): Promise<void>; /** * Processes the queue immediately, bypassing any delay. * This allows you to ensure that all facts have been sent to the server. */ push(): Promise<void>; /** * Create some facts owned by a single-use principal. A key pair is * generated for the principal and used to sign the facts. The private * key is discarded after the facts are saved. * * @param func A function that saves a set of facts and returns one or more of them * @returns The result of the function */ singleUse<T>(func: (principal: User) => Promise<T>): Promise<T>; private validateFact; private removeNullPredecessors; private static getFactError; private error; private prepareFactReference; } export {}; //# sourceMappingURL=jinaga.d.ts.map