@syncsphere/client
v1.0.1
Published
Client-side TypeScript library for SyncSphere real-time state synchronization
Maintainers
Readme
SyncSphere Client Library
The SyncSphere client library provides a simple API for connecting to a SyncSphere server and managing shared state.
Installation
npm install @syncsphere/clientBasic Usage
import { SyncSphere, Action } from '@syncsphere/client';
// Create a new SyncSphere instance
const syncSphere = new SyncSphere('ws://localhost:8080');
// Define an action class
class MyAction extends Action {
constructor(data: any, options?: any) {
super(data, options);
}
}
// Register a scope with an action
const scopeStatus = syncSphere.registerScope('myScope', MyAction);
// Listen to scope loading status
scopeStatus.subscribe(isLoaded => {
console.log('Scope loaded:', isLoaded);
});
// Set user information (optional)
syncSphere.setUser({
id: 'user123',
name: 'John Doe'
});
// Disconnect when done
syncSphere.disconnect();API Reference
SyncSphere
The main class for interacting with the SyncSphere server.
class SyncSphere {
constructor(url: string, token?: string);
// Register a scope with an action
registerScope(scope: string, action: ActionClass): SyncSphereBehaviorSubject<ScopeLoadingStatus>;
// Set user information
setUser(user: IBasicActionUser, options?: ISetUserOptions): void;
// Reset user information
resetUser(): void;
// Reset a specific scope
resetScope(scope: string): void;
// Register a custom scope handler
registerCustomScopeHandler(scope: string, callback: (data: any) => void): void;
// Register a message interceptor
registerInterceptor(interceptor: IMessageInterceptor): void;
// Disconnect from the server
disconnect(): void;
// Get connection status
get statuses(): SyncSphereBehaviorSubject<ConnectorStatuses>;
}Action
Base class for creating custom actions.
class Action<T> {
constructor(data: T, options?: any);
// Action data
data: T;
// Action options
options?: any;
}Types
// Connection status
enum ConnectorStatus {
CONNECTING = 'CONNECTING',
CONNECTED = 'CONNECTED',
DISCONNECTED = 'DISCONNECTED'
}
// Scope loading status
enum ScopeLoadingStatus {
LOADING = 'LOADING',
LOADED = 'LOADED',
ERROR = 'ERROR'
}
// Message interceptor
interface IMessageInterceptor {
interceptMessage?(message: any): any;
interceptResponse?(response: any): any;
}Examples
See the demos directory for complete examples of client usage.
License
ISC
