ssb-conn-db
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -23,7 +23,7 @@ import { AddressData, Opts } from './types'; | ||
update(address: string, x: AddressData | Updater): ConnDB; | ||
get(address: string): AddressData; | ||
get(address: string): AddressData | undefined; | ||
getAddressForId(id: string): string | undefined; | ||
has(address: string): boolean; | ||
delete(address: string): boolean; | ||
entries(): IterableIterator<[string, any]>; | ||
entries(): IterableIterator<[string, AddressData]>; | ||
listen(): any; | ||
@@ -30,0 +30,0 @@ loaded(): Promise<true>; |
@@ -187,7 +187,9 @@ "use strict"; | ||
var existed = this._map.has(address); | ||
this._map.set(address, data); | ||
if (existed) { | ||
var birth = this._map.get(address).birth; | ||
this._map.set(address, __assign({ birth: birth || Date.now() }, data)); | ||
this._notify({ type: 'update', address: address }); | ||
} | ||
else { | ||
this._map.set(address, __assign({ birth: Date.now() }, data)); | ||
this._notify({ type: 'insert', address: address }); | ||
@@ -211,7 +213,7 @@ } | ||
var previous = this._map.get(address); | ||
this._map.set(address, __assign({}, previous, data)); | ||
this._map.set(address, __assign({ birth: previous.birth || Date.now() }, previous, data)); | ||
this._notify({ type: 'update', address: address }); | ||
} | ||
else { | ||
this._map.set(address, data); | ||
this._map.set(address, __assign({ birth: Date.now() }, data)); | ||
this._notify({ type: 'insert', address: address }); | ||
@@ -237,3 +239,3 @@ } | ||
var next = typeof x === 'function' ? x(previous) : x; | ||
this._map.set(address, __assign({}, previous, next)); | ||
this._map.set(address, __assign({ birth: previous.birth || Date.now() }, previous, next)); | ||
this._notify({ type: 'update', address: address }); | ||
@@ -240,0 +242,0 @@ this._scheduleWrite(); |
@@ -5,3 +5,22 @@ export declare type Opts = { | ||
}; | ||
export declare type AddressData = any; | ||
export declare type Statistics = { | ||
mean: number; | ||
stdev: number; | ||
count: number; | ||
sum: number; | ||
sqsum: number; | ||
}; | ||
export declare type AddressData = { | ||
birth?: number; | ||
key?: string; | ||
source?: string; | ||
failure?: number; | ||
stateChange?: number; | ||
duration?: Statistics; | ||
ping?: { | ||
rtt: Statistics; | ||
skew: Statistics; | ||
}; | ||
[name: string]: any; | ||
}; | ||
export declare type ListenEvent = { | ||
@@ -8,0 +27,0 @@ type: 'insert' | 'update' | 'delete'; |
{ | ||
"name": "ssb-conn-db", | ||
"description": "Module that manages a local registry of connectable peers", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/staltz/ssb-conn-db", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -49,2 +49,4 @@ import * as fs from 'fs'; | ||
//#region INTERNAL | ||
private _init(modernPath: string, legacyPath: string) { | ||
@@ -139,6 +141,6 @@ const modernExists = fs.existsSync(modernPath); | ||
/////////////// | ||
//// PUBLIC API | ||
/////////////// | ||
//#endregion | ||
//#region PUBLIC API | ||
public replace(address: string, data: AddressData): ConnDB { | ||
@@ -156,6 +158,8 @@ if (this._closed) { | ||
const existed = this._map.has(address); | ||
this._map.set(address, data); | ||
if (existed) { | ||
const {birth} = this._map.get(address)!; | ||
this._map.set(address, {birth: birth || Date.now(), ...data}); | ||
this._notify({type: 'update', address} as ListenEvent); | ||
} else { | ||
this._map.set(address, {birth: Date.now(), ...data}); | ||
this._notify({type: 'insert', address} as ListenEvent); | ||
@@ -180,7 +184,11 @@ } | ||
if (existed) { | ||
const previous = this._map.get(address); | ||
this._map.set(address, {...previous, ...data}); | ||
const previous = this._map.get(address)!; | ||
this._map.set(address, { | ||
birth: previous.birth || Date.now(), | ||
...previous, | ||
...data, | ||
}); | ||
this._notify({type: 'update', address} as ListenEvent); | ||
} else { | ||
this._map.set(address, data); | ||
this._map.set(address, {birth: Date.now(), ...data}); | ||
this._notify({type: 'insert', address} as ListenEvent); | ||
@@ -206,5 +214,9 @@ } | ||
const previous = this._map.get(address); | ||
const previous = this._map.get(address)!; | ||
const next = typeof x === 'function' ? x(previous) : x; | ||
this._map.set(address, {...previous, ...next}); | ||
this._map.set(address, { | ||
birth: previous.birth || Date.now(), | ||
...previous, | ||
...next, | ||
}); | ||
this._notify({type: 'update', address} as ListenEvent); | ||
@@ -215,3 +227,3 @@ this._scheduleWrite(); | ||
public get(address: string): AddressData { | ||
public get(address: string): AddressData | undefined { | ||
if (this._closed) { | ||
@@ -283,4 +295,6 @@ throw new Error('This ConnDB instance is closed, create a new one.'); | ||
} | ||
//#endregion | ||
} | ||
export = ConnDB; |
@@ -6,4 +6,25 @@ export type Opts = { | ||
export type AddressData = any; | ||
export type Statistics = { | ||
mean: number; | ||
stdev: number; | ||
count: number; | ||
sum: number; | ||
sqsum: number; | ||
}; | ||
export type AddressData = { | ||
birth?: number; | ||
key?: string; | ||
source?: string; | ||
failure?: number; | ||
stateChange?: number; | ||
duration?: Statistics; | ||
ping?: { | ||
rtt: Statistics; | ||
skew: Statistics; | ||
}; | ||
[name: string]: any; | ||
}; | ||
export type ListenEvent = { | ||
@@ -10,0 +31,0 @@ type: 'insert' | 'update' | 'delete'; |
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
164994
1434