@seedprotocol/sdk 0.2.54 → 0.3.1
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/dist/{ArweaveClient-D5iJisYr.js → ArweaveClient-C0g0es2v.js} +2 -2
- package/dist/{ArweaveClient-D5iJisYr.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
- package/dist/{ArweaveClient-HEIIuOfB.js → ArweaveClient-CzkBgiZY.js} +2 -2
- package/dist/{ArweaveClient-HEIIuOfB.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
- package/dist/{Db-jMth8Y2g.js → Db-C4oog-0n.js} +4 -3
- package/dist/{Db-jMth8Y2g.js.map → Db-C4oog-0n.js.map} +1 -1
- package/dist/Db-VPouegkr.js +82 -0
- package/dist/Db-VPouegkr.js.map +1 -0
- package/dist/EasClient-B9a3NsTi.js +10 -0
- package/dist/EasClient-B9a3NsTi.js.map +1 -0
- package/dist/{EasClient-D3wKgt0o.js → EasClient-DQ93i2ci.js} +2 -2
- package/dist/{EasClient-D3wKgt0o.js.map → EasClient-DQ93i2ci.js.map} +1 -1
- package/dist/{FileManager-4mWYO3Mt.js → FileManager-_ErxVuwb.js} +9 -5
- package/dist/FileManager-_ErxVuwb.js.map +1 -0
- package/dist/{Item-CbgjLSUx.js → Item-D9HFPxsK.js} +4 -3
- package/dist/{Item-CbgjLSUx.js.map → Item-D9HFPxsK.js.map} +1 -1
- package/dist/{ItemProperty-DduurIcl.js → ItemProperty-BdtvKrEy.js} +4 -3
- package/dist/{ItemProperty-DduurIcl.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
- package/dist/QueryClient-Bmnv_moN.js +22 -0
- package/dist/QueryClient-Bmnv_moN.js.map +1 -0
- package/dist/{QueryClient-CBOP3OoU.js → QueryClient-hzOtFe99.js} +3 -2
- package/dist/{QueryClient-CBOP3OoU.js.map → QueryClient-hzOtFe99.js.map} +1 -1
- package/dist/bin.js +30 -34
- package/dist/bin.js.map +1 -1
- package/dist/{constants-CIDbABDz.js → constants-Bhl5dZpN.js} +9 -4
- package/dist/constants-Bhl5dZpN.js.map +1 -0
- package/dist/db/configs/node.app.db.config.ts +27 -0
- package/dist/{index-DQNTvzqi.js → index-BAtjd9W-.js} +4 -3
- package/dist/index-BAtjd9W-.js.map +1 -0
- package/dist/{index-CTZqN6WI.js → index-BSR5hv7Q.js} +305 -268
- package/dist/index-BSR5hv7Q.js.map +1 -0
- package/dist/main.js +3 -2
- package/dist/main.js.map +1 -1
- package/dist/node/db/node.app.db.config.ts +1 -1
- package/dist/{seed.schema.config-B-4U2RRq.js → seed.schema.config-BxJYabim.js} +10 -9
- package/dist/seed.schema.config-BxJYabim.js.map +1 -0
- package/dist/src/AppStateSchema.ts +10 -0
- package/dist/src/ArweaveClient.ts +13 -0
- package/dist/src/Attestation.ts +21 -0
- package/dist/src/BaseArweaveClient.ts +13 -0
- package/dist/src/BaseClientManager.ts +35 -0
- package/dist/src/BaseDb.ts +35 -0
- package/dist/src/BaseEasClient.ts +14 -0
- package/dist/src/BaseFileManager.ts +60 -0
- package/dist/src/BaseItem.ts +413 -0
- package/dist/src/BaseItemProperty.ts +416 -0
- package/dist/src/BaseQueryClient.ts +13 -0
- package/dist/src/ClientManager.ts +63 -0
- package/dist/src/ConfigSchema.ts +15 -0
- package/dist/src/Db.ts +82 -0
- package/dist/src/EasClient.ts +13 -0
- package/dist/src/FileDownloader.ts +63 -0
- package/dist/src/FileManager.ts +59 -0
- package/dist/src/IDb.ts +7 -0
- package/dist/src/IEasClient.ts +2 -0
- package/dist/src/IItem.ts +26 -0
- package/dist/src/IItemProperty.ts +28 -0
- package/dist/src/IQueryClient.ts +11 -0
- package/dist/src/ImageResizer.ts +81 -0
- package/dist/src/Item.ts +12 -0
- package/dist/src/ItemProperty.ts +9 -0
- package/dist/src/MetadataSchema.ts +28 -0
- package/dist/src/ModelSchema.ts +46 -0
- package/dist/src/ModelUidSchema.ts +16 -0
- package/dist/src/PropertyUidSchema.ts +16 -0
- package/dist/src/QueryClient.ts +26 -0
- package/dist/src/Schema.ts +17 -0
- package/dist/src/SeedProtocolSdk.d.ts +19 -0
- package/dist/src/SeedSchema.ts +29 -0
- package/dist/src/VersionSchema.ts +16 -0
- package/dist/src/actors.ts +295 -0
- package/dist/src/addModelsToDb.ts +152 -0
- package/dist/src/allItems.ts +31 -0
- package/dist/src/analyzeInput.ts +144 -0
- package/dist/src/browser.app.db.config.ts +27 -0
- package/dist/src/browser.ts +30 -0
- package/dist/src/checkStatus.ts +57 -0
- package/dist/src/clientManagerMachine.ts +59 -0
- package/dist/src/configureFs.ts +98 -0
- package/dist/src/connectToDb.ts +41 -0
- package/dist/src/connectionManager.ts +67 -0
- package/dist/src/constants.ts +125 -0
- package/dist/src/content-hash.ts +30 -0
- package/dist/src/create.ts +39 -0
- package/dist/src/createMetadata.ts +77 -0
- package/dist/src/createNewItem.ts +61 -0
- package/dist/src/createPublishAttempt.ts +16 -0
- package/dist/src/createSeed.ts +31 -0
- package/dist/src/createSeeds.ts +24 -0
- package/dist/src/createVersion.ts +33 -0
- package/dist/src/dbMachine.ts +182 -0
- package/dist/src/deleteItem.ts +19 -0
- package/dist/src/download.ts +208 -0
- package/dist/src/drizzle.ts +78 -0
- package/dist/src/eas.ts +40 -0
- package/dist/src/environment.ts +35 -0
- package/dist/src/eventBus.ts +5 -0
- package/dist/src/events.ts +14 -0
- package/dist/src/fetchDataFromEas.ts +94 -0
- package/dist/src/fetchDbData.ts +19 -0
- package/dist/src/fetchRelatedItems.ts +184 -0
- package/dist/src/fetchSeeds.ts +45 -0
- package/dist/src/fetchVersions.ts +49 -0
- package/dist/src/files.ts +81 -0
- package/dist/src/filesDownload.ts +326 -0
- package/dist/src/fragment-masking.ts +87 -0
- package/dist/src/getExistingItem.ts +60 -0
- package/dist/src/getItem.ts +32 -0
- package/dist/src/getItemData.ts +99 -0
- package/dist/src/getItemProperties.ts +56 -0
- package/dist/src/getItemProperty.ts +34 -0
- package/dist/src/getItems.ts +70 -0
- package/dist/src/getMetadata.ts +40 -0
- package/dist/src/getModelSchemas.ts +89 -0
- package/dist/src/getPropertyData.ts +50 -0
- package/dist/src/getPublishPayload.ts +355 -0
- package/dist/src/getPublishUploads.ts +207 -0
- package/dist/src/getRelationValueData.ts +27 -0
- package/dist/src/getSchemaForItemProperty.ts +68 -0
- package/dist/src/getSchemaForModel.ts +42 -0
- package/dist/src/getSchemaUidForModel.ts +32 -0
- package/dist/src/getSeedData.ts +34 -0
- package/dist/src/getSegmentedItemProperties.ts +65 -0
- package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
- package/dist/src/getVersionData.ts +46 -0
- package/dist/src/getVersionsForVersionUids.ts +39 -0
- package/dist/src/globalMachine.ts +280 -0
- package/dist/src/gql.ts +118 -0
- package/dist/src/graphql.ts +3209 -0
- package/dist/src/helpers.ts +273 -0
- package/dist/src/hydrateExistingItem.ts +137 -0
- package/dist/src/hydrateFromDb.ts +270 -0
- package/dist/src/hydrateNewItem.ts +34 -0
- package/dist/src/imageResize.ts +507 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.ts +4 -0
- package/dist/src/initialize.ts +124 -0
- package/dist/src/internalMachine.ts +241 -0
- package/dist/src/itemMachineAll.ts +157 -0
- package/dist/src/itemMachineSingle.ts +158 -0
- package/dist/src/loadAppDb.ts +44 -0
- package/dist/src/machines.ts +66 -0
- package/dist/src/metadataLatest.ts +34 -0
- package/dist/src/migrate.ts +57 -0
- package/dist/src/model.ts +72 -0
- package/dist/src/modelClass.ts +19 -0
- package/dist/src/node.app.db.config.ts +41 -0
- package/dist/src/prepareDb.ts +29 -0
- package/dist/src/preparePublishRequestData.ts +81 -0
- package/dist/src/processItems.ts +74 -0
- package/dist/src/property.ts +156 -0
- package/dist/src/propertyMachine.ts +202 -0
- package/dist/src/publish.ts +28 -0
- package/dist/src/publishMachine.ts +77 -0
- package/dist/src/queries.ts +13 -0
- package/dist/src/recoverDeletedItem.ts +14 -0
- package/dist/src/reload.ts +33 -0
- package/dist/src/request.ts +45 -0
- package/dist/src/requestAll.ts +157 -0
- package/dist/src/resolveRelatedValue.ts +367 -0
- package/dist/src/resolveRemoteStorage.ts +88 -0
- package/dist/src/saveAppState.ts +50 -0
- package/dist/src/saveConfig.ts +106 -0
- package/dist/src/saveDataToDb.ts +145 -0
- package/dist/src/saveImageSrc.ts +242 -0
- package/dist/src/saveItemStorage.ts +156 -0
- package/dist/src/saveMetadata.ts +18 -0
- package/dist/src/savePublishService.ts +30 -0
- package/dist/src/saveRelation.ts +112 -0
- package/dist/src/seed.schema.config.ts +25 -0
- package/dist/src/seed.ts +10 -0
- package/dist/src/seedProtocol.ts +17 -0
- package/dist/src/services.ts +365 -0
- package/dist/src/setAddresses.ts +16 -0
- package/dist/src/sqlWasmClient.ts +88 -0
- package/dist/src/syncDbWithEas.ts +628 -0
- package/dist/src/trash.ts +29 -0
- package/dist/src/ts-to-proto.ts +101 -0
- package/dist/src/types.ts +12 -0
- package/dist/src/updateItemPropertyValue.ts +243 -0
- package/dist/src/updateMachineContext.ts +21 -0
- package/dist/src/updateMetadata.ts +59 -0
- package/dist/src/upload.ts +86 -0
- package/dist/src/validate.ts +45 -0
- package/dist/src/validateInput.ts +43 -0
- package/dist/src/validateItemData.ts +20 -0
- package/dist/src/versionData.ts +24 -0
- package/dist/src/waitForDb.ts +25 -0
- package/dist/src/waitForFiles.ts +41 -0
- package/dist/src/wasm.d.ts +8300 -0
- package/dist/types/scripts/bin.d.ts.map +1 -1
- package/dist/types/src/browser/helpers/EasClient.d.ts +1 -3
- package/dist/types/src/browser/helpers/EasClient.d.ts.map +1 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
- package/dist/types/src/client/ClientManager.d.ts +44 -0
- package/dist/types/src/client/ClientManager.d.ts.map +1 -0
- package/dist/types/src/client/actions/setAddresses.d.ts.map +1 -1
- package/dist/types/src/client/actors/initialize.d.ts.map +1 -1
- package/dist/types/src/client/actors/saveAppState.d.ts.map +1 -1
- package/dist/types/src/client/clientManagerMachine.d.ts.map +1 -1
- package/dist/types/src/client/index.d.ts +2 -40
- package/dist/types/src/client/index.d.ts.map +1 -1
- package/dist/types/src/db/configs/node.app.db.config.d.ts +3 -0
- package/dist/types/src/db/configs/node.app.db.config.d.ts.map +1 -0
- package/dist/types/src/events/files/download.d.ts.map +1 -1
- package/dist/types/src/helpers/EasClient/BaseEasClient.d.ts.map +1 -1
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
- package/dist/types/src/helpers/constants.d.ts +1 -0
- package/dist/types/src/helpers/constants.d.ts.map +1 -1
- package/dist/types/src/interfaces/IQueryClient.d.ts +7 -1
- package/dist/types/src/interfaces/IQueryClient.d.ts.map +1 -1
- package/dist/types/src/node/db/Db.d.ts +5 -10
- package/dist/types/src/node/db/Db.d.ts.map +1 -1
- package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
- package/dist/types/src/node/helpers/QueryClient.d.ts +2 -4
- package/dist/types/src/node/helpers/QueryClient.d.ts.map +1 -1
- package/dist/types/src/services/allItems/actors/fetchSeeds.d.ts.map +1 -1
- package/dist/types/src/services/db/actors/checkStatus.d.ts.map +1 -1
- package/dist/types/src/services/db/actors/migrate.d.ts.map +1 -1
- package/dist/types/src/services/db/actors/waitForFiles.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/configureFs.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/loadAppDb.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/saveConfig.d.ts.map +1 -1
- package/dist/types/src/services/internal/actors/waitForFiles.d.ts.map +1 -1
- package/dist/types/src/services/internal/internalMachine.d.ts.map +1 -1
- package/package.json +10 -10
- package/dist/Db-DK9wSNIH.js +0 -120
- package/dist/Db-DK9wSNIH.js.map +0 -1
- package/dist/EasClient-jqkT2ZBt.js +0 -13
- package/dist/EasClient-jqkT2ZBt.js.map +0 -1
- package/dist/FileManager-4mWYO3Mt.js.map +0 -1
- package/dist/QueryClient-bKb3SB0W.js +0 -23
- package/dist/QueryClient-bKb3SB0W.js.map +0 -1
- package/dist/constants-CIDbABDz.js.map +0 -1
- package/dist/index-CTZqN6WI.js.map +0 -1
- package/dist/index-DQNTvzqi.js.map +0 -1
- package/dist/package.json +0 -189
- package/dist/seed.schema.config-B-4U2RRq.js.map +0 -1
- package/dist/types/src/client.d.ts +0 -18
- package/dist/types/src/client.d.ts.map +0 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Model, Text } from '@/schema'
|
|
2
|
+
|
|
3
|
+
@Model
|
|
4
|
+
class Seed {
|
|
5
|
+
@Text() uid!: string
|
|
6
|
+
@Text() type!: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
@Model
|
|
10
|
+
class Version {
|
|
11
|
+
@Text() seedUid!: string
|
|
12
|
+
@Text() note!: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@Model
|
|
16
|
+
class Metadata {
|
|
17
|
+
@Text() key!: string
|
|
18
|
+
@Text() value!: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const models = {
|
|
22
|
+
Seed,
|
|
23
|
+
Version,
|
|
24
|
+
Metadata,
|
|
25
|
+
}
|
package/dist/src/seed.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Attestation } from '@/graphql/gql/graphql'
|
|
2
|
+
|
|
3
|
+
export type PropertyToUpdateWithSeed = {
|
|
4
|
+
publishLocalId: string
|
|
5
|
+
propertySchemaUid: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type PublishRequestData = {
|
|
9
|
+
localId: string
|
|
10
|
+
seedIsRevocable: boolean
|
|
11
|
+
seedSchemaUid: string
|
|
12
|
+
seedUid?: string
|
|
13
|
+
versionSchemaUid: string
|
|
14
|
+
versionUid?: string
|
|
15
|
+
propertiesToUpdate: PropertyToUpdateWithSeed[]
|
|
16
|
+
listOfAttestations: Attestation[]
|
|
17
|
+
}
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
2
|
+
import { ActorRef } from 'xstate'
|
|
3
|
+
import { orderBy } from 'lodash-es'
|
|
4
|
+
import { produce } from 'immer'
|
|
5
|
+
import { eventEmitter } from '@/eventBus'
|
|
6
|
+
import pluralize from 'pluralize'
|
|
7
|
+
import { getGlobalService } from '@/services/global'
|
|
8
|
+
import { useSelector } from '@xstate/react'
|
|
9
|
+
import debug from 'debug'
|
|
10
|
+
import { appState } from '@/seedSchema'
|
|
11
|
+
import { like } from 'drizzle-orm'
|
|
12
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
13
|
+
import { MachineIds } from '@/services/internal/constants'
|
|
14
|
+
|
|
15
|
+
const logger = debug('app:react:services')
|
|
16
|
+
|
|
17
|
+
const finalStrings = ['idle', 'ready', 'done', 'success', 'initialized']
|
|
18
|
+
|
|
19
|
+
export const getServiceName = (service: ActorRef<any, any>) => {
|
|
20
|
+
let name = 'actor'
|
|
21
|
+
if (service && service.uniqueKey) {
|
|
22
|
+
name = service.uniqueKey
|
|
23
|
+
}
|
|
24
|
+
if (service && !service.uniqueKey && service.logic && service.logic.config) {
|
|
25
|
+
name = getServiceUniqueKey(service)
|
|
26
|
+
}
|
|
27
|
+
return name
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const getServiceValue = (
|
|
31
|
+
service: ActorRef<any, any>,
|
|
32
|
+
): string | undefined => {
|
|
33
|
+
let value
|
|
34
|
+
if (service && service.getSnapshot() && service.getSnapshot().value) {
|
|
35
|
+
value = service.getSnapshot().value
|
|
36
|
+
}
|
|
37
|
+
if (getServiceName(service) === 'global') {
|
|
38
|
+
if (
|
|
39
|
+
value &&
|
|
40
|
+
typeof value === 'object' &&
|
|
41
|
+
Object.keys(value).length > 0 &&
|
|
42
|
+
Object.keys(value)[0] === 'initialized'
|
|
43
|
+
) {
|
|
44
|
+
value = 'ready'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (value && typeof value === 'object') {
|
|
48
|
+
value = JSON.stringify(value)
|
|
49
|
+
}
|
|
50
|
+
return value
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const getServiceUniqueKey = (service: ActorRef<any, any>) => {
|
|
54
|
+
if (!service || !service.logic || !service.logic.config) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
const config = service.logic.config
|
|
58
|
+
if (!config.id) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
let uniqueKey = config.id
|
|
62
|
+
if (config.id.includes('@seedSdk/')) {
|
|
63
|
+
uniqueKey = config.id.match(/^.*@seedSdk\/(\w+)[\.\w]*/)[1]
|
|
64
|
+
}
|
|
65
|
+
let snapshot
|
|
66
|
+
try {
|
|
67
|
+
snapshot = service.getSnapshot()
|
|
68
|
+
} catch (error) {
|
|
69
|
+
logger('Error:', error)
|
|
70
|
+
return uniqueKey
|
|
71
|
+
}
|
|
72
|
+
if (snapshot) {
|
|
73
|
+
const context = snapshot.context
|
|
74
|
+
if (context && context.dbName) {
|
|
75
|
+
uniqueKey = context.dbName
|
|
76
|
+
}
|
|
77
|
+
if (context && context.modelNamePlural) {
|
|
78
|
+
uniqueKey = context.modelNamePlural
|
|
79
|
+
}
|
|
80
|
+
if (context && context.modelName) {
|
|
81
|
+
uniqueKey = pluralize(context.modelName.toLowerCase())
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return uniqueKey
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const useService = (service: ActorRef<any, any>) => {
|
|
88
|
+
const [timeElapsed, setTimeElapsed] = useState(0)
|
|
89
|
+
|
|
90
|
+
const getPercentComplete = (service: ActorRef<any, any>) => {
|
|
91
|
+
let percentComplete = 0
|
|
92
|
+
if (service.logic.states) {
|
|
93
|
+
const stateNames = []
|
|
94
|
+
const startupStates = []
|
|
95
|
+
|
|
96
|
+
for (const [stateName, state] of Object.entries(service.logic.states)) {
|
|
97
|
+
if (state.tags.includes('loading')) {
|
|
98
|
+
stateNames.push(stateName)
|
|
99
|
+
startupStates.push(state)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const totalStates = startupStates.length
|
|
104
|
+
const value = getServiceValue(service)
|
|
105
|
+
if (finalStrings.includes(value)) {
|
|
106
|
+
return 0
|
|
107
|
+
}
|
|
108
|
+
const stateIndex = stateNames.indexOf(value)
|
|
109
|
+
percentComplete = (stateIndex / totalStates) * 100
|
|
110
|
+
}
|
|
111
|
+
return percentComplete
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const updateTime = useCallback(
|
|
115
|
+
(interval) => {
|
|
116
|
+
const context = service.getSnapshot().context
|
|
117
|
+
const status = service.getSnapshot().value
|
|
118
|
+
if (
|
|
119
|
+
status === 'done' ||
|
|
120
|
+
status === 'success' ||
|
|
121
|
+
status === 'idle' ||
|
|
122
|
+
status === 'ready'
|
|
123
|
+
) {
|
|
124
|
+
clearInterval(interval)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
setTimeElapsed((timeElapsed) => timeElapsed + 1)
|
|
128
|
+
},
|
|
129
|
+
[service],
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
const startInterval = useCallback(() => {
|
|
133
|
+
const interval = setInterval(() => {
|
|
134
|
+
updateTime(interval)
|
|
135
|
+
}, 1000)
|
|
136
|
+
return interval
|
|
137
|
+
}, [updateTime, service])
|
|
138
|
+
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
const interval = startInterval()
|
|
141
|
+
return () => clearInterval(interval)
|
|
142
|
+
}, [])
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
name: getServiceName(service),
|
|
146
|
+
timeElapsed,
|
|
147
|
+
value: getServiceValue(service),
|
|
148
|
+
percentComplete: getPercentComplete(service),
|
|
149
|
+
uniqueKey: getServiceUniqueKey(service),
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export const useIsDbReady = () => {
|
|
154
|
+
const [isDbReady, setIsDbReady] = useState(false)
|
|
155
|
+
|
|
156
|
+
const { internalStatus } = useGlobalServiceStatus()
|
|
157
|
+
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
if (internalStatus === 'ready') {
|
|
160
|
+
setIsDbReady(true)
|
|
161
|
+
}
|
|
162
|
+
}, [internalStatus])
|
|
163
|
+
|
|
164
|
+
return isDbReady
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export const usePersistedSnapshots = () => {
|
|
168
|
+
const [initialized, setInitialized] = useState(false)
|
|
169
|
+
|
|
170
|
+
const hasSavedSnapshots = useHasSavedSnapshots()
|
|
171
|
+
|
|
172
|
+
const { services, percentComplete } = useServices()
|
|
173
|
+
|
|
174
|
+
// Helper function to save all actor snapshots to the database
|
|
175
|
+
const save = useCallback(async () => {
|
|
176
|
+
for (const actor of services) {
|
|
177
|
+
const uniqueKey = getServiceUniqueKey(actor)
|
|
178
|
+
console.log(
|
|
179
|
+
`would save to db with snapshot__${uniqueKey}:`,
|
|
180
|
+
JSON.stringify(actor.getPersistedSnapshot()),
|
|
181
|
+
)
|
|
182
|
+
// await writeAppState(
|
|
183
|
+
// `snapshot__${uniqueKey}`,
|
|
184
|
+
// JSON.stringify(actor.getPersistedSnapshot()),
|
|
185
|
+
// )
|
|
186
|
+
}
|
|
187
|
+
}, [services])
|
|
188
|
+
|
|
189
|
+
// Helper function to load persisted snapshots from the database
|
|
190
|
+
const load = useCallback(async () => {
|
|
191
|
+
const appDb = BaseDb.getAppDb()
|
|
192
|
+
|
|
193
|
+
if (!appDb) {
|
|
194
|
+
return []
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const persistedSnapshots = await appDb
|
|
198
|
+
.select()
|
|
199
|
+
.from(appState)
|
|
200
|
+
.where(like(appState.key, 'snapshot__%'))
|
|
201
|
+
return persistedSnapshots
|
|
202
|
+
}, [])
|
|
203
|
+
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
if (!hasSavedSnapshots || initialized) {
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
const initialize = async () => {
|
|
209
|
+
const persistedSnapshots = await load()
|
|
210
|
+
console.log('persistedSnapshots:', persistedSnapshots)
|
|
211
|
+
setInitialized(true)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
initialize()
|
|
215
|
+
|
|
216
|
+
return () => {
|
|
217
|
+
save() // Save snapshots on unmount
|
|
218
|
+
}
|
|
219
|
+
}, [hasSavedSnapshots, initialized])
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export const useHasSavedSnapshots = () => {
|
|
223
|
+
const [hasSavedSnapshots, setHasSavedSnapshots] = useState(false)
|
|
224
|
+
|
|
225
|
+
const isDbReady = useIsDbReady()
|
|
226
|
+
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
if (isDbReady) {
|
|
229
|
+
const _checkForSnapshots = async (): Promise<void> => {
|
|
230
|
+
const appDb = BaseDb.getAppDb()
|
|
231
|
+
|
|
232
|
+
const rows = await appDb
|
|
233
|
+
.select()
|
|
234
|
+
.from(appState)
|
|
235
|
+
.where(like(appState.key, 'snapshot__%'))
|
|
236
|
+
|
|
237
|
+
if (rows && rows.length > 0) {
|
|
238
|
+
setHasSavedSnapshots(true)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_checkForSnapshots()
|
|
243
|
+
}
|
|
244
|
+
}, [isDbReady])
|
|
245
|
+
|
|
246
|
+
return hasSavedSnapshots
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export const useServices = () => {
|
|
250
|
+
const [actors, setActors] = useState<ActorRef<any, any>[]>([])
|
|
251
|
+
const [percentComplete, setPercentComplete] = useState(5)
|
|
252
|
+
|
|
253
|
+
const actorsMap = new Map<string, ActorRef<any, any>>()
|
|
254
|
+
|
|
255
|
+
useEffect(() => {
|
|
256
|
+
const globalServiceListener = (event) => {
|
|
257
|
+
if (event && event.type === 'init') {
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
if (
|
|
261
|
+
event.actorRef &&
|
|
262
|
+
event.actorRef.logic &&
|
|
263
|
+
event.actorRef.logic.config
|
|
264
|
+
) {
|
|
265
|
+
const service = event.actorRef
|
|
266
|
+
const services = [service]
|
|
267
|
+
|
|
268
|
+
if (service.logic.config.id === MachineIds.GLOBAL) {
|
|
269
|
+
const context = service.getSnapshot().context
|
|
270
|
+
const keys = Object.keys(context)
|
|
271
|
+
for (const key of keys) {
|
|
272
|
+
if (!key.startsWith('internal') && key.endsWith('Service')) {
|
|
273
|
+
const allItemsService = context[key]
|
|
274
|
+
services.push(allItemsService)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
services.forEach((innerService) => {
|
|
280
|
+
const uniqueKey = getServiceUniqueKey(innerService)
|
|
281
|
+
if (!uniqueKey) {
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
innerService.uniqueKey = uniqueKey
|
|
285
|
+
actorsMap.set(uniqueKey, innerService)
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
let actorsArray = Array.from(actorsMap.values())
|
|
289
|
+
actorsArray = orderBy(actorsArray, (a) => a.logic.config.id, ['asc'])
|
|
290
|
+
|
|
291
|
+
setActors(
|
|
292
|
+
produce(actors, (draft) => {
|
|
293
|
+
return actorsArray
|
|
294
|
+
}),
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
eventEmitter.addListener('inspect.globalService', globalServiceListener)
|
|
300
|
+
|
|
301
|
+
return () => {
|
|
302
|
+
eventEmitter.removeListener(
|
|
303
|
+
'inspect.globalService',
|
|
304
|
+
globalServiceListener,
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
}, [])
|
|
308
|
+
|
|
309
|
+
useEffect(() => {
|
|
310
|
+
const globalService = actors.find(
|
|
311
|
+
(actor) => getServiceName(actor) === 'global',
|
|
312
|
+
)
|
|
313
|
+
const internalService = actors.find(
|
|
314
|
+
(actor) => getServiceName(actor) === 'internal',
|
|
315
|
+
)
|
|
316
|
+
if (!globalService || !internalService) {
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
if (
|
|
320
|
+
getServiceValue(globalService) === 'initialized' &&
|
|
321
|
+
getServiceValue(internalService) === 'ready'
|
|
322
|
+
) {
|
|
323
|
+
const denominator = actors.length
|
|
324
|
+
const finishedActors = actors.filter((actor) => {
|
|
325
|
+
const value = getServiceValue(actor)
|
|
326
|
+
return finalStrings.includes(value)
|
|
327
|
+
})
|
|
328
|
+
const numerator = finishedActors.length
|
|
329
|
+
const percentComplete = (numerator / denominator) * 100
|
|
330
|
+
setPercentComplete(percentComplete)
|
|
331
|
+
}
|
|
332
|
+
}, [actors])
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
services: actors,
|
|
336
|
+
percentComplete,
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export const useGlobalServiceStatus = () => {
|
|
341
|
+
const globalService = getGlobalService()
|
|
342
|
+
|
|
343
|
+
const status = useSelector(globalService, (snapshot) => {
|
|
344
|
+
return snapshot.value
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
const internalStatus = useSelector(
|
|
348
|
+
globalService.getSnapshot().context.internalService,
|
|
349
|
+
(snapshot) => {
|
|
350
|
+
if (!snapshot) {
|
|
351
|
+
return
|
|
352
|
+
}
|
|
353
|
+
return snapshot.value
|
|
354
|
+
},
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
const internalService = useSelector(globalService, (snapshot) => {
|
|
358
|
+
return snapshot.context.internalService
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
return {
|
|
362
|
+
status,
|
|
363
|
+
internalStatus,
|
|
364
|
+
}
|
|
365
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { assign } from "xstate";
|
|
2
|
+
import { saveAppState } from "../actors/saveAppState";
|
|
3
|
+
|
|
4
|
+
export const setAddresses = assign(({event, spawn}) => {
|
|
5
|
+
const { addresses } = event
|
|
6
|
+
spawn(saveAppState, {
|
|
7
|
+
input: {
|
|
8
|
+
key: 'addresses',
|
|
9
|
+
value: addresses,
|
|
10
|
+
},
|
|
11
|
+
})
|
|
12
|
+
return {
|
|
13
|
+
addresses,
|
|
14
|
+
isSaving: true,
|
|
15
|
+
}
|
|
16
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { SqliteConnectionManager } from '@/services/db'
|
|
2
|
+
import {
|
|
3
|
+
SqliteRemoteDatabase,
|
|
4
|
+
SqliteRemoteResult,
|
|
5
|
+
} from 'drizzle-orm/sqlite-proxy'
|
|
6
|
+
import { sql } from 'drizzle-orm'
|
|
7
|
+
|
|
8
|
+
let sqlite3InitModule: any
|
|
9
|
+
let sqliteWasmClient: any
|
|
10
|
+
let manager: SqliteConnectionManager | undefined
|
|
11
|
+
let isPreparing = false
|
|
12
|
+
|
|
13
|
+
export const setSqliteWasmClient = (client: any) => {
|
|
14
|
+
sqliteWasmClient = client
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getSqliteWasmClient = async () => {
|
|
18
|
+
if (sqliteWasmClient) {
|
|
19
|
+
return sqliteWasmClient
|
|
20
|
+
}
|
|
21
|
+
if (typeof window === 'undefined') {
|
|
22
|
+
throw new Error('validateInput called from non-browser context')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isPreparing) {
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
isPreparing = true
|
|
30
|
+
|
|
31
|
+
if (!sqlite3InitModule) {
|
|
32
|
+
sqlite3InitModule = await import('@sqlite.org/sqlite-wasm')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!window.sqlite3Worker1Promiser) {
|
|
36
|
+
await sqlite3InitModule()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!window.sqlite3Worker1Promiser) {
|
|
40
|
+
console.error('window.sqlite3Worker1Promiser not found')
|
|
41
|
+
isPreparing = false
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
sqliteWasmClient = await window.sqlite3Worker1Promiser.v2().catch((err) => {
|
|
47
|
+
console.error('Error initializing sqliteWasmClient:', err)
|
|
48
|
+
isPreparing = false
|
|
49
|
+
})
|
|
50
|
+
} catch (err) {
|
|
51
|
+
console.error('Error initializing sqliteWasmClient:', err)
|
|
52
|
+
isPreparing = false
|
|
53
|
+
}
|
|
54
|
+
return sqliteWasmClient
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const getManager = () => {
|
|
58
|
+
return manager
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const setManager = (m: any) => {
|
|
62
|
+
manager = m
|
|
63
|
+
}
|
|
64
|
+
let appDb: SqliteRemoteDatabase<Record<string, unknown>> | undefined
|
|
65
|
+
export const setAppDb = (db: SqliteRemoteDatabase<Record<string, unknown>>) => {
|
|
66
|
+
appDb = db
|
|
67
|
+
}
|
|
68
|
+
export const getAppDb = () => {
|
|
69
|
+
if (!appDb) {
|
|
70
|
+
throw new Error('getAppDb: appDb is undefined')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return appDb
|
|
74
|
+
}
|
|
75
|
+
export const isAppDbReady = () => {
|
|
76
|
+
return !!appDb
|
|
77
|
+
}
|
|
78
|
+
type RunQueryForStatement = (
|
|
79
|
+
statement: string,
|
|
80
|
+
) => Promise<SqliteRemoteResult<unknown>>
|
|
81
|
+
|
|
82
|
+
export const runQueryForStatement: RunQueryForStatement = async (
|
|
83
|
+
statement: string,
|
|
84
|
+
) => {
|
|
85
|
+
const appDb = getAppDb()
|
|
86
|
+
|
|
87
|
+
return appDb.run(sql.raw(statement))
|
|
88
|
+
}
|