Skip to content

Commit

Permalink
fix: #1108; feat: #1331;
Browse files Browse the repository at this point in the history
  • Loading branch information
migbash committed May 22, 2023
1 parent 19444b2 commit 69ef045
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 49 deletions.
31 changes: 17 additions & 14 deletions src/lib/firebase/common.ts
Expand Up @@ -3,7 +3,7 @@ import { dlog, FIREBASE_DEBUG_STYLE, FIREBASE_DEBUG_TAG, FIREBASE_DEBUG_TOGGLE }
import type { FIRE_LNNS, FIREBASE_livescores_now } from "@betarena/scores-lib/types/firebase.js";
import { onValue, ref, type Unsubscribe } from "firebase/database";
import { getTargetRealDbData } from "./firebase.actions.js";
import { db_real } from "./init";
import { realDb } from "./init";

// #region LIVESCORES_NOW

Expand All @@ -23,7 +23,7 @@ export function listenRealTimeLivescoresNowChange

const dataRef = ref
(
db_real,
realDb(),
'livescores_now/'
);

Expand Down Expand Up @@ -140,8 +140,8 @@ export function listenRealTimeScoreboardAll

const dbRef = ref
(
db_real,
'livescores_now_scoreboard'
realDb(),
'livescores_now_scoreboard_test'
);

const listenEventRef = onValue
Expand Down Expand Up @@ -179,19 +179,22 @@ export async function onceRealTimeLiveScoreboard
(
): Promise < void >
{
console.log('onceRealTimeLiveScoreboard | START')

const firebaseData = await getTargetRealDbData
(
`livescores_now_scoreboard`
`livescores_now_scoreboard_test`
);
if (firebaseData != null)
{
console.log('firebaseData', firebaseData)
const data: [
string,
FIRE_LNNS
][] = Object.entries(firebaseData);
generateLiveScoreboardList(data);
}

console.log('firebaseData', firebaseData)
const data: [string, FIRE_LNNS][] =
firebaseData != null
? Object.entries(firebaseData)
: []
;
generateLiveScoreboardList(data);

console.log('onceRealTimeLiveScoreboard | END')
}

/**
Expand Down
62 changes: 38 additions & 24 deletions src/lib/firebase/firebase.actions.ts
@@ -1,5 +1,5 @@
import { db_real } from '$lib/firebase/init';
import { child, get, ref } from 'firebase/database';
import { realDb } from '$lib/firebase/init';
import { child, get, onValue, ref } from 'firebase/database';

import type { Tournament_Fixture_Odds } from '$lib/models/tournaments/fixtures_odds/types';
import type { FIREBASE_odds } from '@betarena/scores-lib/types/firebase.js';
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function getOdds_1
(
ref
(
db_real
realDb()
),
`odds/${year_}/${new_month_}/${day_}/${fixture_id}`
)
Expand Down Expand Up @@ -108,7 +108,7 @@ export async function getOdds_2
(
ref
(
db_real
realDb()
),
`odds/${year_}/${new_month_}/${day_}/${fixture_id}`
)
Expand Down Expand Up @@ -145,37 +145,51 @@ export async function getOdds_2
export async function getTargetRealDbData
(
path: string
): Promise < unknown >
)
{
const data = await get
console.log('getTargetRealDbData | START')

const connectRef = ref
(
realDb()
);

const snapshot = await get
(
child
(
ref
(
db_real
),
connectRef,
path
)
)
.then
);

console.log('DATA OBTAINED!');
if (snapshot.exists()) return snapshot.val();
return null;
}

// TEMP
export async function realDbHeartBeat
(
)
{
const connectedRef = ref
(
(
snapshot
) =>
{
if (snapshot.exists()) return snapshot.val();
return null;
}
)
.catch
realDb(),
".info/connected"
);
onValue
(
connectedRef,
(
error
snap
) =>
{
console.error(error);
if (snap.val() === true) {
console.log("connected");
} else {
console.log("not connected");
}
}
);
return data;
}
58 changes: 47 additions & 11 deletions src/lib/firebase/init.ts
@@ -1,37 +1,73 @@
import {
getApp,
getApps,
initializeApp,
type FirebaseOptions
} from 'firebase/app';
import { deleteApp, getApp, getApps, initializeApp, type FirebaseOptions } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getDatabase } from 'firebase/database';
import { enableLogging, getDatabase } from 'firebase/database';
import { getFirestore } from 'firebase/firestore';
import { getStorage } from 'firebase/storage';

// NOTE: firebase config with non-auth properties skipped;
const firebaseConfig: FirebaseOptions =
{
apiKey: import.meta.env?.VITE_FIREBASE_DB_API_KEY as string,
authDomain: import.meta.env?.VITE_FIREBASE_DB_AUTH_DOMAIN_MAIN as string,
projectId: import.meta.env?.VITE_FIREBASE_DB_PROJECT_ID_MAIN as string,
authDomain: import.meta.env?.VITE_FIREBASE_DB_AUTH_DOMAIN as string,
projectId: import.meta.env?.VITE_FIREBASE_DB_PROJECT_ID as string,
databaseURL: import.meta.env?.VITE_FIREBASE_DB_DATABASE_URL as string,
storageBucket: import.meta.env?.VITE_FIREBASE_DB_STORAGE_BUCKET as string
};

// DOC: https://stackoverflow.com/questions/37652328/how-to-check-if-a-firebase-app-is-already-initialized-on-android/41005100#41005100
// NOTE: initialize the Firebase APP;
export const app =
export const app =
getApps().length === 0
? initializeApp(firebaseConfig)
: getApp()
;

// version-1

// NOTE: Initialize Real-Time-DB and get a reference to the service;
export const db_real = getDatabase(app);
// NOTE: Initialize Firebase Authentication and get a reference to the service;
export const auth = getAuth(app);
// NOTE: Initialize Cloud Storage and get a reference to the service
export const storage = getStorage(app);
// NOTE: Initialize Cloud Firestore and get a reference to the service
export const db_firestore = getFirestore(app);
export const db_firestore = getFirestore(app);

// version-2

export async function firebaseAppDelete
(
)
{
console.log("🔥 APP DELETED")
await deleteApp
(
app
);
}

export async function firebaseAppInit
(
)
{
console.log('(check) initialized Apps', getApps())
if (getApps().length === 0)
{
console.log("🔥 INITIALIZING APP")
initializeApp(firebaseConfig)
}
console.log('(post) initialized Apps', getApps())
}

// => REAL-TIME DB
export function realDb
(
)
{
enableLogging(true)
// forceLongPolling()
return getDatabase
(
getApp()
);
}

0 comments on commit 69ef045

Please sign in to comment.