webspresso 0.0.109 → 0.0.111

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.
@@ -38,6 +38,8 @@ const {
38
38
  createControllerRuntime,
39
39
  queryScopedTargets,
40
40
  isTargetInControllerScope,
41
+ watch,
42
+ getActiveControllerContext,
41
43
  } = require('./controller');
42
44
 
43
45
  const {
@@ -56,9 +58,70 @@ const {
56
58
  } = require('./fragments');
57
59
 
58
60
  const {
59
- createClientDebugger,
60
- summarizePayloadSafely,
61
- } = require('./debug');
61
+ MemoryStorage,
62
+ StorageAdapter,
63
+ getNativeStorageBackend,
64
+ createClientStorage,
65
+ defaultStorage,
66
+ } = require('./storage');
67
+
68
+ const {
69
+ defineClientService,
70
+ createClientServiceRegistry,
71
+ createServiceError,
72
+ } = require('./services');
73
+
74
+ const {
75
+ createClientHostManager,
76
+ } = require('./host');
77
+
78
+ const { isArray, isNumber, NOOP_DEBUGGER, randomId, uid } = require('./utils');
79
+
80
+ const NOOP_DEVTOOL_HANDLE = Object.freeze({
81
+ open() {},
82
+ close() {},
83
+ toggle() {},
84
+ refresh() {},
85
+ destroy() {},
86
+ });
87
+
88
+ let _debugMod = null;
89
+ function getDebugModule() {
90
+ if (process.env.NODE_ENV !== 'production') {
91
+ if (!_debugMod) {
92
+ _debugMod = require('./debug');
93
+ }
94
+ return _debugMod;
95
+ }
96
+ return {
97
+ createClientDebugger: () => NOOP_DEBUGGER,
98
+ summarizePayloadSafely: () => null,
99
+ };
100
+ }
101
+
102
+ let _devtoolMod = null;
103
+ function getDevtoolModule() {
104
+ if (process.env.NODE_ENV !== 'production') {
105
+ if (!_devtoolMod) {
106
+ _devtoolMod = require('./devtool');
107
+ }
108
+ return _devtoolMod;
109
+ }
110
+ return {
111
+ mountDevtool: () => NOOP_DEVTOOL_HANDLE,
112
+ highlightElement: () => {},
113
+ setupReduxDevToolsBridge: () => null,
114
+ };
115
+ }
116
+
117
+ let _liteMod = null;
118
+ function getLiteModule() {
119
+ if (!_liteMod) {
120
+ const reqFn = typeof module !== 'undefined' && module.require ? module.require.bind(module) : require;
121
+ _liteMod = reqFn('./lite');
122
+ }
123
+ return _liteMod;
124
+ }
62
125
 
63
126
  let _serverFragments = null;
