@legendapp/state 3.0.0-beta.36 → 3.0.0-beta.38

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/index.js CHANGED
@@ -146,6 +146,16 @@ function isEvent(value$) {
146
146
  var _a;
147
147
  return value$ && ((_a = value$[symbolGetNode]) == null ? void 0 : _a.isEvent);
148
148
  }
149
+ function setNewValue(parentValue, key, newValue, useSetFn, useMapFn) {
150
+ if (useSetFn) {
151
+ parentValue.add(newValue);
152
+ } else if (useMapFn) {
153
+ parentValue.set(key, newValue);
154
+ } else {
155
+ parentValue[key] = newValue;
156
+ }
157
+ return newValue;
158
+ }
149
159
  function setNodeValue(node, newValue) {
150
160
  var _a;
151
161
  const parentNode = (_a = node.parent) != null ? _a : node;
@@ -169,13 +179,7 @@ function setNodeValue(node, newValue) {
169
179
  delete parentValue[key];
170
180
  }
171
181
  } else {
172
- if (useSetFn) {
173
- parentValue.add(newValue);
174
- } else if (useMapFn) {
175
- parentValue.set(key, newValue);
176
- } else {
177
- parentValue[key] = newValue;
178
- }
182
+ setNewValue(parentValue, key, newValue, useSetFn, useMapFn);
179
183
  }
