@logux/client
Advanced tools
Comparing version 0.15.3 to 0.16.0
@@ -30,2 +30,19 @@ import { createNanoEvents } from 'nanoevents' | ||
function isEqual(obj1, obj2) { | ||
if (!obj1 && !obj2) return true | ||
return JSON.stringify(obj1) === JSON.stringify(obj2) | ||
} | ||
function unsubscribeChannel(client, unsubscribe) { | ||
let json = JSON.stringify({ ...unsubscribe, type: 'logux/subscribe' }) | ||
let subscribers = client.subscriptions[json] | ||
if (subscribers) { | ||
if (subscribers === 1) { | ||
delete client.subscriptions[json] | ||
} else { | ||
client.subscriptions[json] = subscribers - 1 | ||
} | ||
} | ||
} | ||
export class Client { | ||
@@ -92,2 +109,7 @@ constructor(opts = {}) { | ||
this.last = {} | ||
this.subscriptions = {} | ||
let subscribing = {} | ||
let unsubscribing = {} | ||
log.on('preadd', (action, meta) => { | ||
@@ -97,14 +119,37 @@ if (parseId(meta.id).nodeId === this.nodeId && !meta.subprotocol) { | ||
} | ||
if (action.type === 'logux/unsubscribe') { | ||
let wasSubscribed = true | ||
let processedOffline = false | ||
for (let id in subscribing) { | ||
let subscribe = subscribing[id] | ||
if (subscribe.channel === action.channel) { | ||
if (isEqual(action.filer, subscribe.filter)) { | ||
wasSubscribed = false | ||
delete subscribing[id] | ||
log.changeMeta(id, { reasons: [] }) | ||
break | ||
} | ||
} | ||
} | ||
if (wasSubscribed && this.state === 'disconnected') { | ||
processedOffline = true | ||
unsubscribeChannel(this, action) | ||
} | ||
if (wasSubscribed && !processedOffline) { | ||
meta.sync = true | ||
} else { | ||
delete meta.sync | ||
} | ||
} | ||
if (meta.sync && !meta.resubscribe) meta.reasons.push('syncing') | ||
}) | ||
this.last = {} | ||
this.subscriptions = {} | ||
let subscribing = {} | ||
let unsubscribing = {} | ||
this.emitter = createNanoEvents() | ||
this.on('add', (action, meta) => { | ||
let type = action.type | ||
let json, last | ||
let last | ||
if (type === 'logux/processed' || type === 'logux/undo') { | ||
@@ -116,20 +161,10 @@ this.log.removeReason('syncing', { id: action.id }) | ||
} else if (type === 'logux/unsubscribe') { | ||
unsubscribing[meta.id] = action | ||
if (meta.sync) unsubscribing[meta.id] = action | ||
} else if (type === 'logux/processed') { | ||
if (unsubscribing[action.id]) { | ||
let unsubscription = unsubscribing[action.id] | ||
json = JSON.stringify({ ...unsubscription, type: 'logux/subscribe' }) | ||
let subscribers = this.subscriptions[json] | ||
if (subscribers) { | ||
if (subscribers === 1) { | ||
delete this.subscriptions[json] | ||
} else { | ||
this.subscriptions[json] = subscribers - 1 | ||
} | ||
} | ||
} | ||
if (subscribing[action.id]) { | ||
unsubscribeChannel(this, unsubscribing[action.id]) | ||
} else if (subscribing[action.id]) { | ||
let subscription = subscribing[action.id] | ||
delete subscribing[action.id] | ||
json = JSON.stringify(subscription) | ||
let json = JSON.stringify(subscription) | ||
if (this.subscriptions[json]) { | ||
@@ -145,3 +180,3 @@ this.subscriptions[json] += 1 | ||
} | ||
if (type === 'logux/processed' && this.processing[action.id]) { | ||
if (this.processing[action.id]) { | ||
this.processing[action.id][1](meta) | ||
@@ -167,9 +202,2 @@ delete this.processing[action.id] | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (type === 'logux/subscribe' || type === 'logux/unsubscribe') { | ||
if (!meta.sync) { | ||
console.error(type + ' action without meta.sync') | ||
} | ||
} | ||
} | ||
}) | ||
@@ -265,3 +293,3 @@ | ||
let state = this.node.state | ||
if (state === 'synchronized' || state === 'sending') { | ||
if (state === 'synchronized') { | ||
if (disconnected) { | ||
@@ -268,0 +296,0 @@ disconnected = false |
@@ -7,2 +7,3 @@ import { isFirstOlder } from '@logux/core' | ||
request.onerror = e => { | ||
/* istanbul ignore next */ | ||
reject(e.target.error) | ||
@@ -9,0 +10,0 @@ } |
{ | ||
"name": "@logux/client", | ||
"version": "0.15.3", | ||
"version": "0.16.0", | ||
"description": "Logux base components to build web client", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"engines": { | ||
"node": "^12.0.0 || ^14.0.0 || >=16.0.0" | ||
"node": "^14.0.0 || ^16.0.0 || >=18.0.0" | ||
}, | ||
@@ -69,5 +69,5 @@ "peerDependencies": { | ||
"nanodelay": "^2.0.2", | ||
"nanoevents": "^6.0.2", | ||
"nanoid": "^3.3.3" | ||
"nanoevents": "^7.0.1", | ||
"nanoid": "^4.0.0" | ||
} | ||
} |
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
148386
4948
23
+ Addednanoevents@7.0.1(transitive)
+ Addednanoid@4.0.2(transitive)
Updatednanoevents@^7.0.1
Updatednanoid@^4.0.0