@electric-sql/client
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -6,6 +6,8 @@ type Value = string | number | boolean | bigint | null | Value[] | { | ||
interface Header { | ||
[key: string]: Value; | ||
[key: Exclude<string, `operation` | `control`>]: Value; | ||
} | ||
type ControlMessage = { | ||
headers: Header; | ||
headers: Header & { | ||
control: `up-to-date` | `must-refetch`; | ||
}; | ||
}; | ||
@@ -245,2 +247,43 @@ type ChangeMessage<T> = { | ||
export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, FetchError, type IntervalColumn, type IntervalColumnWithPrecision, type Message, type NumericColumn, type Offset, type RegularColumn, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn }; | ||
/** | ||
* Type guard for checking {@link Message} is {@link ChangeMessage}. | ||
* | ||
* See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) | ||
* for information on how to use type guards. | ||
* | ||
* @param message - the message to check | ||
* @returns true if the message is a {@link ChangeMessage} | ||
* | ||
* @example | ||
* ```ts | ||
* if (isChangeMessage(message)) { | ||
* const msgChng: ChangeMessage = message // Ok | ||
* const msgCtrl: ControlMessage = message // Err, type mismatch | ||
* } | ||
* ``` | ||
*/ | ||
declare function isChangeMessage<T extends Value = { | ||
[key: string]: Value; | ||
}>(message: Message<T>): message is ChangeMessage<T>; | ||
/** | ||
* Type guard for checking {@link Message} is {@link ControlMessage}. | ||
* | ||
* See [TS docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) | ||
* for information on how to use type guards. | ||
* | ||
* @param message - the message to check | ||
* @returns true if the message is a {@link ControlMessage} | ||
* | ||
* * @example | ||
* ```ts | ||
* if (isControlMessage(message)) { | ||
* const msgChng: ChangeMessage = message // Err, type mismatch | ||
* const msgCtrl: ControlMessage = message // Ok | ||
* } | ||
* ``` | ||
*/ | ||
declare function isControlMessage<T extends Value = { | ||
[key: string]: Value; | ||
}>(message: Message<T>): message is ControlMessage; | ||
export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, FetchError, type IntervalColumn, type IntervalColumnWithPrecision, type Message, type NumericColumn, type Offset, type RegularColumn, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn, isChangeMessage, isControlMessage }; |
@@ -128,2 +128,10 @@ var __defProp = Object.defineProperty; | ||
// src/helpers.ts | ||
function isChangeMessage(message) { | ||
return `key` in message; | ||
} | ||
function isControlMessage(message) { | ||
return !isChangeMessage(message); | ||
} | ||
// src/client.ts | ||
@@ -198,3 +206,3 @@ var BackoffDefaults = { | ||
async start() { | ||
var _a, _b; | ||
var _a; | ||
this.isUpToDate = false; | ||
@@ -248,3 +256,3 @@ const { url, where, signal } = this.options; | ||
const lastMessage = batch[batch.length - 1]; | ||
if (((_b = lastMessage.headers) == null ? void 0 : _b[`control`]) === `up-to-date` && !this.isUpToDate) { | ||
if (isControlMessage(lastMessage) && lastMessage.headers.control === `up-to-date` && !this.isUpToDate) { | ||
this.isUpToDate = true; | ||
@@ -407,4 +415,3 @@ this.notifyUpToDateSubscribers(); | ||
messages.forEach((message) => { | ||
var _a, _b; | ||
if (`key` in message) { | ||
if (isChangeMessage(message)) { | ||
dataMayHaveChanged = [`insert`, `update`, `delete`].includes( | ||
@@ -425,14 +432,18 @@ message.headers.operation | ||
} | ||
if (((_a = message.headers) == null ? void 0 : _a[`control`]) === `up-to-date`) { | ||
isUpToDate = true; | ||
if (!this.hasNotifiedSubscribersUpToDate) { | ||
newlyUpToDate = true; | ||
if (isControlMessage(message)) { | ||
switch (message.headers.control) { | ||
case `up-to-date`: | ||
isUpToDate = true; | ||
if (!this.hasNotifiedSubscribersUpToDate) { | ||
newlyUpToDate = true; | ||
} | ||
break; | ||
case `must-refetch`: | ||
this.data.clear(); | ||
this.error = false; | ||
isUpToDate = false; | ||
newlyUpToDate = false; | ||
break; | ||
} | ||
} | ||
if (((_b = message.headers) == null ? void 0 : _b[`control`]) === `must-refetch`) { | ||
this.data.clear(); | ||
this.error = false; | ||
isUpToDate = false; | ||
newlyUpToDate = false; | ||
} | ||
}); | ||
@@ -447,2 +458,3 @@ if (newlyUpToDate || isUpToDate && dataMayHaveChanged) { | ||
this.error = e; | ||
this.notify(); | ||
} | ||
@@ -460,4 +472,6 @@ } | ||
Shape, | ||
ShapeStream | ||
ShapeStream, | ||
isChangeMessage, | ||
isControlMessage | ||
}; | ||
//# sourceMappingURL=index.legacy-esm.js.map |
{ | ||
"name": "@electric-sql/client", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Postgres everywhere - your data, in sync, wherever you need it.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -49,3 +49,3 @@ <p align="center"> | ||
```tsx | ||
import { ShapeStream } from 'electric-sql' | ||
import { ShapeStream } from '@electric-sql/client' | ||
@@ -65,3 +65,3 @@ // Passes subscribers rows as they're inserted, updated, or deleted | ||
```tsx | ||
import { ShapeStream, Shape } from 'electric-sql' | ||
import { ShapeStream, Shape } from '@electric-sql/client' | ||
@@ -68,0 +68,0 @@ const stream = new ShapeStream({ |
@@ -1,4 +0,4 @@ | ||
import { ArgumentsType } from 'vitest' | ||
import { Message, Value, Offset, Schema } from './types' | ||
import { MessageParser, Parser } from './parser' | ||
import { isChangeMessage, isControlMessage } from './helpers' | ||
@@ -199,3 +199,3 @@ export type ShapeData = Map<string, { [key: string]: Value }> | ||
options.fetchClient ?? | ||
((...args: ArgumentsType<typeof fetch>) => fetch(...args)) | ||
((...args: Parameters<typeof fetch>) => fetch(...args)) | ||
@@ -274,3 +274,4 @@ this.start() | ||
if ( | ||
lastMessage.headers?.[`control`] === `up-to-date` && | ||
isControlMessage(lastMessage) && | ||
lastMessage.headers.control === `up-to-date` && | ||
!this.isUpToDate | ||
@@ -519,3 +520,3 @@ ) { | ||
messages.forEach((message) => { | ||
if (`key` in message) { | ||
if (isChangeMessage(message)) { | ||
dataMayHaveChanged = [`insert`, `update`, `delete`].includes( | ||
@@ -541,15 +542,18 @@ message.headers.operation | ||
if (message.headers?.[`control`] === `up-to-date`) { | ||
isUpToDate = true | ||
if (!this.hasNotifiedSubscribersUpToDate) { | ||
newlyUpToDate = true | ||
if (isControlMessage(message)) { | ||
switch (message.headers.control) { | ||
case `up-to-date`: | ||
isUpToDate = true | ||
if (!this.hasNotifiedSubscribersUpToDate) { | ||
newlyUpToDate = true | ||
} | ||
break | ||
case `must-refetch`: | ||
this.data.clear() | ||
this.error = false | ||
isUpToDate = false | ||
newlyUpToDate = false | ||
break | ||
} | ||
} | ||
if (message.headers?.[`control`] === `must-refetch`) { | ||
this.data.clear() | ||
this.error = false | ||
isUpToDate = false | ||
newlyUpToDate = false | ||
} | ||
}) | ||
@@ -568,2 +572,3 @@ | ||
this.error = e | ||
this.notify() | ||
} | ||
@@ -570,0 +575,0 @@ } |
export * from './client' | ||
export * from './types' | ||
export * from './helpers' |
@@ -13,7 +13,7 @@ export type Value = | ||
interface Header { | ||
[key: string]: Value | ||
[key: Exclude<string, `operation` | `control`>]: Value | ||
} | ||
export type ControlMessage = { | ||
headers: Header | ||
headers: Header & { control: `up-to-date` | `must-refetch` } | ||
} | ||
@@ -20,0 +20,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
Sorry, the diff of this file is not supported yet
229225
17
2564