@sortlist/cli 0.33.0 → 0.35.0
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.
- package/package.json +4 -1
- package/src/commands/config.ts +3 -4
- package/src/commands/dynamic.ts +18 -16
- package/src/commands/login.ts +3 -58
- package/src/commands/whoami.ts +2 -17
- package/src/graphql/client.ts +3 -1
- package/src/graphql/generated/gql.ts +3 -9
- package/src/graphql/generated/graphql.ts +1029 -4425
- package/src/graphql/operations/common.ts +0 -17
- package/src/graphql/query-builder.ts +1 -1
- package/src/index.ts +1 -5
- package/src/lib/browser.ts +8 -3
- package/src/lib/company.ts +4 -24
- package/src/lib/config.ts +0 -4
- package/src/lib/output.ts +0 -26
- package/src/lib/sentry.ts +0 -7
- package/src/commands/offer.ts +0 -96
- package/src/commands/switch.ts +0 -72
- package/src/lib/parse.ts +0 -8
- package/src/lib/sequence-buffer.ts +0 -95
|
@@ -11,23 +11,6 @@ export const Me = gql(`
|
|
|
11
11
|
email
|
|
12
12
|
firstName
|
|
13
13
|
lastName
|
|
14
|
-
companies {
|
|
15
|
-
nodes {
|
|
16
|
-
id
|
|
17
|
-
name
|
|
18
|
-
slug
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
`);
|
|
24
|
-
|
|
25
|
-
export const CreateSalesOffer = gql(`
|
|
26
|
-
mutation CreateSalesOffer($input: CreateSalesOfferInput!) {
|
|
27
|
-
createSalesOffer(input: $input) {
|
|
28
|
-
inviteUrl
|
|
29
|
-
offerId
|
|
30
|
-
companyId
|
|
31
14
|
}
|
|
32
15
|
}
|
|
33
16
|
`);
|
|
@@ -38,7 +38,7 @@ const PAYLOAD_TYPE_PATTERN = /(?:Response|Payload|MutationPayload)$/;
|
|
|
38
38
|
// ── Selection set builder ──
|
|
39
39
|
|
|
40
40
|
export interface QueryOptions {
|
|
41
|
-
/** Max nesting depth for sub-objects (default:
|
|
41
|
+
/** Max nesting depth for sub-objects (default: 0) */
|
|
42
42
|
depth?: number;
|
|
43
43
|
/** Only include these fields (dot-notation for nested, e.g. "organization.name") */
|
|
44
44
|
fields?: string[];
|
package/src/index.ts
CHANGED
|
@@ -5,8 +5,6 @@ import { configCommand } from './commands/config.ts';
|
|
|
5
5
|
import { buildServiceCommands, formatTypeInspection } from './commands/dynamic.ts';
|
|
6
6
|
import { loginCommand } from './commands/login.ts';
|
|
7
7
|
import { logoutCommand } from './commands/logout.ts';
|
|
8
|
-
import { offerCommand } from './commands/offer.ts';
|
|
9
|
-
import { switchCommand } from './commands/switch.ts';
|
|
10
8
|
import { tokenCommand } from './commands/token.ts';
|
|
11
9
|
import { whoamiCommand } from './commands/whoami.ts';
|
|
12
10
|
import { getIntrospectedSchema, refreshSchema } from './graphql/introspection.ts';
|
|
@@ -96,10 +94,8 @@ const program = new Command()
|
|
|
96
94
|
program.addCommand(loginCommand);
|
|
97
95
|
program.addCommand(logoutCommand);
|
|
98
96
|
program.addCommand(whoamiCommand);
|
|
99
|
-
program.addCommand(switchCommand);
|
|
100
97
|
program.addCommand(tokenCommand);
|
|
101
98
|
program.addCommand(configCommand);
|
|
102
|
-
program.addCommand(offerCommand);
|
|
103
99
|
|
|
104
100
|
// Schema refresh command
|
|
105
101
|
const schemaCommand = new Command('schema').description('Manage the cached GraphQL schema');
|
|
@@ -167,7 +163,7 @@ try {
|
|
|
167
163
|
} catch (err) {
|
|
168
164
|
// Schema fetch failed — still allow non-subgraph commands (login, config, etc.)
|
|
169
165
|
const message = (err as Error).message;
|
|
170
|
-
if (!process.argv.some((a) => ['login', 'logout', 'config', 'token', 'whoami', '
|
|
166
|
+
if (!process.argv.some((a) => ['login', 'logout', 'config', 'token', 'whoami', 'schema'].includes(a))) {
|
|
171
167
|
error(`Could not fetch schema: ${message}`);
|
|
172
168
|
hint('Check your network connection and endpoint: sl config show');
|
|
173
169
|
hint('You can still use: sl login, sl config, sl schema refresh');
|
package/src/lib/browser.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
2
|
|
|
3
3
|
export function openBrowser(url: string): void {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
if (process.platform === 'win32') {
|
|
5
|
+
// `start` is a cmd built-in; the empty string is the window title slot.
|
|
6
|
+
execFile('cmd', ['/c', 'start', '', url], () => {});
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const cmd = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
10
|
+
execFile(cmd, [url], () => {});
|
|
6
11
|
}
|
package/src/lib/company.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { graphql } from '../graphql/client.ts';
|
|
2
|
-
import { Me } from '../graphql/operations/common.ts';
|
|
3
1
|
import { type UserContext } from './config.ts';
|
|
4
2
|
|
|
5
3
|
/**
|
|
@@ -7,29 +5,11 @@ import { type UserContext } from './config.ts';
|
|
|
7
5
|
*
|
|
8
6
|
* Resolution order:
|
|
9
7
|
* 1. --company-id flag
|
|
10
|
-
* 2.
|
|
11
|
-
* 3. config.companyId
|
|
12
|
-
* 4. config.companySlug → resolved via me.companies
|
|
13
|
-
* 5. user has exactly one company → that one
|
|
8
|
+
* 2. config.companyId
|
|
14
9
|
*/
|
|
15
|
-
export
|
|
10
|
+
export function resolveCompanyId(opts: {
|
|
16
11
|
companyId?: string | undefined;
|
|
17
|
-
companySlug?: string | undefined;
|
|
18
12
|
ctx: UserContext | undefined;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (opts.companyId) return opts.companyId;
|
|
22
|
-
|
|
23
|
-
const slug = opts.companySlug || opts.ctx?.companySlug;
|
|
24
|
-
|
|
25
|
-
if (!slug && opts.ctx?.companyId) return opts.ctx.companyId;
|
|
26
|
-
|
|
27
|
-
const data = await graphql(Me, undefined, { endpoint: opts.endpoint });
|
|
28
|
-
const companies = (data.me?.companies?.nodes ?? []).filter(
|
|
29
|
-
(c): c is { id: string; name: string; slug: string } => c != null,
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
if (slug) return companies.find((c) => c.slug === slug)?.id;
|
|
33
|
-
if (companies.length === 1) return companies[0].id;
|
|
34
|
-
return undefined;
|
|
13
|
+
}): string | undefined {
|
|
14
|
+
return opts.companyId || opts.ctx?.companyId;
|
|
35
15
|
}
|
package/src/lib/config.ts
CHANGED
|
@@ -9,8 +9,6 @@ export interface CliConfig {
|
|
|
9
9
|
endpoint: string;
|
|
10
10
|
oauthId?: string;
|
|
11
11
|
companyId?: string;
|
|
12
|
-
companySlug?: string;
|
|
13
|
-
companyName?: string;
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
const DEFAULT_CONFIG: CliConfig = {
|
|
@@ -71,7 +69,6 @@ export function getEndpoint(override?: string): string {
|
|
|
71
69
|
export interface UserContext {
|
|
72
70
|
oauthId?: string;
|
|
73
71
|
companyId?: string;
|
|
74
|
-
companySlug?: string;
|
|
75
72
|
}
|
|
76
73
|
|
|
77
74
|
export function getUserContext(): UserContext {
|
|
@@ -79,6 +76,5 @@ export function getUserContext(): UserContext {
|
|
|
79
76
|
return {
|
|
80
77
|
oauthId: config.oauthId,
|
|
81
78
|
companyId: config.companyId,
|
|
82
|
-
companySlug: config.companySlug,
|
|
83
79
|
};
|
|
84
80
|
}
|
package/src/lib/output.ts
CHANGED
|
@@ -10,7 +10,6 @@ const ansi = {
|
|
|
10
10
|
red: '\x1b[31m',
|
|
11
11
|
green: '\x1b[32m',
|
|
12
12
|
yellow: '\x1b[33m',
|
|
13
|
-
cyan: '\x1b[36m',
|
|
14
13
|
};
|
|
15
14
|
|
|
16
15
|
function c(code: keyof typeof ansi, text: string): string {
|
|
@@ -29,21 +28,6 @@ export function printJson(data: unknown): void {
|
|
|
29
28
|
process.stdout.write(JSON.stringify(data, null, 2) + '\n');
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
// ── Table output (human-readable, no box-drawing characters) ──
|
|
33
|
-
|
|
34
|
-
export function printTable(headers: string[], rows: string[][]): void {
|
|
35
|
-
const colWidths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => (r[i] ?? '').length)));
|
|
36
|
-
|
|
37
|
-
const separator = colWidths.map((w) => '-'.repeat(w + 2)).join('+');
|
|
38
|
-
const formatRow = (row: string[]) => row.map((cell, i) => ` ${(cell ?? '').padEnd(colWidths[i])} `).join('|');
|
|
39
|
-
|
|
40
|
-
process.stdout.write(formatRow(headers) + '\n');
|
|
41
|
-
process.stdout.write(separator + '\n');
|
|
42
|
-
for (const row of rows) {
|
|
43
|
-
process.stdout.write(formatRow(row) + '\n');
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
31
|
// ── Message helpers ──
|
|
48
32
|
// Every function uses a text prefix so status is conveyed independent of color.
|
|
49
33
|
// Color is enhancement only — never the sole status indicator.
|
|
@@ -72,13 +56,3 @@ export function error(msg: string): void {
|
|
|
72
56
|
export function hint(msg: string): void {
|
|
73
57
|
process.stderr.write(`${c('dim', ' ' + msg)}\n`);
|
|
74
58
|
}
|
|
75
|
-
|
|
76
|
-
/** Dim text (e.g. secondary details, IDs). */
|
|
77
|
-
export function dim(msg: string): void {
|
|
78
|
-
process.stderr.write(`${c('dim', msg)}\n`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Section heading for grouping output. */
|
|
82
|
-
export function heading(msg: string): void {
|
|
83
|
-
process.stderr.write(`\n${c('cyan', msg)}\n`);
|
|
84
|
-
}
|
package/src/lib/sentry.ts
CHANGED
|
@@ -55,13 +55,6 @@ export function captureException(error: unknown, extra?: Record<string, unknown>
|
|
|
55
55
|
Sentry.captureException(error, extra ? { extra } : undefined);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
export function setUser(user: { id?: string; email?: string }): void {
|
|
59
|
-
if (isLocalDev) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
Sentry.setUser(user);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
58
|
export function addBreadcrumb(breadcrumb: Sentry.Breadcrumb): void {
|
|
66
59
|
if (isLocalDev) {
|
|
67
60
|
return;
|
package/src/commands/offer.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
|
|
3
|
-
import { graphql } from '../graphql/client.ts';
|
|
4
|
-
import { CreateSalesOffer } from '../graphql/operations/common.ts';
|
|
5
|
-
import { error, hint, info, isJsonMode, printJson, success } from '../lib/output.ts';
|
|
6
|
-
|
|
7
|
-
interface CreateOptions {
|
|
8
|
-
company: string;
|
|
9
|
-
plan: string;
|
|
10
|
-
periodicity: string;
|
|
11
|
-
website?: string;
|
|
12
|
-
email?: string;
|
|
13
|
-
discountPercent?: string;
|
|
14
|
-
discountAmount?: string;
|
|
15
|
-
extraSeats?: string;
|
|
16
|
-
extraCredits?: string;
|
|
17
|
-
expiresInDays?: string;
|
|
18
|
-
endpoint?: string;
|
|
19
|
-
json?: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const createCommand = new Command('create')
|
|
23
|
-
.summary('Create a sales offer and get the prospect invite link')
|
|
24
|
-
.description(
|
|
25
|
-
'Pre-provision a sales-led deal and return the invite link to share with the prospect.\n\n' +
|
|
26
|
-
'Creates the prospect company, its billing customer, and a pending offer (a catalog tier\n' +
|
|
27
|
-
'at a negotiated discount), then prints the onboarding invite URL. The deal is attributed\n' +
|
|
28
|
-
'to you. Requires the billing/offer:write and max/company:write scopes (Sortlist admins).\n\n' +
|
|
29
|
-
'Examples:\n' +
|
|
30
|
-
' sl offer create --company "Acme" --website acme.com --plan growth --discount-percent 20 --email buyer@acme.com\n' +
|
|
31
|
-
' sl offer create --company "Acme" --plan scale --periodicity monthly --extra-seats 3 --json',
|
|
32
|
-
)
|
|
33
|
-
.requiredOption('--company <name>', 'Prospect company name')
|
|
34
|
-
.requiredOption('--plan <plan>', 'Catalog tier: starter, growth, or scale')
|
|
35
|
-
.option('--periodicity <period>', 'Billing periodicity: monthly or annual', 'annual')
|
|
36
|
-
.option('--website <url>', 'Prospect company website (seeds onboarding)')
|
|
37
|
-
.option('--email <email>', 'Prospect email (prefills sign-up and is appended to the link)')
|
|
38
|
-
.option('--discount-percent <n>', 'Percentage discount, 0-100 (exclusive with --discount-amount)')
|
|
39
|
-
.option('--discount-amount <eur>', 'Absolute recurring discount in EUR (exclusive with --discount-percent)')
|
|
40
|
-
.option('--extra-seats <n>', 'Extra seats on top of the tier')
|
|
41
|
-
.option('--extra-credits <n>', 'Extra credits each period on top of the tier')
|
|
42
|
-
.option('--expires-in-days <n>', 'Days until the offer expires (default: never)')
|
|
43
|
-
.action(async function (this: Command) {
|
|
44
|
-
const opts = this.optsWithGlobals() as CreateOptions;
|
|
45
|
-
|
|
46
|
-
if (opts.discountPercent != null && opts.discountAmount != null) {
|
|
47
|
-
error('Pass either --discount-percent or --discount-amount, not both.');
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const expiresAt =
|
|
52
|
-
opts.expiresInDays != null
|
|
53
|
-
? new Date(Date.now() + Number(opts.expiresInDays) * 24 * 60 * 60 * 1000).toISOString()
|
|
54
|
-
: undefined;
|
|
55
|
-
|
|
56
|
-
const input = {
|
|
57
|
-
name: opts.company,
|
|
58
|
-
plan: opts.plan,
|
|
59
|
-
periodicity: opts.periodicity,
|
|
60
|
-
website: opts.website,
|
|
61
|
-
email: opts.email,
|
|
62
|
-
discountPercent: opts.discountPercent != null ? Number(opts.discountPercent) : undefined,
|
|
63
|
-
discountAmountEur: opts.discountAmount != null ? Number(opts.discountAmount) : undefined,
|
|
64
|
-
extraSeats: opts.extraSeats != null ? Number(opts.extraSeats) : undefined,
|
|
65
|
-
extraCredits: opts.extraCredits != null ? Number(opts.extraCredits) : undefined,
|
|
66
|
-
expiresAt,
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
const data = await graphql(CreateSalesOffer, { input }, { endpoint: opts.endpoint });
|
|
71
|
-
const offer = data.createSalesOffer;
|
|
72
|
-
if (offer == null) {
|
|
73
|
-
error('The server did not return an offer.');
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (isJsonMode(opts)) {
|
|
78
|
-
printJson(offer);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
success(`Offer created for ${opts.company}.`);
|
|
83
|
-
info('');
|
|
84
|
-
info('Invite link (share with the prospect):');
|
|
85
|
-
info(` ${offer.inviteUrl}`);
|
|
86
|
-
} catch (caught) {
|
|
87
|
-
error(`Could not create the offer: ${(caught as Error).message}`);
|
|
88
|
-
hint('Check that your account has the billing/offer:write and max/company:write scopes.');
|
|
89
|
-
process.exit(1);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
export const offerCommand = new Command('offer')
|
|
94
|
-
.summary('Manage sales-led offers')
|
|
95
|
-
.description('Create sales-led offers and get the prospect invite link.')
|
|
96
|
-
.addCommand(createCommand);
|
package/src/commands/switch.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
|
|
3
|
-
import { graphql } from '../graphql/client.ts';
|
|
4
|
-
import { Me } from '../graphql/operations/common.ts';
|
|
5
|
-
import { loadConfig, saveConfig } from '../lib/config.ts';
|
|
6
|
-
import { error, hint, info, success } from '../lib/output.ts';
|
|
7
|
-
|
|
8
|
-
interface CompanyChoice {
|
|
9
|
-
id: string;
|
|
10
|
-
name: string;
|
|
11
|
-
slug: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async function fetchCompanies(endpoint?: string): Promise<CompanyChoice[]> {
|
|
15
|
-
const data = await graphql(Me, undefined, { endpoint });
|
|
16
|
-
return (data.me?.companies?.nodes ?? [])
|
|
17
|
-
.filter((c): c is { id: string; name: string; slug: string } => c != null)
|
|
18
|
-
.map((c) => ({ id: c.id, name: c.name ?? c.slug, slug: c.slug }));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const switchCommand = new Command('switch')
|
|
22
|
-
.summary("Switch which company you're working with")
|
|
23
|
-
.description(
|
|
24
|
-
"Switch which company you're working with\n\n" +
|
|
25
|
-
'Run without arguments to see all available companies.\n' +
|
|
26
|
-
'Pass a company slug to switch immediately.\n\n' +
|
|
27
|
-
'Examples:\n' +
|
|
28
|
-
' sl switch List your companies (active one is marked)\n' +
|
|
29
|
-
" sl switch acme Switch to the 'acme' company",
|
|
30
|
-
)
|
|
31
|
-
.argument('[slug]', 'Company slug to switch to (omit to list)')
|
|
32
|
-
.action(async function (this: Command, slug?: string) {
|
|
33
|
-
const opts = this.optsWithGlobals();
|
|
34
|
-
const config = loadConfig();
|
|
35
|
-
const companies = await fetchCompanies(opts.endpoint);
|
|
36
|
-
|
|
37
|
-
if (companies.length === 0) {
|
|
38
|
-
error('No companies found for your account.');
|
|
39
|
-
hint('Make sure your Sortlist account is linked to at least one company.');
|
|
40
|
-
process.exit(1);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// No slug provided — list companies
|
|
44
|
-
if (!slug) {
|
|
45
|
-
for (const c of companies) {
|
|
46
|
-
const marker = c.slug === config.companySlug ? ' ← active' : '';
|
|
47
|
-
info(` ${c.slug} ${c.name}${marker}`);
|
|
48
|
-
}
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Switch to the provided slug
|
|
53
|
-
const selected = companies.find((c) => c.slug === slug);
|
|
54
|
-
if (!selected) {
|
|
55
|
-
error(`Company "${slug}" not found.`);
|
|
56
|
-
info('');
|
|
57
|
-
info(' Available companies:');
|
|
58
|
-
for (const c of companies) {
|
|
59
|
-
info(` ${c.slug} ${c.name}`);
|
|
60
|
-
}
|
|
61
|
-
hint('Run `sl switch <slug>` with one of the slugs above.');
|
|
62
|
-
process.exit(1);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
saveConfig({
|
|
66
|
-
companyId: selected.id,
|
|
67
|
-
companySlug: selected.slug,
|
|
68
|
-
companyName: selected.name,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
success(`Switched to ${selected.name} (${selected.slug})`);
|
|
72
|
-
});
|
package/src/lib/parse.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reorders out-of-sequence streaming events before they reach the event handler.
|
|
3
|
-
*
|
|
4
|
-
* Every subscription event carries an `agentLoopSequence` (monotonic integer starting
|
|
5
|
-
* at 1) and an `agentLoopId` (UUID constant for a single outer agent loop run). When
|
|
6
|
-
* an event arrives with a sequence higher than expected, it is buffered. If the
|
|
7
|
-
* expected event arrives within the timeout, all buffered events are flushed in order.
|
|
8
|
-
* If the timeout expires, the gap is marked as unrecoverable and buffered events are
|
|
9
|
-
* flushed anyway.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const SEQUENCE_WAIT_MS = 1500;
|
|
13
|
-
|
|
14
|
-
export interface SequencedEvent {
|
|
15
|
-
agentLoopSequence?: number | null;
|
|
16
|
-
agentLoopId?: string | null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface SequenceBufferOptions<T extends SequencedEvent> {
|
|
20
|
-
onEvent: (event: T) => void;
|
|
21
|
-
onGap: (from: number, to: number) => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class SequenceBuffer<T extends SequencedEvent> {
|
|
25
|
-
private buffer = new Map<number, T>();
|
|
26
|
-
private expected = 1;
|
|
27
|
-
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
28
|
-
private onEvent: (event: T) => void;
|
|
29
|
-
private onGap: (from: number, to: number) => void;
|
|
30
|
-
|
|
31
|
-
constructor({ onEvent, onGap }: SequenceBufferOptions<T>) {
|
|
32
|
-
this.onEvent = onEvent;
|
|
33
|
-
this.onGap = onGap;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
push(event: T): void {
|
|
37
|
-
const seq = event.agentLoopSequence;
|
|
38
|
-
|
|
39
|
-
// No sequence info — pass through (old server or non-sequenced event)
|
|
40
|
-
if (seq == null) {
|
|
41
|
-
this.onEvent(event);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (seq === this.expected) {
|
|
46
|
-
this.clearTimer();
|
|
47
|
-
this.onEvent(event);
|
|
48
|
-
this.expected++;
|
|
49
|
-
this.flush();
|
|
50
|
-
} else if (seq > this.expected) {
|
|
51
|
-
this.buffer.set(seq, event);
|
|
52
|
-
this.startTimer();
|
|
53
|
-
}
|
|
54
|
-
// seq < expected: duplicate, ignore
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
reset(): void {
|
|
58
|
-
this.buffer.clear();
|
|
59
|
-
this.expected = 1;
|
|
60
|
-
this.clearTimer();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private flush(): void {
|
|
64
|
-
while (this.buffer.has(this.expected)) {
|
|
65
|
-
const event = this.buffer.get(this.expected)!;
|
|
66
|
-
this.buffer.delete(this.expected);
|
|
67
|
-
this.onEvent(event);
|
|
68
|
-
this.expected++;
|
|
69
|
-
}
|
|
70
|
-
if (this.buffer.size > 0) {
|
|
71
|
-
this.startTimer();
|
|
72
|
-
} else {
|
|
73
|
-
this.clearTimer();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private startTimer(): void {
|
|
78
|
-
if (this.timer) return;
|
|
79
|
-
this.timer = setTimeout(() => {
|
|
80
|
-
this.timer = null;
|
|
81
|
-
const missing = this.expected;
|
|
82
|
-
const nextAvailable = Math.min(...this.buffer.keys());
|
|
83
|
-
this.onGap(missing, nextAvailable - 1);
|
|
84
|
-
this.expected = nextAvailable;
|
|
85
|
-
this.flush();
|
|
86
|
-
}, SEQUENCE_WAIT_MS);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private clearTimer(): void {
|
|
90
|
-
if (this.timer) {
|
|
91
|
-
clearTimeout(this.timer);
|
|
92
|
-
this.timer = null;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|