Skip to content

Commit

Permalink
refactor multiplexer to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoventurini committed Oct 17, 2024
1 parent 6ce185a commit 2def9d7
Show file tree
Hide file tree
Showing 8 changed files with 2,482 additions and 295 deletions.
2,293 changes: 2,237 additions & 56 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@babel/eslint-parser": "^7.21.3",
"@babel/eslint-plugin": "^7.19.1",
"@babel/preset-react": "^7.18.6",
"@types/lodash.isempty": "^4.4.9",
"@types/meteor": "^2.9.8",
"@types/node": "^18.16.18",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
Expand Down
1 change: 1 addition & 0 deletions packages/mongo/mongo_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "meteor/minimongo/constants";
import { Meteor } from "meteor/meteor";
import { ObserveHandle } from './observe_handle';
import { ObserveMultiplexer } from './observe_multiplex';

MongoInternals = {};

Expand Down
73 changes: 41 additions & 32 deletions packages/mongo/observe_handle.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
import { ObserveHandleCallback, ObserveMultiplexer } from './observe_multiplex';

let nextObserveHandleId = 1;

export type ObserveHandleCallbackInternal = '_added' | '_addedBefore' | '_changed' | '_movedBefore' | '_removed';

// When the callbacks do not mutate the arguments, we can skip a lot of data clones
export class ObserveHandle {
_stopped: boolean;
_id: number;
_multiplexer: any;
nonMutatingCallbacks: boolean;

constructor(multiplexer: any, callbacks: any, nonMutatingCallbacks: boolean) {
this._multiplexer = multiplexer;

multiplexer.callbackNames().forEach((name) => {
if (callbacks[name]) {
this['_' + name] = callbacks[name];
} else if (name === "addedBefore" && callbacks.added) {
this._addedBefore = async function (id, fields, before) {
await callbacks.added(id, fields);
};
}
});

this._stopped = false;
this._id = nextObserveHandleId++;
this.nonMutatingCallbacks = nonMutatingCallbacks;
}

async stop() {
if (this._stopped) return;
this._stopped = true;
await this._multiplexer.removeHandle(this._id);
}

_addedBefore(id: any, fields: any, before: any) {
throw new Error("Method not implemented.");
}
_id: number;
_multiplexer: ObserveMultiplexer;
nonMutatingCallbacks: boolean;
_stopped: boolean;

_added?: (...args: any[]) => void;
_addedBefore?: (...args: any[]) => void;
_changed?: (...args: any[]) => void;
_movedBefore?: (...args: any[]) => void;
_removed?: (...args: any[]) => void;

constructor(multiplexer: any, callbacks: Record<ObserveHandleCallback, any>, nonMutatingCallbacks: boolean) {
this._multiplexer = multiplexer;

multiplexer.callbackNames().forEach((name: ObserveHandleCallback) => {
if (callbacks[name]) {
this[`_${name}` as ObserveHandleCallbackInternal] = callbacks[name];
return;
}

if (name === "addedBefore" && callbacks.added) {
this._addedBefore = async function (id, fields, before) {
await callbacks.added(id, fields);
};
}
});

this._stopped = false;
this._id = nextObserveHandleId++;
this.nonMutatingCallbacks = nonMutatingCallbacks;
}

async stop() {
if (this._stopped) return;
this._stopped = true;
await this._multiplexer.removeHandle(this._id);
}
}
206 changes: 0 additions & 206 deletions packages/mongo/observe_multiplex.js

This file was deleted.

Loading

0 comments on commit 2def9d7

Please sign in to comment.