Skip to content

Commit

Permalink
add timeout option to listwatches
Browse files Browse the repository at this point in the history
  • Loading branch information
sanoel committed Apr 24, 2024
1 parent 133898b commit c19eaba
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 175 deletions.
324 changes: 162 additions & 162 deletions .yarn/releases/yarn-4.1.0.cjs → .yarn/releases/yarn-4.1.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ compressionLevel: mixed

enableGlobalCache: false

yarnPath: .yarn/releases/yarn-4.1.0.cjs
yarnPath: .yarn/releases/yarn-4.1.1.cjs
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oada/list-lib",
"version": "5.0.1",
"version": "5.0.2",
"description": "Library for processing items in an OADA list",
"main": "dist/index.js",
"type": "module",
Expand Down Expand Up @@ -125,7 +125,7 @@
"optional": true
}
},
"packageManager": "yarn@4.1.0",
"packageManager": "yarn@4.1.1",
"volta": {
"node": "20.11.0"
},
Expand Down
28 changes: 18 additions & 10 deletions src/ListWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ export class ListWatch<Item = unknown> {
readonly itemsPath;
/**
* The unique name of this service/watch
*/
*/
readonly name;
/**
* The unique name of this service/watch
*/
readonly timeout;

readonly #conn;
readonly #watch;
Expand All @@ -116,6 +120,7 @@ export class ListWatch<Item = unknown> {
resume = true,
conn,
persistInterval = 1000,
timeout,
// If no assert given, assume all items valid
// eslint-disable-next-line @typescript-eslint/no-empty-function
assertItem = () => {},
Expand All @@ -129,6 +134,7 @@ export class ListWatch<Item = unknown> {
this.tree = tree;
this.itemsPath = itemsPath;
this.name = name;
this.timeout = timeout;
this.#conn = conn;
this.#assertItem = assertItem;
this.#emitter = new EventEmitter<EventTypes<Item>, this>();
Expand Down Expand Up @@ -187,7 +193,7 @@ export class ListWatch<Item = unknown> {
persistInterval,
})
: undefined;
this.#watch = this.#initialize(onNewList);
this.#watch = this.#initialize(onNewList, timeout);
}

/**
Expand Down Expand Up @@ -240,11 +246,12 @@ export class ListWatch<Item = unknown> {
/**
* Fetch the contents of the corresponding list item
*/
async #getItem(itemEvent: ItemEvent<Item>): Promise<Item> {
async #getItem(itemEvent: ItemEvent<Item>, timeout?: number): Promise<Item> {
// Needed because TS is weird about asserts...
const assertItem: TypeAssert<Item> = this.#assertItem;
const { data: item } = await this.#conn.get({
path: join(this.path, itemEvent.pointer),
timeout
});
assertItem(item);
return item;
Expand Down Expand Up @@ -366,18 +373,18 @@ export class ListWatch<Item = unknown> {
/**
* Do async stuff for initializing ourself since constructors are synchronous
*/
async #initialize(assume: AssumeState = AssumeState.New) {
async #initialize(assume: AssumeState = AssumeState.New, timeout?: number) {
const { path } = this;
const conn = this.#conn;

log.debug('Ensuring %s exists', path);
try {
await conn.head({ path });
await conn.head({ path, timeout});
} catch (error: unknown) {
// @ts-expect-error darn errors
if (error?.status === 403 || error?.status === 404) {
// Create it
await conn.put({ path, data: {} });
await conn.put({ path, data: {}, timeout });
log.trace('Created %s because it did not exist', path);
} else {
log.error({ error });
Expand All @@ -393,6 +400,7 @@ export class ListWatch<Item = unknown> {
path,
rev: this.#meta?.rev,
type: 'tree',
timeout,
});

if (!foundMeta) {
Expand All @@ -402,7 +410,7 @@ export class ListWatch<Item = unknown> {
}

case AssumeState.New: {
await this.#handleStartingItems();
await this.#handleStartingItems(timeout);
break;
}

Expand All @@ -427,9 +435,9 @@ export class ListWatch<Item = unknown> {
*
* @todo Remove need for tree GET
*/
async #handleStartingItems() {
async #handleStartingItems(timeout?: number) {
const { path, tree, itemsPath } = this;
const { data: json } = await this.#conn.get({ path, tree });
const { data: json } = await this.#conn.get({ path, tree, timeout });
if (
typeof json !== 'object' ||
json === null ||
Expand All @@ -445,7 +453,7 @@ export class ListWatch<Item = unknown> {
path: itemsPath,
json,
});
const listRev = Number(json._rev);
const listRev = Number((json as unknown as {_rev: number})._rev);
for await (const { value, pointer } of items) {
const itemChange = {
item: value,
Expand Down
5 changes: 5 additions & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export interface Options<Item> {
* Called when the list in new to this lib (i.e., we have no _meta about it)
*/
onNewList?: AssumeState;
/**
* Timeout for the watches created in OADAClient
*/
timeout?: number;

}

/**
Expand Down

0 comments on commit c19eaba

Please sign in to comment.