@seedprotocol/sdk 0.1.36 → 0.1.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.
Files changed (46) hide show
  1. package/dist/{index-CAT7d9F9.js → index-C10-kbIJ.js} +143 -50
  2. package/dist/index-C10-kbIJ.js.map +1 -0
  3. package/dist/{index-Ba6KDFRa.js → index-aq3KM2AI.js} +3 -3
  4. package/dist/index-aq3KM2AI.js.map +1 -0
  5. package/dist/main.js +2 -2
  6. package/dist/{seed-BksepU1H.js → seed-D1Jb9jY7.js} +2 -2
  7. package/dist/{seed-BksepU1H.js.map → seed-D1Jb9jY7.js.map} +1 -1
  8. package/dist/{seed-D27QqqZj.js → seed-GJkiyGC5.js} +4 -4
  9. package/dist/{seed-D27QqqZj.js.map → seed-GJkiyGC5.js.map} +1 -1
  10. package/dist/{seed-NqlwXMHy.js → seed-mY1NrOwT.js} +4 -4
  11. package/dist/{seed-NqlwXMHy.js.map → seed-mY1NrOwT.js.map} +1 -1
  12. package/dist/{seed.schema.config-CBSMvlVo.js → seed.schema.config-Lv1DZAJh.js} +2 -2
  13. package/dist/{seed.schema.config-CBSMvlVo.js.map → seed.schema.config-Lv1DZAJh.js.map} +1 -1
  14. package/dist/types/src/browser/client.d.ts.map +1 -1
  15. package/dist/types/src/browser/db/write.d.ts +1 -0
  16. package/dist/types/src/browser/db/write.d.ts.map +1 -1
  17. package/dist/types/src/browser/events/files/download.d.ts.map +1 -1
  18. package/dist/types/src/browser/events/services/allItems.d.ts +6 -0
  19. package/dist/types/src/browser/events/services/allItems.d.ts.map +1 -0
  20. package/dist/types/src/browser/events/services/index.d.ts +3 -0
  21. package/dist/types/src/browser/events/services/index.d.ts.map +1 -0
  22. package/dist/types/src/browser/item/all/actors/fetchDbData.d.ts +1 -2
  23. package/dist/types/src/browser/item/all/actors/fetchDbData.d.ts.map +1 -1
  24. package/dist/types/src/browser/item/all/actors/initialize.d.ts +3 -1
  25. package/dist/types/src/browser/item/all/actors/initialize.d.ts.map +1 -1
  26. package/dist/types/src/browser/item/all/actors/processItems.d.ts.map +1 -1
  27. package/dist/types/src/browser/item/class.d.ts.map +1 -1
  28. package/dist/types/src/browser/react/item.d.ts.map +1 -1
  29. package/dist/types/src/browser/react/property.d.ts.map +1 -1
  30. package/dist/types/src/browser/react/services.d.ts +3 -0
  31. package/dist/types/src/browser/react/services.d.ts.map +1 -1
  32. package/dist/types/src/browser/services/global/actors/initialize.d.ts.map +1 -1
  33. package/dist/types/src/browser/services/global/globalMachine.d.ts.map +1 -1
  34. package/dist/types/src/browser/services/internal/constants.d.ts +1 -0
  35. package/dist/types/src/browser/services/internal/constants.d.ts.map +1 -1
  36. package/dist/types/src/shared/helpers/db.d.ts +0 -1
  37. package/dist/types/src/shared/helpers/db.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/dist/index-Ba6KDFRa.js.map +0 -1
  40. package/dist/index-CAT7d9F9.js.map +0 -1
  41. package/dist/types/src/browser/events/services/internal/index.d.ts +0 -1
  42. package/dist/types/src/browser/events/services/internal/index.d.ts.map +0 -1
  43. package/dist/types/src/browser/services/syncDbWithEas/actors.d.ts +0 -2
  44. package/dist/types/src/browser/services/syncDbWithEas/actors.d.ts.map +0 -1
  45. package/dist/types/src/browser/services/syncDbWithEas/machine.d.ts +0 -3
  46. package/dist/types/src/browser/services/syncDbWithEas/machine.d.ts.map +0 -1
