@peerbit/shared-log 2.0.1 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,127 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import B from "benchmark";
11
+ import { deserialize, field, option, serialize, variant } from "@dao-xyz/borsh";
12
+ import { LSession } from "@peerbit/test-utils";
13
+ import { v4 as uuid } from "uuid";
14
+ import crypto from "crypto";
15
+ import { Program } from "@peerbit/program";
16
+ import { Replicator, SharedLog } from "../index.js";
17
+ // Run with "node --loader ts-node/esm ./src/__benchmark__/index.ts"
18
+ let Document = class Document {
19
+ id;
20
+ name;
21
+ number;
22
+ bytes;
23
+ constructor(opts) {
24
+ if (opts) {
25
+ this.id = opts.id;
26
+ this.name = opts.name;
27
+ this.number = opts.number;
28
+ this.bytes = opts.bytes;
29
+ }
30
+ }
31
+ };
32
+ __decorate([
33
+ field({ type: "string" }),
34
+ __metadata("design:type", String)
35
+ ], Document.prototype, "id", void 0);
36
+ __decorate([
37
+ field({ type: option("string") }),
38
+ __metadata("design:type", String)
39
+ ], Document.prototype, "name", void 0);
40
+ __decorate([
41
+ field({ type: option("u64") }),
42
+ __metadata("design:type", BigInt)
43
+ ], Document.prototype, "number", void 0);
44
+ __decorate([
45
+ field({ type: Uint8Array }),
46
+ __metadata("design:type", Uint8Array)
47
+ ], Document.prototype, "bytes", void 0);
48
+ Document = __decorate([
49
+ variant("document"),
50
+ __metadata("design:paramtypes", [Document])
51
+ ], Document);
52
+ let TestStore = class TestStore extends Program {
53
+ logs;
54
+ constructor(properties) {
55
+ super();
56
+ this.logs = properties?.logs || new SharedLog();
57
+ }
58
+ async open(options) {
59
+ await this.logs.open({
60
+ ...options,
61
+ encoding: {
62
+ decoder: (bytes) => deserialize(bytes, Document),
63
+ encoder: (data) => serialize(data),
64
+ },
65
+ });
66
+ }
67
+ };
68
+ __decorate([
69
+ field({ type: SharedLog }),
70
+ __metadata("design:type", SharedLog)
71
+ ], TestStore.prototype, "logs", void 0);
72
+ TestStore = __decorate([
73
+ variant("test_shared_log"),
74
+ __metadata("design:paramtypes", [Object])
75
+ ], TestStore);
76
+ const peersCount = 1;
77
+ const session = await LSession.connected(peersCount);
78
+ const store = new TestStore({
79
+ logs: new SharedLog({
80
+ id: new Uint8Array(32),
81
+ }),
82
+ });
83
+ const client = session.peers[0];
84
+ await client.open(store, {
85
+ args: {
86
+ role: new Replicator(),
87
+ trim: { type: "length", to: 100 },
88
+ onChange: (change) => {
89
+ change.added.forEach(async (entry) => {
90
+ const doc = await entry.getPayloadValue();
91
+ resolver.get(doc.id)();
92
+ resolver.delete(doc.id);
93
+ });
94
+ },
95
+ },
96
+ });
97
+ const resolver = new Map();
98
+ const suite = new B.Suite();
99
+ suite
100
+ .add("put", {
101
+ fn: async (deferred) => {
102
+ const doc = new Document({
103
+ id: uuid(),
104
+ name: "hello",
105
+ number: 1n,
106
+ bytes: crypto.randomBytes(1200),
107
+ });
108
+ resolver.set(doc.id, () => {
109
+ deferred.resolve();
110
+ });
111
+ await store.logs.append(doc, { meta: { next: [] } });
112
+ },
113
+ minSamples: 300,
114
+ defer: true,
115
+ })
116
+ .on("cycle", (event) => {
117
+ console.log(String(event.target));
118
+ })
119
+ .on("error", (err) => {
120
+ throw err;
121
+ })
122
+ .on("complete", async function (...args) {
123
+ await store.drop();
124
+ await session.stop();
125
+ })
126
+ .run();
127
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/__benchmark__/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,CAAC,MAAM,WAAW,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAQ,MAAM,aAAa,CAAC;AAE1D,oEAAoE;AAEpE,IACM,QAAQ,GADd,MACM,QAAQ;IAEb,EAAE,CAAS;IAGX,IAAI,CAAU;IAGd,MAAM,CAAU;IAGhB,KAAK,CAAa;IAElB,YAAY,IAAc;QACzB,IAAI,IAAI,EAAE;YACT,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;YAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YACtB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;YAC1B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;SACxB;IACF,CAAC;CACD,CAAA;AAnBA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;oCACf;AAGX;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;;sCACpB;AAGd;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,E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AE,CAAC,KAAU,EAAE,EAAE;IAC3B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;KACD,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;IACpB,MAAM,GAAG,CAAC;AACX,CAAC,CAAC;KACD,EAAE,CAAC,UAAU,EAAE,KAAK,WAAsB,GAAG,IAAW;IACxD,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;IACnB,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;AACtB,CAAC,CAAC;KACD,GAAG,EAAE,CAAC"}
@@ -1,14 +1,7 @@
1
1
  import { Entry } from "@peerbit/log";
