@seedprotocol/sdk 0.1.36 → 0.1.37

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 (45) hide show
  1. package/dist/{index-CAT7d9F9.js → index-CsLLp7nN.js} +140 -50
  2. package/dist/index-CsLLp7nN.js.map +1 -0
  3. package/dist/{index-Ba6KDFRa.js → index-D3rzHFx5.js} +3 -3
  4. package/dist/index-D3rzHFx5.js.map +1 -0
  5. package/dist/main.js +2 -2
  6. package/dist/{seed-BksepU1H.js → seed-B_KC4Qsr.js} +2 -2
  7. package/dist/{seed-BksepU1H.js.map → seed-B_KC4Qsr.js.map} +1 -1
  8. package/dist/{seed-NqlwXMHy.js → seed-DY1HnBke.js} +4 -4
  9. package/dist/{seed-NqlwXMHy.js.map → seed-DY1HnBke.js.map} +1 -1
  10. package/dist/{seed-D27QqqZj.js → seed-DZ28dPJI.js} +4 -4
  11. package/dist/{seed-D27QqqZj.js.map → seed-DZ28dPJI.js.map} +1 -1
  12. package/dist/{seed.schema.config-CBSMvlVo.js → seed.schema.config-CcFj0B6S.js} +2 -2
  13. package/dist/{seed.schema.config-CBSMvlVo.js.map → seed.schema.config-CcFj0B6S.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/react/item.d.ts.map +1 -1
  28. package/dist/types/src/browser/react/property.d.ts.map +1 -1
  29. package/dist/types/src/browser/react/services.d.ts +3 -0
  30. package/dist/types/src/browser/react/services.d.ts.map +1 -1
  31. package/dist/types/src/browser/services/global/actors/initialize.d.ts.map +1 -1
  32. package/dist/types/src/browser/services/global/globalMachine.d.ts.map +1 -1
  33. package/dist/types/src/browser/services/internal/constants.d.ts +1 -0
  34. package/dist/types/src/browser/services/internal/constants.d.ts.map +1 -1
  35. package/dist/types/src/shared/helpers/db.d.ts +0 -1
  36. package/dist/types/src/shared/helpers/db.d.ts.map +1 -1
  37. package/package.json +1 -1
  38. package/dist/index-Ba6KDFRa.js.map +0 -1
  39. package/dist/index-CAT7d9F9.js.map +0 -1
  40. package/dist/types/src/browser/events/services/internal/index.d.ts +0 -1
  41. package/dist/types/src/browser/events/services/internal/index.d.ts.map +0 -1
  42. package/dist/types/src/browser/services/syncDbWithEas/actors.d.ts +0 -2
  43. package/dist/types/src/browser/services/syncDbWithEas/actors.d.ts.map +0 -1
  44. package/dist/types/src/browser/services/syncDbWithEas/machine.d.ts +0 -3
  45. 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-D3rzHFx5.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`;
@@ -3847,8 +3848,12 @@ const processItems = fromCallback(({ sendBack, input: { context } }) => {
3847
3848
  });
3848
3849
  _processItems().then(() => {
3849
3850
  sendBack({ type: 'processItemsSuccess' });
3851
+ const modelName = ModelClass.originalConstructor.name;
3850
3852
  eventEmitter.emit('item.requestAll', {
3851
- modelName: ModelClass.originalConstructor.name,
3853
+ modelName,
3854
+ });
3855
+ eventEmitter.emit('service.save', {
3856
+ modelName,
3852
3857
  });
3853
3858
  sendBack({
3854
3859
  type: 'updateTimes',
@@ -4270,10 +4275,40 @@ const initialize = fromCallback(({ sendBack, input: { event, context } }) => {
4270
4275
  internalService.send({ type: 'init', endpoints, addresses });
4271
4276
  });
4272
4277
  });
4278
+ const _initAllItemsServices = () => __awaiter(void 0, void 0, void 0, function* () {
4279
+ const appDb = getAppDb();
4280
+ const rows = yield appDb
4281
+ .select()
4282
+ .from(appState)
4283
+ .where(like(appState.key, 'snapshot__%'));
4284
+ const payloadObj = {
4285
+ create: {},
4286
+ restore: {},
4287
+ };
4288
+ const modelNamesRestored = [];
4289
+ if (rows && rows.length > 0) {
4290
+ console.log('rows', rows);
4291
+ for (const row of rows) {
4292
+ const modelName = row.key.replace('snapshot__', '');
4293
+ payloadObj.restore[modelName] = JSON.parse(row.value);
4294
+ modelNamesRestored.push(modelName);
4295
+ }
4296
+ }
4297
+ for (const [modelName, ModelClass] of Object.entries(models)) {
4298
+ if (!modelNamesRestored.includes(modelName)) {
4299
+ payloadObj.create[modelName] = ModelClass;
4300
+ }
4301
+ }
4302
+ sendBack(Object.assign({ type: GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES }, payloadObj));
4303
+ });
4273
4304
  _initFileSystem().then(() => {
4274
4305
  logger$g('[global/actors] File system initialized');
4275
4306
  });
4276
- _initInternal().then(() => {
4307
+ _initInternal()
4308
+ .then(() => {
4309
+ return _initAllItemsServices();
4310
+ })
4311
+ .then(() => {
4277
4312
  logger$g('[global/actors] Internal initialized');
4278
4313
  sendBack({ type: GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY });
4279
4314
  internalSubscription === null || internalSubscription === void 0 ? void 0 : internalSubscription.unsubscribe();
@@ -4318,7 +4353,7 @@ const addModelsToDb = fromCallback(({ sendBack, input: { context } }) => {
4318
4353
  if (!models$1) {
4319
4354
  return;
4320
4355
  }
4321
- const { models: SeedModels } = yield import('./seed.schema.config-CBSMvlVo.js');
4356
+ const { models: SeedModels } = yield import('./seed.schema.config-CcFj0B6S.js');
4322
4357
  const allModels = Object.assign(Object.assign({}, SeedModels), models$1);
4323
4358
  let hasModelsInDb = false;
4324
4359
  const schemaDefsByModelName = new Map();
@@ -5700,22 +5735,6 @@ const globalMachine = setup({
5700
5735
  });
5701
5736
  },
5702
5737
  }),
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
5738
  ],
5720
5739
  },
5721
5740
  },
@@ -5736,6 +5755,34 @@ const globalMachine = setup({
5736
5755
  }),
5737
5756
  },
5738
5757
  [GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY]: ADDING_MODELS_TO_DB,
5758
+ [GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES]: {
5759
+ actions: [
5760
+ assign(({ event, spawn }) => {
5761
+ const allItemsServices = {};
5762
+ for (const [modelName, ModelClass] of Object.entries(event.create)) {
5763
+ const service = spawn(itemMachineAll, {
5764
+ systemId: modelName,
5765
+ input: {
5766
+ modelName,
5767
+ ModelClass,
5768
+ modelSchema: ModelClass.schema,
5769
+ items: [],
5770
+ },
5771
+ });
5772
+ allItemsServices[`${modelName}Service`] = service;
5773
+ }
5774
+ for (const [modelName, snapshot] of Object.entries(event.restore)) {
5775
+ const service = createActor(itemMachineAll, {
5776
+ snapshot,
5777
+ });
5778
+ service.start();
5779
+ allItemsServices[`${modelName}Service`] = service;
5780
+ }
5781
+ return allItemsServices;
5782
+ }),
5783
+ raise({ type: 'allItemsServicesCreated' }),
5784
+ ],
5785
+ },
5739
5786
  },
5740
5787
  invoke: {
5741
5788
  src: 'initialize',
@@ -5782,11 +5829,11 @@ const globalMachine = setup({
5782
5829
  (_a) => __awaiter(void 0, [_a], void 0, function* ({ context }) {
5783
5830
  let SeedClass;
5784
5831
  if (context.environment === 'node') {
5785
- const { SeedNode } = yield import('./seed-D27QqqZj.js');
5832
+ const { SeedNode } = yield import('./seed-DZ28dPJI.js');
5786
5833
  SeedClass = SeedNode;
5787
5834
  }
5788
5835
  else {
5789
- const { SeedBrowser } = yield import('./seed-NqlwXMHy.js');
5836
+ const { SeedBrowser } = yield import('./seed-DY1HnBke.js');
5790
5837
  SeedClass = SeedBrowser;
5791
5838
  }
5792
5839
  return SeedClass;
@@ -5944,6 +5991,16 @@ const getServiceUniqueKey = (service) => {
5944
5991
  }
5945
5992
  return uniqueKey;
5946
5993
  };
5994
+ const useIsDbReady = () => {
5995
+ const [isDbReady, setIsDbReady] = useState(false);
5996
+ const { internalStatus } = useGlobalServiceStatus();
5997
+ useEffect(() => {
5998
+ if (internalStatus === 'ready') {
5999
+ setIsDbReady(true);
6000
+ }
6001
+ }, [internalStatus]);
6002
+ return isDbReady;
6003
+ };
5947
6004
  const useServices = () => {
5948
6005
  const [actors, setActors] = useState([]);
5949
6006
  const [percentComplete, setPercentComplete] = useState(5);
@@ -5956,12 +6013,26 @@ const useServices = () => {
5956
6013
  if (event.actorRef &&
5957
6014
  event.actorRef.logic &&
5958
6015
  event.actorRef.logic.config) {
5959
- const uniqueKey = getServiceUniqueKey(event.actorRef);
5960
- if (!uniqueKey) {
5961
- return;
6016
+ const service = event.actorRef;
6017
+ const services = [service];
6018
+ if (service.logic.config.id === MachineIds.GLOBAL) {
6019
+ const context = service.getSnapshot().context;
6020
+ const keys = Object.keys(context);
6021
+ for (const key of keys) {
6022
+ if (!key.startsWith('internal') && key.endsWith('Service')) {
6023
+ const allItemsService = context[key];
6024
+ services.push(allItemsService);
6025
+ }
6026
+ }
5962
6027
  }
5963
- event.actorRef.uniqueKey = uniqueKey;
5964
- actorsMap.set(uniqueKey, event.actorRef);
6028
+ services.forEach((innerService) => {
6029
+ const uniqueKey = getServiceUniqueKey(innerService);
6030
+ if (!uniqueKey) {
6031
+ return;
6032
+ }
6033
+ innerService.uniqueKey = uniqueKey;
6034
+ actorsMap.set(uniqueKey, innerService);
6035
+ });
5965
6036
  let actorsArray = Array.from(actorsMap.values());
5966
6037
  actorsArray = orderBy(actorsArray, (a) => a.logic.config.id, ['asc']);
5967
6038
  setActors(produce(actors, (draft) => {
@@ -6083,6 +6154,7 @@ const useItems = ({ modelName, options }) => {
6083
6154
  const [items, setItems] = useImmer([]);
6084
6155
  const [isReadingDb, setIsReadingDb] = useState(false);
6085
6156
  const [isInitialized, setIsInitialized] = useState(false);
6157
+ const isDbReady = useIsDbReady();
6086
6158
  const modelNameRef = useRef(modelName);
6087
6159
  const readFromDb = useCallback((event) => __awaiter(void 0, void 0, void 0, function* () {
6088
6160
  if (!event ||
@@ -6095,16 +6167,16 @@ const useItems = ({ modelName, options }) => {
6095
6167
  const allItems = yield Item.all(modelNameRef.current);
6096
6168
  setItems(() => allItems);
6097
6169
  setIsReadingDb(false);
6098
- }), [modelName]);
6170
+ }), [modelName, isReadingDb]);
6099
6171
  useEffect(() => {
6100
- if (isAppDbReady() && !isInitialized) {
6172
+ if (isDbReady && !isInitialized) {
6101
6173
  const _fetchItems = () => __awaiter(void 0, void 0, void 0, function* () {
6102
6174
  yield readFromDb({ modelName });
6103
6175
  setIsInitialized(true);
6104
6176
  });
6105
6177
  _fetchItems();
6106
6178
  }
6107
- }, [isInitialized]);
6179
+ }, [isInitialized, isDbReady]);
6108
6180
  useEffect(() => {
6109
6181
  eventEmitter.addListener('item.requestAll', readFromDb);
6110
6182
  return () => {
@@ -6219,6 +6291,9 @@ const useItemProperty = ({ propertyName, seedLocalId, seedUid, }) => {
6219
6291
  logger$1(`[useItemPropertyTest] [readFromDb] no property found for Item.${seedLocalId}.${propertyName}`);
6220
6292
  return;
6221
6293
  }
6294
+ if (foundProperty.status === 'waitingForDb') {
6295
+ foundProperty.getService().send({ type: 'waitForDbSuccess' });
6296
+ }
6222
6297
  setProperty(() => foundProperty);
6223
6298
  setIsInitialized(true);
6224
6299
  }), [internalStatus, isReadingFromDb]);
@@ -6527,6 +6602,20 @@ const setupPropertyEventHandlers = () => {
6527
6602
  eventEmitter.addListener('property.saveRelation.request', propertySaveRelationHandler);
6528
6603
  };
6529
6604
 
6605
+ const saveServiceHandler = (event) => __awaiter(void 0, void 0, void 0, function* () {
6606
+ const globalService = getGlobalService();
6607
+ if (!globalService) {
6608
+ return;
6609
+ }
6610
+ const { modelName } = event;
6611
+ const service = globalService.getSnapshot().context[`${modelName}Service`];
6612
+ yield writeAppState(`snapshot__${modelName}`, JSON.stringify(service.getPersistedSnapshot()));
6613
+ });
6614
+
6615
+ const setupServiceHandlers = () => {
6616
+ eventEmitter.addListener('service.save', saveServiceHandler);
6617
+ };
6618
+
6530
6619
  const client = {
6531
6620
  init: ({ config, addresses }) => {
6532
6621
  const { endpoints, models } = config;
@@ -6537,6 +6626,7 @@ const client = {
6537
6626
  setupAllItemsEventHandlers();
6538
6627
  setupServicesEventHandlers();
6539
6628
  setupPropertyEventHandlers();
6629
+ setupServiceHandlers();
6540
6630
  if (areFsListenersReady()) {
6541
6631
  eventEmitter.emit('fs.init');
6542
6632
  }
@@ -6544,7 +6634,7 @@ const client = {
6544
6634
  console.error('fs listeners not ready during init');
6545
6635
  }
6546
6636
  globalService.send({ type: 'init', endpoints, models, addresses });
6547
- import('./seed.schema.config-CBSMvlVo.js').then(({ models }) => {
6637
+ import('./seed.schema.config-CcFj0B6S.js').then(({ models }) => {
6548
6638
  for (const [key, value] of Object.entries(models)) {
6549
6639
  setModel(key, value);
6550
6640
  }
@@ -6601,4 +6691,4 @@ if (isNode()) {
6601
6691
  }
6602
6692
 
6603
6693
  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
6694
+ //# sourceMappingURL=index-CsLLp7nN.js.map