@legendapp/state 3.0.0-alpha.16 → 3.0.0-alpha.18

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.d.mts CHANGED
@@ -394,8 +394,8 @@ declare const internal: {
394
394
  };
395
395
  pendingNodes: Map<NodeValue, () => void>;
396
396
  dirtyNodes: Set<NodeValue>;
397
- replacer: ((this: any, key: string, value: any) => any) | undefined;
398
- reviver: ((this: any, key: string, value: any) => any) | undefined;
397
+ replacer: undefined | ((this: any, key: string, value: any) => any);
398
+ reviver: undefined | ((this: any, key: string, value: any) => any);
399
399
  };
400
400
  initializePathType: typeof initializePathType;
401
401
  observableFns: Map<string, (node: NodeValue, ...args: any[]) => any>;
package/index.d.ts CHANGED
@@ -394,8 +394,8 @@ declare const internal: {
394
394
  };
395
395
  pendingNodes: Map<NodeValue, () => void>;
396
396
  dirtyNodes: Set<NodeValue>;
397
- replacer: ((this: any, key: string, value: any) => any) | undefined;
398
- reviver: ((this: any, key: string, value: any) => any) | undefined;
397
+ replacer: undefined | ((this: any, key: string, value: any) => any);
398
+ reviver: undefined | ((this: any, key: string, value: any) => any);
399
399
  };
400
400
  initializePathType: typeof initializePathType;
401
401
  observableFns: Map<string, (node: NodeValue, ...args: any[]) => any>;
package/index.js CHANGED
@@ -38,7 +38,7 @@ function isSet(obj) {
38
38
  }
39
39
  function isNumber(obj) {
40
40
  const n = obj;
41
- return n - n < 1;
41
+ return typeof n === "number" && n - n < 1;
42
42
  }
