Skip to content

Commit

Permalink
Merge pull request #793 from ibeckermayer/ibeckermayer/ts-conversion-…
Browse files Browse the repository at this point in the history
…observable-map

Typescript conversion for `src/observable/map`
  • Loading branch information
MidhunSureshR authored Oct 10, 2022
2 parents 664038b + d025c11 commit 8ef1633
Show file tree
Hide file tree
Showing 31 changed files with 554 additions and 387 deletions.
6 changes: 5 additions & 1 deletion .ts-eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module.exports = {
rules: {
"@typescript-eslint/no-floating-promises": 2,
"@typescript-eslint/no-misused-promises": 2,
"semi": ["error", "always"]
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["warn"],
"no-undef": "off",
"semi": ["error", "always"],
"@typescript-eslint/explicit-function-return-type": ["error"]
}
};
2 changes: 1 addition & 1 deletion src/domain/SessionPickerViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {SortedArray} from "../observable/index.js";
import {SortedArray} from "../observable";
import {ViewModel} from "./ViewModel";
import {avatarInitials, getIdentifierColorNumber} from "./avatar";

Expand Down
4 changes: 2 additions & 2 deletions src/domain/session/leftpanel/LeftPanelViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {RoomTileViewModel} from "./RoomTileViewModel.js";
import {InviteTileViewModel} from "./InviteTileViewModel.js";
import {RoomBeingCreatedTileViewModel} from "./RoomBeingCreatedTileViewModel.js";
import {RoomFilter} from "./RoomFilter.js";
import {ApplyMap} from "../../../observable/map/ApplyMap.js";
import {addPanelIfNeeded} from "../../navigation/index";
import {ApplyMap} from "../../../observable";
import {addPanelIfNeeded} from "../../navigation";

