This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example-app.ts
218 lines (201 loc) · 6.91 KB
/
example-app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
AuthorKeypair,
Crypto,
FormatValidatorEs4,
isErr,
LiveQueryEvent,
Logger,
LogLevel,
Peer,
QueryFollower,
setDefaultLogLevel,
setLogLevel,
sleep,
StorageAsync,
StorageDriverAsyncMemory,
} from "./mod.ts";
//--------------------------------------------------
let loggerMain = new Logger("main", "whiteBright");
let loggerBusEvents = new Logger("main storage bus events", "white");
let loggerQueryFollowerCallbacks1 = new Logger(
"main query follower 1 callback",
"red",
);
let loggerQueryFollowerCallbacks2 = new Logger(
"main query follower 2 callback",
"red",
);
let loggerPeerMapEvents = new Logger("peer map event", "redBright");
setDefaultLogLevel(LogLevel.Debug);
setLogLevel("main", LogLevel.Debug);
//================================================================================
let main = async () => {
let share = "+gardening.abc";
// setup
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("-----------\\");
loggerMain.info("setup");
loggerMain.info("share =", share);
//loggerMain.info('set global crypto driver')
//setGlobalCryptoDriver(CryptoDriverTweetnacl); // this is the default
loggerMain.info("instantiate storageDriver and storage");
let storageDriver = new StorageDriverAsyncMemory(share);
let storage = new StorageAsync(
share,
FormatValidatorEs4,
storageDriver,
);
let peer = new Peer();
peer.storageMap.bus.on("*", (channel, data) => {
loggerPeerMapEvents.debug(`${channel}`);
});
peer.addStorage(storage);
loggerMain.info("generate a keypair");
let keypair = await Crypto.generateAuthorKeypair("suzy") as AuthorKeypair;
if (isErr(keypair)) {
console.error(keypair);
Deno.exit(1);
}
loggerMain.info(" keypair =", keypair);
loggerMain.info("-----------/");
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("-----------\\");
loggerMain.info("adding a queryFollower");
// add a QueryFollower
let qf1 = new QueryFollower(
storage,
{
historyMode: "all",
orderBy: "localIndex ASC",
//startAfter: { localIndex: 1 },
//filter: { path: '/posts/post-0001.txt' },
},
);
qf1.bus.on(async (event: LiveQueryEvent) => {
if (event.kind === "existing") {
loggerQueryFollowerCallbacks1.debug(
"got an existing doc",
event.doc,
);
} else if (event.kind === "idle") {
loggerQueryFollowerCallbacks1.debug(
"query follower is caught up, switching to live mode",
);
} else if (event.kind === "success") {
loggerQueryFollowerCallbacks1.debug("got a new doc", event.doc);
} else if (event.kind === "queryFollowerDidClose") {
loggerQueryFollowerCallbacks1.debug("query follower closed");
}
});
loggerMain.info("hatching it");
await qf1.hatch();
loggerMain.info("-----------/");
// write some docs
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("-----------\\");
let numDocsToWrite = 2;
loggerMain.info(`setting ${numDocsToWrite} docs`);
for (let ii = 0; ii < numDocsToWrite; ii++) {
loggerMain.blank();
loggerMain.info(`setting #${ii}`);
let result = await storage.set(keypair, {
format: "es.4",
path: `/posts/post-${("" + ii).padStart(4, "0")}.txt`,
content: `Hello ${ii}`,
});
loggerMain.info(" set result", result);
}
loggerMain.info("-----------/");
// get all docs
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("-----------\\");
loggerMain.info("getting all docs");
let allDocs = await storage.getAllDocs();
for (let ii = 0; ii < allDocs.length; ii++) {
loggerMain.info(`doc ${ii + 1} of ${allDocs.length}:`, allDocs[ii]);
}
loggerMain.info("-----------/");
// add another QueryFollower
// now that we have some docs, this will have to catch up
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("-----------\\");
loggerMain.info("adding a queryFollower");
let qf2 = new QueryFollower(
storage,
{
historyMode: "all",
orderBy: "localIndex ASC",
//startAfter: { localIndex: 1 },
//filter: { path: '/posts/post-0000.txt' },
},
);
qf2.bus.on(async (event: LiveQueryEvent) => {
if (event.kind === "existing") {
loggerQueryFollowerCallbacks2.debug(
"got an existing doc",
event.doc,
);
} else if (event.kind === "idle") {
loggerQueryFollowerCallbacks2.debug(
"query follower is caught up, switching to live mode",
);
} else if (event.kind === "success") {
loggerQueryFollowerCallbacks2.debug("got a new doc", event.doc);
} else if (event.kind === "queryFollowerDidClose") {
loggerQueryFollowerCallbacks2.debug("query follower closed");
}
});
loggerMain.info("hatching it");
await qf2.hatch();
loggerMain.info("-----------/");
// sleep
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("---------------------------------------");
loggerMain.info("-----------------------------------------");
loggerMain.info("-------------------------------------------");
loggerMain.info("sleep 100");
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
await sleep(100);
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("done sleeping 100");
loggerMain.info("-------------------------------------------");
loggerMain.info("-----------------------------------------");
loggerMain.info("---------------------------------------");
storage.bus.on("willClose", async () => {
loggerBusEvents.debug("storage willClose... sleeping 1 second...");
await sleep(1000);
loggerBusEvents.debug("...storage willClose done.");
}, { mode: "blocking" });
storage.bus.on("didClose", async () => {
loggerBusEvents.debug("storage didClose... sleeping 1 second...");
await sleep(1000);
loggerBusEvents.debug("...storage didClose done.");
}, { mode: "nonblocking" });
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
loggerMain.info("closing storage");
loggerMain.info("-------------------------------------------");
await storage.close(true); // erase=true for this example, but normally use erase=false
loggerMain.info("-------------------------------------------");
loggerMain.blank();
loggerMain.blank();
loggerMain.blank();
};
main();