@squidcloud/client 1.0.46 → 1.0.48

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,19 @@
1
+ import { Observable } from 'rxjs';
2
+ /** A handler for a distributed lock that can be released. */
3
+ export interface DistributedLock {
4
+ /**
5
+ * Releases the lock.
6
+ * @returns A promise that resolves when the lock is released.
7
+ */
8
+ release(): Promise<void>;
9
+ /**
10
+ * Whether the lock has been released.
11
+ * @returns True if the lock has been released.
12
+ */
13
+ isReleased(): boolean;
14
+ /**
15
+ * Observes when the lock is released (It may be released due to a connection issue)
16
+ * @returns An observable that emits when the lock is released.
17
+ */
18
+ observeRelease(): Observable<void>;
19
+ }
@@ -66,14 +66,41 @@ export declare class DocumentReference<T extends DocumentData = any> {
66
66
  * is part of a transaction.
67
67
  *
68
68
  * @param data The data to update - can be partial.
69
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
70
+ * immediately.
69
71
  */
70
72
  update(data: Partial<Record<Paths<T>, any>>, transactionId?: TransactionId): Promise<void>;
71
73
  /**
72
74
  * Similar to {@link update}, but only updates the given path.
75
+ * @param path The path to update.
76
+ * @param value The value to set at the given path.
77
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
78
+ * immediately.
73
79
  */
74
80
  setInPath(path: Paths<T>, value: any, transactionId?: TransactionId): Promise<void>;
75
- /** Similar to `update`, but only deletes the given path. */
81
+ /**
82
+ * Similar to `update`, but only deletes the given path.
83
+ * @param path The path to delete.
84
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
85
+ * immediately.
86
+ */
76
87
  deleteInPath(path: Paths<T>, transactionId?: TransactionId): Promise<void>;
88
+ /**
89
+ * Increments the value at the given path by the given value. The value may be both positive and negative.
90
+ * @param path The path to the value to increment.
91
+ * @param value The value to increment by.
92
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
93
+ * immediately.
94
+ */
95
+ incrementInPath(path: Paths<T>, value: number, transactionId?: TransactionId): Promise<void>;
96
+ /**
97
+ * Decrements the value at the given path by the given value. The value may be both positive and negative.
98
+ * @param path The path to the value to decrement.
99
+ * @param value The value to decrement by.
100
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
101
+ * immediately.
102
+ */
103
+ decrementInPath(path: Paths<T>, value: number, transactionId?: TransactionId): Promise<void>;
77
104
  /**
78
105
  * Inserts the document with the given data. If the document already exists, the operation will be treated as
79
106
  * `upsert`. The `insert` will be reflected optimistically locally and will be applied to the server later. If a
@@ -84,6 +111,10 @@ export declare class DocumentReference<T extends DocumentData = any> {
84
111
  *
85
112
  * The returned promise will resolve once the `insert` has been applied to the server or immediately if the `insert`
86
113
  * is part of a transaction.
114
+ *
115
+ * @param data The data to insert.
116
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
117
+ * immediately.
87
118
  */
88
119
  insert(data: T, transactionId?: TransactionId): Promise<void>;
89
120
  /**
@@ -95,6 +126,9 @@ export declare class DocumentReference<T extends DocumentData = any> {
95
126
  *
96
127
  * The returned promise will resolve once the `delete` has been applied to the server or immediately if the `delete`
97
128
  * is part of a transaction.
129
+ *
130
+ * @param transactionId The transaction to use for this operation. If not provided, the operation will be applied
131
+ * immediately.
98
132
  */
99
133
  delete(transactionId?: TransactionId): Promise<void>;
100
134
  }
@@ -2,7 +2,7 @@ import { ClientId, MessageFromClient, MessageToClient } from '@squidcloud/common
2
2
  import { Observable } from 'rxjs';
3
3
  import { DestructManager } from './destruct.manager';
4
4
  export interface SocketManagerInterface {
5
- observeNotifications(): Observable<MessageToClient>;
5
+ observeNotifications<T extends MessageToClient>(): Observable<T>;
6
6
  observeConnectionReady(): Observable<boolean>;
7
7
  sendMessage(message: MessageFromClient): void;
8
8
  }
