@koolbase/core 12.37.0 → 12.38.1

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.
@@ -875,6 +875,9 @@ class KoolbaseDatabase {
875
875
  await (0, cache_store_js_1.cacheRecord)(userId, base.collection, recordId, merged, base.revision);
876
876
  // And in the saved lists, so they show what the user just changed.
877
877
  await (0, cache_store_js_2.updateInCachedQueries)(userId, base.collection, recordId, data);
878
+ // What shows it hears of it, as after an online write (R125): a shown
879
+ // record reads its saved copy, which holds the change.
880
+ void (0, query_refresh_js_1.refreshCollectionQueries)(base.collection);
878
881
  // Optimistic: durable locally and queued to send, not yet accepted.
879
882
  return {
880
883
  id: recordId,
@@ -1,6 +1,7 @@
1
1
  export * from './types.js';
2
2
  export * from './errors.js';
3
3
  export * from './network.js';
4
+ export * from './memory-auth-storage.js';
4
5
  export * from './aggregate.js';
5
6
  export * from './conflict.js';
6
7
  export * from './pending-write.js';
package/dist/cjs/index.js CHANGED
@@ -23,6 +23,7 @@ exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.Res
23
23
  __exportStar(require("./types.js"), exports);
24
24
  __exportStar(require("./errors.js"), exports);
25
25
  __exportStar(require("./network.js"), exports);
26
+ __exportStar(require("./memory-auth-storage.js"), exports);
26
27
  __exportStar(require("./aggregate.js"), exports);
27
28
  __exportStar(require("./conflict.js"), exports);
28
29
  __exportStar(require("./pending-write.js"), exports);
@@ -0,0 +1,12 @@
1
+ import type { KoolbaseAuthStorage, KoolbaseSession } from './types.js';
2
+ /**
3
+ * A session kept in memory only (R49): nothing survives the process. For
4
+ * tests -- Koolbase.initializeForTesting uses it -- and for a host that must
5
+ * not persist a session. The twin of the Flutter SDK's InMemoryAuthStorage.
6
+ */
7
+ export declare class MemoryAuthStorage implements KoolbaseAuthStorage {
8
+ private session;
9
+ saveSession(session: KoolbaseSession): Promise<void>;
10
+ readSession(): Promise<KoolbaseSession | null>;
11
+ clear(): Promise<void>;
12
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryAuthStorage = void 0;
4
+ /**
5
+ * A session kept in memory only (R49): nothing survives the process. For
6
+ * tests -- Koolbase.initializeForTesting uses it -- and for a host that must
7
+ * not persist a session. The twin of the Flutter SDK's InMemoryAuthStorage.
8
+ */
9
+ class MemoryAuthStorage {
10
+ constructor() {
11
+ this.session = null;
12
+ }
13
+ async saveSession(session) {
14
+ this.session = session;
15
+ }
16
+ async readSession() {
17
+ return this.session;
18
+ }
19
+ async clear() {
20
+ this.session = null;
21
+ }
22
+ }
23
+ exports.MemoryAuthStorage = MemoryAuthStorage;
@@ -1,3 +1,4 @@
1
+ import type { FetchLike } from './types.js';
1
2
  /**
2
3
  * Network failures, made actionable.
3
4
  *
@@ -17,6 +18,14 @@ export declare class KoolbaseNetworkError extends TypeError {
17
18
  readonly origin?: string;
18
19
  constructor(url: string, cause: unknown);
19
20
  }
21
+ /**
22
+ * Installed by Koolbase.initialize from KoolbaseConfig.fetch: the fetch every
23
+ * JSON request the SDK makes goes through -- a proxy, a logger, or a test's
24
+ * own server (initializeForTesting). Null is the global fetch, read at each
25
+ * call. File bytes sent to a storage URL are not JSON requests and use the
26
+ * global fetch.
27
+ */
28
+ export declare function setFetch(fn: FetchLike | null): void;
20
29
  /** fetch, with network failures turned into a KoolbaseNetworkError, and a
21
30
  * failed answer traced in a development build (#169). */
22
31
  export declare function koolbaseFetch(input: string, init?: RequestInit): Promise<Response>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.KoolbaseNetworkError = void 0;
4
+ exports.setFetch = setFetch;
4
5
  exports.koolbaseFetch = koolbaseFetch;
5
6
  exports.setSessionRenewal = setSessionRenewal;
6
7
  exports.sessionFetch = sessionFetch;
@@ -42,11 +43,23 @@ class KoolbaseNetworkError extends TypeError {
42
43
  }
43
44
  }
44
45
  exports.KoolbaseNetworkError = KoolbaseNetworkError;
46
+ // ─── The fetch every JSON request goes through (R49) ───────────────────
47
+ let installedFetch = null;
48
+ /**
49
+ * Installed by Koolbase.initialize from KoolbaseConfig.fetch: the fetch every
50
+ * JSON request the SDK makes goes through -- a proxy, a logger, or a test's
51
+ * own server (initializeForTesting). Null is the global fetch, read at each
52
+ * call. File bytes sent to a storage URL are not JSON requests and use the
53
+ * global fetch.
54
+ */
55
+ function setFetch(fn) {
56
+ installedFetch = fn;
57
+ }
45
58
  /** fetch, with network failures turned into a KoolbaseNetworkError. Not
46
59
  * logged: sessionFetch's first attempt may yet be answered by renewal. */
47
60
  async function sendOnce(input, init) {
48
61
  try {
49
- return await fetch(input, init);
62
+ return await (installedFetch ?? fetch)(input, init);
50
63
  }
51
64
  catch (e) {
52
65
  // A request the caller cancelled keeps its meaning.
@@ -28,6 +28,7 @@ export declare class KoolbaseRecordController {
28
28
  private readonly live;
29
29
  private unlive;
30
30
  private liveTimer;
31
+ private unregister;
31
32
  constructor(db: RecordReader, collection: string, id: string, options?: {
32
33
  live?: boolean;
33
34
  });
@@ -40,6 +41,14 @@ export declare class KoolbaseRecordController {
40
41
  private showSaved;
41
42
  /** The record again, from the server. What is shown stays while it runs, and stays if it fails. */
42
43
  refresh(): Promise<void>;
44
+ /**
45
+ * After the app's own write to this collection (R125): the device's saved
46
+ * copy at once -- which already holds the change, made online or queued
47
+ * offline -- then the server's, SILENTLY (no `refreshing`). A read still in
48
+ * flight is superseded; if the server cannot be reached, the saved copy
49
+ * stays.
50
+ */
51
+ private refreshAfterWrite;
43
52
  /** Stops all updates. A result still in flight is dropped when it lands. */
44
53
  dispose(): void;
45
54
  /**
@@ -23,6 +23,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.KoolbaseRecordController = exports.initialRecordState = void 0;
24
24
  const database_errors_js_1 = require("./database-errors.js");
25
25
  const live_source_js_1 = require("./live-source.js");
26
+ const query_refresh_js_1 = require("./query-refresh.js");
26
27
  exports.initialRecordState = Object.freeze({
27
28
  status: 'loading',
28
29
  record: null,
@@ -39,6 +40,9 @@ class KoolbaseRecordController {
39
40
  // The realtime subscription, and its pending debounced re-read.
40
41
  this.unlive = null;
41
42
  this.liveTimer = null;
43
+ // Registered for the app's own writes to this collection (R125), as a
44
+ // collection controller is.
45
+ this.unregister = null;
42
46
  this.db = db;
43
47
  this.collection = collection;
44
48
  this.id = id;
@@ -64,6 +68,8 @@ class KoolbaseRecordController {
64
68
  if (this.live && this.id) {
65
69
  this.unlive ?? (this.unlive = (0, live_source_js_1.liveSource)()?.subscribe(this.collection, (event) => this.onLive(event)) ?? null);
66
70
  }
71
+ if (this.id)
72
+ this.unregister ?? (this.unregister = (0, query_refresh_js_1.registerQueryRefresher)(this.collection, () => this.refreshAfterWrite()));
67
73
  await this.showSaved();
68
74
  await this.fetch();
69
75
  }
@@ -94,9 +100,37 @@ class KoolbaseRecordController {
94
100
  if (!this.isStale(gen))
95
101
  this.set({ refreshing: false });
96
102
  }
103
+ /**
104
+ * After the app's own write to this collection (R125): the device's saved
105
+ * copy at once -- which already holds the change, made online or queued
106
+ * offline -- then the server's, SILENTLY (no `refreshing`). A read still in
107
+ * flight is superseded; if the server cannot be reached, the saved copy
108
+ * stays.
109
+ */
110
+ async refreshAfterWrite() {
111
+ if (this.disposed || !this.id)
112
+ return;
113
+ const gen = ++this.generation;
114
+ if (this.db.getSaved) {
115
+ try {
116
+ const saved = await this.db.getSaved(this.id);
117
+ if (saved && !this.isStale(gen) && (saved.collection === undefined || saved.collection === this.collection)) {
118
+ this.set({ status: 'loaded', record: saved, error: null, isSaved: true });
119
+ }
120
+ }
121
+ catch {
122
+ // No saved copy to show: the server's answer decides.
123
+ }
124
+ }
125
+ if (this.isStale(gen))
126
+ return;
127
+ await this.fetch();
128
+ }
97
129
  /** Stops all updates. A result still in flight is dropped when it lands. */
98
130
  dispose() {
99
131
  this.disposed = true;
132
+ this.unregister?.();
133
+ this.unregister = null;
100
134
  this.unlive?.();
101
135
  this.unlive = null;
102
136
  if (this.liveTimer) {
@@ -872,6 +872,9 @@ export class KoolbaseDatabase {
872
872
  await cacheRecord(userId, base.collection, recordId, merged, base.revision);
873
873
  // And in the saved lists, so they show what the user just changed.
874
874
  await updateInCachedQueries(userId, base.collection, recordId, data);
875
+ // What shows it hears of it, as after an online write (R125): a shown
876
+ // record reads its saved copy, which holds the change.
877
+ void refreshCollectionQueries(base.collection);
875
878
  // Optimistic: durable locally and queued to send, not yet accepted.
876
879
  return {
877
880
  id: recordId,
@@ -1,6 +1,7 @@
1
1
  export * from './types.js';
2
2
  export * from './errors.js';
3
3
  export * from './network.js';
4
+ export * from './memory-auth-storage.js';
4
5
  export * from './aggregate.js';
5
6
  export * from './conflict.js';
6
7
  export * from './pending-write.js';
package/dist/esm/index.js CHANGED
@@ -6,6 +6,7 @@
6
6
  export * from './types.js';
7
7
  export * from './errors.js';
8
8
  export * from './network.js';
9
+ export * from './memory-auth-storage.js';
9
10
  export * from './aggregate.js';
10
11
  export * from './conflict.js';
11
12
  export * from './pending-write.js';
@@ -0,0 +1,12 @@
1
+ import type { KoolbaseAuthStorage, KoolbaseSession } from './types.js';
2
+ /**
3
+ * A session kept in memory only (R49): nothing survives the process. For
4
+ * tests -- Koolbase.initializeForTesting uses it -- and for a host that must
5
+ * not persist a session. The twin of the Flutter SDK's InMemoryAuthStorage.
6
+ */
7
+ export declare class MemoryAuthStorage implements KoolbaseAuthStorage {
8
+ private session;
9
+ saveSession(session: KoolbaseSession): Promise<void>;
10
+ readSession(): Promise<KoolbaseSession | null>;
11
+ clear(): Promise<void>;
12
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A session kept in memory only (R49): nothing survives the process. For
3
+ * tests -- Koolbase.initializeForTesting uses it -- and for a host that must
4
+ * not persist a session. The twin of the Flutter SDK's InMemoryAuthStorage.
5
+ */
6
+ export class MemoryAuthStorage {
7
+ constructor() {
8
+ this.session = null;
9
+ }
10
+ async saveSession(session) {
11
+ this.session = session;
12
+ }
13
+ async readSession() {
14
+ return this.session;
15
+ }
16
+ async clear() {
17
+ this.session = null;
18
+ }
19
+ }
@@ -1,3 +1,4 @@
1
+ import type { FetchLike } from './types.js';
1
2
  /**
2
3
  * Network failures, made actionable.
3
4
  *
@@ -17,6 +18,14 @@ export declare class KoolbaseNetworkError extends TypeError {
17
18
  readonly origin?: string;
18
19
  constructor(url: string, cause: unknown);
19
20
  }
21
+ /**
22
+ * Installed by Koolbase.initialize from KoolbaseConfig.fetch: the fetch every
23
+ * JSON request the SDK makes goes through -- a proxy, a logger, or a test's
24
+ * own server (initializeForTesting). Null is the global fetch, read at each
25
+ * call. File bytes sent to a storage URL are not JSON requests and use the
26
+ * global fetch.
27
+ */
28
+ export declare function setFetch(fn: FetchLike | null): void;
20
29
  /** fetch, with network failures turned into a KoolbaseNetworkError, and a
21
30
  * failed answer traced in a development build (#169). */
22
31
  export declare function koolbaseFetch(input: string, init?: RequestInit): Promise<Response>;
@@ -35,11 +35,23 @@ export class KoolbaseNetworkError extends TypeError {
35
35
  Object.setPrototypeOf(this, new.target.prototype);
36
36
  }
37
37
  }
38
+ // ─── The fetch every JSON request goes through (R49) ───────────────────
39
+ let installedFetch = null;
40
+ /**
41
+ * Installed by Koolbase.initialize from KoolbaseConfig.fetch: the fetch every
42
+ * JSON request the SDK makes goes through -- a proxy, a logger, or a test's
43
+ * own server (initializeForTesting). Null is the global fetch, read at each
44
+ * call. File bytes sent to a storage URL are not JSON requests and use the
45
+ * global fetch.
46
+ */
47
+ export function setFetch(fn) {
48
+ installedFetch = fn;
49
+ }
38
50
  /** fetch, with network failures turned into a KoolbaseNetworkError. Not
39
51
  * logged: sessionFetch's first attempt may yet be answered by renewal. */
40
52
  async function sendOnce(input, init) {
41
53
  try {
42
- return await fetch(input, init);
54
+ return await (installedFetch ?? fetch)(input, init);
43
55
  }
44
56
  catch (e) {
45
57
  // A request the caller cancelled keeps its meaning.
@@ -28,6 +28,7 @@ export declare class KoolbaseRecordController {
28
28
  private readonly live;
29
29
  private unlive;
30
30
  private liveTimer;
31
+ private unregister;
31
32
  constructor(db: RecordReader, collection: string, id: string, options?: {
32
33
  live?: boolean;
33
34
  });
@@ -40,6 +41,14 @@ export declare class KoolbaseRecordController {
40
41
  private showSaved;
41
42
  /** The record again, from the server. What is shown stays while it runs, and stays if it fails. */
42
43
  refresh(): Promise<void>;
44
+ /**
45
+ * After the app's own write to this collection (R125): the device's saved
46
+ * copy at once -- which already holds the change, made online or queued
47
+ * offline -- then the server's, SILENTLY (no `refreshing`). A read still in
48
+ * flight is superseded; if the server cannot be reached, the saved copy
49
+ * stays.
50
+ */
51
+ private refreshAfterWrite;
43
52
  /** Stops all updates. A result still in flight is dropped when it lands. */
44
53
  dispose(): void;
45
54
  /**
@@ -20,6 +20,7 @@
20
20
  // found is notFound. No saved copy: the ordinary loading/error states.
21
21
  import { KoolbaseNotFoundError } from './database-errors.js';
22
22
  import { liveSource } from './live-source.js';
23
+ import { registerQueryRefresher } from './query-refresh.js';
23
24
  export const initialRecordState = Object.freeze({
24
25
  status: 'loading',
25
26
  record: null,
@@ -36,6 +37,9 @@ export class KoolbaseRecordController {
36
37
  // The realtime subscription, and its pending debounced re-read.
37
38
  this.unlive = null;
38
39
  this.liveTimer = null;
40
+ // Registered for the app's own writes to this collection (R125), as a
41
+ // collection controller is.
42
+ this.unregister = null;
39
43
  this.db = db;
40
44
  this.collection = collection;
41
45
  this.id = id;
@@ -61,6 +65,8 @@ export class KoolbaseRecordController {
61
65
  if (this.live && this.id) {
62
66
  this.unlive ?? (this.unlive = liveSource()?.subscribe(this.collection, (event) => this.onLive(event)) ?? null);
63
67
  }
68
+ if (this.id)
69
+ this.unregister ?? (this.unregister = registerQueryRefresher(this.collection, () => this.refreshAfterWrite()));
64
70
  await this.showSaved();
65
71
  await this.fetch();
66
72
  }
@@ -91,9 +97,37 @@ export class KoolbaseRecordController {
91
97
  if (!this.isStale(gen))
92
98
  this.set({ refreshing: false });
93
99
  }
100
+ /**
101
+ * After the app's own write to this collection (R125): the device's saved
102
+ * copy at once -- which already holds the change, made online or queued
103
+ * offline -- then the server's, SILENTLY (no `refreshing`). A read still in
104
+ * flight is superseded; if the server cannot be reached, the saved copy
105
+ * stays.
106
+ */
107
+ async refreshAfterWrite() {
108
+ if (this.disposed || !this.id)
109
+ return;
110
+ const gen = ++this.generation;
111
+ if (this.db.getSaved) {
112
+ try {
113
+ const saved = await this.db.getSaved(this.id);
114
+ if (saved && !this.isStale(gen) && (saved.collection === undefined || saved.collection === this.collection)) {
115
+ this.set({ status: 'loaded', record: saved, error: null, isSaved: true });
116
+ }
117
+ }
118
+ catch {
119
+ // No saved copy to show: the server's answer decides.
120
+ }
121
+ }
122
+ if (this.isStale(gen))
123
+ return;
124
+ await this.fetch();
125
+ }
94
126
  /** Stops all updates. A result still in flight is dropped when it lands. */
95
127
  dispose() {
96
128
  this.disposed = true;
129
+ this.unregister?.();
130
+ this.unregister = null;
97
131
  this.unlive?.();
98
132
  this.unlive = null;
99
133
  if (this.liveTimer) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.37.0",
3
+ "version": "12.38.1",
4
4
  "description": "Koolbase SDK core \u2014 shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/esm/index.d.ts",