2
2
  import { Log } from "@peerbit/log";
3
3
  import { TransportMessage } from "./message.js";
4
- export declare class MinReplicas {
5
- get value(): number;
6
- }
7
- export declare class AbsolutMinReplicas extends MinReplicas {
8
- _value: number;
9
- constructor(value: number);
10
- get value(): number;
11
- }
4
+ import { Cache } from "@peerbit/cache";
12
5
  /**
13
6
  * This thing allows use to faster sync since we can provide
14
7
  * references that can be read concurrently to
@@ -24,19 +17,29 @@ export declare class EntryWithRefs<T> {
24
17
  }
25
18
  export declare class ExchangeHeadsMessage<T> extends TransportMessage {
26
19
  heads: EntryWithRefs<T>[];
27
- minReplicas?: MinReplicas;
28
20
  reserved: Uint8Array;
29
21
  constructor(props: {
30
22
  logId: Uint8Array;
31
23
  heads: EntryWithRefs<T>[];
32
- minReplicas?: MinReplicas;
33
24
  });
34
25
  }
35
26
  export declare class RequestHeadsMessage extends TransportMessage {
36
27
  address: string;
37
28
  constructor(props: {
38
- topic: string;
39
29
  address: string;
40
30
  });
41
31
  }
42
- export declare const createExchangeHeadsMessage: (log: Log<any>, heads: Entry<any>[], includeReferences: boolean) => Promise<ExchangeHeadsMessage<any>>;
32
+ export declare class RequestIHave extends TransportMessage {
33
+ hashes: string[];
34
+ constructor(props: {
35
+ hashes: string[];
36
+ });
37
+ }
38
+ export declare class ResponseIHave extends TransportMessage {
39
+ hashes: string[];
40
+ constructor(props: {
41
+ hashes: string[];
42
+ });
43
+ }
44
+ export declare const createExchangeHeadsMessage: (log: Log<any>, heads: Entry<any>[], gidParentCache: Cache<Entry<any>[]>) => Promise<ExchangeHeadsMessage<any>>;
45
+ export declare const allEntriesWithUniqueGids: (log: Log<any>, entry: Entry<any>, gidParentCache: Cache<Entry<any>[]>) => Promise<Entry<any>[]>;
@@ -7,30 +7,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
- import { variant, option, field, vec, fixedArray } from "@dao-xyz/borsh";
11
- import { Entry } from "@peerbit/log";
10
+ import { variant, field, vec, fixedArray } from "@dao-xyz/borsh";
11
+ import { Entry, EntryType } from "@peerbit/log";
12
12
  import { logger as loggerFn } from "@peerbit/logger";
13
13
  import { TransportMessage } from "./message.js";
14
14
  const logger = loggerFn({ module: "exchange-heads" });
15
- export class MinReplicas {
16
- get value() {
17
- throw new Error("Not implemented");
18
- }
19
- }
20
- export let AbsolutMinReplicas = class AbsolutMinReplicas extends MinReplicas {
21
- _value;
22
- constructor(value) {
23
- super();
24
- this._value = value;
25
- }
26
- get value() {
27
- return this._value;
28
- }
29
- };
30
- AbsolutMinReplicas = __decorate([
31
- variant(0),
32
- __metadata("design:paramtypes", [Number])
33
- ], AbsolutMinReplicas);
34
15
  /**
35
16
  * This thing allows use to faster sync since we can provide
36
17
  * references that can be read concurrently to
@@ -58,22 +39,16 @@ EntryWithRefs = __decorate([
58
39
  ], EntryWithRefs);
59
40
  export let ExchangeHeadsMessage = class ExchangeHeadsMessage extends TransportMessage {
60
41
  heads;
61
- minReplicas;
62
42
  reserved = new Uint8Array(4);
63
43
  constructor(props) {
64
44
  super();
65
45
  this.heads = props.heads;
66
- this.minReplicas = props.minReplicas;
67
46
  }
68
47
  };
69
48
  __decorate([
70
49
  field({ type: vec(EntryWithRefs) }),
71
50
  __metadata("design:type", Array)
72
51
  ], ExchangeHeadsMessage.prototype, "heads", void 0);
73
- __decorate([
74
- field({ type: option(MinReplicas) }),
75
- __metadata("design:type", MinReplicas)
76
- ], ExchangeHeadsMessage.prototype, "minReplicas", void 0);
77
52
  __decorate([
78
53
  field({ type: fixedArray("u8", 4) }),
79
54
  __metadata("design:type", Uint8Array)
@@ -86,9 +61,8 @@ export let RequestHeadsMessage = class RequestHeadsMessage extends TransportMess
86
61
  address;
87
62
  constructor(props) {
88
63
  super();
89
- if (props) {
90
- this.address = props.address;
91
- }
64
+ this.address = props.address;
65
+ throw new Error("Unsupported"); // This message should not be used yet
92
66
  }
93
67
  };
94
68
  __decorate([
@@ -99,16 +73,41 @@ RequestHeadsMessage = __decorate([
99
73
  variant([0, 1]),
100
74
  __metadata("design:paramtypes", [Object])
101
75
  ], RequestHeadsMessage);
102
- export const createExchangeHeadsMessage = async (log, heads, includeReferences) => {
76
+ export let RequestIHave = class RequestIHave extends TransportMessage {
77
+ hashes;
78
+ constructor(props) {
79
+ super();
80
+ this.hashes = props.hashes;
81
+ }
82
+ };
83
+ __decorate([
84
+ field({ type: vec("string") }),
85
+ __metadata("design:type", Array)
86
+ ], RequestIHave.prototype, "hashes", void 0);
87
+ RequestIHave = __decorate([
88
+ variant([0, 2]),
89
+ __metadata("design:paramtypes", [Object])
90
+ ], RequestIHave);
91
+ export let ResponseIHave = class ResponseIHave extends TransportMessage {
92
+ hashes;
93
+ constructor(props) {
94
+ super();
95
+ this.hashes = props.hashes;
96
+ }
97
+ };
98
+ __decorate([
99
+ field({ type: vec("string") }),
100
+ __metadata("design:type", Array)
101
+ ], ResponseIHave.prototype, "hashes", void 0);
102
+ ResponseIHave = __decorate([
103
+ variant([0, 3]),
104
+ __metadata("design:paramtypes", [Object])
105
+ ], ResponseIHave);
106
+ export const createExchangeHeadsMessage = async (log, heads, gidParentCache) => {
103
107
  const headsSet = new Set(heads);
104
108
  const headsWithRefs = await Promise.all(heads.map(async (head) => {
105
- const refs = !includeReferences
106
- ? []
107
- : (await log.getReferenceSamples(head, {
108
- pointerCount: 8,
109
- memoryLimit: 1e6 / heads.length,
110
- })) // 1mb total limit split on all heads
111
- .filter((r) => !headsSet.has(r)); // pick a proportional amount of refs so we can efficiently load the log. TODO should be equidistant for good performance?
109
+ const refs = (await allEntriesWithUniqueGids(log, head, gidParentCache)) // 1mb total limit split on all heads
110
+ .filter((r) => !headsSet.has(r));
112
111
  return new EntryWithRefs({
113
112
  entry: head,
114
113
  references: refs,
@@ -120,4 +119,38 @@ export const createExchangeHeadsMessage = async (log, heads, includeReferences)
120
119
  heads: headsWithRefs,
121
120
  });
122
121
  };
122
+ export const allEntriesWithUniqueGids = async (log, entry, gidParentCache) => {
123
+ const cachedValue = gidParentCache.get(entry.hash);
124
+ if (cachedValue != null) {
125
+ return cachedValue;
126
+ }
127
+ // TODO optimize this
128
+ const map = new Map();
129
+ let curr = [entry];
130
+ while (curr.length > 0) {
131
+ const nexts = [];
132
+ for (const element of curr) {
133
+ if (!map.has(element.meta.gid)) {
134
+ map.set(element.meta.gid, element);
135
+ if (element.meta.type === EntryType.APPEND) {
136
+ for (const next of element.meta.next) {
137
+ const indexedEntry = log.entryIndex.getShallow(next);
138
+ if (!indexedEntry) {
139
+ logger.error("Failed to find indexed entry for hash: " + next);
140
+ }
141
+ else {
142
+ nexts.push(indexedEntry);
143
+ }
144
+ }
145
+ }
146
+ }
147
+ curr = nexts;
148
+ }
149
+ }
150
+ const value = [
151
+ ...(await Promise.all([...map.values()].map((x) => log.entryIndex.get(x.hash)))),
152
+ ].filter((x) => !!x);
153
+ gidParentCache.add(entry.hash, value);
154
+ return value;
155
+ };
123
156
  //# sourceMappingURL=exchange-heads.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"exchange-heads.js","sourceRoot":"","sources":["../../src/exchange-heads.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EA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AAM,EAAE;oBAC3C,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE;wBACrC,MAAM,YAAY,GAAG,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;wBACrD,IAAI,CAAC,YAAY,EAAE;4BAClB,MAAM,CAAC,KAAK,CAAC,yCAAyC,GAAG,IAAI,CAAC,CAAC;yBAC/D;6BAAM;4BACN,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;yBACzB;qBACD;iBACD;aACD;YACD,IAAI,GAAG,KAAK,CAAC;SACb;KACD;IACD,MAAM,KAAK,GAAG;QACb,GAAG,CAAC,MAAM,OAAO,CAAC,GAAG,CACpB,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CACxD,CAAC;KACF,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAiB,CAAC;IACrC,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACtC,OAAO,KAAK,CAAC;AACd,CAAC,CAAC"}
@@ -1,24 +1,38 @@
1
- import { QueryContext, RPC } from "@peerbit/rpc";
1
+ import { RequestContext, RPC } from "@peerbit/rpc";
2
2
  import { TransportMessage } from "./message.js";
3
3
  import { AppendOptions, Entry, Log, LogEvents, LogProperties } from "@peerbit/log";
4
4
  import { Program } from "@peerbit/program";
5
- import { MinReplicas } from "./exchange-heads.js";
5
+ import { PublicSignKey } from "@peerbit/crypto";
6
6
  import { SubscriptionEvent, UnsubcriptionEvent } from "@peerbit/pubsub-interface";
7
7
  import { Observer, Replicator, Role } from "./role.js";
8
+ import { AbsoluteReplicas, MinReplicas } from "./replication.js";
9
+ export * from "./replication.js";
8
10
  export { Observer, Replicator, Role };
9
11
  export declare const logger: import("pino").Logger<import("pino").LoggerOptions | import("pino").DestinationStream>;
10
12
  export type SyncFilter = (entries: Entry<any>) => Promise<boolean> | boolean;
13
+ type ReplicationLimits = {
14
+ min: MinReplicas;
15
+ max?: MinReplicas;
16
+ };
17
+ export type ReplicationLimitsOptions = Partial<ReplicationLimits> | {
18
+ min?: number;
19
+ max?: number;
20
+ };
11
21
  export interface SharedLogOptions {
12
- minReplicas?: number;
22
+ replicas?: ReplicationLimitsOptions;
13
23
  sync?: SyncFilter;
14
24
  role?: Role;
25
+ respondToIHaveTimeout?: number;
26
+ canReplicate?: (publicKey: PublicSignKey) => Promise<boolean> | boolean;
15
27
  }
16
28
  export declare const DEFAULT_MIN_REPLICAS = 2;
17
29
  export type Args<T> = LogProperties<T> & LogEvents<T> & SharedLogOptions;
30
+ export type SharedAppendOptions<T> = AppendOptions<T> & {
31
+ replicas?: AbsoluteReplicas | number;
32
+ };
18
33
  export declare class SharedLog<T = Uint8Array> extends Program<Args<T>> {
19
34
  log: Log<T>;
20
35
  rpc: RPC<TransportMessage, TransportMessage>;
21
- private _minReplicas;
22
36
  private _sync?;
23
37
  private _role;
24
38
  private _sortedPeersCache;
@@ -26,14 +40,21 @@ export declare class SharedLog<T = Uint8Array> extends Program<Args<T>> {
26
40
  private _gidPeersHistory;
27
41
  private _onSubscriptionFn;
28
42
  private _onUnsubscriptionFn;
43
+ private _canReplicate?;
29
44
  private _logProperties?;
45
+ private _loadedOnce;
46
+ private _gidParentCache;
47
+ private _respondToIHaveTimeout;
48
+ private _pendingDeletes;
49
+ private _pendingIHave;
50
+ replicas: ReplicationLimits;
30
51
  constructor(properties?: {
31
52
  id?: Uint8Array;
32
53
  });
33
- get minReplicas(): MinReplicas;
34
- set minReplicas(minReplicas: MinReplicas);
35
- get role(): Role;
36
- append(data: T, options?: AppendOptions<T> | undefined): Promise<{
54
+ get role(): Observer | Replicator;
55
+ updateRole(role: Observer | Replicator): Promise<void>;
56
+ private initializeWithRole;
57
+ append(data: T, options?: SharedAppendOptions<T> | undefined): Promise<{
37
58
  entry: Entry<T>;
38
59
  removed: Entry<T>[];
39
60
  }>;
@@ -42,33 +63,40 @@ export declare class SharedLog<T = Uint8Array> extends Program<Args<T>> {
42
63
  private _close;
43
64
  close(from?: Program): Promise<boolean>;
44
65
  drop(from?: Program): Promise<boolean>;
45
- _onMessage(msg: TransportMessage, context: QueryContext): Promise<TransportMessage | undefined>;
66
+ _onMessage(msg: TransportMessage, context: RequestContext): Promise<TransportMessage | undefined>;
46
67
  getReplicatorsSorted(): {
47
68
  hash: string;
48
69
  timestamp: number;
49
70
  }[] | undefined;
50
71
  isLeader(slot: {
51
72
  toString(): string;
52
- }, numberOfLeaders?: number): Promise<boolean>;
73
+ }, numberOfLeaders: number): Promise<boolean>;
53
74
  findLeaders(subject: {
54
75
  toString(): string;
55
- }, numberOfLeaders?: number): Promise<string[]>;
76
+ }, numberOfLeadersUnbounded: number): Promise<string[]>;
56
77
  private modifySortedSubscriptionCache;
57
- handleSubscriptionChange(fromHash: string, changes: {
78
+ handleSubscriptionChange(publicKey: PublicSignKey, changes: {
58
79
  topic: string;
59
80
  data?: Uint8Array;
60
81
  }[], subscribed: boolean): Promise<void>;
82
+ pruneSafely(entries: Entry<any>[], options?: {
83
+ timeout: number;
84
+ }): Promise<any>[];
61
85
  /**
62
86
  * When a peers join the networkk and want to participate the leaders for particular log subgraphs might change, hence some might start replicating, might some stop
63
87
  * This method will go through my owned entries, and see whether I should share them with a new leader, and/or I should stop care about specific entries
64
88
  * @param channel
65
89
  */
66
90
  replicationReorganization(): Promise<boolean>;
67
- replicators(): {
91
+ /**
92
+ *
93
+ * @returns groups where at least one in any group will have the entry you are looking for
94
+ */
95
+ getDiscoveryGroups(): {
68
96
  hash: string;
69
97
  timestamp: number;
70
98
  }[][];
71
- replicator(gid: any): Promise<boolean>;
99
+ replicator(entry: Entry<any>): Promise<boolean>;
72
100
  _onUnsubscription(evt: CustomEvent<UnsubcriptionEvent>): Promise<void>;
73
101
  _onSubscription(evt: CustomEvent<SubscriptionEvent>): Promise<void>;
74
102
  }