Skip to content

Commit

Permalink
feat: Get instance comparison by hand (#1332)
Browse files Browse the repository at this point in the history
No Symbol.hasInstance voodoo. Babel deserves its name.
  • Loading branch information
ukstv authored Apr 29, 2021
1 parent 279d729 commit 8dbdc1b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/ceramic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const DEFAULT_NETWORK = Networks.INMEMORY

const normalizeStreamID = (streamId: StreamID | string): StreamID => {
const streamRef = StreamRef.from(streamId)
if (streamRef instanceof StreamID) {
if (StreamID.isInstance(streamRef)) {
return streamRef
} else {
throw new Error(`Not StreamID: ${streamRef}`)
Expand Down Expand Up @@ -483,7 +483,7 @@ class Ceramic implements CeramicApi {
opts = { ...DEFAULT_LOAD_OPTS, ...opts };
const streamRef = StreamRef.from(streamId)
const base$ = await this._loadStream(streamRef.baseID, opts)
if (streamRef instanceof CommitID) {
if (CommitID.isInstance(streamRef)) {
// Here CommitID is requested, let's return stream at specific commit
const snapshot$ = await this.repository.stateManager.rewind(base$, streamRef)
return streamFromState<T>(this.context, this._streamHandlers, snapshot$.value)
Expand Down
6 changes: 2 additions & 4 deletions packages/streamid/src/__tests__/commit-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ test('#baseID', () => {

test('instanceof', () => {
const commitId = new CommitID('tile', BASE_CID_STRING, COMMIT_CID_STRING);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const instanceSpy = jest.spyOn(CommitID, Symbol.hasInstance);
expect(commitId instanceof CommitID).toBeTruthy();
const instanceSpy = jest.spyOn(CommitID, 'isInstance');
expect(CommitID.isInstance(commitId)).toBeTruthy();
expect(instanceSpy).toBeCalledWith(commitId);
});
12 changes: 5 additions & 7 deletions packages/streamid/src/__tests__/stream-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ describe('#atCommit', () => {
});

test('instanceof', () => {
const streamId = StreamID.fromString(STREAM_ID_STRING)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const instanceSpy = jest.spyOn(StreamID, Symbol.hasInstance)
expect(streamId instanceof StreamID).toBeTruthy()
expect(instanceSpy).toBeCalledWith(streamId)
})
const streamId = StreamID.fromString(STREAM_ID_STRING);
const instanceSpy = jest.spyOn(StreamID, 'isInstance');
expect(StreamID.isInstance(streamId)).toBeTruthy();
expect(instanceSpy).toBeCalledWith(streamId);
});
5 changes: 4 additions & 1 deletion packages/streamid/src/commit-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export class CommitID implements StreamRef {
static fromBytes = fromBytes;
static fromString = fromString;

static [Symbol.hasInstance](instance: any): boolean {
// WORKAROUND. Weird replacement for Symbol.hasInstance due to
// this old bug in Babel https://github.com/babel/babel/issues/4452
// which is used by CRA, which is widely popular.
static isInstance(instance: any): instance is CommitID {
return typeof instance === 'object' && '_tag' in instance && instance._tag === TAG;
}

Expand Down
7 changes: 5 additions & 2 deletions packages/streamid/src/stream-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class StreamID implements StreamRef {
static fromBytes = fromBytes;
static fromString = fromString;

static [Symbol.hasInstance](instance: any): boolean {
// WORKARDOUND Weird replacement for Symbol.hasInstance due to
// this old bug in Babel https://github.com/babel/babel/issues/4452
// which is used by CRA, which is widely popular.
static isInstance(instance: any): instance is StreamID {
return typeof instance === 'object' && '_tag' in instance && instance._tag === TAG;
}

Expand Down Expand Up @@ -133,7 +136,7 @@ export class StreamID implements StreamRef {
* Compare equality with another StreamID.
*/
equals(other: StreamID): boolean {
if (other instanceof StreamID) {
if (StreamID.isInstance(other)) {
return this.type === other.type && this.cid.equals(other.cid);
} else {
return false;
Expand Down
4 changes: 2 additions & 2 deletions packages/streamid/src/stream-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export namespace StreamRef {
*/
// eslint-disable-next-line no-inner-declarations
export function from(input: StreamID | CommitID | string | Uint8Array): StreamID | CommitID {
if (input instanceof StreamID) {
if (StreamID.isInstance(input)) {
return input;
} else if (input instanceof CommitID) {
} else if (CommitID.isInstance(input)) {
return input;
} else if (input instanceof Uint8Array) {
// Lazy computation: try CommitID, then StreamID, then complain
Expand Down

0 comments on commit 8dbdc1b

Please sign in to comment.