the wcore package coordinates actions across the storage layer orchestrating the wave object store, the wave pubsub system, and the wave rpc system Ensures that the initial data is present in the store, creates an initial window if needed
()
| 30 | |
| 31 | // Ensures that the initial data is present in the store, creates an initial window if needed |
| 32 | func EnsureInitialData() (bool, error) { |
| 33 | // does not need to run in a transaction since it is called on startup |
| 34 | ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second) |
| 35 | defer cancelFn() |
| 36 | client, err := wstore.DBGetSingleton[*waveobj.Client](ctx) |
| 37 | firstLaunch := false |
| 38 | if err == wstore.ErrNotFound { |
| 39 | client, err = CreateClient(ctx) |
| 40 | if err != nil { |
| 41 | return false, fmt.Errorf("error creating client: %w", err) |
| 42 | } |
| 43 | firstLaunch = true |
| 44 | } |
| 45 | if client.TempOID == "" { |
| 46 | log.Println("client.TempOID is empty") |
| 47 | client.TempOID = uuid.NewString() |
| 48 | err = wstore.DBUpdate(ctx, client) |
| 49 | if err != nil { |
| 50 | return firstLaunch, fmt.Errorf("error updating client: %w", err) |
| 51 | } |
| 52 | } |
| 53 | if client.InstallId == "" { |
| 54 | log.Println("client.InstallId is empty") |
| 55 | client.InstallId = uuid.NewString() |
| 56 | err = wstore.DBUpdate(ctx, client) |
| 57 | if err != nil { |
| 58 | return firstLaunch, fmt.Errorf("error updating client: %w", err) |
| 59 | } |
| 60 | } |
| 61 | log.Printf("clientid: %s\n", client.OID) |
| 62 | wstore.SetClientId(client.OID) |
| 63 | if len(client.WindowIds) == 1 { |
| 64 | log.Println("client has one window") |
| 65 | CheckAndFixWindow(ctx, client.WindowIds[0]) |
| 66 | return firstLaunch, nil |
| 67 | } |
| 68 | if len(client.WindowIds) > 0 { |
| 69 | log.Println("client has windows") |
| 70 | return firstLaunch, nil |
| 71 | } |
| 72 | wsId := "" |
| 73 | if firstLaunch { |
| 74 | log.Println("client has no windows and first launch, creating starter workspace") |
| 75 | starterWs, err := CreateWorkspace(ctx, "Starter workspace", "custom@wave-logo-solid", "#58C142", false, true) |
| 76 | if err != nil { |
| 77 | return firstLaunch, fmt.Errorf("error creating starter workspace: %w", err) |
| 78 | } |
| 79 | wsId = starterWs.OID |
| 80 | } |
| 81 | _, err = CreateWindow(ctx, nil, wsId) |
| 82 | if err != nil { |
| 83 | return firstLaunch, fmt.Errorf("error creating window: %w", err) |
| 84 | } |
| 85 | return firstLaunch, nil |
| 86 | } |
| 87 | |
| 88 | func CreateClient(ctx context.Context) (*waveobj.Client, error) { |
| 89 | client := &waveobj.Client{ |
no test coverage detected