@@ -18,7 +18,7 @@ export declare class SocketManager implements SocketManagerInterface {
18
18
  private socket;
19
19
  private firstConnection;
20
20
  constructor(clientId: ClientId, socketIoEndpoint: string, messageNotificationWrapper: (fn: () => any) => any, destructManager: DestructManager, extraHeaders?: Record<string, string>);
21
- observeNotifications(): Observable<MessageToClient>;
21
+ observeNotifications<T extends MessageToClient>(): Observable<T>;
22
22
  observeConnectionReady(): Observable<boolean>;
23
23
  sendMessage(message: MessageFromClient): void;
24
24
  private connect;
@@ -3,6 +3,7 @@ import { Observable } from 'rxjs';
3
3
  import { CollectionReference } from './collection-reference';
4
4
  import { GraphQLClient } from './graphql-client';
5
5
  import { TransactionId } from './types';
6
+ import { DistributedLock } from './distributed-lock.manager';
6
7
  /** The different options that can be used to initialize a Squid instance. */
7
8
  export interface SquidOptions {
8
9
  /**
@@ -56,6 +57,7 @@ export declare class Squid {
56
57
  private readonly graphqlClientFactory;
57
58
  private readonly destructManager;
58
59
  private readonly documentIdentityService;
60
+ private readonly distributedLockManager;
59
61
  private static readonly squidInstancesMap;
60
62
  /**
61
63
  * Creates a new instance of Squid with the given options.
@@ -162,6 +164,16 @@ export declare class Squid {
162
164
  * @returns A GraphQL client for the given integration.
163
165
  */
164
166
  graphql: (integrationId: IntegrationId) => GraphQLClient;
167
+ /**
168
+ * Returns a distributed lock for the given mutex. The lock can be used to synchronize access to a shared resource.
169
+ * The lock will be released when the release method on the returned object is invoked or whenever the connection
170
+ * with the server is lost.
171
+ * @param mutex A string that uniquely identifies the lock.
172
+ * @param exclusive Whether the lock should be exclusive or not. If the lock is exclusive, only one client can hold
173
+ * the lock.
174
+ * @returns A promise that resolves with the lock object. The promise will reject if failed to acquire the lock.
175
+ */
176
+ distributedLock: (mutex: string, exclusive?: boolean) => Promise<DistributedLock>;
165
177
  /**
166
178
  * Destructs the Squid Client. Unsubscribes from all ongoing queries or requests, and clears the local data.
167
179
  * After invoking this method, the Squid client will not be usable.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squidcloud/client",
3
- "version": "1.0.46",
3
+ "version": "1.0.48",
4
4
  "description": "A typescript implementation of the Squid client",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/typescript-client/src/index.d.ts",
@@ -1 +0,0 @@
1
- export {};
@@ -1,20 +0,0 @@
1
- import { ClientRequestId } from '@squidcloud/common';
2
- import { DestructManager } from '../destruct.manager';
3
- import { RpcManager } from '../rpc.manager';
4
- import { FakeSocketManager } from './fake-socket.manager';
5
- export declare class FakeRpcManager extends RpcManager {
6
- lastQueryClientRequestId: ClientRequestId | undefined;
7
- lastPostRequest?: {
8
- path: string;
9
- message: any;
10
- };
11
- allPostRequests: Array<{
12
- path: string;
13
- message: any;
14
- }>;
15
- errorResponses: Array<any>;
16
- constructor(socketManager: FakeSocketManager, destructManager: DestructManager);
17
- setStaticHeader(key: string, value: string): void;
18
- deleteStaticHeader(key: string): void;
19
- post<T>(path: string, message: any): Promise<T>;
20
- }
@@ -1,9 +0,0 @@
1
- import { MessageFromClient, MessageToClient } from '@squidcloud/common';
2
- import { Observable, Subject } from 'rxjs';
3
- import { SocketManagerInterface } from '../socket.manager';
4
- export declare class FakeSocketManager implements SocketManagerInterface {
5
- messagesToClient: Subject<MessageToClient>;
6
- observeConnectionReady(): Observable<boolean>;
7
- observeNotifications(): Observable<MessageToClient>;
8
- sendMessage(message: MessageFromClient): void;
9
- }
@@ -1,79 +0,0 @@
1
- import { ClientRequestId, CollectionName, DocId, IntegrationId, LockManager, MutationType, SquidDocument } from '@squidcloud/common';
2
- import { Subject, Subscription } from 'rxjs';
3
- import { CollectionReference } from '../collection-reference';
4
- import { CollectionReferenceFactory } from '../collection-reference.factory';
5
- import { DataManager } from '../data.manager';
6
- import { DestructManager } from '../destruct.manager';
7
- import DocumentIdentityService from '../document-identity.service';
8
- import { DocumentReferenceFactory } from '../document-reference.factory';
9
- import { DocumentStore } from '../document-store';
10
- import { MutationSender } from '../mutation/mutation-sender';
11
- import { JoinQueryBuilderFactory } from '../query/join-query-builder.factory';
12
- import { QueryBuilderFactory } from '../query/query-builder.factory';
13
- import { QuerySubscriptionManager } from '../query/query-subscription.manager';
14
- import { FakeRpcManager } from './fake-rpc.manager';
15
- import { FakeSocketManager } from './fake-socket.manager';
16
- export type TestDocTypeA = {
17
- a: number;
18
- b?: {
19
- c: number;
20
- };
21
- };
22
- export type TestDocTypeB = {
23
- b: number;
24
- c?: {
25
- d: number;
26
- };
27
- };
28
- export type QueryResponse = {
29
- resCount: number;
30
- results: Array<TestDocTypeA> | undefined;
31
- receivedSubject: Subject<void>;
32
- };
33
- export type JoinQueryResponse = {
34
- resCount: number;
35
- results: Array<{
36
- a: TestDocTypeA;
37
- b: TestDocTypeB | undefined;
38
- }> | undefined;
39
- receivedSubject: Subject<void>;
40
- subscription: Subscription;
41
- };
42
- export declare class SquidEnvSetup {
43
- readonly querySubscriptionManager: QuerySubscriptionManager;
44
- readonly socketManager: FakeSocketManager;
45
- readonly rpcManager: FakeRpcManager;
46
- readonly documentStore: DocumentStore;
47
- readonly documentReferenceFactory: DocumentReferenceFactory;
48
- readonly collectionReferenceFactory: CollectionReferenceFactory;
49
- readonly queryBuilderFactory: QueryBuilderFactory;
50
- readonly joinQueryBuilderFactory: JoinQueryBuilderFactory;
51
- readonly lockManager: LockManager;
52
- readonly mutationSender: MutationSender;
53
- readonly dataManager: DataManager;
54
- readonly clientId = "fakeClientId";
55
- readonly destructManager: DestructManager;
56
- readonly documentIdentityService: DocumentIdentityService;
57
- readonly collectionA: CollectionReference<TestDocTypeA>;
58
- readonly collectionB: CollectionReference<TestDocTypeB>;
59
- constructor();
60
- simulateQueryResponse(docs: Array<Partial<SquidDocument>>, collectionName: CollectionName, integrationId?: IntegrationId, clientRequestId?: ClientRequestId): void;
61
- simulateMutationsResponse(updates: Array<{
62
- mutationType: MutationType;
63
- collectionName: CollectionName;
64
- integrationId?: IntegrationId;
65
- clientRequestId?: ClientRequestId;
66
- doc: Partial<SquidDocument> & {
67
- __docId__: DocId;
68
- };
69
- }>, mutationsTimestamp?: number): Promise<void>;
70
- simulateErrorResponse(message: string): void;
71
- runAsync(fn: () => any): Promise<void>;
72
- getBuiltInDocId(idValue: string): string;
73
- executeQueryOnCollectionA(subscribe?: boolean): QueryResponse;
74
- executeJoinQuery(subscribe?: boolean): JoinQueryResponse;
75
- simulateResponseForCollectionA(id?: string, a?: number): void;
76
- simulateResponseForCollectionB(id: string, b: number): void;
77
- }
78
- export declare let squidEnvSetup: SquidEnvSetup;
79
- export declare function squidEnvSetupBeforeEach(): void;