|
1 | 1 | import React from 'react'; |
2 | | -import { createBaseLogger, LogLevel, PowerSyncDatabase, SyncClientImplementation } from '@powersync/react-native'; |
| 2 | +import { createPowerSyncLogger, LogLevels, PowerSyncDatabase, SyncClientImplementation } from '@powersync/react-native'; |
3 | 3 | import { SupabaseConnector } from '@/supabase/SupabaseConnector'; |
4 | 4 | import { AppSchema } from '@/powersync/AppSchema'; |
5 | 5 | import { OPSqliteOpenFactory } from '@powersync/op-sqlite'; |
6 | 6 |
|
7 | | -const logger = createBaseLogger(); |
8 | | -logger.useDefaults(); |
9 | | -logger.setLevel(LogLevel.DEBUG); |
| 7 | +const logger = createPowerSyncLogger({ minLevel: LogLevels.debug }); |
10 | 8 |
|
11 | 9 | export class System { |
12 | | - connector: SupabaseConnector; |
13 | | - powersync: PowerSyncDatabase; |
14 | | - |
15 | | - constructor() { |
16 | | - this.connector = new SupabaseConnector(); |
17 | | - |
18 | | - const opSqlite = new OPSqliteOpenFactory({ |
19 | | - dbFilename: 'powersync.db' |
20 | | - }); |
21 | | - |
22 | | - this.powersync = new PowerSyncDatabase({ |
23 | | - schema: AppSchema, |
24 | | - database: opSqlite, |
25 | | - logger: logger |
26 | | - }); |
27 | | - } |
28 | | - |
29 | | - async init() { |
30 | | - await this.connector.signInAnonymously(); |
31 | | - |
32 | | - await this.powersync.init(); |
33 | | - |
34 | | - await this.powersync.connect(this.connector, { |
35 | | - clientImplementation: SyncClientImplementation.RUST |
36 | | - }); |
37 | | - |
38 | | - this.powersync.registerListener({ |
39 | | - statusChanged: (status) => { |
40 | | - const hasSynced = Boolean(status.lastSyncedAt); |
41 | | - const downloading = status.dataFlowStatus?.downloading || false; |
42 | | - const uploading = status.dataFlowStatus?.uploading || false; |
43 | | - console.log( |
44 | | - '[PowerSync] Status changed:', |
45 | | - hasSynced ? '✅ Synced' : '⏳ Not yet synced', |
46 | | - downloading ? '📥 Downloading' : '✅ Not downloading', |
47 | | - uploading ? '📤 Uploading' : '✅ Not uploading' |
48 | | - ); |
49 | | - }, |
50 | | - }); |
51 | | - } |
52 | | - |
53 | | - async disconnect() { |
54 | | - await this.powersync.disconnect(); |
55 | | - } |
| 10 | + connector: SupabaseConnector; |
| 11 | + powersync: PowerSyncDatabase; |
| 12 | + |
| 13 | + constructor() { |
| 14 | + this.connector = new SupabaseConnector(); |
| 15 | + |
| 16 | + const opSqlite = new OPSqliteOpenFactory({ |
| 17 | + dbFilename: 'powersync.db' |
| 18 | + }); |
| 19 | + |
| 20 | + this.powersync = new PowerSyncDatabase({ |
| 21 | + schema: AppSchema, |
| 22 | + database: opSqlite, |
| 23 | + logger: logger |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + async init() { |
| 28 | + await this.connector.signInAnonymously(); |
| 29 | + |
| 30 | + await this.powersync.init(); |
| 31 | + |
| 32 | + await this.powersync.connect(this.connector, { |
| 33 | + clientImplementation: SyncClientImplementation.RUST |
| 34 | + }); |
| 35 | + |
| 36 | + this.powersync.registerListener({ |
| 37 | + statusChanged: (status) => { |
| 38 | + const hasSynced = Boolean(status.lastSyncedAt); |
| 39 | + const downloading = status.dataFlowStatus?.downloading || false; |
| 40 | + const uploading = status.dataFlowStatus?.uploading || false; |
| 41 | + console.log( |
| 42 | + '[PowerSync] Status changed:', |
| 43 | + hasSynced ? '✅ Synced' : '⏳ Not yet synced', |
| 44 | + downloading ? '📥 Downloading' : '✅ Not downloading', |
| 45 | + uploading ? '📤 Uploading' : '✅ Not uploading' |
| 46 | + ); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + async disconnect() { |
| 52 | + await this.powersync.disconnect(); |
| 53 | + } |
56 | 54 | } |
57 | 55 |
|
58 | 56 | export const system = new System(); |
59 | 57 |
|
60 | 58 | export const SystemContext = React.createContext(system); |
61 | 59 | export const useSystem = () => React.useContext(SystemContext); |
62 | | - |
63 | | - |
0 commit comments