@wireflowapp/core 0.0.0-stage → 1.0.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/README.md CHANGED
@@ -1,3 +1,29 @@
1
- # Temporary Holding Version
1
+ # @wireflowapp/core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The foundational core package for **Wireflow**, providing common connection types, centralized configuration management, and robust retry orchestration.
4
+
5
+ ## Features
6
+
7
+ - **Standard Connection Status**: Defines uniform `"Idle" | "Connecting" | "Connected" | "Failed"` statuses.
8
+ - **RetryManager**: Authoritative retry scheduling, preventing duplicate timers and race conditions.
9
+ - **WireFlowManager**: Hierarchical configuration manager honoring hook overrides > manager defaults > library defaults.
10
+ - **Zero Runtime Dependencies**: Ultra-lightweight and framework-agnostic.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @wireflowapp/core
16
+ ```
17
+
18
+ ## Quick Start
19
+
20
+ ```ts
21
+ import { WireFlowManager, RetryManager, ConnectionStatus } from "@wireflowapp/core";
22
+
23
+ // Centralized configuration
24
+ const manager = new WireFlowManager({
25
+ retryAttempts: 3,
26
+ retryDelay: 2000,
27
+ });
28
+ WireFlowManager.setDefault(manager);
29
+ ```
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Shared connection status across all Wireflow transports and framework adapters.
3
+ */
4
+ type ConnectionStatus = "Idle" | "Connecting" | "Connected" | "Failed";
5
+ /**
6
+ * Standard public connection API surface returned by Wireflow connections.
7
+ */
8
+ interface ConnectionResult {
9
+ /**
10
+ * Current status of the connection.
11
+ */
12
+ readonly status: ConnectionStatus;
13
+ /**
14
+ * Initiates an initial connection attempt.
15
+ */
16
+ connect: () => void;
17
+ /**
18
+ * Manually triggers a retry attempt if currently idle or failed.
19
+ * Does nothing if already connected or currently connecting.
20
+ */
21
+ retry: () => void;
22
+ /**
23
+ * Cleanly disconnects the active connection and aborts pending retries.
24
+ */
25
+ disconnect: () => void;
26
+ }
27
+ /**
28
+ * Shared configuration options for connection and retry policies.
29
+ */
30
+ interface WireFlowConfig {
31
+ /**
32
+ * Number of retry attempts after the initial failure.
33
+ * Total max attempts = retryAttempts + 1.
34
+ * @default 5
35
+ */
36
+ retryAttempts?: number;
37
+ /**
38
+ * Delay in milliseconds between retry attempts.
39
+ * @default 3000
40
+ */
41
+ retryDelay?: number;
42
+ /**
43
+ * Whether to automatically connect upon creation or mount.
44
+ * @default true
45
+ */
46
+ autoConnect?: boolean;
47
+ /**
48
+ * Whether to automatically schedule retries when an established connection drops unexpectedly.
49
+ * @default true
50
+ */
51
+ retryOnDisconnect?: boolean;
52
+ }
53
+ /**
54
+ * Resolved, non-nullable Wireflow configuration with library defaults applied.
55
+ */
56
+ interface ResolvedWireFlowConfig {
57
+ retryAttempts: number;
58
+ retryDelay: number;
59
+ autoConnect: boolean;
60
+ retryOnDisconnect: boolean;
61
+ }
62
+ interface WireFlowConnection extends ConnectionResult {
63
+ }
64
+ /**
65
+ * Default configuration values defined by the Wireflow specification.
66
+ */
67
+ declare const defaultConfig: Required<WireFlowConfig>;
68
+ declare const DEFAULT_WIREFLOW_CONFIG: ResolvedWireFlowConfig;
69
+
70
+ interface RetryManagerOptions {
71
+ /**
72
+ * Number of retries permitted after initial connection failure.
73
+ * Total attempts = initial attempt (1) + retryAttempts.
74
+ * @default 5
75
+ */
76
+ retryAttempts?: number;
77
+ /**
78
+ * Delay in milliseconds between retry attempts.
79
+ * @default 3000
80
+ */
81
+ retryDelay?: number;
82
+ /**
83
+ * Callback fired right before a retry timer starts.
84
+ */
85
+ onRetryAttempt?: (attempt: number, maxAttempts: number, delay: number) => void;
86
+ /**
87
+ * Callback fired when all retry attempts have been exhausted.
88
+ */
89
+ onExhausted?: () => void;
90
+ }
91
+ /**
92
+ * Reusable retry coordinator managing backoff timers, attempt tracking,
93
+ * and exhaustion callbacks.
94
+ */
95
+ declare class RetryManager {
96
+ private _maxAttempts;
97
+ private _delay;
98
+ private _currentAttempt;
99
+ private _timer;
100
+ private _onRetryAttempt?;
101
+ private _onExhausted?;
102
+ constructor(options?: RetryManagerOptions);
103
+ /**
104
+ * Number of retries executed or currently scheduled in this retry cycle.
105
+ */
106
+ get attempts(): number;
107
+ /**
108
+ * Configured maximum number of retry attempts.
109
+ */
110
+ get maxAttempts(): number;
111
+ /**
112
+ * Configured delay in milliseconds.
113
+ */
114
+ get delay(): number;
115
+ /**
116
+ * Indicates whether a retry delay timer is currently active.
117
+ */
118
+ get isRetrying(): boolean;
119
+ /**
120
+ * Indicates whether all configured retry attempts have been used up.
121
+ */
122
+ isExhausted(): boolean;
123
+ /**
124
+ * Dynamically update configuration if needed.
125
+ */
126
+ updateConfig(options: Partial<RetryManagerOptions>): void;
127
+ /**
128
+ * Schedules the next retry execution after `retryDelay` ms.
129
+ * Prevents duplicate timers if a retry is already in flight.
130
+ * If all attempts are exhausted, triggers `onExhausted` and returns false.
131
+ *
132
+ * @param action The connection attempt function to invoke when the timer fires.
133
+ * @returns `true` if a retry was scheduled; `false` if already scheduled or exhausted.
134
+ */
135
+ scheduleRetry(action: () => void | Promise<void>): boolean;
136
+ /**
137
+ * Cancels any pending scheduled retry timer without resetting the attempt counter.
138
+ */
139
+ cancel(): void;
140
+ /**
141
+ * Resets the attempt counter to 0 and cancels any active retry timer.
142
+ * Should be called when a connection successfully connects or when manually disconnecting.
143
+ */
144
+ reset(): void;
145
+ /**
146
+ * Disposes of all internal timers and callbacks.
147
+ */
148
+ dispose(): void;
149
+ }
150
+
151
+ /**
152
+ * WireFlowManager coordinates centralized configuration defaults for Wireflow
153
+ * connections across an application.
154
+ *
155
+ * It enforces the configuration resolution priority:
156
+ * 1. Hook/Client-specific options (highest)
157
+ * 2. WireFlowManager configuration (middle)
158
+ * 3. Library defaults (baseline)
159
+ *
160
+ * Actual transport connection lifecycles remain encapsulated within
161
+ * transport-specific clients.
162
+ */
163
+ declare class WireFlowManager {
164
+ private static defaultInstance;
165
+ private _config;
166
+ constructor(customConfig?: WireFlowConfig);
167
+ /**
168
+ * Retrieves the current configuration defaults held by this manager instance.
169
+ */
170
+ get config(): ResolvedWireFlowConfig;
171
+ /**
172
+ * Updates one or more configuration defaults on this manager instance.
173
+ */
174
+ updateConfig(newConfig: WireFlowConfig): void;
175
+ /**
176
+ * Resolves final configuration by merging specific options with manager defaults
177
+ * and base library defaults.
178
+ *
179
+ * Priority:
180
+ * 1. overrideConfig (hook or client specific)
181
+ * 2. this._config (manager instance defaults)
182
+ * 3. DEFAULT_WIREFLOW_CONFIG (library defaults)
183
+ */
184
+ resolveConfig(overrideConfig?: WireFlowConfig): ResolvedWireFlowConfig;
185
+ /**
186
+ * Static helper to resolve options directly against the default singleton
187
+ * manager (if set) or library defaults.
188
+ */
189
+ static resolve(overrideConfig?: WireFlowConfig): ResolvedWireFlowConfig;
190
+ /**
191
+ * Assigns an optional global default WireFlowManager instance.
192
+ */
193
+ static setDefault(manager: WireFlowManager | null): void;
194
+ /**
195
+ * Gets the current global default WireFlowManager instance, if one was set.
196
+ */
197
+ static getDefault(): WireFlowManager | null;
198
+ }
199
+
200
+ export { type ConnectionResult, type ConnectionStatus, DEFAULT_WIREFLOW_CONFIG, type ResolvedWireFlowConfig, RetryManager, type RetryManagerOptions, type WireFlowConfig, type WireFlowConnection, WireFlowManager, defaultConfig };
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Shared connection status across all Wireflow transports and framework adapters.
3
+ */
4
+ type ConnectionStatus = "Idle" | "Connecting" | "Connected" | "Failed";
5
+ /**
6
+ * Standard public connection API surface returned by Wireflow connections.
7
+ */
8
+ interface ConnectionResult {
9
+ /**
10
+ * Current status of the connection.
11
+ */
12
+ readonly status: ConnectionStatus;
13
+ /**
14
+ * Initiates an initial connection attempt.
15
+ */
16
+ connect: () => void;
17
+ /**
18
+ * Manually triggers a retry attempt if currently idle or failed.
19
+ * Does nothing if already connected or currently connecting.
20
+ */
21
+ retry: () => void;
22
+ /**
23
+ * Cleanly disconnects the active connection and aborts pending retries.
24
+ */
25
+ disconnect: () => void;
26
+ }
27
+ /**
28
+ * Shared configuration options for connection and retry policies.
29
+ */
30
+ interface WireFlowConfig {
31
+ /**
32
+ * Number of retry attempts after the initial failure.
33
+ * Total max attempts = retryAttempts + 1.
34
+ * @default 5
35
+ */
36
+ retryAttempts?: number;
37
+ /**
38
+ * Delay in milliseconds between retry attempts.
39
+ * @default 3000
40
+ */
41
+ retryDelay?: number;
42
+ /**
43
+ * Whether to automatically connect upon creation or mount.
44
+ * @default true
45
+ */
46
+ autoConnect?: boolean;
47
+ /**
48
+ * Whether to automatically schedule retries when an established connection drops unexpectedly.
49
+ * @default true
50
+ */
51
+ retryOnDisconnect?: boolean;
52
+ }
53
+ /**
54
+ * Resolved, non-nullable Wireflow configuration with library defaults applied.
55
+ */
56
+ interface ResolvedWireFlowConfig {
57
+ retryAttempts: number;
58
+ retryDelay: number;
59
+ autoConnect: boolean;
60
+ retryOnDisconnect: boolean;
61
+ }
62
+ interface WireFlowConnection extends ConnectionResult {
63
+ }
64
+ /**
65
+ * Default configuration values defined by the Wireflow specification.
66
+ */
67
+ declare const defaultConfig: Required<WireFlowConfig>;
68
+ declare const DEFAULT_WIREFLOW_CONFIG: ResolvedWireFlowConfig;
69
+
70
+ interface RetryManagerOptions {
71
+ /**
72
+ * Number of retries permitted after initial connection failure.
73
+ * Total attempts = initial attempt (1) + retryAttempts.
74
+ * @default 5
75
+ */
76
+ retryAttempts?: number;
77
+ /**
78
+ * Delay in milliseconds between retry attempts.
79
+ * @default 3000
80
+ */
81
+ retryDelay?: number;
82
+ /**
83
+ * Callback fired right before a retry timer starts.
84
+ */
85
+ onRetryAttempt?: (attempt: number, maxAttempts: number, delay: number) => void;
86
+ /**
87
+ * Callback fired when all retry attempts have been exhausted.
88
+ */
89
+ onExhausted?: () => void;
90
+ }
91
+ /**
92
+ * Reusable retry coordinator managing backoff timers, attempt tracking,
93
+ * and exhaustion callbacks.
94
+ */
95
+ declare class RetryManager {
96
+ private _maxAttempts;
97
+ private _delay;
98
+ private _currentAttempt;
99
+ private _timer;
100
+ private _onRetryAttempt?;
101
+ private _onExhausted?;
102
+ constructor(options?: RetryManagerOptions);
103
+ /**
104
+ * Number of retries executed or currently scheduled in this retry cycle.
105
+ */
106
+ get attempts(): number;
107
+ /**
108
+ * Configured maximum number of retry attempts.
109
+ */
110
+ get maxAttempts(): number;
111
+ /**
112
+ * Configured delay in milliseconds.
113
+ */
114
+ get delay(): number;
115
+ /**
116
+ * Indicates whether a retry delay timer is currently active.
117
+ */
118
+ get isRetrying(): boolean;
119
+ /**
120
+ * Indicates whether all configured retry attempts have been used up.
121
+ */
122
+ isExhausted(): boolean;
123
+ /**
124
+ * Dynamically update configuration if needed.
125
+ */
126
+ updateConfig(options: Partial<RetryManagerOptions>): void;
127
+ /**
128
+ * Schedules the next retry execution after `retryDelay` ms.
129
+ * Prevents duplicate timers if a retry is already in flight.
130
+ * If all attempts are exhausted, triggers `onExhausted` and returns false.
131
+ *
132
+ * @param action The connection attempt function to invoke when the timer fires.
133
+ * @returns `true` if a retry was scheduled; `false` if already scheduled or exhausted.
134
+ */
135
+ scheduleRetry(action: () => void | Promise<void>): boolean;
136
+ /**
137
+ * Cancels any pending scheduled retry timer without resetting the attempt counter.
138
+ */
139
+ cancel(): void;
140
+ /**
141
+ * Resets the attempt counter to 0 and cancels any active retry timer.
142
+ * Should be called when a connection successfully connects or when manually disconnecting.
143
+ */
144
+ reset(): void;
145
+ /**
146
+ * Disposes of all internal timers and callbacks.
147
+ */
148
+ dispose(): void;
149
+ }
150
+
151
+ /**
152
+ * WireFlowManager coordinates centralized configuration defaults for Wireflow
153
+ * connections across an application.
154
+ *
155
+ * It enforces the configuration resolution priority:
156
+ * 1. Hook/Client-specific options (highest)
157
+ * 2. WireFlowManager configuration (middle)
158
+ * 3. Library defaults (baseline)
159
+ *
160
+ * Actual transport connection lifecycles remain encapsulated within
161
+ * transport-specific clients.
162
+ */
163
+ declare class WireFlowManager {
164
+ private static defaultInstance;
165
+ private _config;
166
+ constructor(customConfig?: WireFlowConfig);
167
+ /**
168
+ * Retrieves the current configuration defaults held by this manager instance.
169
+ */
170
+ get config(): ResolvedWireFlowConfig;
171
+ /**
172
+ * Updates one or more configuration defaults on this manager instance.
173
+ */
174
+ updateConfig(newConfig: WireFlowConfig): void;
175
+ /**
176
+ * Resolves final configuration by merging specific options with manager defaults
177
+ * and base library defaults.
178
+ *
179
+ * Priority:
180
+ * 1. overrideConfig (hook or client specific)
181
+ * 2. this._config (manager instance defaults)
182
+ * 3. DEFAULT_WIREFLOW_CONFIG (library defaults)
183
+ */
184
+ resolveConfig(overrideConfig?: WireFlowConfig): ResolvedWireFlowConfig;
185
+ /**
186
+ * Static helper to resolve options directly against the default singleton
187
+ * manager (if set) or library defaults.
188
+ */
189
+ static resolve(overrideConfig?: WireFlowConfig): ResolvedWireFlowConfig;
190
+ /**
191
+ * Assigns an optional global default WireFlowManager instance.
192
+ */
193
+ static setDefault(manager: WireFlowManager | null): void;
194
+ /**
195
+ * Gets the current global default WireFlowManager instance, if one was set.
196
+ */
197
+ static getDefault(): WireFlowManager | null;
198
+ }
199
+
200
+ export { type ConnectionResult, type ConnectionStatus, DEFAULT_WIREFLOW_CONFIG, type ResolvedWireFlowConfig, RetryManager, type RetryManagerOptions, type WireFlowConfig, type WireFlowConnection, WireFlowManager, defaultConfig };
package/dist/index.js ADDED
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ DEFAULT_WIREFLOW_CONFIG: () => DEFAULT_WIREFLOW_CONFIG,
24
+ RetryManager: () => RetryManager,
25
+ WireFlowManager: () => WireFlowManager,
26
+ defaultConfig: () => defaultConfig
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/types/index.ts
31
+ var defaultConfig = Object.freeze({
32
+ autoConnect: true,
33
+ retryOnDisconnect: true,
34
+ retryAttempts: 5,
35
+ retryDelay: 3e3
36
+ });
37
+ var DEFAULT_WIREFLOW_CONFIG = defaultConfig;
38
+
39
+ // src/retry/RetryManager.ts
40
+ var RetryManager = class {
41
+ _maxAttempts;
42
+ _delay;
43
+ _currentAttempt = 0;
44
+ _timer = null;
45
+ _onRetryAttempt;
46
+ _onExhausted;
47
+ constructor(options = {}) {
48
+ this._maxAttempts = Math.max(0, options.retryAttempts ?? DEFAULT_WIREFLOW_CONFIG.retryAttempts);
49
+ this._delay = Math.max(0, options.retryDelay ?? DEFAULT_WIREFLOW_CONFIG.retryDelay);
50
+ this._onRetryAttempt = options.onRetryAttempt;
51
+ this._onExhausted = options.onExhausted;
52
+ }
53
+ /**
54
+ * Number of retries executed or currently scheduled in this retry cycle.
55
+ */
56
+ get attempts() {
57
+ return this._currentAttempt;
58
+ }
59
+ /**
60
+ * Configured maximum number of retry attempts.
61
+ */
62
+ get maxAttempts() {
63
+ return this._maxAttempts;
64
+ }
65
+ /**
66
+ * Configured delay in milliseconds.
67
+ */
68
+ get delay() {
69
+ return this._delay;
70
+ }
71
+ /**
72
+ * Indicates whether a retry delay timer is currently active.
73
+ */
74
+ get isRetrying() {
75
+ return this._timer !== null;
76
+ }
77
+ /**
78
+ * Indicates whether all configured retry attempts have been used up.
79
+ */
80
+ isExhausted() {
81
+ return this._currentAttempt >= this._maxAttempts;
82
+ }
83
+ /**
84
+ * Dynamically update configuration if needed.
85
+ */
86
+ updateConfig(options) {
87
+ if (typeof options.retryAttempts === "number") {
88
+ this._maxAttempts = Math.max(0, options.retryAttempts);
89
+ }
90
+ if (typeof options.retryDelay === "number") {
91
+ this._delay = Math.max(0, options.retryDelay);
92
+ }
93
+ if (options.onRetryAttempt !== void 0) {
94
+ this._onRetryAttempt = options.onRetryAttempt;
95
+ }
96
+ if (options.onExhausted !== void 0) {
97
+ this._onExhausted = options.onExhausted;
98
+ }
99
+ }
100
+ /**
101
+ * Schedules the next retry execution after `retryDelay` ms.
102
+ * Prevents duplicate timers if a retry is already in flight.
103
+ * If all attempts are exhausted, triggers `onExhausted` and returns false.
104
+ *
105
+ * @param action The connection attempt function to invoke when the timer fires.
106
+ * @returns `true` if a retry was scheduled; `false` if already scheduled or exhausted.
107
+ */
108
+ scheduleRetry(action) {
109
+ if (this._timer !== null) {
110
+ return false;
111
+ }
112
+ if (this.isExhausted()) {
113
+ this._onExhausted?.();
114
+ return false;
115
+ }
116
+ this._currentAttempt += 1;
117
+ this._onRetryAttempt?.(this._currentAttempt, this._maxAttempts, this._delay);
118
+ this._timer = setTimeout(() => {
119
+ this._timer = null;
120
+ try {
121
+ const result = action();
122
+ if (result instanceof Promise) {
123
+ result.catch(() => {
124
+ });
125
+ }
126
+ } catch {
127
+ }
128
+ }, this._delay);
129
+ return true;
130
+ }
131
+ /**
132
+ * Cancels any pending scheduled retry timer without resetting the attempt counter.
133
+ */
134
+ cancel() {
135
+ if (this._timer !== null) {
136
+ clearTimeout(this._timer);
137
+ this._timer = null;
138
+ }
139
+ }
140
+ /**
141
+ * Resets the attempt counter to 0 and cancels any active retry timer.
142
+ * Should be called when a connection successfully connects or when manually disconnecting.
143
+ */
144
+ reset() {
145
+ this.cancel();
146
+ this._currentAttempt = 0;
147
+ }
148
+ /**
149
+ * Disposes of all internal timers and callbacks.
150
+ */
151
+ dispose() {
152
+ this.reset();
153
+ this._onRetryAttempt = void 0;
154
+ this._onExhausted = void 0;
155
+ }
156
+ };
157
+
158
+ // src/manager/WireFlowManager.ts
159
+ var WireFlowManager = class _WireFlowManager {
160
+ static defaultInstance = null;
161
+ _config;
162
+ constructor(customConfig = {}) {
163
+ this._config = {
164
+ retryAttempts: customConfig.retryAttempts ?? DEFAULT_WIREFLOW_CONFIG.retryAttempts,
165
+ retryDelay: customConfig.retryDelay ?? DEFAULT_WIREFLOW_CONFIG.retryDelay,
166
+ autoConnect: customConfig.autoConnect ?? DEFAULT_WIREFLOW_CONFIG.autoConnect,
167
+ retryOnDisconnect: customConfig.retryOnDisconnect ?? DEFAULT_WIREFLOW_CONFIG.retryOnDisconnect
168
+ };
169
+ }
170
+ /**
171
+ * Retrieves the current configuration defaults held by this manager instance.
172
+ */
173
+ get config() {
174
+ return { ...this._config };
175
+ }
176
+ /**
177
+ * Updates one or more configuration defaults on this manager instance.
178
+ */
179
+ updateConfig(newConfig) {
180
+ if (newConfig.retryAttempts !== void 0) {
181
+ this._config.retryAttempts = Math.max(0, newConfig.retryAttempts);
182
+ }
183
+ if (newConfig.retryDelay !== void 0) {
184
+ this._config.retryDelay = Math.max(0, newConfig.retryDelay);
185
+ }
186
+ if (newConfig.autoConnect !== void 0) {
187
+ this._config.autoConnect = newConfig.autoConnect;
188
+ }
189
+ if (newConfig.retryOnDisconnect !== void 0) {
190
+ this._config.retryOnDisconnect = newConfig.retryOnDisconnect;
191
+ }
192
+ }
193
+ /**
194
+ * Resolves final configuration by merging specific options with manager defaults
195
+ * and base library defaults.
196
+ *
197
+ * Priority:
198
+ * 1. overrideConfig (hook or client specific)
199
+ * 2. this._config (manager instance defaults)
200
+ * 3. DEFAULT_WIREFLOW_CONFIG (library defaults)
201
+ */
202
+ resolveConfig(overrideConfig) {
203
+ return {
204
+ retryAttempts: overrideConfig?.retryAttempts ?? this._config.retryAttempts,
205
+ retryDelay: overrideConfig?.retryDelay ?? this._config.retryDelay,
206
+ autoConnect: overrideConfig?.autoConnect ?? this._config.autoConnect,
207
+ retryOnDisconnect: overrideConfig?.retryOnDisconnect ?? this._config.retryOnDisconnect
208
+ };
209
+ }
210
+ /**
211
+ * Static helper to resolve options directly against the default singleton
212
+ * manager (if set) or library defaults.
213
+ */
214
+ static resolve(overrideConfig) {
215
+ if (_WireFlowManager.defaultInstance) {
216
+ return _WireFlowManager.defaultInstance.resolveConfig(overrideConfig);
217
+ }
218
+ return {
219
+ retryAttempts: overrideConfig?.retryAttempts ?? DEFAULT_WIREFLOW_CONFIG.retryAttempts,
220
+ retryDelay: overrideConfig?.retryDelay ?? DEFAULT_WIREFLOW_CONFIG.retryDelay,
221
+ autoConnect: overrideConfig?.autoConnect ?? DEFAULT_WIREFLOW_CONFIG.autoConnect,
222
+ retryOnDisconnect: overrideConfig?.retryOnDisconnect ?? DEFAULT_WIREFLOW_CONFIG.retryOnDisconnect
223
+ };
224
+ }
225
+ /**
226
+ * Assigns an optional global default WireFlowManager instance.
227
+ */
228
+ static setDefault(manager) {
229
+ _WireFlowManager.defaultInstance = manager;
230
+ }
231
+ /**
232
+ * Gets the current global default WireFlowManager instance, if one was set.
233
+ */
234
+ static getDefault() {
235
+ return _WireFlowManager.defaultInstance;
236
+ }
237
+ };
238
+ // Annotate the CommonJS export names for ESM import in node:
239
+ 0 && (module.exports = {
240
+ DEFAULT_WIREFLOW_CONFIG,
241
+ RetryManager,
242
+ WireFlowManager,
243
+ defaultConfig
244
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,214 @@
1
+ // src/types/index.ts
2
+ var defaultConfig = Object.freeze({
3
+ autoConnect: true,
4
+ retryOnDisconnect: true,
5
+ retryAttempts: 5,
6
+ retryDelay: 3e3
7
+ });
8
+ var DEFAULT_WIREFLOW_CONFIG = defaultConfig;
9
+
10
+ // src/retry/RetryManager.ts
11
+ var RetryManager = class {
12
+ _maxAttempts;
13
+ _delay;
14
+ _currentAttempt = 0;
15
+ _timer = null;
16
+ _onRetryAttempt;
17
+ _onExhausted;
18
+ constructor(options = {}) {
19
+ this._maxAttempts = Math.max(0, options.retryAttempts ?? DEFAULT_WIREFLOW_CONFIG.retryAttempts);
20
+ this._delay = Math.max(0, options.retryDelay ?? DEFAULT_WIREFLOW_CONFIG.retryDelay);
21
+ this._onRetryAttempt = options.onRetryAttempt;
22
+ this._onExhausted = options.onExhausted;
23
+ }
24
+ /**
25
+ * Number of retries executed or currently scheduled in this retry cycle.
26
+ */
27
+ get attempts() {
28
+ return this._currentAttempt;
29
+ }
30
+ /**
31
+ * Configured maximum number of retry attempts.
32
+ */
33
+ get maxAttempts() {
34
+ return this._maxAttempts;
35
+ }
36
+ /**
37
+ * Configured delay in milliseconds.
38
+ */
39
+ get delay() {
40
+ return this._delay;
41
+ }
42
+ /**
43
+ * Indicates whether a retry delay timer is currently active.
44
+ */
45
+ get isRetrying() {
46
+ return this._timer !== null;
47
+ }
48
+ /**
49
+ * Indicates whether all configured retry attempts have been used up.
50
+ */
51
+ isExhausted() {
52
+ return this._currentAttempt >= this._maxAttempts;
53
+ }
54
+ /**
55
+ * Dynamically update configuration if needed.
56
+ */
57
+ updateConfig(options) {
58
+ if (typeof options.retryAttempts === "number") {
59
+ this._maxAttempts = Math.max(0, options.retryAttempts);
60
+ }
61
+ if (typeof options.retryDelay === "number") {
62
+ this._delay = Math.max(0, options.retryDelay);
63
+ }
64
+ if (options.onRetryAttempt !== void 0) {
65
+ this._onRetryAttempt = options.onRetryAttempt;
66
+ }
67
+ if (options.onExhausted !== void 0) {
68
+ this._onExhausted = options.onExhausted;
69
+ }
70
+ }
71
+ /**
72
+ * Schedules the next retry execution after `retryDelay` ms.
73
+ * Prevents duplicate timers if a retry is already in flight.
74
+ * If all attempts are exhausted, triggers `onExhausted` and returns false.
75
+ *
76
+ * @param action The connection attempt function to invoke when the timer fires.
77
+ * @returns `true` if a retry was scheduled; `false` if already scheduled or exhausted.
78
+ */
79
+ scheduleRetry(action) {
80
+ if (this._timer !== null) {
81
+ return false;
82
+ }
83
+ if (this.isExhausted()) {
84
+ this._onExhausted?.();
85
+ return false;
86
+ }
87
+ this._currentAttempt += 1;
88
+ this._onRetryAttempt?.(this._currentAttempt, this._maxAttempts, this._delay);
89
+ this._timer = setTimeout(() => {
90
+ this._timer = null;
91
+ try {
92
+ const result = action();
93
+ if (result instanceof Promise) {
94
+ result.catch(() => {
95
+ });
96
+ }
97
+ } catch {
98
+ }
99
+ }, this._delay);
100
+ return true;
101
+ }
102
+ /**
103
+ * Cancels any pending scheduled retry timer without resetting the attempt counter.
104
+ */
105
+ cancel() {
106
+ if (this._timer !== null) {
107
+ clearTimeout(this._timer);
108
+ this._timer = null;
109
+ }
110
+ }
111
+ /**
112
+ * Resets the attempt counter to 0 and cancels any active retry timer.
113
+ * Should be called when a connection successfully connects or when manually disconnecting.
114
+ */
115
+ reset() {
116
+ this.cancel();
117
+ this._currentAttempt = 0;
118
+ }
119
+ /**
120
+ * Disposes of all internal timers and callbacks.
121
+ */
122
+ dispose() {
123
+ this.reset();
124
+ this._onRetryAttempt = void 0;
125
+ this._onExhausted = void 0;
126
+ }
127
+ };
128
+
129
+ // src/manager/WireFlowManager.ts
130
+ var WireFlowManager = class _WireFlowManager {
131
+ static defaultInstance = null;
132
+ _config;
133
+ constructor(customConfig = {}) {
134
+ this._config = {
135
+ retryAttempts: customConfig.retryAttempts ?? DEFAULT_WIREFLOW_CONFIG.retryAttempts,
136
+ retryDelay: customConfig.retryDelay ?? DEFAULT_WIREFLOW_CONFIG.retryDelay,
137
+ autoConnect: customConfig.autoConnect ?? DEFAULT_WIREFLOW_CONFIG.autoConnect,
138
+ retryOnDisconnect: customConfig.retryOnDisconnect ?? DEFAULT_WIREFLOW_CONFIG.retryOnDisconnect
139
+ };
140
+ }
141
+ /**
142
+ * Retrieves the current configuration defaults held by this manager instance.
143
+ */
144
+ get config() {
145
+ return { ...this._config };
146
+ }
147
+ /**
148
+ * Updates one or more configuration defaults on this manager instance.
149
+ */
150
+ updateConfig(newConfig) {
151
+ if (newConfig.retryAttempts !== void 0) {
152
+ this._config.retryAttempts = Math.max(0, newConfig.retryAttempts);
153
+ }
154
+ if (newConfig.retryDelay !== void 0) {
155
+ this._config.retryDelay = Math.max(0, newConfig.retryDelay);
156
+ }
157
+ if (newConfig.autoConnect !== void 0) {
158
+ this._config.autoConnect = newConfig.autoConnect;
159
+ }
160
+ if (newConfig.retryOnDisconnect !== void 0) {
161
+ this._config.retryOnDisconnect = newConfig.retryOnDisconnect;
162
+ }
163
+ }
164
+ /**
165
+ * Resolves final configuration by merging specific options with manager defaults
166
+ * and base library defaults.
167
+ *
168
+ * Priority:
169
+ * 1. overrideConfig (hook or client specific)
170
+ * 2. this._config (manager instance defaults)
171
+ * 3. DEFAULT_WIREFLOW_CONFIG (library defaults)
172
+ */
173
+ resolveConfig(overrideConfig) {
174
+ return {
175
+ retryAttempts: overrideConfig?.retryAttempts ?? this._config.retryAttempts,
176
+ retryDelay: overrideConfig?.retryDelay ?? this._config.retryDelay,
177
+ autoConnect: overrideConfig?.autoConnect ?? this._config.autoConnect,
178
+ retryOnDisconnect: overrideConfig?.retryOnDisconnect ?? this._config.retryOnDisconnect
179
+ };
180
+ }
181
+ /**
182
+ * Static helper to resolve options directly against the default singleton
183
+ * manager (if set) or library defaults.
184
+ */
185
+ static resolve(overrideConfig) {
186
+ if (_WireFlowManager.defaultInstance) {
187
+ return _WireFlowManager.defaultInstance.resolveConfig(overrideConfig);
188
+ }
189
+ return {
190
+ retryAttempts: overrideConfig?.retryAttempts ?? DEFAULT_WIREFLOW_CONFIG.retryAttempts,
191
+ retryDelay: overrideConfig?.retryDelay ?? DEFAULT_WIREFLOW_CONFIG.retryDelay,
192
+ autoConnect: overrideConfig?.autoConnect ?? DEFAULT_WIREFLOW_CONFIG.autoConnect,
193
+ retryOnDisconnect: overrideConfig?.retryOnDisconnect ?? DEFAULT_WIREFLOW_CONFIG.retryOnDisconnect
194
+ };
195
+ }
196
+ /**
197
+ * Assigns an optional global default WireFlowManager instance.
198
+ */
199
+ static setDefault(manager) {
200
+ _WireFlowManager.defaultInstance = manager;
201
+ }
202
+ /**
203
+ * Gets the current global default WireFlowManager instance, if one was set.
204
+ */
205
+ static getDefault() {
206
+ return _WireFlowManager.defaultInstance;
207
+ }
208
+ };
209
+ export {
210
+ DEFAULT_WIREFLOW_CONFIG,
211
+ RetryManager,
212
+ WireFlowManager,
213
+ defaultConfig
214
+ };
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@wireflowapp/core",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "Core connection primitives, types, and retry management for Wireflow",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ }
19
+ },
20
+ "sideEffects": false,
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "keywords": [
28
+ "real-time",
29
+ "connection",
30
+ "websocket",
31
+ "socketio",
32
+ "retry",
33
+ "reconnection"
34
+ ],
35
+ "author": "",
36
+ "license": "MIT",
37
+ "scripts": {
38
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
39
+ "typecheck": "tsc --noEmit",
40
+ "test": "vitest run"
41
+ }
6
42
  }