180
184
  } finally {
181
185
  parentNode.isSetting--;
@@ -194,7 +198,7 @@ function getNodeValue(node) {
194
198
  let child = node.root._;
195
199
  for (let i = count - 1; child && i >= 0; i--) {
196
200
  const key = arrNodeKeys[i];
197
- child = key !== "size" && (isMap(child) || child instanceof WeakMap) ? child.get(key) : child[key];
201
+ child = key !== "size" && isMap(child) ? child.get(key) : child[key];
198
202
  }
199
203
  return child;
200
204
  }
@@ -229,7 +233,9 @@ function ensureNodeValue(node) {
229
233
  if (!value || isFunction(value)) {
230
234
  if (isChildNode(node)) {
231
235
  const parent = ensureNodeValue(node.parent);
232
- value = parent[node.key] = {};
236
+ const useSetFn = isSet(parent);
237
+ const useMapFn = isMap(parent);
238
+ value = setNewValue(parent, node.key, {}, useSetFn, useMapFn);
233
239
  } else {
234
240
  value = node.root._ = {};
235
241
  }
package/index.mjs CHANGED
@@ -144,6 +144,16 @@ function isEvent(value$) {
144
144
  var _a;
145
145
  return value$ && ((_a = value$[symbolGetNode]) == null ? void 0 : _a.isEvent);
146
146
  }
147
+ function setNewValue(parentValue, key, newValue, useSetFn, useMapFn) {
148
+ if (useSetFn) {
149
+ parentValue.add(newValue);
150
+ } else if (useMapFn) {
151
+ parentValue.set(key, newValue);
152
+ } else {
153
+ parentValue[key] = newValue;
154
+ }
155
+ return newValue;
156
+ }
147
157
  function setNodeValue(node, newValue) {
148
158
  var _a;
149
159
  const parentNode = (_a = node.parent) != null ? _a : node;
@@ -167,13 +177,7 @@ function setNodeValue(node, newValue) {
167
177
  delete parentValue[key];
168
178
  }
169
179
  } else {
170
- if (useSetFn) {
171
- parentValue.add(newValue);
172
- } else if (useMapFn) {
173
- parentValue.set(key, newValue);
174
- } else {
175
- parentValue[key] = newValue;
176
- }
180
+ setNewValue(parentValue, key, newValue, useSetFn, useMapFn);
177
181
  }
178
182
  } finally {
179
183
  parentNode.isSetting--;
@@ -192,7 +196,7 @@ function getNodeValue(node) {
192
196
  let child = node.root._;
193
197
  for (let i = count - 1; child && i >= 0; i--) {
194
198
  const key = arrNodeKeys[i];
195
- child = key !== "size" && (isMap(child) || child instanceof WeakMap) ? child.get(key) : child[key];
199
+ child = key !== "size" && isMap(child) ? child.get(key) : child[key];
196
200
  }
197
201
  return child;
198
202
  }
@@ -227,7 +231,9 @@ function ensureNodeValue(node) {
227
231
  if (!value || isFunction(value)) {
228
232
  if (isChildNode(node)) {
229
233
  const parent = ensureNodeValue(node.parent);
230
- value = parent[node.key] = {};
234
+ const useSetFn = isSet(parent);
235
+ const useMapFn = isMap(parent);
236
+ value = setNewValue(parent, node.key, {}, useSetFn, useMapFn);
231
237
  } else {
232
238
  value = node.root._ = {};
233
239
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-beta.36",
3
+ "version": "3.0.0-beta.38",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
@@ -5,6 +5,12 @@ var state = require('@legendapp/state');
5
5
  // src/persist-plugins/indexeddb.ts
6
6
  var MetadataSuffix = "__legend_metadata";
7
7
  var PrimitiveName = "__legend_primitive";
8
+ function getIndexedDB() {
9
+ if (typeof globalThis === "undefined") {
10
+ return void 0;
11
+ }
12
+ return globalThis.indexedDB;
13
+ }
8
14
  function requestToPromise(request) {
9
15
  return new Promise((resolve) => request.onsuccess = () => resolve());
10
16
  }
@@ -21,8 +27,9 @@ var ObservablePersistIndexedDB = class {
21
27
  }
22
28
  async initialize(configOptions) {
23
29
  const config = this.configuration || configOptions.indexedDB;
24
- if (typeof indexedDB === "undefined")
30
+ if (!getIndexedDB()) {
25
31
  return;
32
+ }
26
33
  if (process.env.NODE_ENV === "development" && !config) {
27
34
  console.error("[legend-state] Must configure ObservablePersistIndexedDB");
28
35
  }
@@ -69,6 +76,9 @@ var ObservablePersistIndexedDB = class {
69
76
  loadTable(table, config) {
70
77
  var _a;
71
78
  if (!this.db) {
79
+ if (!getIndexedDB()) {
80
+ return;
81
+ }
72
82
  throw new Error(
73
83
  "[legend-state] ObservablePersistIndexedDB loading without being initialized. This may happen when running outside of a browser."
74
84
  );
@@ -147,7 +157,7 @@ var ObservablePersistIndexedDB = class {
147
157
  }
148
158
  async set(table, changes, config) {
149
159
  var _a, _b;
150
- if (typeof indexedDB === "undefined")
160
+ if (!getIndexedDB())
151
161
  return;
152
162
  if (!this.pendingSaves.has(config)) {
153
163
  this.pendingSaves.set(config, {});
@@ -238,7 +248,7 @@ var ObservablePersistIndexedDB = class {
238
248
  delete this.tableData[tableName];
239
249
  delete this.tableData[tableName + "_transformed"];
240
250
  }
241
- if (typeof indexedDB === "undefined")
251
+ if (!getIndexedDB())
242
252
  return;
243
253
  this.deleteMetadata(table, config);
244
254
  if (data) {
@@ -3,6 +3,12 @@ import { when, isPromise, observable, setAtPath, isPrimitive } from '@legendapp/
3
3
  // src/persist-plugins/indexeddb.ts
4
4
  var MetadataSuffix = "__legend_metadata";
5
5
  var PrimitiveName = "__legend_primitive";
6
+ function getIndexedDB() {
7
+ if (typeof globalThis === "undefined") {
8
+ return void 0;
9
+ }
10
+ return globalThis.indexedDB;
11
+ }
6
12
  function requestToPromise(request) {
7
13
  return new Promise((resolve) => request.onsuccess = () => resolve());
8
14
  }
@@ -19,8 +25,9 @@ var ObservablePersistIndexedDB = class {
19
25
  }
20
26
  async initialize(configOptions) {
21
27
  const config = this.configuration || configOptions.indexedDB;
22
- if (typeof indexedDB === "undefined")
28
+ if (!getIndexedDB()) {
23
29
  return;
30
+ }
24
31
  if (process.env.NODE_ENV === "development" && !config) {
25
32
  console.error("[legend-state] Must configure ObservablePersistIndexedDB");
26
33
  }
@@ -67,6 +74,9 @@ var ObservablePersistIndexedDB = class {
67
74
  loadTable(table, config) {
68
75
  var _a;
69
76
  if (!this.db) {
77
+ if (!getIndexedDB()) {
78
+ return;
79
+ }
70
80
  throw new Error(
71
81
  "[legend-state] ObservablePersistIndexedDB loading without being initialized. This may happen when running outside of a browser."
72
82
  );
@@ -145,7 +155,7 @@ var ObservablePersistIndexedDB = class {
145
155
  }
146
156
  async set(table, changes, config) {
147
157
  var _a, _b;
148
- if (typeof indexedDB === "undefined")
158
+ if (!getIndexedDB())
149
159
  return;
150
160
  if (!this.pendingSaves.has(config)) {
151
161
  this.pendingSaves.set(config, {});
@@ -236,7 +246,7 @@ var ObservablePersistIndexedDB = class {
236
246
  delete this.tableData[tableName];
237
247
  delete this.tableData[tableName + "_transformed"];
238
248
  }
239
- if (typeof indexedDB === "undefined")
249
+ if (!getIndexedDB())
240
250
  return;
241
251
  this.deleteMetadata(table, config);
242
252
  if (data) {
package/react-web.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { FCReactiveObject } from '@legendapp/state/react';
2
+ import { JSX } from 'react';
2
3
 
3
4
  type IReactive = FCReactiveObject<JSX.IntrinsicElements>;
4
5
  declare const $React: IReactive;
package/react-web.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { FCReactiveObject } from '@legendapp/state/react';
2
+ import { JSX } from 'react';
2
3
 
3
4
  type IReactive = FCReactiveObject<JSX.IntrinsicElements>;
4
5
  declare const $React: IReactive;
@@ -11,7 +11,8 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
11
11
  type IsUnionOfStrings<T> = [T] extends [string] ? ([T] extends [UnionToIntersection<T>] ? false : true) : false;
12
12
  type SupabaseSchemaOf<Client extends SupabaseClient> = Client extends SupabaseClient<infer _, infer __, infer Schema> ? Schema : never;
13
13
  type SupabaseTableOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = DatabaseOf<Client>[SchemaName]['Tables'];
14
- type SupabaseCollectionOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = keyof SupabaseTableOf<Client, IsUnionOfStrings<SchemaName> extends true ? 'public' : SchemaName>;
14
+ type SupabaseViewOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = DatabaseOf<Client>[SchemaName]['Views'];
15
+ type SupabaseCollectionOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = keyof SupabaseTableOf<Client, IsUnionOfStrings<SchemaName> extends true ? 'public' : SchemaName> | keyof SupabaseViewOf<Client, IsUnionOfStrings<SchemaName> extends true ? 'public' : SchemaName>;
15
16
  type SupabaseRowOf<Client extends SupabaseClient, Collection extends SupabaseCollectionOf<Client, SchemaName>, SchemaName extends SchemaNameOf<Client>> = SupabaseTableOf<Client, SchemaName>[Collection]['Row'];
16
17
  type SyncedSupabaseConfig<TRemote extends {
17
18
  id: string | number;
@@ -50,4 +51,4 @@ declare function configureSyncedSupabase(config: SyncedSupabaseConfiguration): v
50
51
  declare function syncedSupabase<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName> & string, SchemaName extends SchemaNameOf<Client> = 'public', AsOption extends CrudAsOption = 'object', TRemote = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote>(props: SyncedSupabasePropsWithSelect<Client, Collection, SchemaName, AsOption, TRemote, TLocal>): SyncedCrudReturnType<TLocal, AsOption>;
51
52
  declare function syncedSupabase<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName> & string, SchemaName extends SchemaNameOf<Client> = 'public', AsOption extends CrudAsOption = 'object', TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote>(props: SyncedSupabaseProps<Client, Collection, SchemaName, AsOption, TRemote, TLocal>): SyncedCrudReturnType<TLocal, AsOption>;
52
53
 
53
- export { type SupabaseCollectionOf, type SupabaseRowOf, type SupabaseSchemaOf, type SupabaseTableOf, type SyncedSupabaseConfig, type SyncedSupabaseConfiguration, configureSyncedSupabase, getSyncedSupabaseConfiguration, syncedSupabase };
54
+ export { type SupabaseCollectionOf, type SupabaseRowOf, type SupabaseSchemaOf, type SupabaseTableOf, type SupabaseViewOf, type SyncedSupabaseConfig, type SyncedSupabaseConfiguration, configureSyncedSupabase, getSyncedSupabaseConfiguration, syncedSupabase };
@@ -11,7 +11,8 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
11
11
  type IsUnionOfStrings<T> = [T] extends [string] ? ([T] extends [UnionToIntersection<T>] ? false : true) : false;
12
12
  type SupabaseSchemaOf<Client extends SupabaseClient> = Client extends SupabaseClient<infer _, infer __, infer Schema> ? Schema : never;
13
13
  type SupabaseTableOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = DatabaseOf<Client>[SchemaName]['Tables'];
14
- type SupabaseCollectionOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = keyof SupabaseTableOf<Client, IsUnionOfStrings<SchemaName> extends true ? 'public' : SchemaName>;
14
+ type SupabaseViewOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = DatabaseOf<Client>[SchemaName]['Views'];
15
+ type SupabaseCollectionOf<Client extends SupabaseClient, SchemaName extends SchemaNameOf<Client>> = keyof SupabaseTableOf<Client, IsUnionOfStrings<SchemaName> extends true ? 'public' : SchemaName> | keyof SupabaseViewOf<Client, IsUnionOfStrings<SchemaName> extends true ? 'public' : SchemaName>;
15
16
  type SupabaseRowOf<Client extends SupabaseClient, Collection extends SupabaseCollectionOf<Client, SchemaName>, SchemaName extends SchemaNameOf<Client>> = SupabaseTableOf<Client, SchemaName>[Collection]['Row'];
16
17
  type SyncedSupabaseConfig<TRemote extends {
17
18
  id: string | number;
@@ -50,4 +51,4 @@ declare function configureSyncedSupabase(config: SyncedSupabaseConfiguration): v
50
51
  declare function syncedSupabase<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName> & string, SchemaName extends SchemaNameOf<Client> = 'public', AsOption extends CrudAsOption = 'object', TRemote = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote>(props: SyncedSupabasePropsWithSelect<Client, Collection, SchemaName, AsOption, TRemote, TLocal>): SyncedCrudReturnType<TLocal, AsOption>;
51
52
  declare function syncedSupabase<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName> & string, SchemaName extends SchemaNameOf<Client> = 'public', AsOption extends CrudAsOption = 'object', TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote>(props: SyncedSupabaseProps<Client, Collection, SchemaName, AsOption, TRemote, TLocal>): SyncedCrudReturnType<TLocal, AsOption>;
52
53
 
53
- export { type SupabaseCollectionOf, type SupabaseRowOf, type SupabaseSchemaOf, type SupabaseTableOf, type SyncedSupabaseConfig, type SyncedSupabaseConfiguration, configureSyncedSupabase, getSyncedSupabaseConfiguration, syncedSupabase };
54
+ export { type SupabaseCollectionOf, type SupabaseRowOf, type SupabaseSchemaOf, type SupabaseTableOf, type SupabaseViewOf, type SyncedSupabaseConfig, type SyncedSupabaseConfiguration, configureSyncedSupabase, getSyncedSupabaseConfiguration, syncedSupabase };
package/sync.js CHANGED
@@ -957,6 +957,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
957
957
  };
958
958
  const { get, subscribe } = syncOptions;
959
959
  if (get || subscribe) {
960
+ const callSync = () => sync();
960
961
  sync = async (options) => {
961
962
  if (isSynced && (!getNodeValue(getNode(syncState$)).isSyncEnabled || state.shouldIgnoreUnobserved(node, sync))) {
962
963
  if (unsubscribe) {
@@ -1095,7 +1096,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
1095
1096
  }
1096
1097
  );
1097
1098
  },
1098
- refresh: () => state.when(syncState$.isLoaded, () => sync()),
1099
+ refresh: () => state.when(syncState$.isLoaded, callSync),
1099
1100
  onError: (error) => onGetError(error, {
1100
1101
  source: "subscribe",
1101
1102
  subscribeParams,
@@ -1205,9 +1206,9 @@ function syncObservable(obs$, syncOptionsOrSynced) {
1205
1206
  }
1206
1207
  };
1207
1208
  if (waitFor) {
1208
- state.whenReady(waitFor, () => state.trackSelector(runGet, () => sync()));
1209
+ state.whenReady(waitFor, () => state.trackSelector(runGet, callSync));
1209
1210
  } else {
1210
- state.trackSelector(runGet, () => sync());
1211
+ state.trackSelector(runGet, callSync);
1211
1212
  }
1212
1213
  if (!isSynced) {
1213
1214
  isSynced = true;
package/sync.mjs CHANGED
@@ -955,6 +955,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
955
955
  };
956
956
  const { get, subscribe } = syncOptions;
957
957
  if (get || subscribe) {
958
+ const callSync = () => sync();
958
959
  sync = async (options) => {
959
960
  if (isSynced && (!getNodeValue(getNode(syncState$)).isSyncEnabled || shouldIgnoreUnobserved(node, sync))) {
960
961
  if (unsubscribe) {
@@ -1093,7 +1094,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
1093
1094
  }
1094
1095
  );
1095
1096
  },
1096
- refresh: () => when(syncState$.isLoaded, () => sync()),
1097
+ refresh: () => when(syncState$.isLoaded, callSync),
1097
1098
  onError: (error) => onGetError(error, {
1098
1099
  source: "subscribe",
1099
1100
  subscribeParams,
@@ -1203,9 +1204,9 @@ function syncObservable(obs$, syncOptionsOrSynced) {
1203
1204
  }
1204
1205
  };
1205
1206
  if (waitFor) {
1206
- whenReady(waitFor, () => trackSelector(runGet, () => sync()));
1207
+ whenReady(waitFor, () => trackSelector(runGet, callSync));
1207
1208
  } else {
1208
- trackSelector(runGet, () => sync());
1209
+ trackSelector(runGet, callSync);
1209
1210
  }
1210
1211
  if (!isSynced) {
1211
1212
  isSynced = true;