64
127
  function getServerFragments() {
@@ -90,10 +153,12 @@ function createClientRuntime(options = {}) {
90
153
  options.debugger ||
91
154
  (options.debug && typeof options.debug === 'object' && typeof options.debug.getSnapshot === 'function'
92
155
  ? options.debug
93
- : createClientDebugger({
94
- enabled: Boolean(options.debug),
95
- logger: options.debugLogger,
96
- }));
156
+ : options.debug
157
+ ? getDebugModule().createClientDebugger({
158
+ enabled: true,
159
+ logger: options.debugLogger,
160
+ })
161
+ : NOOP_DEBUGGER);
97
162
 
98
163
  const datastar = createDatastarAdapter({
99
164
  datastar: options.datastar,
@@ -108,6 +173,14 @@ function createClientRuntime(options = {}) {
108
173
  endpoint: options.fragmentEndpoint || DEFAULT_FRAGMENT_ENDPOINT,
109
174
  datastar,
110
175
  debug,
176
+ staleTime: options.staleTime,
177
+ refetchOnWindowFocus: options.refetchOnWindowFocus,
178
+ refetchOnFocus: options.refetchOnFocus,
179
+ refetchOnReconnect: options.refetchOnReconnect,
180
+ refetchOnMount: options.refetchOnMount,
181
+ refetchOnVisit: options.refetchOnVisit,
182
+ refetchInterval: options.refetchInterval,
183
+ refetchIntervalInBackground: options.refetchIntervalInBackground,
111
184
  getCsrfToken: options.getCsrfToken || (() => discoverCsrfToken(rootOption)),
112
185
  onBeforeNodeRemove(removedEl) {
113
186
  if (controllers) {
@@ -137,11 +210,26 @@ function createClientRuntime(options = {}) {
137
210
 
138
211
  const api = createApiClient(apiConfig);
139
212
 
213
+ let runtime = null;
214
+
215
+ const services =
216
+ options.services && typeof options.services.get === 'function' && typeof options.services.register === 'function'
217
+ ? options.services
218
+ : createClientServiceRegistry({
219
+ root: rootOption,
220
+ api,
221
+ debug,
222
+ getRuntime: () => runtime,
223
+ services: options.services,
224
+ });
225
+
140
226
  controllers = createControllerRuntime({
227
+ root: rootOption,
141
228
  datastar,
142
229
  fragments,
143
230
  debug,
144
231
  apiConfig,
232
+ services,
145
233
  lazyLoader: options.lazyLoader,
146
234
  });
147
235
 
@@ -200,23 +288,37 @@ function createClientRuntime(options = {}) {
200
288
  observer = null;
201
289
  }
202
290
  controllers.destroyAll();
291
+ if (fragments && typeof fragments.dispose === 'function') {
292
+ fragments.dispose();
293
+ }
294
+ services.dispose();
295
+ if (runtime && runtime.devtool && typeof runtime.devtool.destroy === 'function') {
296
+ try {
297
+ runtime.devtool.destroy();
298
+ } catch (_e) {}
299
+ runtime.devtool = null;
300
+ }
203
301
  }
204
302
 
205
- const runtime = {
303
+ runtime = {
206
304
  api,
305
+ services,
207
306
  fragments,
208
307
  controllers,
209
308
  datastar,
210
309
  adapter: datastar,
211
310
  debug,
212
311
  register: (name, def, meta) => controllers.register(name, def, meta),
312
+ registerService: (name, def) => services.register(name, def),
213
313
  mount,
214
314
  unmount,
215
315
  destroy,
316
+ dispose: destroy,
216
317
  getControllerInstance: (el, name) => controllers.getInstanceForElement(el, name),
217
318
  getController: (el, name) => controllers.getInstanceForElement(el, name),
218
319
  invalidate: (tags, meta) => fragments.invalidate(tags, meta),
219
320
  refresh: (idOrName, input, opts) => fragments.refresh(idOrName, input, opts),
321
+ mountDevtool: (opts) => getDevtoolModule().mountDevtool(runtime, opts),
220
322
  forms: {
221
323
  parse: parseFormElement,
222
324
  create: (formEl, formOpts = {}) =>
@@ -224,6 +326,7 @@ function createClientRuntime(options = {}) {
224
326
  api,
225
327
  datastar,
226
328
  debug,
329
+ services,
227
330
  ...formOpts,
228
331
  }),
229
332
  },
@@ -240,12 +343,52 @@ function createClientRuntime(options = {}) {
240
343
  * Boot the client runtime on DOMContentLoaded (or immediately if ready).
241
344
  */
242
345
  function bootClient(options = {}) {
346
+ const bootRoot = options.root || options.document || (typeof document !== 'undefined' ? document : null);
347
+ if (
348
+ options.hmr !== false &&
349
+ bootRoot &&
350
+ bootRoot.__wBootedClientRuntime &&
351
+ typeof bootRoot.__wBootedClientRuntime.destroy === 'function'
352
+ ) {
353
+ try {
354
+ bootRoot.__wBootedClientRuntime.destroy();
355
+ } catch (_e) {}
356
+ }
357
+
243
358
  const runtime = createClientRuntime(options);
359
+ if (bootRoot && (typeof bootRoot === 'object' || typeof bootRoot === 'function')) {
360
+ try {
361
+ Object.defineProperty(bootRoot, '__wBootedClientRuntime', {
362
+ value: runtime,
363
+ configurable: true,
364
+ enumerable: false,
365
+ writable: true,
366
+ });
367
+ } catch (_e) {
368
+ bootRoot.__wBootedClientRuntime = runtime;
369
+ }
370
+ }
371
+
372
+ const shouldMountDevtools =
373
+ options.devtools === true ||
374
+ (options.devtools !== false &&
375
+ (options.debug ||
376
+ options.dev ||
377
+ (typeof window !== 'undefined' &&
378
+ (window.__WEBSPRESSO_DEVTOOLS_ENABLED__ || window.__WEBSPRESSO_DEV__)) ||
379
+ (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production')));
380
+
244
381
  if (typeof document !== 'undefined') {
382
+ const onReady = () => {
383
+ runtime.mount();
384
+ if (shouldMountDevtools) {
385
+ runtime.devtool = getDevtoolModule().mountDevtool(runtime, options.devtoolsOptions || {});
386
+ }
387
+ };
245
388
  if (document.readyState === 'loading') {
246
- document.addEventListener('DOMContentLoaded', () => runtime.mount(), { once: true });
389
+ document.addEventListener('DOMContentLoaded', onReady, { once: true });
247
390
  } else {
248
- runtime.mount();
391
+ onReady();
249
392
  }
250
393
  }
251
394
  return runtime;
@@ -258,14 +401,16 @@ module.exports = {
258
401
  createClientRuntime,
259
402
  bootClient,
260
403
  get createLiteClientRuntime() {
261
- return require('./lite').createLiteClientRuntime;
404
+ return getLiteModule().createLiteClientRuntime;
262
405
  },
263
406
  get bootLiteClient() {
264
- return require('./lite').bootLiteClient;
407
+ return getLiteModule().bootLiteClient;
265
408
  },
266
409
  createControllerRuntime,
267
410
  queryScopedTargets,
268
411
  isTargetInControllerScope,
412
+ watch,
413
+ getActiveControllerContext,
269
414
 
270
415
  // Typed API Client & Request Management
271
416
  ClientRequestError,
@@ -335,13 +480,43 @@ module.exports = {
335
480
  createDatastarAdapter,
336
481
  createSignalsEngine,
337
482
  evaluateStateExpression,
483
+ isArray,
484
+ isNumber,
485
+ randomId,
486
+ uid,
338
487
  getByPath,
339
488
  setByPath,
340
489
  morphDomTrees,
341
490
  parseHtmlFragment,
342
491
 
343
- // Debug & Telemetry
344
- createClientDebugger,
345
- summarizePayloadSafely,
492
+ // Debug & Telemetry & DevTools
493
+ get createClientDebugger() {
494
+ return getDebugModule().createClientDebugger;
495
+ },
496
+ get summarizePayloadSafely() {
497
+ return getDebugModule().summarizePayloadSafely;
498
+ },
499
+ get mountDevtool() {
500
+ return getDevtoolModule().mountDevtool;
501
+ },
502
+ get highlightElement() {
503
+ return getDevtoolModule().highlightElement;
504
+ },
505
+ get setupReduxDevToolsBridge() {
506
+ return getDevtoolModule().setupReduxDevToolsBridge;
507
+ },
508
+
509
+ // Client Storage & Persistence
510
+ MemoryStorage,
511
+ StorageAdapter,
512
+ getNativeStorageBackend,
513
+ createClientStorage,
514
+ defaultStorage,
515
+
516
+ // Client Services & Optional Lazy UI Hosts
517
+ defineClientService,
518
+ createClientServiceRegistry,
519
+ createServiceError,
520
+ createClientHostManager,
346
521
  };
347
522
 
@@ -3,10 +3,11 @@
3
3
  /**
4
4
  * Webspresso Lightweight Client Runtime (`core/client/lite.js`)
5
5
  *
6
- * Micro-footprint (~8 KB Brotli) client runtime for server-driven applications.
6
+ * Micro-footprint (~16.1 KB Brotli) client runtime for server-driven applications.
7
7
  * Includes:
8
- * - SSR Fragment coordinator (batch refresh, invalidation, DOM morphing, focus preservation)
9
- * - Native HTML Form enhancement (`<form data-w-form>`, validation errors, double-submit protection)
8
+ * - SSR Fragment coordinator (batch refresh, invalidation, auto-revalidation, DOM morphing, focus preservation)
9
+ * - Native HTML Form enhancement (`<form data-w-form>`, validation errors, lifecycle hooks, draft persistence)
10
+ * - Shared Client Services & optional lazy UI hosts (`defineClientService`, `services.get`)
10
11
  * - Typed API client with auto-invalidation
11
12
  * Excludes:
12
13
  * - Reactive signals engine, controller proxies, and complex task coordinators.
@@ -36,8 +37,55 @@ const {
36
37
  deduplicateParentChildFragments,
37
38
  } = require('./fragments');
38
39
 
40
+ const {
41
+ createClientStorage,
42
+ defaultStorage,
43
+ StorageAdapter,
44
+ MemoryStorage,
45
+ } = require('./storage');
46
+
39
47
  const { NOOP_DEBUGGER } = require('./utils');
40
48
 
49
+ const NOOP_SERVICES = Object.freeze({
50
+ get(name) {
51
+ const err = new Error(`Unknown client service "${name}".`);
52
+ err.code = 'CLIENT_SERVICE_NOT_FOUND';
53
+ return Promise.reject(err);
54
+ },
55
+ has() {
56
+ return false;
57
+ },
58
+ isInitialized() {
59
+ return false;
60
+ },
61
+ peek() {
62
+ return undefined;
63
+ },
64
+ register() {},
65
+ registerMany() {},
66
+ dispose() {},
67
+ destroy() {},
68
+ isDisposed: false,
69
+ });
70
+
71
+ let _servicesMod = null;
72
+ function getServicesModule() {
73
+ if (!_servicesMod) {
74
+ const reqFn = typeof module !== 'undefined' && module.require ? module.require.bind(module) : require;
75
+ _servicesMod = reqFn('./services');
76
+ }
77
+ return _servicesMod;
78
+ }
79
+
80
+ let _hostMod = null;
81
+ function getHostModule() {
82
+ if (!_hostMod) {
83
+ const reqFn = typeof module !== 'undefined' && module.require ? module.require.bind(module) : require;
84
+ _hostMod = reqFn('./host');
85
+ }
86
+ return _hostMod;
87
+ }
88
+
41
89
  let _serverFragments = null;
42
90
  function getServerFragments() {
43
91
  if (!_serverFragments) {
@@ -59,6 +107,14 @@ function createLiteClientRuntime(options = {}) {
59
107
  fetch: options.fetch,
60
108
  endpoint: options.fragmentEndpoint || DEFAULT_FRAGMENT_ENDPOINT,
61
109
  debug,
110
+ staleTime: options.staleTime,
111
+ refetchOnWindowFocus: options.refetchOnWindowFocus,
112
+ refetchOnFocus: options.refetchOnFocus,
113
+ refetchOnReconnect: options.refetchOnReconnect,
114
+ refetchOnMount: options.refetchOnMount,
115
+ refetchOnVisit: options.refetchOnVisit,
116
+ refetchInterval: options.refetchInterval,
117
+ refetchIntervalInBackground: options.refetchIntervalInBackground,
62
118
  getCsrfToken: options.getCsrfToken || (() => discoverCsrfToken(rootOption)),
63
119
  });
64
120
 
@@ -73,6 +129,21 @@ function createLiteClientRuntime(options = {}) {
73
129
 
74
130
  const api = createApiClient(apiConfig);
75
131
 
132
+ let runtime = null;
133
+
134
+ const services =
135
+ options.services && typeof options.services.get === 'function' && typeof options.services.register === 'function'
136
+ ? options.services
137
+ : options.services && typeof options.services === 'object'
138
+ ? getServicesModule().createClientServiceRegistry({
139
+ root: rootOption,
140
+ api,
141
+ debug,
142
+ getRuntime: () => runtime,
143
+ services: options.services,
144
+ })
145
+ : NOOP_SERVICES;
146
+
76
147
  const formsRegistry = new Map();
77
148
  function getFormHandle(nameOrEl) {
78
149
  if (!nameOrEl) return null;
@@ -86,6 +157,7 @@ function createLiteClientRuntime(options = {}) {
86
157
  el,
87
158
  createFormHandle(el, {
88
159
  api,
160
+ services,
89
161
  onInvalidate: (tags) => fragments.invalidate(tags),
90
162
  })
91
163
  );
@@ -98,6 +170,7 @@ function createLiteClientRuntime(options = {}) {
98
170
  nameOrEl,
99
171
  createFormHandle(nameOrEl, {
100
172
  api,
173
+ services,
101
174
  onInvalidate: (tags) => fragments.invalidate(tags),
102
175
  })
103
176
  );
@@ -119,16 +192,35 @@ function createLiteClientRuntime(options = {}) {
119
192
  }
120
193
  }
121
194
 
122
- return {
195
+ function destroy() {
196
+ for (const handle of formsRegistry.values()) {
197
+ try {
198
+ handle.destroy();
199
+ } catch (_e) {}
200
+ }
201
+ formsRegistry.clear();
202
+ if (fragments && typeof fragments.dispose === 'function') {
203
+ fragments.dispose();
204
+ }
205
+ services.dispose();
206
+ }
207
+
208
+ runtime = {
123
209
  fragments,
124
210
  api,
211
+ services,
212
+ registerService: (name, def) => services.register(name, def),
125
213
  forms: {
126
214
  get: getFormHandle,
127
215
  },
128
216
  invalidate: (tags, meta) => fragments.invalidate(tags, meta),
129
217
  refresh: (idOrName, input, opts) => fragments.refresh(idOrName, input, opts),
130
218
  boot,
219
+ destroy,
220
+ dispose: destroy,
131
221
  };
222
+
223
+ return runtime;
132
224
  }
133
225
 
134
226
  /**
@@ -160,5 +252,21 @@ module.exports = {
160
252
  apiResult: (data, meta) => getServerFragments().apiResult(data, meta),
161
253
  isApiResultEnvelope: (val) => getServerFragments().isApiResultEnvelope(val),
162
254
  defineFragment: (def) => getServerFragments().defineFragment(def),
255
+ createClientStorage,
256
+ defaultStorage,
257
+ StorageAdapter,
258
+ MemoryStorage,
259
+ get defineClientService() {
260
+ return getServicesModule().defineClientService;
261
+ },
262
+ get createClientServiceRegistry() {
263
+ return getServicesModule().createClientServiceRegistry;
264
+ },
265
+ get createServiceError() {
266
+ return getServicesModule().createServiceError;
267
+ },
268
+ get createClientHostManager() {
269
+ return getHostModule().createClientHostManager;
270
+ },
163
271
  };
164
272