export class LeftPanelViewModel extends ViewModel {
constructor(options) {
Expand Down
4 changes: 2 additions & 2 deletions src/domain/session/rightpanel/MemberListViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class MemberListViewModel extends ViewModel {
const vm = new MemberTileViewModel(this.childOptions({member, emitChange, mediaRepository}));
this.nameDisambiguator.disambiguate(vm);
return vm;
}
const updater = (vm, params, newMember) => {
};
const updater = (params, vm, newMember) => {
vm.updateFrom(newMember);
this.nameDisambiguator.disambiguate(vm);
};
Expand Down
2 changes: 1 addition & 1 deletion src/domain/session/room/timeline/ReactionsViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {ObservableMap} from "../../../../observable/map/ObservableMap";
import {ObservableMap} from "../../../../observable";

export class ReactionsViewModel {
constructor(parentTile) {
Expand Down
3 changes: 1 addition & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ export {
MappedList,
AsyncMappedList,
ConcatList,
ObservableMap
} from "./observable/index";
} from "./observable";
export {
BaseObservableValue,
ObservableValue,
Expand Down
8 changes: 4 additions & 4 deletions src/matrix/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {RoomStatus} from "./room/common";
import {RoomBeingCreated} from "./room/RoomBeingCreated";
import {Invite} from "./room/Invite.js";
import {Pusher} from "./push/Pusher";
import { ObservableMap } from "../observable/index.js";
import {ObservableMap} from "../observable";
import {User} from "./User.js";
import {DeviceMessageHandler} from "./DeviceMessageHandler.js";
import {Account as E2EEAccount} from "./e2ee/Account.js";
Expand Down Expand Up @@ -192,7 +192,7 @@ export class Session {
/**
* Enable secret storage by providing the secret storage credential.
* This will also see if there is a megolm key backup and try to enable that if so.
*
*
* @param {string} type either "passphrase" or "recoverykey"
* @param {string} credential either the passphrase or the recovery key, depending on the type
* @return {Promise} resolves or rejects after having tried to enable secret storage
Expand Down Expand Up @@ -663,7 +663,7 @@ export class Session {
if (this._e2eeAccount && deviceOneTimeKeysCount) {
changes.e2eeAccountChanges = this._e2eeAccount.writeSync(deviceOneTimeKeysCount, txn, log);
}

const deviceLists = syncResponse.device_lists;
if (this._deviceTracker && Array.isArray(deviceLists?.changed) && deviceLists.changed.length) {
await log.wrap("deviceLists", log => this._deviceTracker.writeDeviceChanges(deviceLists.changed, txn, log));
Expand Down Expand Up @@ -908,7 +908,7 @@ export class Session {
Creates an empty (summary isn't loaded) the archived room if it isn't
loaded already, assuming sync will either remove it (when rejoining) or
write a full summary adopting it from the joined room when leaving
@internal
*/
createOrGetArchivedRoomForSync(roomId) {
Expand Down
2 changes: 1 addition & 1 deletion src/matrix/room/members/MemberList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {ObservableMap} from "../../../observable/map/ObservableMap";
import {ObservableMap} from "../../../observable";
import {RetainedValue} from "../../../utils/RetainedValue";

export class MemberList extends RetainedValue {
Expand Down
14 changes: 7 additions & 7 deletions src/matrix/room/timeline/Timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {SortedArray, AsyncMappedList, ConcatList, ObservableArray} from "../../../observable/index.js";
import {SortedArray, AsyncMappedList, ConcatList, ObservableArray} from "../../../observable";
import {Disposables} from "../../../utils/Disposables";
import {Direction} from "./Direction";
import {TimelineReader} from "./persistence/TimelineReader.js";
Expand Down Expand Up @@ -45,7 +45,7 @@ export class Timeline {
});
this._readerRequest = null;
this._allEntries = null;
/** Stores event entries that we had to fetch from hs/storage for reply previews (because they were not in timeline) */
/** Stores event entries that we had to fetch from hs/storage for reply previews (because they were not in timeline) */
this._contextEntriesNotInTimeline = new Map();
/** Only used to decrypt non-persisted context entries fetched from the homeserver */
this._decryptEntries = null;
Expand Down Expand Up @@ -189,7 +189,7 @@ export class Timeline {
// before it has any subscriptions, we bail out if this isn't
// the case yet. This can happen when sync adds or replaces entries
// before load has finished and the view has subscribed to the timeline.
//
//
// Once the subscription is setup, MappedList will set up the local
// relations as needed with _applyAndEmitLocalRelationChange,
// so we're not missing anything by bailing out.
Expand Down Expand Up @@ -239,7 +239,7 @@ export class Timeline {
if (err.name === "CompareError") {
// see FragmentIdComparer, if the replacing entry is on a fragment
// that is currently not loaded into the FragmentIdComparer, it will
// throw a CompareError, and it means that the event is not loaded
// throw a CompareError, and it means that the event is not loaded
// in the timeline (like when receiving a relation for an event
// that is not loaded in memory) so we can just drop this error as
// replacing an event that is not already loaded is a no-op.
Expand Down Expand Up @@ -311,7 +311,7 @@ export class Timeline {
* - timeline
* - storage
* - homeserver
* @param {EventEntry[]} entries
* @param {EventEntry[]} entries
*/
async _loadContextEntriesWhereNeeded(entries) {
for (const entry of entries) {
Expand Down Expand Up @@ -392,7 +392,7 @@ export class Timeline {
* [loadAtTop description]
* @param {[type]} amount [description]
* @return {boolean} true if the top of the timeline has been reached
*
*
*/
async loadAtTop(amount) {
if (this._disposables.isDisposed) {
Expand Down Expand Up @@ -547,7 +547,7 @@ export function tests() {
content: {},
relatedEventId: event2.event_id
}}));
// 4. subscribe (it's now safe to iterate timeline.entries)
// 4. subscribe (it's now safe to iterate timeline.entries)
timeline.entries.subscribe(new ListObserver());
// 5. check the local relation got correctly aggregated
const locallyRedacted = await poll(() => Array.from(timeline.entries)[0].isRedacting);
Expand Down
11 changes: 6 additions & 5 deletions src/observable/BaseObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export abstract class BaseObservable<T> {
if (this._handlers.size === 1) {
this.onSubscribeFirst();
}
return () => {
return (): undefined => {
return this.unsubscribe(handler);
};
}
Expand Down Expand Up @@ -63,22 +63,23 @@ export abstract class BaseObservable<T> {
// Add iterator over handlers here
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function tests() {
class Collection extends BaseObservable<{}> {
firstSubscribeCalls: number = 0;
firstUnsubscribeCalls: number = 0;

onSubscribeFirst() { this.firstSubscribeCalls += 1; }
onUnsubscribeLast() { this.firstUnsubscribeCalls += 1; }
onSubscribeFirst(): void { this.firstSubscribeCalls += 1; }
onUnsubscribeLast(): void { this.firstUnsubscribeCalls += 1; }
}

return {
test_unsubscribe(assert) {
test_unsubscribe(assert): void {
const c = new Collection();
const unsubscribe = c.subscribe({});
unsubscribe();
assert.equal(c.firstSubscribeCalls, 1);
assert.equal(c.firstUnsubscribeCalls, 1);
}
}
};
}
35 changes: 18 additions & 17 deletions src/observable/ObservableValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {SubscriptionHandle} from "./BaseObservable";

// like an EventEmitter, but doesn't have an event type
export abstract class BaseObservableValue<T> extends BaseObservable<(value: T) => void> {
emit(argument: T) {
emit(argument: T): void {
for (const h of this._handlers) {
h(argument);
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class WaitForHandle<T> implements IWaitHandle<T> {
return this._promise;
}

dispose() {
dispose(): void {
if (this._subscription) {
this._subscription();
this._subscription = null;
Expand All @@ -82,7 +82,7 @@ class WaitForHandle<T> implements IWaitHandle<T> {

class ResolvedWaitForHandle<T> implements IWaitHandle<T> {
constructor(public promise: Promise<T>) {}
dispose() {}
dispose(): void {}
}

export class ObservableValue<T> extends BaseObservableValue<T> {
Expand Down Expand Up @@ -113,7 +113,7 @@ export class RetainedObservableValue<T> extends ObservableValue<T> {
this._freeCallback = freeCallback;
}

onUnsubscribeLast() {
onUnsubscribeLast(): void {
super.onUnsubscribeLast();
this._freeCallback();
}
Expand All @@ -130,15 +130,15 @@ export class FlatMapObservableValue<P, C> extends BaseObservableValue<C | undefi
super();
}

onUnsubscribeLast() {
onUnsubscribeLast(): void {
super.onUnsubscribeLast();
this.sourceSubscription = this.sourceSubscription!();
if (this.targetSubscription) {
this.targetSubscription = this.targetSubscription();
}
}

onSubscribeFirst() {
onSubscribeFirst(): void {
super.onSubscribeFirst();
this.sourceSubscription = this.source.subscribe(() => {
this.updateTargetSubscription();
Expand All @@ -147,7 +147,7 @@ export class FlatMapObservableValue<P, C> extends BaseObservableValue<C | undefi
this.updateTargetSubscription();
}

private updateTargetSubscription() {
private updateTargetSubscription(): void {
const sourceValue = this.source.get();
if (sourceValue) {
const target = this.mapper(sourceValue);
Expand All @@ -174,9 +174,10 @@ export class FlatMapObservableValue<P, C> extends BaseObservableValue<C | undefi
}
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function tests() {
return {
"set emits an update": assert => {
"set emits an update": (assert): void => {
const a = new ObservableValue<number>(0);
let fired = false;
const subscription = a.subscribe(v => {
Expand All @@ -187,7 +188,7 @@ export function tests() {
assert(fired);
subscription();
},
"set doesn't emit if value hasn't changed": assert => {
"set doesn't emit if value hasn't changed": (assert): void => {
const a = new ObservableValue(5);
let fired = false;
const subscription = a.subscribe(() => {
Expand All @@ -198,32 +199,32 @@ export function tests() {
assert(!fired);
subscription();
},
"waitFor promise resolves on matching update": async assert => {
"waitFor promise resolves on matching update": async (assert): Promise<void> => {
const a = new ObservableValue(5);
const handle = a.waitFor(v => v === 6);
Promise.resolve().then(() => {
await Promise.resolve().then(() => {
a.set(6);
});
await handle.promise;
assert.strictEqual(a.get(), 6);
},
"waitFor promise rejects when disposed": async assert => {
"waitFor promise rejects when disposed": async (assert): Promise<void> => {
const a = new ObservableValue<number>(0);
const handle = a.waitFor(() => false);
Promise.resolve().then(() => {
await Promise.resolve().then(() => {
handle.dispose();
});
await assert.rejects(handle.promise, AbortError);
},
"flatMap.get": assert => {
"flatMap.get": (assert): void => {
const a = new ObservableValue<undefined | {count: ObservableValue<number>}>(undefined);
const countProxy = a.flatMap(a => a!.count);
assert.strictEqual(countProxy.get(), undefined);
const count = new ObservableValue<number>(0);
a.set({count});
assert.strictEqual(countProxy.get(), 0);
},
"flatMap update from source": assert => {
"flatMap update from source": (assert): void => {
const a = new ObservableValue<undefined | {count: ObservableValue<number>}>(undefined);
const updates: (number | undefined)[] = [];
a.flatMap(a => a!.count).subscribe(count => {
Expand All @@ -233,7 +234,7 @@ export function tests() {
a.set({count});
assert.deepEqual(updates, [0]);
},
"flatMap update from target": assert => {
"flatMap update from target": (assert): void => {
const a = new ObservableValue<undefined | {count: ObservableValue<number>}>(undefined);
const updates: (number | undefined)[] = [];
a.flatMap(a => a!.count).subscribe(count => {
Expand All @@ -244,5 +245,5 @@ export function tests() {
count.set(5);
assert.deepEqual(updates, [0, 5]);
}
}
};
}
48 changes: 0 additions & 48 deletions src/observable/index.js

This file was deleted.

Loading

0 comments on commit 8ef1633

Please sign in to comment.