@@ -2,8 +2,8 @@ import { _ as __awaiter, d as __rest, i as internalPropertyNames, I as INTERNAL_
2
2
  import path, { basename } from 'path';
3
3
  import 'reflect-metadata';
4
4
  import { Type } from '@sinclair/typebox';
5
- import { fromCallback, setup, assign, createActor, waitFor, emit } from 'xstate';
6
- import { sql, relations, and, eq, max, or, count, isNull } from 'drizzle-orm';
5
+ import { fromCallback, setup, assign, createActor, waitFor, emit, raise } from 'xstate';
6
+ import { sql, relations, and, eq, max, or, count, isNull, like } from 'drizzle-orm';
7
7
  import { fs, configureSingle } from '@zenfs/core';
8
8
  import 'dayjs';
9
9
  import { customAlphabet } from 'nanoid';
@@ -24,8 +24,8 @@ import { useState, useEffect, useRef, useCallback } from 'react';
24
24
  import { useImmer } from 'use-immer';
25
25
  import { useSelector } from '@xstate/react';
26
26
  import { createBrowserInspector } from '@statelyai/inspect';
27
- import { drizzle } from 'drizzle-orm/sqlite-proxy';
28
27
  import { WebAccess } from '@zenfs/dom';
28
+ import { drizzle } from 'drizzle-orm/sqlite-proxy';
29
29
 
30
30
  const isNode = () => {
31
31
  return (typeof process !== 'undefined' &&
@@ -853,21 +853,6 @@ const resolveRemoteStorage = fromCallback(({ sendBack, input: { context } }) =>
853
853
  });
854
854
  });
855
855
 
