@legendapp/state 3.0.0-alpha.24 → 3.0.0-alpha.26

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-alpha.24",
3
+ "version": "3.0.0-alpha.26",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
@@ -61,7 +61,7 @@ function syncedCrud(props) {
61
61
  const isObs = state.isObservable(result);
62
62
  const value = isObs ? result.peek() : result;
63
63
  if (value) {
64
- result = !isObs && (result[fieldDeleted] || result.__deleted) ? state.internal.symbolDelete : result;
64
+ result = !isObs && (result[fieldDeleted] || result[state.symbolDelete]) ? state.internal.symbolDelete : result;
65
65
  if (asArray) {
66
66
  out.push(result);
67
67
  } else if (asMap) {
@@ -73,6 +73,16 @@ function syncedCrud(props) {
73
73
  }
74
74
  return out;
75
75
  };
76
+ const transformRows = (data) => {
77
+ return Promise.all(
78
+ data.map(
79
+ (value) => (
80
+ // Skip transforming any children with symbolDelete or fieldDeleted because they'll get deleted by resultsToOutType
81
+ value[state.symbolDelete] || fieldDeleted && value[fieldDeleted] ? value : transform.load(value, "get")
82
+ )
83
+ )
84
+ );
85
+ };
76
86
  const get = getFn || listFn ? (getParams) => {
77
87
  const { updateLastSync, lastSync, value } = getParams;
78
88
  if (listFn) {
@@ -89,7 +99,7 @@ function syncedCrud(props) {
89
99
  return resultsToOutType(transformed);
90
100
  }
91
101
  };
92
- const processTransformed = (data) => {
102
+ const processResults = (data) => {
93
103
  data || (data = []);
94
104
  if (fieldUpdatedAt) {
95
105
  const newLastSync = computeLastSync(data, fieldUpdatedAt, fieldCreatedAt);
@@ -99,11 +109,11 @@ function syncedCrud(props) {
99
109
  }
100
110
  let transformed = data;
101
111
  if (transform == null ? void 0 : transform.load) {
102
- transformed = Promise.all(data.map((value2) => transform.load(value2, "get")));
112
+ transformed = transformRows(data);
103
113
  }
104
114
  return state.isPromise(transformed) ? transformed.then(toOut) : toOut(transformed);
105
115
  };
106
- return state.isPromise(listPromise) ? listPromise.then(processTransformed) : processTransformed(listPromise);
116
+ return state.isPromise(listPromise) ? listPromise.then(processResults) : processResults(listPromise);
107
117
  } else if (getFn) {
108
118
  const dataPromise = getFn(getParams);
109
119
  const processData = (data) => {
@@ -299,10 +309,7 @@ function syncedCrud(props) {
299
309
  } else if (fieldDeleted && updateFn) {
300
310
  const valueId = valuePrevious[fieldId];
301
311
  if (valueId) {
302
- updateFn(
303
- { ...valueId ? { [fieldId]: valueId } : {}, [fieldDeleted]: true },
304
- params
305
- );
312
+ updateFn({ ...{ [fieldId]: valueId }, [fieldDeleted]: true }, params);
306
313
  } else {
307
314
  console.error("[legend-state]: deleting item without an id");
308
315
  }
@@ -327,7 +334,7 @@ function syncedCrud(props) {
327
334
  if (newLastSync) {
328
335
  paramsForUpdate.lastSync = newLastSync;
329
336
  }
330
- const rowsTransformed = (transform == null ? void 0 : transform.load) ? await Promise.all(rows.map((row) => transform.load(row, "get"))) : rows;
337
+ const rowsTransformed = (transform == null ? void 0 : transform.load) ? await transformRows(rows) : rows;
331
338
  paramsForUpdate.value = resultsToOutType(rowsTransformed);
332
339
  params.update(paramsForUpdate);
333
340
  }
@@ -59,7 +59,7 @@ function syncedCrud(props) {
59
59
  const isObs = isObservable(result);
60
60
  const value = isObs ? result.peek() : result;
61
61
  if (value) {
62
- result = !isObs && (result[fieldDeleted] || result.__deleted) ? internal.symbolDelete : result;
62
+ result = !isObs && (result[fieldDeleted] || result[symbolDelete]) ? internal.symbolDelete : result;
63
63
  if (asArray) {
64
64
  out.push(result);
65
65
  } else if (asMap) {
@@ -71,6 +71,16 @@ function syncedCrud(props) {
71
71
  }
72
72
  return out;
73
73
  };
74
+ const transformRows = (data) => {
75
+ return Promise.all(
76
+ data.map(
77
+ (value) => (
78
+ // Skip transforming any children with symbolDelete or fieldDeleted because they'll get deleted by resultsToOutType
79
+ value[symbolDelete] || fieldDeleted && value[fieldDeleted] ? value : transform.load(value, "get")
80
+ )
81
+ )
82
+ );
83
+ };
74
84
  const get = getFn || listFn ? (getParams) => {
75
85
  const { updateLastSync, lastSync, value } = getParams;
76
86
  if (listFn) {
@@ -87,7 +97,7 @@ function syncedCrud(props) {
87
97
  return resultsToOutType(transformed);
88
98
  }
89
99
  };
90
- const processTransformed = (data) => {
100
+ const processResults = (data) => {
91
101
  data || (data = []);
92
102
  if (fieldUpdatedAt) {
93
103
  const newLastSync = computeLastSync(data, fieldUpdatedAt, fieldCreatedAt);
@@ -97,11 +107,11 @@ function syncedCrud(props) {
97
107
  }
98
108
  let transformed = data;
99
109
  if (transform == null ? void 0 : transform.load) {
100
- transformed = Promise.all(data.map((value2) => transform.load(value2, "get")));
110
+ transformed = transformRows(data);
101
111
  }
102
112
  return isPromise(transformed) ? transformed.then(toOut) : toOut(transformed);
103
113
  };
104
- return isPromise(listPromise) ? listPromise.then(processTransformed) : processTransformed(listPromise);
114
+ return isPromise(listPromise) ? listPromise.then(processResults) : processResults(listPromise);
105
115
  } else if (getFn) {
106
116
  const dataPromise = getFn(getParams);
107
117
  const processData = (data) => {
@@ -297,10 +307,7 @@ function syncedCrud(props) {
297
307
  } else if (fieldDeleted && updateFn) {
298
308
  const valueId = valuePrevious[fieldId];
299
309
  if (valueId) {
300
- updateFn(
301
- { ...valueId ? { [fieldId]: valueId } : {}, [fieldDeleted]: true },
302
- params
303
- );
310
+ updateFn({ ...{ [fieldId]: valueId }, [fieldDeleted]: true }, params);
304
311
  } else {
305
312
  console.error("[legend-state]: deleting item without an id");
306
313
  }
@@ -325,7 +332,7 @@ function syncedCrud(props) {
325
332
  if (newLastSync) {
326
333
  paramsForUpdate.lastSync = newLastSync;
327
334
  }
328
- const rowsTransformed = (transform == null ? void 0 : transform.load) ? await Promise.all(rows.map((row) => transform.load(row, "get"))) : rows;
335
+ const rowsTransformed = (transform == null ? void 0 : transform.load) ? await transformRows(rows) : rows;
329
336
  paramsForUpdate.value = resultsToOutType(rowsTransformed);
330
337
  params.update(paramsForUpdate);
331
338
  }
@@ -248,8 +248,13 @@ function syncedFirebase(props) {
248
248
  if (!didList)
249
249
  return;
250
250
  const key = snap.key;
251
+ const val = snap.val();
252
+ if (fieldId && !val[fieldId]) {
253
+ val[fieldId] = key;
254
+ }
255
+ val[state.symbolDelete] = true;
251
256
  update2({
252
- value: [{ [key]: state.symbolDelete }],
257
+ value: [val],
253
258
  mode: "assign"
254
259
  });
255
260
  };
@@ -246,8 +246,13 @@ function syncedFirebase(props) {
246
246
  if (!didList)
247
247
  return;
248
248
  const key = snap.key;
249
+ const val = snap.val();
250
+ if (fieldId && !val[fieldId]) {
251
+ val[fieldId] = key;
252
+ }
253
+ val[symbolDelete] = true;
249
254
  update2({
250
- value: [{ [key]: symbolDelete }],
255
+ value: [val],
251
256
  mode: "assign"
252
257
  });
253
258
  };
@@ -263,7 +263,7 @@ function syncedKeel(props) {
263
263
  onSaved,
264
264
  fieldCreatedAt,
265
265
  fieldUpdatedAt,
266
- fieldDeleted: fieldDeleted || "deleted",
266
+ fieldDeleted,
267
267
  changesSince,
268
268
  updatePartial: true,
269
269
  subscribe,
@@ -257,7 +257,7 @@ function syncedKeel(props) {
257
257
  onSaved,
258
258
  fieldCreatedAt,
259
259
  fieldUpdatedAt,
260
- fieldDeleted: fieldDeleted || "deleted",
260
+ fieldDeleted,
261
261
  changesSince,
262
262
  updatePartial: true,
263
263
  subscribe,
@@ -59,8 +59,8 @@ function syncedSupabase(props) {
59
59
  }
60
60
  return data || [];
61
61
  } : void 0;
62
- const upsert = async (input) => {
63
- const res = await client.from(collection).upsert(input).select();
62
+ const create = !actions || actions.includes("create") ? async (input) => {
63
+ const res = await client.from(collection).insert(input).select();
64
64
  const { data, error } = res;
65
65
  if (data) {
66
66
  const created = data[0];
@@ -68,9 +68,17 @@ function syncedSupabase(props) {
68
68
  } else {
69
69
  throw new Error(error == null ? void 0 : error.message);
70
70
  }
71
- };
72
- const create = !actions || actions.includes("create") ? upsert : void 0;
73
- const update = !actions || actions.includes("update") ? upsert : void 0;
71
+ } : void 0;
72
+ const update = !actions || actions.includes("update") ? async (input) => {
73
+ const res = await client.from(collection).update(input).eq("id", input.id).select();
74
+ const { data, error } = res;
75
+ if (data) {
76
+ const created = data[0];
77
+ return created;
78
+ } else {
79
+ throw new Error(error == null ? void 0 : error.message);
80
+ }
81
+ } : void 0;
74
82
  const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? async (input) => {
75
83
  const id = input.id;
76
84
  const res = await client.from(collection).delete().eq("id", id).select();
@@ -108,9 +116,9 @@ function syncedSupabase(props) {
108
116
  });
109
117
  }
110
118
  } else if (eventType === "DELETE") {
111
- const { id } = old;
119
+ old[state.symbolDelete] = true;
112
120
  update2({
113
- value: [{ [id]: state.symbolDelete }]
121
+ value: [old]
114
122
  });
115
123
  }
116
124
  }
@@ -57,8 +57,8 @@ function syncedSupabase(props) {
57
57
  }
58
58
  return data || [];
59
59
  } : void 0;
60
- const upsert = async (input) => {
61
- const res = await client.from(collection).upsert(input).select();
60
+ const create = !actions || actions.includes("create") ? async (input) => {
61
+ const res = await client.from(collection).insert(input).select();
62
62
  const { data, error } = res;
63
63
  if (data) {
64
64
  const created = data[0];
@@ -66,9 +66,17 @@ function syncedSupabase(props) {
66
66
  } else {
67
67
  throw new Error(error == null ? void 0 : error.message);
68
68
  }
69
- };
70
- const create = !actions || actions.includes("create") ? upsert : void 0;
71
- const update = !actions || actions.includes("update") ? upsert : void 0;
69
+ } : void 0;
70
+ const update = !actions || actions.includes("update") ? async (input) => {
71
+ const res = await client.from(collection).update(input).eq("id", input.id).select();
72
+ const { data, error } = res;
73
+ if (data) {
74
+ const created = data[0];
75
+ return created;
76
+ } else {
77
+ throw new Error(error == null ? void 0 : error.message);
78
+ }
79
+ } : void 0;
72
80
  const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? async (input) => {
73
81
  const id = input.id;
74
82
  const res = await client.from(collection).delete().eq("id", id).select();
@@ -106,9 +114,9 @@ function syncedSupabase(props) {
106
114
  });
107
115
  }
108
116
  } else if (eventType === "DELETE") {
109
- const { id } = old;
117
+ old[symbolDelete] = true;
110
118
  update2({
111
- value: [{ [id]: symbolDelete }]
119
+ value: [old]
112
120
  });
113
121
  }
114
122
  }