-
Notifications
You must be signed in to change notification settings - Fork 2k
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
use Symbol.hasInstance for looser instanceof checks #989
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, it} from 'mocha'; | ||
import chai from 'chai'; | ||
import attachHasInstanceSymbol from '../attachHasInstanceSymbol'; | ||
import { GraphQLInputObjectType as RealInputType } from '../../type'; | ||
|
||
|
||
describe('attachHasInstanceSymbol()', () => { | ||
it('passes instanceof checks for types for other package instances', () => { | ||
class GraphQLInputObjectType { | ||
constructor() {} | ||
} | ||
|
||
attachHasInstanceSymbol(GraphQLInputObjectType); | ||
|
||
chai.expect(new GraphQLInputObjectType() instanceof RealInputType) | ||
.to.equal(true); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* @flow */ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
// eslint-disable-next-line flowtype/no-weak-types | ||
export default function attachHasInstanceSymbol(ctor: Function): void { | ||
if (typeof Symbol === 'undefined' || !Symbol.for || !Symbol.hasInstance) { | ||
return; | ||
} | ||
const tag = `@@typeof/${ctor.name}`; | ||
|
||
Object.defineProperty(ctor, Symbol.hasInstance, { | ||
value: function $hasInstance(instance) { | ||
return instance && instance[Symbol.for(tag)] === true; | ||
}, | ||
}); | ||
|
||
ctor.prototype[Symbol.for(tag)] = true; | ||
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. Why 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. one fetches a global symbol by name vs creating a new symbol: Symbol('foo') == Symbol('foo') // false
Symbol.for('foo') == Symbol.for('foo') // true 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. I understand, but why is that desirable here? Imo the symbol should be private or if it's public, it should be exported, no retrievable for everyone through a magic string. You just need to save the symbol in a variable and reuse it. Otherwise you might as well just use a regular property. 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. Its not using a symbol for privacy, it's to avoid muddying up people's namespace on the class for things they can use as property names. With the symbol you don't have to worry (nearly as much) about accidentally overwriting the tag in an instance when you just wanted to define an unrelated property. Incidentally this approach is how React tags elements as "ReactElements" |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what a better type here would be maybe GraphQLType?