856
- const writeAppState = (db, key, value) => __awaiter(void 0, void 0, void 0, function* () {
857
- yield db
858
- .insert(appState)
859
- .values({
860
- key,
861
- value,
862
- })
863
- .onConflictDoUpdate({
864
- target: appState.key,
865
- set: {
866
- value,
867
- },
868
- // setWhere: sql`key = '${key}'`,
869
- });
870
- });
871
856
  const escapeSqliteString = (value) => {
872
857
  if (typeof value !== 'string') {
873
858
  throw new Error(`Value must be a string, instead got: ${JSON.stringify(value)}`);
@@ -999,7 +984,7 @@ const hydrateFromDb = fromCallback(({ sendBack, input: { context } }) => {
999
984
  renderValue: refResolvedDisplayValue,
1000
985
  });
1001
986
  if (propertyNameFromDb === 'storageTransactionId') {
1002
- const { Item } = yield import('./index-Ba6KDFRa.js');
987
+ const { Item } = yield import('./index-aq3KM2AI.js');
1003
988
  const item = yield Item.find({
1004
989
  seedLocalId,
1005
990
  modelName: itemModelName,
@@ -1296,6 +1281,21 @@ const updateItemPropertyValue = (_a) => __awaiter(void 0, [_a], void 0, function
1296
1281
  yield runQueryForStatement(newPropertyStatement);
1297
1282
  sendItemUpdateEvent({ modelName, seedLocalId, seedUid });
1298
1283
  });
1284
+ const writeAppState = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
1285
+ const appDb = getAppDb();
1286
+ yield appDb
1287
+ .insert(appState)
1288
+ .values({
1289
+ key,
1290
+ value,
1291
+ })
1292
+ .onConflictDoUpdate({
1293
+ target: appState.key,
1294
+ set: {
1295
+ value,
1296
+ },
1297
+ });
1298
+ });
1299
1299
 
1300
1300
  var MachineIds;
1301
1301
  (function (MachineIds) {
@@ -1349,6 +1349,7 @@ const INTERNAL_LOADING_APP_DB_SUCCESS = `${INTERNAL}.${LOADING_APP_DB$1}.success
1349
1349
  const INTERNAL_SAVING_CONFIG_SUCCESS = `${INTERNAL}.savingConfig.success`;
1350
1350
  const GLOBAL_INITIALIZING_SEND_CONFIG = `${GLOBAL}.${INITIALIZING$1}.sendConfig`;
1351
1351
  const GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY = `${GLOBAL}.${INITIALIZING$1}.internalServiceReady`;
1352
+ const GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES = `${GLOBAL}.${INITIALIZING$1}.createAllItemsServices`;
1352
1353
  const GLOBAL_ADDING_MODELS_TO_DB_SUCCESS = `${GLOBAL}.${ADDING_MODELS_TO_DB$1}.success`;
1353
1354
  const DB_CHECK_STATUS_UPDATE_PATHS = `${DB}.${CHECKING_STATUS$1}.updatePaths`;
1354
1355
  const DB_CHECK_STATUS_EXISTS = `${DB}.${CHECKING_STATUS$1}.exists`;
@@ -3408,6 +3409,9 @@ class Item {
3408
3409
  props.storageTransactionId = this._storageTransactionId;
3409
3410
  }
3410
3411
  const propertyInstance = ItemProperty.create(props);
3412
+ if (!propertyInstance) {
3413
+ return;
3414
+ }
3411
3415
  if (!propertyInstance.alias) {
3412
3416
  this._service.send({
3413
3417
  type: 'addPropertyInstance',
@@ -3847,8 +3851,12 @@ const processItems = fromCallback(({ sendBack, input: { context } }) => {
3847
3851
  });
3848
3852
  _processItems().then(() => {
3849
3853
  sendBack({ type: 'processItemsSuccess' });
3854
+ const modelName = ModelClass.originalConstructor.name;
3850
3855
  eventEmitter.emit('item.requestAll', {
3851
- modelName: ModelClass.originalConstructor.name,
3856
+ modelName,
3857
+ });
3858
+ eventEmitter.emit('service.save', {
3859
+ modelName,
3852
3860
  });
3853
3861
  sendBack({
3854
3862
  type: 'updateTimes',
@@ -4270,10 +4278,40 @@ const initialize = fromCallback(({ sendBack, input: { event, context } }) => {
4270
4278
  internalService.send({ type: 'init', endpoints, addresses });
4271
4279
  });
4272
4280
  });
4281
+ const _initAllItemsServices = () => __awaiter(void 0, void 0, void 0, function* () {
4282
+ const appDb = getAppDb();
4283
+ const rows = yield appDb
4284
+ .select()
4285
+ .from(appState)
4286
+ .where(like(appState.key, 'snapshot__%'));
4287
+ const payloadObj = {
4288
+ create: {},
4289
+ restore: {},
4290
+ };
4291
+ const modelNamesRestored = [];
4292
+ if (rows && rows.length > 0) {
4293
+ console.log('rows', rows);
4294
+ for (const row of rows) {
4295
+ const modelName = row.key.replace('snapshot__', '');
4296
+ payloadObj.restore[modelName] = JSON.parse(row.value);
4297
+ modelNamesRestored.push(modelName);
4298
+ }
4299
+ }
4300
+ for (const [modelName, ModelClass] of Object.entries(models)) {
4301
+ if (!modelNamesRestored.includes(modelName)) {
4302
+ payloadObj.create[modelName] = ModelClass;
4303
+ }
4304
+ }
4305
+ sendBack(Object.assign({ type: GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES }, payloadObj));
4306
+ });
4273
4307
  _initFileSystem().then(() => {
4274
4308
  logger$g('[global/actors] File system initialized');
4275
4309
  });
4276
- _initInternal().then(() => {
4310
+ _initInternal()
4311
+ .then(() => {
4312
+ return _initAllItemsServices();
4313
+ })
4314
+ .then(() => {
4277
4315
  logger$g('[global/actors] Internal initialized');
4278
4316
  sendBack({ type: GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY });
4279
4317
  internalSubscription === null || internalSubscription === void 0 ? void 0 : internalSubscription.unsubscribe();
@@ -4318,7 +4356,7 @@ const addModelsToDb = fromCallback(({ sendBack, input: { context } }) => {
4318
4356
  if (!models$1) {
4319
4357
  return;
4320
4358
  }
4321
- const { models: SeedModels } = yield import('./seed.schema.config-CBSMvlVo.js');
4359
+ const { models: SeedModels } = yield import('./seed.schema.config-Lv1DZAJh.js');
4322
4360
  const allModels = Object.assign(Object.assign({}, SeedModels), models$1);
4323
4361
  let hasModelsInDb = false;
4324
4362
  const schemaDefsByModelName = new Map();
@@ -5700,22 +5738,6 @@ const globalMachine = setup({
5700
5738
  });
5701
5739
  },
5702
5740
  }),
5703
- assign(({ event, spawn }) => {
5704
- const allItemsServices = {};
5705
- for (const [modelName, ModelClass] of Object.entries(event.models)) {
5706
- const service = spawn(itemMachineAll, {
5707
- systemId: modelName,
5708
- input: {
5709
- modelName,
5710
- ModelClass,
5711
- modelSchema: ModelClass.schema,
5712
- items: [],
5713
- },
5714
- });
5715
- allItemsServices[`${modelName}Service`] = service;
5716
- }
5717
- return allItemsServices;
5718
- }),
5719
5741
  ],
5720
5742
  },
5721
5743
  },
@@ -5736,6 +5758,34 @@ const globalMachine = setup({
5736
5758
  }),
5737
5759
  },
5738
5760
  [GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY]: ADDING_MODELS_TO_DB,
5761
+ [GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES]: {
5762
+ actions: [
5763
+ assign(({ event, spawn }) => {
5764
+ const allItemsServices = {};
5765
+ for (const [modelName, ModelClass] of Object.entries(event.create)) {
5766
+ const service = spawn(itemMachineAll, {
5767
+ systemId: modelName,
5768
+ input: {
5769
+ modelName,
5770
+ ModelClass,
5771
+ modelSchema: ModelClass.schema,
5772
+ items: [],
5773
+ },
5774
+ });
5775
+ allItemsServices[`${modelName}Service`] = service;
5776
+ }
5777
+ for (const [modelName, snapshot] of Object.entries(event.restore)) {
5778
+ const service = createActor(itemMachineAll, {
5779
+ snapshot,
5780
+ });
5781
+ service.start();
5782
+ allItemsServices[`${modelName}Service`] = service;
5783
+ }
5784
+ return allItemsServices;
5785
+ }),
5786
+ raise({ type: 'allItemsServicesCreated' }),
5787
+ ],
5788
+ },
5739
5789
  },
5740
5790
  invoke: {
5741
5791
  src: 'initialize',
@@ -5782,11 +5832,11 @@ const globalMachine = setup({
5782
5832
  (_a) => __awaiter(void 0, [_a], void 0, function* ({ context }) {
5783
5833
  let SeedClass;
5784
5834
  if (context.environment === 'node') {
5785
- const { SeedNode } = yield import('./seed-D27QqqZj.js');
5835
+ const { SeedNode } = yield import('./seed-GJkiyGC5.js');
5786
5836
  SeedClass = SeedNode;
5787
5837
  }
5788
5838
  else {
5789
- const { SeedBrowser } = yield import('./seed-NqlwXMHy.js');
5839
+ const { SeedBrowser } = yield import('./seed-mY1NrOwT.js');
5790
5840
  SeedClass = SeedBrowser;
5791
5841
  }
5792
5842
  return SeedClass;
@@ -5944,6 +5994,16 @@ const getServiceUniqueKey = (service) => {
5944
5994
  }
5945
5995
  return uniqueKey;
5946
5996
  };
5997
+ const useIsDbReady = () => {
5998
+ const [isDbReady, setIsDbReady] = useState(false);
5999
+ const { internalStatus } = useGlobalServiceStatus();
6000
+ useEffect(() => {
6001
+ if (internalStatus === 'ready') {
6002
+ setIsDbReady(true);
6003
+ }
6004
+ }, [internalStatus]);
6005
+ return isDbReady;
6006
+ };
5947
6007
  const useServices = () => {
5948
6008
  const [actors, setActors] = useState([]);
5949
6009
  const [percentComplete, setPercentComplete] = useState(5);
@@ -5956,12 +6016,26 @@ const useServices = () => {
5956
6016
  if (event.actorRef &&
5957
6017
  event.actorRef.logic &&
5958
6018
  event.actorRef.logic.config) {
5959
- const uniqueKey = getServiceUniqueKey(event.actorRef);
5960
- if (!uniqueKey) {
5961
- return;
6019
+ const service = event.actorRef;
6020
+ const services = [service];
6021
+ if (service.logic.config.id === MachineIds.GLOBAL) {
6022
+ const context = service.getSnapshot().context;
6023
+ const keys = Object.keys(context);
6024
+ for (const key of keys) {
6025
+ if (!key.startsWith('internal') && key.endsWith('Service')) {
6026
+ const allItemsService = context[key];
6027
+ services.push(allItemsService);
6028
+ }
6029
+ }
5962
6030
  }
5963
- event.actorRef.uniqueKey = uniqueKey;
5964
- actorsMap.set(uniqueKey, event.actorRef);
6031
+ services.forEach((innerService) => {
6032
+ const uniqueKey = getServiceUniqueKey(innerService);
6033
+ if (!uniqueKey) {
6034
+ return;
6035
+ }
6036
+ innerService.uniqueKey = uniqueKey;
6037
+ actorsMap.set(uniqueKey, innerService);
6038
+ });
5965
6039
  let actorsArray = Array.from(actorsMap.values());
5966
6040
  actorsArray = orderBy(actorsArray, (a) => a.logic.config.id, ['asc']);
5967
6041
  setActors(produce(actors, (draft) => {
@@ -6083,6 +6157,7 @@ const useItems = ({ modelName, options }) => {
6083
6157
  const [items, setItems] = useImmer([]);
6084
6158
  const [isReadingDb, setIsReadingDb] = useState(false);
6085
6159
  const [isInitialized, setIsInitialized] = useState(false);
6160
+ const isDbReady = useIsDbReady();
6086
6161
  const modelNameRef = useRef(modelName);
6087
6162
  const readFromDb = useCallback((event) => __awaiter(void 0, void 0, void 0, function* () {
6088
6163
  if (!event ||
@@ -6095,16 +6170,16 @@ const useItems = ({ modelName, options }) => {
6095
6170
  const allItems = yield Item.all(modelNameRef.current);
6096
6171
  setItems(() => allItems);
6097
6172
  setIsReadingDb(false);
6098
- }), [modelName]);
6173
+ }), [modelName, isReadingDb]);
6099
6174
  useEffect(() => {
6100
- if (isAppDbReady() && !isInitialized) {
6175
+ if (isDbReady && !isInitialized) {
6101
6176
  const _fetchItems = () => __awaiter(void 0, void 0, void 0, function* () {
6102
6177
  yield readFromDb({ modelName });
6103
6178
  setIsInitialized(true);
6104
6179
  });
6105
6180
  _fetchItems();
6106
6181
  }
6107
- }, [isInitialized]);
6182
+ }, [isInitialized, isDbReady]);
6108
6183
  useEffect(() => {
6109
6184
  eventEmitter.addListener('item.requestAll', readFromDb);
6110
6185
  return () => {
@@ -6219,6 +6294,9 @@ const useItemProperty = ({ propertyName, seedLocalId, seedUid, }) => {
6219
6294
  logger$1(`[useItemPropertyTest] [readFromDb] no property found for Item.${seedLocalId}.${propertyName}`);
6220
6295
  return;
6221
6296
  }
6297
+ if (foundProperty.status === 'waitingForDb') {
6298
+ foundProperty.getService().send({ type: 'waitForDbSuccess' });
6299
+ }
6222
6300
  setProperty(() => foundProperty);
6223
6301
  setIsInitialized(true);
6224
6302
  }), [internalStatus, isReadingFromDb]);
@@ -6527,6 +6605,20 @@ const setupPropertyEventHandlers = () => {
6527
6605
  eventEmitter.addListener('property.saveRelation.request', propertySaveRelationHandler);
6528
6606
  };
6529
6607
 
6608
+ const saveServiceHandler = (event) => __awaiter(void 0, void 0, void 0, function* () {
6609
+ const globalService = getGlobalService();
6610
+ if (!globalService) {
6611
+ return;
6612
+ }
6613
+ const { modelName } = event;
6614
+ const service = globalService.getSnapshot().context[`${modelName}Service`];
6615
+ yield writeAppState(`snapshot__${modelName}`, JSON.stringify(service.getPersistedSnapshot()));
6616
+ });
6617
+
6618
+ const setupServiceHandlers = () => {
6619
+ eventEmitter.addListener('service.save', saveServiceHandler);
6620
+ };
6621
+
6530
6622
  const client = {
6531
6623
  init: ({ config, addresses }) => {
6532
6624
  const { endpoints, models } = config;
@@ -6537,6 +6629,7 @@ const client = {
6537
6629
  setupAllItemsEventHandlers();
6538
6630
  setupServicesEventHandlers();
6539
6631
  setupPropertyEventHandlers();
6632
+ setupServiceHandlers();
6540
6633
  if (areFsListenersReady()) {
6541
6634
  eventEmitter.emit('fs.init');
6542
6635
  }
@@ -6544,7 +6637,7 @@ const client = {
6544
6637
  console.error('fs listeners not ready during init');
6545
6638
  }
6546
6639
  globalService.send({ type: 'init', endpoints, models, addresses });
6547
- import('./seed.schema.config-CBSMvlVo.js').then(({ models }) => {
6640
+ import('./seed.schema.config-Lv1DZAJh.js').then(({ models }) => {
6548
6641
  for (const [key, value] of Object.entries(models)) {
6549
6642
  setModel(key, value);
6550
6643
  }
@@ -6601,4 +6694,4 @@ if (isNode()) {
6601
6694
  }
6602
6695
 
6603
6696
  export { CHILD_SNAPSHOT as C, GET_SCHEMAS as G, Item as I, Json as J, List as L, Model as M, Property as P, Relation as R, Text as T, GET_SEEDS as a, GET_SEED_IDS as b, GET_STORAGE_TRANSACTION_ID as c, GET_VERSIONS as d, GET_PROPERTIES as e, GET_ALL_PROPERTIES_FOR_ALL_VERSIONS as f, itemMachineAll as g, internalMachine as h, itemMachineSingle as i, isNode as j, isBrowser as k, isReactNative as l, ImageSrc as m, ItemProperty as n, useItem as o, useItemProperties as p, useCreateItem as q, useItemProperty as r, useDeleteItem as s, useGlobalServiceStatus as t, useItems as u, useServices as v, withSeed as w, getGlobalService as x, client as y, getCorrectId as z };
6604
- //# sourceMappingURL=index-CAT7d9F9.js.map
6697
+ //# sourceMappingURL=index-C10-kbIJ.js.map