Skip to content

Commit

Permalink
refactor: rename add method of managers to _add
Browse files Browse the repository at this point in the history
  • Loading branch information
iShibi committed Oct 9, 2021
1 parent 888087b commit df76674
Show file tree
Hide file tree
Showing 16 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/books/BlocksBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class BlocksBook extends BaseBook {
const rawUsers = data.data;
const rawIncludes = data.includes;
for (const rawUser of rawUsers) {
const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes }, false);
const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes }, false);
blockedUsersCollection.set(user.id, user);
}
return blockedUsersCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/ComposedTweetsBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class ComposedTweetsBook extends BaseBook {
const rawTweets = data.data;
const rawIncludes = data.includes;
for (const rawTweet of rawTweets) {
const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes }, false);
const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes }, false);
tweetsCollection.set(tweet.id, tweet);
}
return tweetsCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/FollowersBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class FollowersBook extends BaseBook {
const rawUsers = data.data;
const rawIncludes = data.includes;
for (const rawUser of rawUsers) {
const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes });
const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes });
followersCollection.set(user.id, user);
}
return followersCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/FollowingsBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class FollowingsBook extends BaseBook {
const rawUsers = data.data;
const rawIncludes = data.includes;
for (const rawUser of rawUsers) {
const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes });
const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes });
followingsCollection.set(user.id, user);
}
return followingsCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/LikedTweetsBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class LikedTweetsBook extends BaseBook {
const rawTweets = data.data;
const rawIncludes = data.includes;
for (const rawTweet of rawTweets) {
const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes });
const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes });
likedTweetsCollection.set(tweet.id, tweet);
}
return likedTweetsCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/MentionsBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class MentionsBook extends BaseBook {
const rawTweets = data.data;
const rawIncludes = data.includes;
for (const rawTweet of rawTweets) {
const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes });
const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes });
mentioningTweetsCollection.set(tweet.id, tweet);
}
return mentioningTweetsCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/MutesBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class MutesBook extends BaseBook {
const rawUsers = data.data;
const rawIncludes = data.includes;
for (const rawUser of rawUsers) {
const user = this.client.users.add(rawUser.id, { data: rawUser, includes: rawIncludes }, false);
const user = this.client.users._add(rawUser.id, { data: rawUser, includes: rawIncludes }, false);
mutedUsersCollection.set(user.id, user);
}
return mutedUsersCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/books/SearchTweetsBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class SearchTweetsBook extends BaseBook {
const rawTweets = data.data;
const rawIncludes = data.includes;
for (const rawTweet of rawTweets) {
const tweet = this.client.tweets.add(rawTweet.id, { data: rawTweet, includes: rawIncludes });
const tweet = this.client.tweets._add(rawTweet.id, { data: rawTweet, includes: rawIncludes });
tweetsCollection.set(tweet.id, tweet);
}
return tweetsCollection;
Expand Down
4 changes: 2 additions & 2 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class Client extends BaseClient {
}
try {
const rawData: GetFilteredTweetStreamResponse = JSON.parse(data);
const tweet = this.tweets.add(rawData.data.id, rawData, false);
const tweet = this.tweets._add(rawData.data.id, rawData, false);
const matchingRules = rawData.matching_rules.reduce((col, rule) => {
col.set(rule.id, new MatchingRule(rule));
return col;
Expand Down Expand Up @@ -245,7 +245,7 @@ export class Client extends BaseClient {
}
try {
const rawTweet: GetSampledTweetStreamResponse = JSON.parse(data);
const tweet = this.tweets.add(rawTweet.data.id, rawTweet, false);
const tweet = this.tweets._add(rawTweet.data.id, rawTweet, false);
this.emit(ClientEvents.SAMPLED_TWEET_CREATE, tweet);
} catch (error) {
// twitter sends corrupted data sometimes that throws error while parsing it
Expand Down
2 changes: 1 addition & 1 deletion src/managers/BaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class BaseManager<K extends string, R, T extends { id: K }> {
* @param data The raw data returned by the API for this structure
* @param cacheAfterFetching Whether to store the structure in the manager's cache
*/
add<RawData>(id: K, data: RawData, cacheAfterFetching = true): T {
_add<RawData>(id: K, data: RawData, cacheAfterFetching = true): T {
const entry = new this._holds(this.client, data);
if (cacheAfterFetching) this.cache.set(id, entry);
return entry;
Expand Down
6 changes: 3 additions & 3 deletions src/managers/FilteredStreamRuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class FilteredStreamRuleManager extends BaseManager<
requestData,
);
return res.data.reduce((createdRules, rawRule) => {
const rule = this.add(rawRule.id, rawRule);
const rule = this._add(rawRule.id, rawRule);
return createdRules.set(rule.id, rule);
}, new Collection<Snowflake, FilteredStreamRule>());
}
Expand Down Expand Up @@ -138,7 +138,7 @@ export class FilteredStreamRuleManager extends BaseManager<
const res: GetFilteredTweetStreamRulesResponse = await this.client._api.tweets.search.stream.rules.get(requestData);
const rawRule = res.data?.[0];
if (!rawRule) throw new CustomError('RULE_NOT_FOUND');
return this.add(rawRule.id, rawRule, options.cacheAfterFetching);
return this._add(rawRule.id, rawRule, options.cacheAfterFetching);
}

async #fetchMultipleRules(
Expand All @@ -154,7 +154,7 @@ export class FilteredStreamRuleManager extends BaseManager<
const rawRules = res.data;
if (!rawRules?.length) return fetchedRules;
for (const rawRule of rawRules) {
const rule = this.add(rawRule.id, rawRule, options?.cacheAfterFetching);
const rule = this._add(rawRule.id, rawRule, options?.cacheAfterFetching);
fetchedRules.set(rule.id, rule);
}
return fetchedRules;
Expand Down
2 changes: 1 addition & 1 deletion src/managers/ListManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ListManager extends BaseManager<Snowflake, ListResolvable, List> {
};
const requestData = new RequestData({ body, isUserContext: true });
const res: PostListCreateResponse = await this.client._api.lists.post(requestData);
const list = this.add(res.data.id, res.data);
const list = this._add(res.data.id, res.data);
return list;
}

Expand Down
8 changes: 4 additions & 4 deletions src/managers/SpaceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class SpaceManager extends BaseManager<Snowflake, SpaceResolvable, Space>
const rawSpaces = data.data;
const rawSpacesIncludes = data.includes;
for (const rawSpace of rawSpaces) {
const space = this.add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching);
const space = this._add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching);
fetchedSpaceCollection.set(space.id, space);
}
return fetchedSpaceCollection;
Expand Down Expand Up @@ -111,7 +111,7 @@ export class SpaceManager extends BaseManager<Snowflake, SpaceResolvable, Space>
const rawSpaces = data.data;
const rawSpacesIncludes = data.includes;
for (const rawSpace of rawSpaces) {
const space = this.add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching);
const space = this._add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching);
fetchedSpaceCollection.set(space.id, space);
}
return fetchedSpaceCollection;
Expand All @@ -132,7 +132,7 @@ export class SpaceManager extends BaseManager<Snowflake, SpaceResolvable, Space>
};
const requestData = new RequestData({ query });
const data: GetSingleSpaceByIdResponse = await this.client._api.spaces(spaceId).get(requestData);
return this.add(data.data.id, data, options.cacheAfterFetching);
return this._add(data.data.id, data, options.cacheAfterFetching);
}

