-
-
Notifications
You must be signed in to change notification settings - Fork 0
Hypercore v1 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RangerMauve
wants to merge
4
commits into
main
Choose a base branch
from
hypercoreV1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hypercore v1 #5
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,36 +7,70 @@ import { Permissions } from './Permissions' | |
import { Sync } from './Sync' | ||
import { randomBytes } from 'crypto' | ||
import { Timestamp } from '@consento/hlc' | ||
import { Feed } from './Feed' | ||
import { Feed, FeedLoader, defaultFeedLoader } from './Feed' | ||
|
||
export interface MemberOptions { | ||
id?: ID | ||
initiator?: ID | ||
loadFeed?: FeedLoader | ||
} | ||
|
||
export class Member { | ||
readonly feed: Feed | ||
readonly syncState: Sync | ||
readonly id: ID | ||
readonly initiator: ID | ||
_feed?: Feed | ||
_syncState?: Sync | ||
readonly _id: ID | ||
initiator: ID | ||
private readonly loadFeed: FeedLoader | ||
|
||
static async create (opts?: MemberOptions): Promise<Member> { | ||
const member = new Member(opts) | ||
await member.init() | ||
|
||
return member | ||
} | ||
|
||
constructor ({ | ||
id = randomBytes(8).toString('hex'), | ||
initiator = id | ||
initiator = id, | ||
loadFeed = defaultFeedLoader | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking over the code the name |
||
}: MemberOptions = {}) { | ||
this.id = id | ||
this._id = id | ||
this.initiator = initiator | ||
this.feed = new Feed(id) | ||
this.loadFeed = loadFeed | ||
} | ||
|
||
isInitiator (): boolean { | ||
return this.id === this.initiator | ||
} | ||
|
||
async init (): Promise<void> { | ||
this._feed = await this.loadFeed(this._id) | ||
|
||
// Workaround for when feeds give you a new ID based on your given ID | ||
// TODO: Clean this up? | ||
if (this.initiator === this._id) this.initiator = this.id | ||
|
||
this.syncState = new Sync(initiator) | ||
this._syncState = new Sync(this.initiator, this.loadFeed) | ||
|
||
this.syncState.addFeed(this.feed) | ||
await this.syncState.addFeed(this.feed) | ||
|
||
if (id === initiator) { | ||
this.requestAdd(this.id) | ||
if (this.isInitiator()) { | ||
await this.requestAdd(this.id) | ||
} | ||
} | ||
|
||
get syncState (): Sync { | ||
return this._syncState as Sync | ||
} | ||
|
||
get id (): ID { | ||
return this.feed.id | ||
} | ||
|
||
get feed (): Feed { | ||
return this._feed as Feed | ||
} | ||
|
||
get permissions (): Permissions { | ||
return this.syncState.permissions | ||
} | ||
|
@@ -45,52 +79,52 @@ export class Member { | |
return this.syncState.knownMembers | ||
} | ||
|
||
sync (other: Member): void { | ||
this.syncState.sync(other.syncState) | ||
async sync (other: Member): Promise<void> { | ||
await this.syncState.sync(other.syncState) | ||
} | ||
|
||
processFeeds (): void { | ||
this.syncState.processFeeds() | ||
async processFeeds (): Promise<void> { | ||
await this.syncState.processFeeds() | ||
} | ||
|
||
requestAdd (who: ID): Request { | ||
const req = this.feed.addRequest({ | ||
async requestAdd (who: ID): Promise<Request> { | ||
const req = await this.feed.addRequest({ | ||
operation: 'add', | ||
who, | ||
timestamp: this.now() | ||
}) | ||
this.processFeeds() | ||
await this.processFeeds() | ||
return req | ||
} | ||
|
||
requestRemove (who: ID): Request { | ||
const req = this.feed.addRequest({ | ||
async requestRemove (who: ID): Promise<Request> { | ||
const req = await this.feed.addRequest({ | ||
operation: 'remove', | ||
who, | ||
timestamp: this.now() | ||
}) | ||
this.processFeeds() | ||
await this.processFeeds() | ||
|
||
return req | ||
} | ||
|
||
acceptRequest ({ id }: Request): Response { | ||
async acceptRequest ({ id }: Request): Promise<Response> { | ||
const res = this.feed.addResponse({ | ||
id, | ||
response: 'accept', | ||
timestamp: this.now() | ||
}) | ||
this.processFeeds() | ||
return res | ||
await this.processFeeds() | ||
return await res | ||
} | ||
|
||
denyRequest ({ id }: Request): Response { | ||
const res = this.feed.addResponse({ | ||
async denyRequest ({ id }: Request): Promise<Response> { | ||
const res = await this.feed.addResponse({ | ||
id, | ||
response: 'deny', | ||
timestamp: this.now() | ||
}) | ||
this.processFeeds() | ||
await this.processFeeds() | ||
return res | ||
} | ||
|
||
|
@@ -107,15 +141,26 @@ export class Member { | |
}) | ||
} | ||
|
||
signUnsigned (): Response[] { | ||
async signUnsigned (): Promise<Response[]> { | ||
const toSign = this.getUnsignedRequests() | ||
const responses = [] | ||
|
||
for (const req of toSign) { | ||
const res = await this.acceptRequest(req) | ||
responses.push(res) | ||
} | ||
|
||
const responses = toSign.map((req) => this.acceptRequest(req)) | ||
await this.processFeeds() | ||
|
||
return responses | ||
} | ||
|
||
private now (): Timestamp { | ||
async close (): Promise<void> { | ||
// TODO: Should we clear resources here? | ||
await this.syncState.close() | ||
} | ||
|
||
now (): Timestamp { | ||
return this.syncState.permissions.clock.now() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.