43
43
  function isEmpty(obj) {
44
44
  if (!obj)
package/index.mjs CHANGED
@@ -36,7 +36,7 @@ function isSet(obj) {
36
36
  }
37
37
  function isNumber(obj) {
38
38
  const n = obj;
39
- return n - n < 1;
39
+ return typeof n === "number" && n - n < 1;
40
40
  }
41
41
  function isEmpty(obj) {
42
42
  if (!obj)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-alpha.16",
3
+ "version": "3.0.0-alpha.18",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
@@ -216,7 +216,10 @@ function syncedCrud(props) {
216
216
  }
217
217
  } else {
218
218
  if (updateFn) {
219
- updates.set(item.id, item);
219
+ updates.set(
220
+ item.id,
221
+ updates.has(item.id) ? Object.assign(updates.get(item.id), item) : item
222
+ );
220
223
  } else {
221
224
  console.log("[legend-state] missing update function");
222
225
  }
@@ -214,7 +214,10 @@ function syncedCrud(props) {
214
214
  }
215
215
  } else {
216
216
  if (updateFn) {
217
- updates.set(item.id, item);
217
+ updates.set(
218
+ item.id,
219
+ updates.has(item.id) ? Object.assign(updates.get(item.id), item) : item
220
+ );
218
221
  } else {
219
222
  console.log("[legend-state] missing update function");
220
223
  }
@@ -1,4 +1,4 @@
1
- import { SyncedGetSetSubscribeBaseParams, SyncedOptions } from '@legendapp/state/sync';
1
+ import { SyncedGetSetSubscribeBaseParams, SyncedOptions, SyncedSetParams } from '@legendapp/state/sync';
2
2
  import { SyncedCrudPropsBase, CrudAsOption, SyncedCrudReturnType, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
3
3
 
4
4
  interface KeelObjectBase {
@@ -55,7 +55,13 @@ interface SyncedKeelConfiguration extends Omit<SyncedCrudPropsBase<any>, keyof S
55
55
  realtimePlugin?: KeelRealtimePlugin;
56
56
  as?: Exclude<CrudAsOption, 'value'>;
57
57
  enabled?: boolean;
58
- onError?: (params: APIResult<any>['error']) => void;
58
+ onError?: (params: {
59
+ type: 'create' | 'update' | 'delete';
60
+ params: SyncedSetParams<any>;
61
+ input: any;
62
+ action: string;
63
+ error: APIResult<any>['error'];
64
+ }) => void;
59
65
  }
60
66
  interface SyncedKeelPropsManyBase<TRemote extends {
61
67
  id: string;
@@ -1,4 +1,4 @@
1
- import { SyncedGetSetSubscribeBaseParams, SyncedOptions } from '@legendapp/state/sync';
1
+ import { SyncedGetSetSubscribeBaseParams, SyncedOptions, SyncedSetParams } from '@legendapp/state/sync';
2
2
  import { SyncedCrudPropsBase, CrudAsOption, SyncedCrudReturnType, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
3
3
 
4
4
  interface KeelObjectBase {
@@ -55,7 +55,13 @@ interface SyncedKeelConfiguration extends Omit<SyncedCrudPropsBase<any>, keyof S
55
55
  realtimePlugin?: KeelRealtimePlugin;
56
56
  as?: Exclude<CrudAsOption, 'value'>;
57
57
  enabled?: boolean;
58
- onError?: (params: APIResult<any>['error']) => void;
58
+ onError?: (params: {
59
+ type: 'create' | 'update' | 'delete';
60
+ params: SyncedSetParams<any>;
61
+ input: any;
62
+ action: string;
63
+ error: APIResult<any>['error'];
64
+ }) => void;
59
65
  }
60
66
  interface SyncedKeelPropsManyBase<TRemote extends {
61
67
  id: string;
@@ -183,7 +183,7 @@ function syncedKeel(props) {
183
183
  }
184
184
  }
185
185
  };
186
- const handleSetError = async (error, params, from) => {
186
+ const handleSetError = async (error, params, input, fn, from) => {
187
187
  var _a, _b, _c;
188
188
  const { retryNum, cancelRetry, update: update2 } = params;
189
189
  if (from === "create" && ((_a = error.message) == null ? void 0 : _a.includes("for the unique")) && ((_b = error.message) == null ? void 0 : _b.includes("must be unique"))) {
@@ -203,7 +203,7 @@ function syncedKeel(props) {
203
203
  cancelRetry();
204
204
  }
205
205
  } else if (error.type === "bad_request") {
206
- (_c = keelConfig.onError) == null ? void 0 : _c.call(keelConfig, error);
206
+ (_c = keelConfig.onError) == null ? void 0 : _c.call(keelConfig, { error, params, input, type: from, action: fn.name || fn.toString() });
207
207
  if (retryNum > 4) {
208
208
  cancelRetry();
209
209
  }
@@ -214,13 +214,15 @@ function syncedKeel(props) {
214
214
  }
215
215
  };
216
216
  const create = createParam ? async (input, params) => {
217
+ console.log(createParam.toString());
217
218
  const { data, error } = await createParam(convertObjectToCreate(input));
218
219
  if (error) {
219
- await handleSetError(error, params, "create");
220
+ await handleSetError(error, params, input, createParam, "create");
220
221
  }
221
222
  return data;
222
223
  } : void 0;
223
224
  const update = updateParam ? async (input, params) => {
225
+ console.log(updateParam.toString());
224
226
  const id = input.id;
225
227
  const values = convertObjectToCreate(input);
226
228
  delete values.id;
@@ -229,7 +231,7 @@ function syncedKeel(props) {
229
231
  if (!state.isEmpty(values)) {
230
232
  const { data, error } = await updateParam({ where: { id }, values });
231
233
  if (error) {
232
- await handleSetError(error, params, "update");
234
+ await handleSetError(error, params, input, updateParam, "update");
233
235
  }
234
236
  return data;
235
237
  }
@@ -237,7 +239,7 @@ function syncedKeel(props) {
237
239
  const deleteFn = deleteParam ? async (value, params) => {
238
240
  const { data, error } = await deleteParam({ id: value.id });
239
241
  if (error) {
240
- await handleSetError(error, params, "delete");
242
+ await handleSetError(error, params, value, deleteParam, "delete");
241
243
  }
242
244
  return data;
243
245
  } : void 0;
@@ -177,7 +177,7 @@ function syncedKeel(props) {
177
177
  }
178
178
  }
179
179
  };
180
- const handleSetError = async (error, params, from) => {
180
+ const handleSetError = async (error, params, input, fn, from) => {
181
181
  var _a, _b, _c;
182
182
  const { retryNum, cancelRetry, update: update2 } = params;
183
183
  if (from === "create" && ((_a = error.message) == null ? void 0 : _a.includes("for the unique")) && ((_b = error.message) == null ? void 0 : _b.includes("must be unique"))) {
@@ -197,7 +197,7 @@ function syncedKeel(props) {
197
197
  cancelRetry();
198
198
  }
199
199
  } else if (error.type === "bad_request") {
200
- (_c = keelConfig.onError) == null ? void 0 : _c.call(keelConfig, error);
200
+ (_c = keelConfig.onError) == null ? void 0 : _c.call(keelConfig, { error, params, input, type: from, action: fn.name || fn.toString() });
201
201
  if (retryNum > 4) {
202
202
  cancelRetry();
203
203
  }
@@ -208,13 +208,15 @@ function syncedKeel(props) {
208
208
  }
209
209
  };
210
210
  const create = createParam ? async (input, params) => {
211
+ console.log(createParam.toString());
211
212
  const { data, error } = await createParam(convertObjectToCreate(input));
212
213
  if (error) {
213
- await handleSetError(error, params, "create");
214
+ await handleSetError(error, params, input, createParam, "create");
214
215
  }
215
216
  return data;
216
217
  } : void 0;
217
218
  const update = updateParam ? async (input, params) => {
219
+ console.log(updateParam.toString());
218
220
  const id = input.id;
219
221
  const values = convertObjectToCreate(input);
220
222
  delete values.id;
@@ -223,7 +225,7 @@ function syncedKeel(props) {
223
225
  if (!isEmpty(values)) {
224
226
  const { data, error } = await updateParam({ where: { id }, values });
225
227
  if (error) {
226
- await handleSetError(error, params, "update");
228
+ await handleSetError(error, params, input, updateParam, "update");
227
229
  }
228
230
  return data;
229
231
  }
@@ -231,7 +233,7 @@ function syncedKeel(props) {
231
233
  const deleteFn = deleteParam ? async (value, params) => {
232
234
  const { data, error } = await deleteParam({ id: value.id });
233
235
  if (error) {
234
- await handleSetError(error, params, "delete");
236
+ await handleSetError(error, params, value, deleteParam, "delete");
235
237
  }
236
238
  return data;
237
239
  } : void 0;