Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit f74afb3

Browse files
authored
Merge pull request #89 from synonymdev/core-data
Core data
2 parents 6d27273 + 6079285 commit f74afb3

18 files changed

Lines changed: 1959 additions & 10997 deletions

File tree

declarations.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ declare module 'hyperswarm' {
1919

2020
class Server extends EventEmitter {
2121
listen: (keyPair: KeyPair) => Promise<void>;
22+
close: () => Promise<void>;
2223
address: () => {
2324
publicKey: Uint8Array;
2425
host: string;
@@ -60,7 +61,7 @@ declare module 'hyperswarm' {
6061
status(topic: Uint8Array): Discovery | undefined;
6162
topics(): IterableIterator<Discovery>;
6263
listen(): Promise<undefined>;
63-
destroy(): Promise<undefined>;
64+
destroy(): Promise<void>;
6465
joinPeer(key: Uint8Array): undefined;
6566
leave(topic: Uint8Array): Promise<any>;
6667
join(
@@ -128,7 +129,7 @@ declare module 'corestore' {
128129
keyPair?: KeyPair,
129130
encryptionKey?: Uint8Array | undefined
130131
}>
131-
_closing: null | Promise<any>;
132+
closing: null | Promise<any>;
132133
_preready: (core: Hypercore) => Promise<void>
133134
storage: any;
134135

examples/drives/watch/server.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import b4a from 'b4a';
22
import logUpdate from 'log-update';
3-
import SDK from '@synonymdev/slashtags-sdk';
3+
import SDK, { SlashURL } from '@synonymdev/slashtags-sdk';
44
import RAM from 'random-access-memory'
55

66
console.log('Setting up slashtag...');
@@ -10,11 +10,17 @@ const sdk = new SDK({
1010
});
1111

1212
const alice = sdk.slashtag('alice');
13-
const drive = alice.drivestore.get()
14-
await drive.ready()
15-
console.log('Serving feed...');
13+
await alice.ready()
14+
await alice.coreData.create('/trades-feed/latest-trade', b4a.from(''));
1615

17-
setInterval(() => {
16+
const FILE_PATH = '/trades-feed/latest-trade'
17+
18+
console.log(`Serving feed ${await alice.coreData.createURL(FILE_PATH)}\n`);
19+
20+
await write()
21+
setInterval(write, 500);
22+
23+
function write() {
1824
const trade = {
1925
amount: Math.random(),
2026
price: Math.ceil(60000 + Math.random() * 60000),
@@ -23,5 +29,5 @@ setInterval(() => {
2329
const tradeString = JSON.stringify(trade, null, 2);
2430
logUpdate('Latest trade:', tradeString);
2531

26-
drive.put('/feeds/bitfinex/latest-trade', b4a.from(tradeString));
27-
}, 500);
32+
return alice.coreData.update(FILE_PATH, b4a.from(tradeString));
33+
}

examples/drives/watch/watcher.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@ import logUpdate from 'log-update';
33
import SDK from '@synonymdev/slashtags-sdk';
44
import RAM from 'random-access-memory'
55

6-
const driveOpts = {
7-
key: b4a.from(
8-
'69b04ea6e3b62245048a8efe8c17c6affb91e07ea1e28c911c2acdfd4d851f5c',
9-
'hex',
10-
)
11-
};
6+
const url = `slash:pgar7jzdsatrkbrkt59eaf6gi973dad6w8te3rehfmg94ucfd7qy/trades-feed/latest-trade#driveKey=gcrnqop9u8h9yy7qqrmrm5ae47xdwoqya431crp1snz3m8h3mwpo&encryptionKey=n9ukimccktfppduqs3eenh8np1yt8ykn4px56h1wnqe8mianbrhy`;
127

138
console.log('Setting up slashtag...');
14-
const sdk = new SDK({
15-
storage: RAM,
16-
primaryKey: b4a.from('b'.repeat(64), 'hex'),
17-
});
18-
const watcher = sdk.drive(driveOpts.key)
19-
await watcher.update()
9+
const sdk = new SDK({ storage: RAM });
10+
const slashtag = sdk.slashtag();
2011

21-
watcher.core.on('append', async () => {
22-
const trade = await watcher.get('/feeds/bitfinex/latest-trade')
23-
const tradeString = b4a.toString(trade);
24-
logUpdate('Latest trade:', tradeString);
25-
});
12+
slashtag.coreData.subscribe(
13+
url,
14+
(trade) => {
15+
if (!trade) return
16+
const tradeString = b4a.toString(trade);
17+
logUpdate('Latest trade:', tradeString);
18+
}
19+
)

0 commit comments

Comments
 (0)