@liveblocks/client
Advanced tools
Comparing version 0.7.3 to 0.8.0
@@ -1,5 +0,3 @@ | ||
import { AuthEndpoint } from "./types"; | ||
import { AuthEndpoint, AuthenticationToken } from "./types"; | ||
export default function auth(endpoint: AuthEndpoint, room: string): Promise<string>; | ||
export declare function parseToken(token: string): { | ||
actor: number; | ||
}; | ||
export declare function parseToken(token: string): AuthenticationToken; |
import { RecordData, List } from "."; | ||
import { Doc, Record } from "./doc"; | ||
import { Others, Presence, ClientOptions, Room, InitialStorageFactory, MyPresenceCallback, OthersEventCallback, StorageCallback, AuthEndpoint, LiveStorageState, LiveStorage, EventCallback, User, Connection, Serializable, ErrorCallback } from "./types"; | ||
import { Others, Presence, ClientOptions, Room, InitialStorageFactory, MyPresenceCallback, OthersEventCallback, StorageCallback, AuthEndpoint, LiveStorageState, LiveStorage, EventCallback, User, Connection, Serializable, ErrorCallback, AuthenticationToken, ConnectionCallback } from "./types"; | ||
import { ClientMessage, Op } from "./live"; | ||
@@ -29,2 +29,3 @@ declare type IdFactory = () => string; | ||
error: ErrorCallback[]; | ||
connection: ConnectionCallback[]; | ||
}; | ||
@@ -64,3 +65,3 @@ me: Presence; | ||
onMessage: (event: MessageEvent) => void; | ||
authenticationSuccess: (connectionId: number, socket: WebSocket) => void; | ||
authenticationSuccess: (token: AuthenticationToken, socket: WebSocket) => void; | ||
heartbeat: () => void; | ||
@@ -77,2 +78,3 @@ onNavigatorOnline: () => void; | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -85,2 +87,3 @@ unsubscribe: { | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -98,3 +101,4 @@ updatePresence: <T_6 extends Serializable>(overrides: Partial<T_6>) => void; | ||
selectors: { | ||
getConnectionState: () => Connection; | ||
getConnectionState: () => "failed" | "closed" | "open" | "connecting" | "authenticating" | "unavailable"; | ||
getCurrentUser: <TPresence extends Serializable = Serializable>() => User<TPresence> | null; | ||
getPresence: <T_14 extends Serializable>() => T_14; | ||
@@ -101,0 +105,0 @@ getOthers: <T_15 extends Serializable>() => Others<T_15>; |
@@ -47,3 +47,4 @@ "use strict"; | ||
value === "event" || | ||
value === "error"); | ||
value === "error" || | ||
value === "connection"); | ||
} | ||
@@ -78,3 +79,3 @@ function makeIdFactory(connectionId) { | ||
const token = yield authentication_1.default(context.authEndpoint, context.room); | ||
const connectionId = authentication_1.parseToken(token).actor; | ||
const parsedToken = authentication_1.parseToken(token); | ||
const socket = new WebSocket(`${context.liveblocksServer}/?token=${token}`); | ||
@@ -85,3 +86,3 @@ socket.addEventListener("message", onMessage); | ||
socket.addEventListener("error", onError); | ||
authenticationSuccess(connectionId, socket); | ||
authenticationSuccess(parsedToken, socket); | ||
} | ||
@@ -126,4 +127,15 @@ catch (er) { | ||
function getConnectionState() { | ||
return state.connection; | ||
return state.connection.state; | ||
} | ||
function getCurrentUser() { | ||
return state.connection.state === "open" || | ||
state.connection.state === "connecting" | ||
? { | ||
connectionId: state.connection.id, | ||
id: state.connection.userId, | ||
info: state.connection.userInfo, | ||
presence: getPresence(), | ||
} | ||
: null; | ||
} | ||
function connect() { | ||
@@ -156,5 +168,10 @@ if (typeof window === "undefined") { | ||
} | ||
function authenticationSuccess(connectionId, socket) { | ||
updateConnection({ state: "connecting", id: connectionId }); | ||
state.idFactory = makeIdFactory(connectionId); | ||
function authenticationSuccess(token, socket) { | ||
updateConnection({ | ||
state: "connecting", | ||
id: token.actor, | ||
userInfo: token.info, | ||
userId: token.id, | ||
}); | ||
state.idFactory = makeIdFactory(token.actor); | ||
state.socket = socket; | ||
@@ -327,2 +344,5 @@ } | ||
state.connection = connection; | ||
for (const listener of state.listeners.connection) { | ||
listener(connection.state); | ||
} | ||
} | ||
@@ -338,5 +358,10 @@ function getRetryDelay() { | ||
state.intervalHandles.heartbeat = effects.startHeartbeatInterval(); | ||
updateConnection({ state: "open", id: state.connection.id }); | ||
state.numberOfRetry = 0; | ||
tryFlushing(); | ||
if (state.connection.state === "connecting") { | ||
updateConnection(Object.assign(Object.assign({}, state.connection), { state: "open" })); | ||
state.numberOfRetry = 0; | ||
tryFlushing(); | ||
} | ||
else { | ||
// TODO | ||
} | ||
} | ||
@@ -575,2 +600,3 @@ function heartbeat() { | ||
getConnectionState, | ||
getCurrentUser, | ||
// Presence | ||
@@ -595,2 +621,3 @@ getPresence, | ||
error: [], | ||
connection: [], | ||
}, | ||
@@ -640,2 +667,3 @@ numberOfRetry: 0, | ||
getConnectionState: machine.selectors.getConnectionState, | ||
getCurrentUser: machine.selectors.getCurrentUser, | ||
subscribe: machine.subscribe, | ||
@@ -642,0 +670,0 @@ unsubscribe: machine.unsubscribe, |
@@ -70,2 +70,3 @@ import { RecordData, Record, List } from "./doc"; | ||
}; | ||
declare type ConnectionState = "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting"; | ||
export declare type Connection = { | ||
@@ -76,2 +77,4 @@ state: "closed" | "authenticating" | "unavailable" | "failed"; | ||
id: number; | ||
userId?: string; | ||
userInfo?: any; | ||
}; | ||
@@ -94,3 +97,4 @@ export declare type OthersEvent<T extends Presence = Presence> = { | ||
disconnect(): void; | ||
getConnectionState(): Connection; | ||
getConnectionState(): ConnectionState; | ||
getCurrentUser<TPresence extends Presence = Presence>(): User<TPresence> | null; | ||
subscribe: { | ||
@@ -101,3 +105,4 @@ <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void; | ||
<T extends RecordData>(type: "storage", listener: StorageCallback<T>): void; | ||
(type: "error", listener: (error: Error) => void): void; | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -109,3 +114,4 @@ unsubscribe: { | ||
<T extends RecordData>(type: "storage", listener: StorageCallback<T>): void; | ||
(type: "error", listener: (error: Error) => void): void; | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -134,2 +140,3 @@ getPresence: <T extends Presence>() => T; | ||
export declare type ErrorCallback = (error: Error) => void; | ||
export declare type ConnectionCallback = (state: ConnectionState) => void; | ||
export declare type RoomEventCallbackMap = { | ||
@@ -141,2 +148,3 @@ storage: StorageCallback; | ||
error: ErrorCallback; | ||
connection: ConnectionCallback; | ||
}; | ||
@@ -154,2 +162,7 @@ export declare type CreateRecord = Room["createRecord"]; | ||
}; | ||
export declare type AuthenticationToken = { | ||
actor: number; | ||
id?: string; | ||
info?: any; | ||
}; | ||
export {}; |
@@ -1,5 +0,3 @@ | ||
import { AuthEndpoint } from "./types"; | ||
import { AuthEndpoint, AuthenticationToken } from "./types"; | ||
export default function auth(endpoint: AuthEndpoint, room: string): Promise<string>; | ||
export declare function parseToken(token: string): { | ||
actor: number; | ||
}; | ||
export declare function parseToken(token: string): AuthenticationToken; |
import { RecordData, List } from "."; | ||
import { Doc, Record } from "./doc"; | ||
import { Others, Presence, ClientOptions, Room, InitialStorageFactory, MyPresenceCallback, OthersEventCallback, StorageCallback, AuthEndpoint, LiveStorageState, LiveStorage, EventCallback, User, Connection, Serializable, ErrorCallback } from "./types"; | ||
import { Others, Presence, ClientOptions, Room, InitialStorageFactory, MyPresenceCallback, OthersEventCallback, StorageCallback, AuthEndpoint, LiveStorageState, LiveStorage, EventCallback, User, Connection, Serializable, ErrorCallback, AuthenticationToken, ConnectionCallback } from "./types"; | ||
import { ClientMessage, Op } from "./live"; | ||
@@ -29,2 +29,3 @@ declare type IdFactory = () => string; | ||
error: ErrorCallback[]; | ||
connection: ConnectionCallback[]; | ||
}; | ||
@@ -64,3 +65,3 @@ me: Presence; | ||
onMessage: (event: MessageEvent) => void; | ||
authenticationSuccess: (connectionId: number, socket: WebSocket) => void; | ||
authenticationSuccess: (token: AuthenticationToken, socket: WebSocket) => void; | ||
heartbeat: () => void; | ||
@@ -77,2 +78,3 @@ onNavigatorOnline: () => void; | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -85,2 +87,3 @@ unsubscribe: { | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -98,3 +101,4 @@ updatePresence: <T_6 extends Serializable>(overrides: Partial<T_6>) => void; | ||
selectors: { | ||
getConnectionState: () => Connection; | ||
getConnectionState: () => "failed" | "closed" | "open" | "connecting" | "authenticating" | "unavailable"; | ||
getCurrentUser: <TPresence extends Serializable = Serializable>() => User<TPresence> | null; | ||
getPresence: <T_14 extends Serializable>() => T_14; | ||
@@ -101,0 +105,0 @@ getOthers: <T_15 extends Serializable>() => Others<T_15>; |
@@ -25,3 +25,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
value === "event" || | ||
value === "error"); | ||
value === "error" || | ||
value === "connection"); | ||
} | ||
@@ -56,3 +57,3 @@ function makeIdFactory(connectionId) { | ||
const token = yield auth(context.authEndpoint, context.room); | ||
const connectionId = parseToken(token).actor; | ||
const parsedToken = parseToken(token); | ||
const socket = new WebSocket(`${context.liveblocksServer}/?token=${token}`); | ||
@@ -63,3 +64,3 @@ socket.addEventListener("message", onMessage); | ||
socket.addEventListener("error", onError); | ||
authenticationSuccess(connectionId, socket); | ||
authenticationSuccess(parsedToken, socket); | ||
} | ||
@@ -104,4 +105,15 @@ catch (er) { | ||
function getConnectionState() { | ||
return state.connection; | ||
return state.connection.state; | ||
} | ||
function getCurrentUser() { | ||
return state.connection.state === "open" || | ||
state.connection.state === "connecting" | ||
? { | ||
connectionId: state.connection.id, | ||
id: state.connection.userId, | ||
info: state.connection.userInfo, | ||
presence: getPresence(), | ||
} | ||
: null; | ||
} | ||
function connect() { | ||
@@ -134,5 +146,10 @@ if (typeof window === "undefined") { | ||
} | ||
function authenticationSuccess(connectionId, socket) { | ||
updateConnection({ state: "connecting", id: connectionId }); | ||
state.idFactory = makeIdFactory(connectionId); | ||
function authenticationSuccess(token, socket) { | ||
updateConnection({ | ||
state: "connecting", | ||
id: token.actor, | ||
userInfo: token.info, | ||
userId: token.id, | ||
}); | ||
state.idFactory = makeIdFactory(token.actor); | ||
state.socket = socket; | ||
@@ -305,2 +322,5 @@ } | ||
state.connection = connection; | ||
for (const listener of state.listeners.connection) { | ||
listener(connection.state); | ||
} | ||
} | ||
@@ -316,5 +336,10 @@ function getRetryDelay() { | ||
state.intervalHandles.heartbeat = effects.startHeartbeatInterval(); | ||
updateConnection({ state: "open", id: state.connection.id }); | ||
state.numberOfRetry = 0; | ||
tryFlushing(); | ||
if (state.connection.state === "connecting") { | ||
updateConnection(Object.assign(Object.assign({}, state.connection), { state: "open" })); | ||
state.numberOfRetry = 0; | ||
tryFlushing(); | ||
} | ||
else { | ||
// TODO | ||
} | ||
} | ||
@@ -553,2 +578,3 @@ function heartbeat() { | ||
getConnectionState, | ||
getCurrentUser, | ||
// Presence | ||
@@ -572,2 +598,3 @@ getPresence, | ||
error: [], | ||
connection: [], | ||
}, | ||
@@ -616,2 +643,3 @@ numberOfRetry: 0, | ||
getConnectionState: machine.selectors.getConnectionState, | ||
getCurrentUser: machine.selectors.getCurrentUser, | ||
subscribe: machine.subscribe, | ||
@@ -618,0 +646,0 @@ unsubscribe: machine.unsubscribe, |
@@ -70,2 +70,3 @@ import { RecordData, Record, List } from "./doc"; | ||
}; | ||
declare type ConnectionState = "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting"; | ||
export declare type Connection = { | ||
@@ -76,2 +77,4 @@ state: "closed" | "authenticating" | "unavailable" | "failed"; | ||
id: number; | ||
userId?: string; | ||
userInfo?: any; | ||
}; | ||
@@ -94,3 +97,4 @@ export declare type OthersEvent<T extends Presence = Presence> = { | ||
disconnect(): void; | ||
getConnectionState(): Connection; | ||
getConnectionState(): ConnectionState; | ||
getCurrentUser<TPresence extends Presence = Presence>(): User<TPresence> | null; | ||
subscribe: { | ||
@@ -101,3 +105,4 @@ <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void; | ||
<T extends RecordData>(type: "storage", listener: StorageCallback<T>): void; | ||
(type: "error", listener: (error: Error) => void): void; | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -109,3 +114,4 @@ unsubscribe: { | ||
<T extends RecordData>(type: "storage", listener: StorageCallback<T>): void; | ||
(type: "error", listener: (error: Error) => void): void; | ||
(type: "error", listener: ErrorCallback): void; | ||
(type: "connection", listener: ConnectionCallback): void; | ||
}; | ||
@@ -134,2 +140,3 @@ getPresence: <T extends Presence>() => T; | ||
export declare type ErrorCallback = (error: Error) => void; | ||
export declare type ConnectionCallback = (state: ConnectionState) => void; | ||
export declare type RoomEventCallbackMap = { | ||
@@ -141,2 +148,3 @@ storage: StorageCallback; | ||
error: ErrorCallback; | ||
connection: ConnectionCallback; | ||
}; | ||
@@ -154,2 +162,7 @@ export declare type CreateRecord = Room["createRecord"]; | ||
}; | ||
export declare type AuthenticationToken = { | ||
actor: number; | ||
id?: string; | ||
info?: any; | ||
}; | ||
export {}; |
{ | ||
"name": "@liveblocks/client", | ||
"version": "0.7.3", | ||
"version": "0.8.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/cjs/index.js", |
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
209233
38
3813