Skip to content

Commit

Permalink
Timestamp proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Dec 17, 2024
1 parent 9b451d4 commit 7dfec74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
26 changes: 20 additions & 6 deletions packages/app/lib/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ const mapOfDeprecationReplacements = {
disableIndexAutoCreation: 'disablePersistentCacheIndexAutoCreation()',
deleteAllIndexes: 'deleteAllPersistentCacheIndexes()',
},
FirestoreTimestamp: {
seconds: NO_REPLACEMENT,
nanoseconds: NO_REPLACEMENT,
},
},
};

Expand Down Expand Up @@ -277,20 +281,30 @@ function getInstanceName(target) {

export function createDeprecationProxy(instance) {
return new Proxy(instance, {
construct(target, args) {
// needed for Timestamp which we pass as static, when we construct new instance, we need to wrap it in proxy again.
return createDeprecationProxy(new target(...args));
},
get(target, prop, receiver) {
const originalMethod = target[prop];

if (prop === 'constructor') {
return target.constructor;
}

if (target && target.constructor && target.constructor.name === 'FirestoreTimestamp') {
deprecationConsoleWarning('firestore', prop, 'FirestoreTimestamp', false);
}

if (
prop === 'Filter' ||
prop === 'FieldValue' ||
prop === 'Timestamp' ||
prop === 'GeoPoint' ||
prop === 'Blob' ||
prop === 'FieldPath'
target &&
target.name === 'firebaseModuleWithApp' &&
(prop === 'Filter' ||
prop === 'FieldValue' ||
prop === 'Timestamp' ||
prop === 'GeoPoint' ||
prop === 'Blob' ||
prop === 'FieldPath')
) {
// Firestore statics
deprecationConsoleWarning('firestore', prop, 'statics', false);
Expand Down
25 changes: 25 additions & 0 deletions packages/firestore/__tests__/firestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ describe('Firestore', function () {
let persistentCacheIndexManagerV9Deprecation: CheckV9DeprecationFunction;
let firestoreRefV9Deprecation: CheckV9DeprecationFunction;
let staticsV9Deprecation: CheckV9DeprecationFunction;
let timestampV9Deprecation: CheckV9DeprecationFunction;

beforeEach(function () {
firestoreRefV9Deprecation = createCheckV9Deprecation(['firestore']);
Expand All @@ -741,6 +742,8 @@ describe('Firestore', function () {

staticsV9Deprecation = createCheckV9Deprecation(['firestore', 'statics']);

timestampV9Deprecation = createCheckV9Deprecation(['firestore', 'FirestoreTimestamp']);

// @ts-ignore test
jest.spyOn(FirebaseModule.prototype, 'native', 'get').mockImplementation(() => {
return new Proxy(
Expand Down Expand Up @@ -1372,5 +1375,27 @@ describe('Firestore', function () {
);
});
});

describe('Timestamp', function () {
it('Timestamp.seconds', function () {
const timestamp = new firestore.Timestamp(2, 3);
timestampV9Deprecation(
// no corresponding method
() => {},
() => timestamp.seconds,
'seconds',
);
});

it('Timestamp.nanoseconds', function () {
const timestamp = new firestore.Timestamp(2000, 3000000);
timestampV9Deprecation(
// no corresponding method
() => {},
() => timestamp.nanoseconds,
'nanoseconds',
);
});
});
});
});
2 changes: 1 addition & 1 deletion packages/firestore/lib/FirestoreStatics.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
FieldPath: FirestoreFieldPath,
FieldValue: createDeprecationProxy(FirestoreFieldValue),
GeoPoint: FirestoreGeoPoint,
Timestamp: FirestoreTimestamp,
Timestamp: createDeprecationProxy(FirestoreTimestamp),
Filter: createDeprecationProxy(Filter),

CACHE_SIZE_UNLIMITED: -1,
Expand Down

0 comments on commit 7dfec74

Please sign in to comment.