ts: Convert sub_store module to TypeScript.

We also upgrade @babel/preset-typescript to the latest version to
get support for constant enums.
This commit is contained in:
Priyank Patel 2021-08-05 17:07:59 +00:00 committed by Tim Abbott
parent 0d91472c64
commit 397821660f
4 changed files with 85 additions and 42 deletions

View File

@ -9,7 +9,7 @@
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"@babel/preset-typescript": "^7.15.0",
"@babel/register": "^7.6.2",
"@formatjs/intl": "^1.9.7",
"@giphy/js-components": "^5.0.5",

View File

@ -1,40 +0,0 @@
import * as blueslip from "./blueslip";
const subs_by_stream_id = new Map();
export function get(stream_id) {
return subs_by_stream_id.get(stream_id);
}
export function validate_stream_ids(stream_ids) {
const good_ids = [];
const bad_ids = [];
for (const stream_id of stream_ids) {
if (subs_by_stream_id.has(stream_id)) {
good_ids.push(stream_id);
} else {
bad_ids.push(stream_id);
}
}
if (bad_ids.length > 0) {
blueslip.warn(`We have untracked stream_ids: ${bad_ids}`);
}
return good_ids;
}
export function clear() {
subs_by_stream_id.clear();
}
export function delete_sub(stream_id) {
subs_by_stream_id.delete(stream_id);
}
export function add_hydrated_sub(stream_id, sub) {
// The only code that should call this directly is
// in stream_data.js. Grep there to find callers.
subs_by_stream_id.set(stream_id, sub);
}

83
static/js/sub_store.ts Normal file
View File

@ -0,0 +1,83 @@
import * as blueslip from "./blueslip";
export const enum StreamPostPolicy {
EVERYONE = 1,
ADMINS = 2,
RESTRICT_NEW_MEMBERS = 3,
MODERATORS = 4,
}
export const enum SubscriptionRole {
STREAM_ADMINISTRATOR = 20,
MEMBER = 50,
}
export type Stream = {
date_created: number;
description: string;
first_message_id: number | null;
history_public_to_subscribers: boolean;
invite_only: boolean;
is_announcement_only: boolean;
is_web_public: boolean;
message_retention_days: number | null;
name: string;
rendered_description: string;
stream_id: number;
stream_post_policy: StreamPostPolicy;
};
export type StreamSubscription = Stream & {
audible_notifications: boolean | null;
color: string;
desktop_notifications: boolean | null;
email_address: string;
email_notifications: boolean | null;
in_home_view: boolean;
is_muted: boolean;
pin_to_top: boolean;
push_notifications: boolean | null;
role: SubscriptionRole;
stream_weekly_traffic: number | null;
subscribers?: number[];
wildcard_mentions_notify: boolean | null;
};
const subs_by_stream_id = new Map<number, StreamSubscription>();
export function get(stream_id: number): StreamSubscription | undefined {
return subs_by_stream_id.get(stream_id);
}
export function validate_stream_ids(stream_ids: number[]): number[] {
const good_ids = [];
const bad_ids = [];
for (const stream_id of stream_ids) {
if (subs_by_stream_id.has(stream_id)) {
good_ids.push(stream_id);
} else {
bad_ids.push(stream_id);
}
}
if (bad_ids.length > 0) {
blueslip.warn(`We have untracked stream_ids: ${bad_ids}`);
}
return good_ids;
}
export function clear(): void {
subs_by_stream_id.clear();
}
export function delete_sub(stream_id: number): void {
subs_by_stream_id.delete(stream_id);
}
export function add_hydrated_sub(stream_id: number, sub: StreamSubscription): void {
// The only code that should call this directly is
// in stream_data.js. Grep there to find callers.
subs_by_stream_id.set(stream_id, sub);
}

View File

@ -908,7 +908,7 @@
"@babel/types" "^7.4.4"
esutils "^2.0.2"
"@babel/preset-typescript@^7.3.3":
"@babel/preset-typescript@^7.15.0":
version "7.15.0"
resolved "https://registry.yarnpkg.com/@babel/preset-typescript/-/preset-typescript-7.15.0.tgz#e8fca638a1a0f64f14e1119f7fe4500277840945"
integrity sha512-lt0Y/8V3y06Wq/8H/u0WakrqciZ7Fz7mwPDHWUJAXlABL5hiUG42BNlRXiELNjeWjO5rWmnNKlx+yzJvxezHow==