Skip to content

Commit 817616d

Browse files
committed
Remove lodash from @powersync/common.
1 parent 2bb6bce commit 817616d

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

packages/common/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@rollup/plugin-json": "^6.1.0",
3737
"@rollup/plugin-node-resolve": "15.2.3",
3838
"@rollup/plugin-terser": "^0.4.4",
39-
"@types/lodash": "^4.14.197",
4039
"@types/node": "^20.5.9",
4140
"@types/uuid": "^9.0.1",
4241
"async-mutex": "^0.4.0",
@@ -45,7 +44,6 @@
4544
"can-ndjson-stream": "^1.0.2",
4645
"cross-fetch": "^4.0.0",
4746
"event-iterator": "^2.0.0",
48-
"lodash": "^4.17.21",
4947
"rollup": "4.14.3",
5048
"rsocket-core": "1.0.0-alpha.3",
5149
"rsocket-websocket-client": "1.0.0-alpha.3",

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Mutex } from 'async-mutex';
22
import { EventIterator } from 'event-iterator';
33
import Logger, { ILogger } from 'js-logger';
4-
import throttle from 'lodash/throttle';
54
import {
65
BatchedUpdateNotification,
76
DBAdapter,
@@ -16,6 +15,7 @@ import { Schema } from '../db/schema/Schema';
1615
import { BaseObserver } from '../utils/BaseObserver';
1716
import { ControlledExecutor } from '../utils/ControlledExecutor';
1817
import { mutexRunExclusive } from '../utils/mutex';
18+
import { throttleTrailing } from '../utils/throttle.js';
1919
import { SQLOpenFactory, SQLOpenOptions, isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory';
2020
import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector';
2121
import { BucketStorageAdapter, PSInternalTable } from './sync/bucket/BucketStorageAdapter';
@@ -898,14 +898,13 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
898898
await onChange(e);
899899
});
900900

901-
const flushTableUpdates = throttle(
901+
const flushTableUpdates = throttleTrailing(
902902
() =>
903903
this.handleTableChanges(changedTables, watchedTables, (intersection) => {
904904
if (resolvedOptions?.signal?.aborted) return;
905905
executor.schedule({ changedTables: intersection });
906906
}),
907-
throttleMs,
908-
{ leading: false, trailing: true }
907+
throttleMs
909908
);
910909

911910
const dispose = this.database.registerListener({

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import throttle from 'lodash/throttle';
2-
31
import Logger, { ILogger } from 'js-logger';
42

53
import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus';
@@ -18,6 +16,7 @@ import {
1816
isStreamingSyncCheckpointDiff,
1917
isStreamingSyncData
2018
} from './streaming-sync-types';
19+
import { throttleLeadingTrailing } from 'src/utils/throttle';
2120

2221
export enum LockType {
2322
CRUD = 'crud',
@@ -142,16 +141,12 @@ export abstract class AbstractStreamingSyncImplementation
142141
});
143142
this.abortController = null;
144143

145-
this.triggerCrudUpload = throttle(
146-
() => {
147-
if (!this.syncStatus.connected || this.syncStatus.dataFlowStatus.uploading) {
148-
return;
149-
}
150-
this._uploadAllCrud();
151-
},
152-
this.options.crudUploadThrottleMs,
153-
{ trailing: true }
154-
);
144+
this.triggerCrudUpload = throttleLeadingTrailing(() => {
145+
if (!this.syncStatus.connected || this.syncStatus.dataFlowStatus.uploading) {
146+
return;
147+
}
148+
this._uploadAllCrud();
149+
}, this.options.crudUploadThrottleMs!);
155150
}
156151

157152
async waitForReady() {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Throttle a function to be called at most once every "wait" milliseconds,
3+
* on the trailing edge.
4+
*
5+
* Roughly equivalent to lodash/throttle with {leading: false, trailing: true}
6+
*/
7+
export function throttleTrailing(func: () => void, wait: number) {
8+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
9+
10+
const later = () => {
11+
func();
12+
timeoutId = null;
13+
};
14+
15+
return function () {
16+
if (timeoutId == null) {
17+
timeoutId = setTimeout(later, wait);
18+
}
19+
};
20+
}
21+
22+
/**
23+
* Throttle a function to be called at most once every "wait" milliseconds,
24+
* on the leading and trailing edge.
25+
*
26+
* Roughly equivalent to lodash/throttle with {leading: true, trailing: true}
27+
*/
28+
export function throttleLeadingTrailing(func: () => void, wait: number) {
29+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
30+
let lastCallTime: number = 0;
31+
32+
const invokeFunction = () => {
33+
func();
34+
lastCallTime = Date.now();
35+
timeoutId = null;
36+
};
37+
38+
return function () {
39+
const now = Date.now();
40+
const timeToWait = wait - (now - lastCallTime);
41+
42+
if (timeToWait <= 0) {
43+
// Leading edge: Call the function immediately if enough time has passed
44+
invokeFunction();
45+
} else if (!timeoutId) {
46+
// Set a timeout for the trailing edge if not already set
47+
timeoutId = setTimeout(invokeFunction, timeToWait);
48+
}
49+
};
50+
}

pnpm-lock.yaml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)