export default function cloneWithRef( element: any, newRef: any, ): React.ReactElement<any> { const previousRef = element.ref invariant( typeof previousRef !== 'string', 'Cannot connect React DnD to an element with an existing string ref. ' + 'Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. ' + 'Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute', ) if (!previousRef) { // When there is no ref on the element, use the new ref directly return cloneElement(element, { ref: newRef, }) } return cloneElement(element, { ref: (node: any) => { newRef(node) if (previousRef) { previousRef(node) } }, }) }
function * aiJudgeScore () { const { board, player, ai, version } = yield select() const scores = [] const judge = judgeScores[version] invariant(judge, 'version error') const chess = getCandidate(player) for (let r = 0; r < 8; r += 1) { for (let c = 0; c < 8; c += 1) { if (board[r][c] === chess) { let score = judge(board, ai, r, c) scores.push({ row: r, col: c, score: score }) } } } invariant(scores.length, 'Invalid State: Candidates not place') const { score } = head(orderBy(scores, 'score', 'desc')) const { row, col } = sample(filter(scores, ['score', score])) // A little random yield delay(300) // A little delay yield put( pushLog({ player, pos: `(${row}, ${col})` }) ) yield call(flipAllChess, { row, col, player }) yield put(placeChess(row, col, player)) }
public subscribeToStateChange( listener: Listener, options: { handlerIds: string[] | undefined } = { handlerIds: undefined }, ): Unsubscribe { const { handlerIds } = options invariant(typeof listener === 'function', 'listener must be a function.') invariant( typeof handlerIds === 'undefined' || Array.isArray(handlerIds), 'handlerIds, when specified, must be an array of strings.', ) let prevStateId = this.store.getState().stateId const handleChange = () => { const state = this.store.getState() const currentStateId = state.stateId try { const canSkipListener = currentStateId === prevStateId || (currentStateId === prevStateId + 1 && !areDirty(state.dirtyHandlerIds, handlerIds)) if (!canSkipListener) { listener() } } finally { prevStateId = currentStateId } } return this.store.subscribe(handleChange) }
function verifyInvariants(monitor: DragDropMonitor) { invariant(monitor.isDragging(), 'Cannot call drop while not dragging.') invariant( !monitor.didDrop(), 'Cannot call drop twice during one drag operation.', ) }
function verifyInvariants( sourceIds: string[], monitor: DragDropMonitor, registry: HandlerRegistry, ) { invariant(!monitor.isDragging(), 'Cannot call beginDrag while dragging.') for (const s of sourceIds) { invariant(registry.getSource(s), 'Expected sourceIds to be registered.') } }
function getCurrentPosition( success: GeoSuccessCallback, error: GeoErrorCallback = () => {}, options: LocationOptions = {} ): void { invariant(typeof success === 'function', 'Must provide a valid success callback.'); invariant(typeof options === 'object', 'options must be an object.'); _getCurrentPositionAsyncWrapper(success, error, options); }
export function validateSourceContract(source: DragSource) { invariant( typeof source.canDrag === 'function', 'Expected canDrag to be a function.', ) invariant( typeof source.beginDrag === 'function', 'Expected beginDrag to be a function.', ) invariant( typeof source.endDrag === 'function', 'Expected endDrag to be a function.', ) }
export function validateTargetContract(target: DropTarget) { invariant( typeof target.canDrop === 'function', 'Expected canDrop to be a function.', ) invariant( typeof target.hover === 'function', 'Expected hover to be a function.', ) invariant( typeof target.drop === 'function', 'Expected beginDrag to be a function.', ) }
export function useDrop< DragObject extends DragObjectWithType, DropResult, CollectedProps >( spec: DropTargetHookSpec<DragObject, DropResult, CollectedProps>, ): [CollectedProps, ConnectDropTarget] { const specRef = useRef(spec) invariant(spec.accept != null, 'accept must be defined') const [monitor, connector] = useDropTargetMonitor() useDropHandler(specRef, monitor, connector) const result: CollectedProps = useMonitorOutput( monitor, specRef.current.collect || (() => ({} as CollectedProps)), () => connector.reconnect(), ) const connectDropTarget = useMemo(() => connector.hooks.dropTarget(), [ connector, ]) useEffect(() => { connector.dropTargetOptions = spec.options || null connector.reconnect() }, [spec.options]) return [result, connectDropTarget] }
export function timeoutForField(field: GraphQLField<any, any>) { const fieldDirectives = field.astNode && field.astNode.directives const directive = fieldDirectives && fieldDirectives.find(directive => directive.name.value === "timeout") if (directive) { const args = directive && directive.arguments const arg = args && args[0] invariant(arg && arg.name.value === "ms", "graphqlTimeoutMiddleware: The `@timeout(ms: …)` argument is required.") const value = arg!.value if (value.kind === "IntValue") { return parseInt(value.value) } else { invariant(false, `graphqlTimeoutMiddleware: Expected \`@timeout(ms: …)\` to be a \`IntValue\`, got \`${value.kind}\` instead.`) return null } } return null }