async #fetchMultipleSpaces(
Expand All @@ -152,7 +152,7 @@ export class SpaceManager extends BaseManager<Snowflake, SpaceResolvable, Space>
const rawSpaces = data.data;
const rawSpacesIncludes = data.includes;
for (const rawSpace of rawSpaces) {
const space = this.add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching);
const space = this._add(rawSpace.id, { data: rawSpace, includes: rawSpacesIncludes }, options.cacheAfterFetching);
fetchedSpaceCollection.set(space.id, space);
}
return fetchedSpaceCollection;
Expand Down
4 changes: 2 additions & 2 deletions src/managers/TweetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class TweetManager extends BaseManager<Snowflake, TweetResolvable, Tweet>
};
const requestData = new RequestData({ query });
const data: GetSingleTweetByIdResponse = await this.client._api.tweets(tweetId).get(requestData);
return this.add(data.data.id, data, options.cacheAfterFetching);
return this._add(data.data.id, data, options.cacheAfterFetching);
}

async #fetchMultipleTweets(
Expand All @@ -356,7 +356,7 @@ export class TweetManager extends BaseManager<Snowflake, TweetResolvable, Tweet>
const rawTweets = data.data;
const rawTweetsIncludes = data.includes;
for (const rawTweet of rawTweets) {
const tweet = this.add(rawTweet.id, { data: rawTweet, includes: rawTweetsIncludes }, options.cacheAfterFetching);
const tweet = this._add(rawTweet.id, { data: rawTweet, includes: rawTweetsIncludes }, options.cacheAfterFetching);
fetchedTweetCollection.set(tweet.id, tweet);
}
return fetchedTweetCollection;
Expand Down
4 changes: 2 additions & 2 deletions src/managers/UserManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class UserManager extends BaseManager<Snowflake, UserResolvable, User> {
const rawUsers = data.data;
const rawUsersIncludes = data.includes;
for (const rawUser of rawUsers) {
const user = this.add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching);
const user = this._add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching);
fetchedUserCollection.set(user.id, user);
}
return fetchedUserCollection;
Expand Down Expand Up @@ -443,7 +443,7 @@ export class UserManager extends BaseManager<Snowflake, UserResolvable, User> {
const rawUsers = data.data;
const rawUsersIncludes = data.includes;
for (const rawUser of rawUsers) {
const user = this.add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching);
const user = this._add(rawUser.id, { data: rawUser, includes: rawUsersIncludes }, options.cacheAfterFetching);
fetchedUserCollection.set(user.id, user);
}
return fetchedUserCollection;
Expand Down
6 changes: 3 additions & 3 deletions src/managers/__tests__/BaseManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const id: Snowflake = '1234567890';
const rawData = { id };
const client = new Client();
const baseManager = new BaseManager(client, BaseStructure);
const baseStructure = baseManager.add(id, rawData);
const baseStructure = baseManager._add(id, rawData);

// BaseManager#add
// BaseManager#_add
test('Add base structure to cache', () => {
expect(baseManager.add(id, rawData)).toBeInstanceOf(BaseStructure);
expect(baseManager._add(id, rawData)).toBeInstanceOf(BaseStructure);
});

// BaseManager#resolveId
Expand Down

0 comments on commit df76674

Please sign in to comment.