Skip to content

Commit

Permalink
ESM updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
wehriam committed Feb 2, 2022
1 parent a4fb5a1 commit bdcf2bd
Show file tree
Hide file tree
Showing 35 changed files with 1,134 additions and 286 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ node_modules

# Users Environment Variables
.lock-wscript
go-ipfs
go-ipfs
.DS_Store
76 changes: 39 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,34 @@ bob.get('a'); // 1

#### Table of Contents

- [constructor](#constructor)
- [Parameters](#parameters)
- [ipfsSync](#ipfssync)
- [dump](#dump)
- [getIpfsHash](#getipfshash)
- [ipfsPeerCount](#ipfspeercount)
- [shutdown](#shutdown)
- [IpfsObservedRemoveSet#readyPromise](#ipfsobservedremovesetreadypromise)
* [constructor](#constructor)
* [Parameters](#parameters)
* [ipfsSync](#ipfssync)
* [dump](#dump)
* [getIpfsHash](#getipfshash)
* [ipfsPeerCount](#ipfspeercount)
* [shutdown](#shutdown)
* [IpfsObservedRemoveSet#readyPromise](#ipfsobservedremovesetreadypromise)

### constructor

Create an observed-remove CRDT.

#### Parameters

- `ipfs` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Object implementing the [core IPFS API](https://github.com/ipfs/interface-ipfs-core#api), most likely a [js-ipfs](https://github.com/ipfs/js-ipfs) or [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) object.
- `topic` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** IPFS pubub topic to use in synchronizing the CRDT.
- `entries` **Iterable<V>** Iterable of initial values (optional, default `[]`)
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
- `options.maxAge` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Max age of insertion/deletion identifiers (optional, default `5000`)
- `options.bufferPublishing` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Interval by which to buffer 'publish' events (optional, default `20`)
* `ipfs` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Object implementing the [core IPFS API](https://github.com/ipfs/interface-ipfs-core#api), most likely a [js-ipfs](https://github.com/ipfs/js-ipfs) or [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) object.
* `topic` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** IPFS pubub topic to use in synchronizing the CRDT.
* `entries` **Iterable\<V>** Iterable of initial values (optional, default `[]`)
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)

* `options.maxAge` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Max age of insertion/deletion identifiers (optional, default `5000`)
* `options.bufferPublishing` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Interval by which to buffer 'publish' events (optional, default `20`)

### ipfsSync

Publish an IPFS hash of an array containing all of the object's insertions and deletions.

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>**
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<any>>**

### dump

Expand All @@ -104,7 +105,7 @@ Return a sorted array containing all of the set's insertions and deletions.

Stores and returns an IPFS hash of the current insertions and deletions

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**

### ipfsPeerCount

Expand All @@ -122,42 +123,43 @@ Returns **void**

Resolves when IPFS topic subscriptions are confirmed.

Type: [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;void>
Type: [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<void>

## Map API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

- [constructor](#constructor)
- [Parameters](#parameters)
- [ipfsSync](#ipfssync)
- [dump](#dump)
- [getIpfsHash](#getipfshash)
- [ipfsPeerCount](#ipfspeercount)
- [shutdown](#shutdown)
- [IpfsObservedRemoveSet#readyPromise](#ipfsobservedremovesetreadypromise)
* [constructor](#constructor)
* [Parameters](#parameters)
* [ipfsSync](#ipfssync)
* [dump](#dump)
* [getIpfsHash](#getipfshash)
* [ipfsPeerCount](#ipfspeercount)
* [shutdown](#shutdown)
* [IpfsObservedRemoveSet#readyPromise](#ipfsobservedremovesetreadypromise)

### constructor

Create an observed-remove CRDT.

#### Parameters

- `ipfs` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Object implementing the [core IPFS API](https://github.com/ipfs/interface-ipfs-core#api), most likely a [js-ipfs](https://github.com/ipfs/js-ipfs) or [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) object.
- `topic` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** IPFS pubub topic to use in synchronizing the CRDT.
- `entries` **Iterable&lt;V>** Iterable of initial values (optional, default `[]`)
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
- `options.maxAge` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Max age of insertion/deletion identifiers (optional, default `5000`)
- `options.bufferPublishing` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Interval by which to buffer 'publish' events (optional, default `20`)
- `options.chunkPubSub` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Chunk pubsub messages for values greater than 1 MB (optional, default `false`)
* `ipfs` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Object implementing the [core IPFS API](https://github.com/ipfs/interface-ipfs-core#api), most likely a [js-ipfs](https://github.com/ipfs/js-ipfs) or [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) object.
* `topic` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** IPFS pubub topic to use in synchronizing the CRDT.
* `entries` **Iterable\<V>** Iterable of initial values (optional, default `[]`)
* `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)

* `options.maxAge` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Max age of insertion/deletion identifiers (optional, default `5000`)
* `options.bufferPublishing` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Interval by which to buffer 'publish' events (optional, default `20`)
* `options.chunkPubSub` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Chunk pubsub messages for values greater than 1 MB (optional, default `false`)

### ipfsSync

Publish an IPFS hash of an array containing all of the object's insertions and deletions.

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;any>>**
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<any>>**

### dump

Expand All @@ -167,22 +169,22 @@ Return a sorted array containing all of the set's insertions and deletions.

Stores and returns an IPFS hash of the current insertions and deletions

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**

### ipfsPeerCount

Current number of IPFS pubsub peers.

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**

### shutdown

Gracefully shutdown

Returns **void**
Returns **void**

### IpfsObservedRemoveSet#readyPromise

Resolves when IPFS topic subscriptions are confirmed.

Type: [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;void>
Type: [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<void>
75 changes: 62 additions & 13 deletions dist/cjs/map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 44 additions & 8 deletions dist/cjs/map.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,56 @@ export default class IpfsObservedRemoveMap<K, V> extends ObservedRemoveMap<K, V>
}
});
try {
const promises = [this.ipfs.pubsub.subscribe(this.topic, this.boundHandleQueueMessage, { discover: true, signal: this.abortController.signal })];
await this.ipfs.pubsub.subscribe(this.topic, this.boundHandleQueueMessage, { discover: true, signal: this.abortController.signal });
if (!this.disableSync) {
promises.push(this.ipfs.pubsub.subscribe(`${this.topic}:hash`, this.boundHandleHashMessage, { discover: true, signal: this.abortController.signal }));
await this.ipfs.pubsub.subscribe(`${this.topic}:hash`, this.boundHandleHashMessage, { discover: true, signal: this.abortController.signal });
this.waitForPeersThenSendHash();
}
await Promise.all(promises);
} catch (error) {
if (error.type !== 'aborted') {
throw error;
}
}
}

async waitForPeers():Promise<void> {
while (true) {
try {
const peerIds = await this.ipfs.pubsub.peers(this.topic, { timeout: 10000, signal: this.abortController.signal });
if (this.abortController.signal.aborted) {
return;
}
if (peerIds.length > 0) {
break;
}
} catch (error) {
if (error.name === 'TimeoutError') {
continue;
}
throw error;
}
}
if (this.disableSync) {
return;
}
while (true) {
try {
const peerIds = await this.ipfs.pubsub.peers(`${this.topic}:hash`, { timeout: 10000, signal: this.abortController.signal });
if (this.abortController.signal.aborted) {
return;
}
if (peerIds.length > 0) {
break;
}
} catch (error) {
if (error.name === 'TimeoutError') {
continue;
}
throw error;
}
}
}

async waitForPeersThenSendHash():Promise<void> {
if (!this.active) {
return;
Expand All @@ -184,7 +221,7 @@ export default class IpfsObservedRemoveMap<K, V> extends ObservedRemoveMap<K, V>
this.debouncedIpfsSync();
} else {
await new Promise((resolve) => setTimeout(resolve, 10000));
setImmediate(() => {
queueMicrotask(() => {
this.waitForPeersThenSendHash();
});
}
Expand All @@ -194,7 +231,7 @@ export default class IpfsObservedRemoveMap<K, V> extends ObservedRemoveMap<K, V>
this.emit('error', error);
}
if (this.active && error.name === 'TimeoutError') {
setImmediate(() => {
queueMicrotask(() => {
this.waitForPeersThenSendHash();
});
}
Expand Down Expand Up @@ -277,11 +314,10 @@ export default class IpfsObservedRemoveMap<K, V> extends ObservedRemoveMap<K, V>
const timeout = setTimeout(() => {
unsubscribeAbortController.abort();
}, 5000);
const promises = [this.ipfs.pubsub.unsubscribe(this.topic, this.boundHandleQueueMessage, { signal: unsubscribeAbortController.signal })];
await this.ipfs.pubsub.unsubscribe(this.topic, this.boundHandleQueueMessage, { signal: unsubscribeAbortController.signal });
if (!this.disableSync) {
promises.push(this.ipfs.pubsub.unsubscribe(`${this.topic}:hash`, this.boundHandleHashMessage, { signal: unsubscribeAbortController.signal }));
await this.ipfs.pubsub.unsubscribe(`${this.topic}:hash`, this.boundHandleHashMessage, { signal: unsubscribeAbortController.signal });
}
await Promise.all(promises);
clearTimeout(timeout);
} catch (error) {
if (!notSubscribedRegex.test(error.message)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/map.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit bdcf2bd

Please sign in to comment.