@graphql-live/client
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -11,11 +11,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { io } from "socket.io-client"; | ||
import { applyPatch } from "fast-json-patch"; | ||
import { applyPatch, deepClone } from "fast-json-patch"; | ||
export function createClient({ url, context, socketOptions } = {}) { | ||
const socket = url ? io(url, socketOptions) : io(socketOptions); | ||
let isOffline = false; | ||
let disconnected = false; | ||
let currentId = 0; | ||
// This stores all ongoing operations waiting for results (possibly result streams) : | ||
const operations = new Map(); | ||
const onConnect = () => { | ||
if (isOffline) { | ||
isOffline = false; | ||
if (disconnected) { | ||
disconnected = false; | ||
// If this was a reconnection, re-execute all active operations, to be up-to-date : | ||
for (const record of operations.values()) | ||
@@ -26,3 +28,3 @@ record.execute(); | ||
const onDisconnect = () => { | ||
isOffline = true; | ||
disconnected = true; | ||
}; | ||
@@ -33,5 +35,5 @@ const onOperationResult = ({ id, patch, isFinal }) => { | ||
return; | ||
const previousResult = record.latestResult; | ||
record.latestResult = applyPatch(previousResult, patch).newDocument; | ||
console.log("Patched", previousResult, "with", patch); | ||
// When a patch is sent by the server, apply it to the latest known result and send | ||
// the updated result to the observer : | ||
record.latestResult = applyPatch(deepClone(record.latestResult), patch).newDocument; | ||
record.observer.next(record.latestResult); | ||
@@ -52,10 +54,8 @@ if (isFinal) { | ||
}; | ||
setTimeout(() => { | ||
socket.disconnect(); | ||
setTimeout(() => { | ||
socket.connect(); | ||
}, 6000); | ||
}, 15000); | ||
const execute = (operation, observer) => { | ||
// For each new operation, a unique id is created : | ||
const id = currentId++; | ||
// Also, a record is added to the operation store, keeping track of the observer interested | ||
// in results, the latest known result, and a function to execute the operation (might be | ||
// used for re-executions on reconnections) : | ||
const record = { | ||
@@ -66,2 +66,3 @@ observer, | ||
return __awaiter(this, void 0, void 0, function* () { | ||
record.latestResult = {}; | ||
const payload = { | ||
@@ -68,0 +69,0 @@ id, |
{ | ||
"name": "@graphql-live/client", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "The client part of the GraphQLive package", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { io, ManagerOptions, SocketOptions } from "socket.io-client"; | ||
import { ExecutionResult } from "graphql"; | ||
import { applyPatch, Operation as PatchOperation } from "fast-json-patch"; | ||
import { | ||
applyPatch, | ||
deepClone, | ||
Operation as PatchOperation | ||
} from "fast-json-patch"; | ||
@@ -47,9 +51,12 @@ export type ClientOptions = { | ||
const socket = url ? io(url, socketOptions) : io(socketOptions); | ||
let isOffline = false; | ||
let disconnected = false; | ||
let currentId = 0; | ||
// This stores all ongoing operations waiting for results (possibly result streams) : | ||
const operations = new Map<number, OperationRecord>(); | ||
const onConnect = () => { | ||
if (isOffline) { | ||
isOffline = false; | ||
if (disconnected) { | ||
disconnected = false; | ||
// If this was a reconnection, re-execute all active operations, to be up-to-date : | ||
for (const record of operations.values()) record.execute(); | ||
@@ -60,3 +67,3 @@ } | ||
const onDisconnect = () => { | ||
isOffline = true; | ||
disconnected = true; | ||
}; | ||
@@ -67,5 +74,8 @@ | ||
if (!record) return; | ||
const previousResult = record.latestResult; | ||
record.latestResult = applyPatch(previousResult, patch).newDocument; | ||
console.log("Patched", previousResult, "with", patch); | ||
// When a patch is sent by the server, apply it to the latest known result and send | ||
// the updated result to the observer : | ||
record.latestResult = applyPatch( | ||
deepClone(record.latestResult), | ||
patch | ||
).newDocument; | ||
record.observer.next(record.latestResult); | ||
@@ -89,11 +99,8 @@ if (isFinal) { | ||
setTimeout(() => { | ||
socket.disconnect(); | ||
setTimeout(() => { | ||
socket.connect(); | ||
}, 6000); | ||
}, 15000); | ||
const execute = (operation: Operation, observer: ResultObserver) => { | ||
// For each new operation, a unique id is created : | ||
const id = currentId++; | ||
// Also, a record is added to the operation store, keeping track of the observer interested | ||
// in results, the latest known result, and a function to execute the operation (might be | ||
// used for re-executions on reconnections) : | ||
const record: OperationRecord = { | ||
@@ -103,2 +110,3 @@ observer, | ||
async execute() { | ||
record.latestResult = {}; | ||
const payload: OperationPayload = { | ||
@@ -105,0 +113,0 @@ id, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
193640
297