Skip to content

Commit

Permalink
refactor: use identifiers types in react-dnd utils
Browse files Browse the repository at this point in the history
  • Loading branch information
darthtrevino committed Mar 7, 2019
1 parent 983f9da commit ed2fc9d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/react-dnd/src/createSourceConnector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
declare var require: any

import wrapConnectorHooks from './wrapConnectorHooks'
import { Backend, Unsubscribe } from 'dnd-core'
import { Backend, Unsubscribe, Identifier } from 'dnd-core'
const shallowEqual = require('shallowequal')

export default function createSourceConnector(backend: Backend) {
let currentHandlerId: string
let currentHandlerId: Identifier

let currentDragSourceNode: any
let currentDragSourceOptions: any
Expand Down Expand Up @@ -45,7 +45,7 @@ export default function createSourceConnector(backend: Backend) {
}
}

function receiveHandlerId(handlerId: string) {
function receiveHandlerId(handlerId: Identifier) {
if (handlerId === currentHandlerId) {
return
}
Expand Down
20 changes: 10 additions & 10 deletions packages/react-dnd/src/createSourceMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ let isCallingIsDragging = false

class SourceMonitor implements DragSourceMonitor {
private internalMonitor: DragDropMonitor
private sourceId: string | undefined
private sourceId: Identifier | undefined

constructor(manager: DragDropManager<any>) {
this.internalMonitor = manager.getMonitor()
}

public receiveHandlerId(sourceId: string) {
public receiveHandlerId(sourceId: Identifier) {
this.sourceId = sourceId
}

public getHandlerId(): string | undefined {
public getHandlerId(): Identifier | undefined {
return this.sourceId
}

Expand All @@ -38,7 +38,7 @@ class SourceMonitor implements DragSourceMonitor {

try {
isCallingCanDrag = true
return this.internalMonitor.canDragSource(this.sourceId as string)
return this.internalMonitor.canDragSource(this.sourceId as Identifier)
} finally {
isCallingCanDrag = false
}
Expand All @@ -53,25 +53,25 @@ class SourceMonitor implements DragSourceMonitor {

try {
isCallingIsDragging = true
return this.internalMonitor.isDraggingSource(this.sourceId as string)
return this.internalMonitor.isDraggingSource(this.sourceId!)
} finally {
isCallingIsDragging = false
}
}

public subscribeToStateChange(
listener: Listener,
options?: { handlerIds: string[] | undefined },
options?: { handlerIds: Identifier[] | undefined },
): Unsubscribe {
return this.internalMonitor.subscribeToStateChange(listener, options)
}

public isDraggingSource(sourceId: string): boolean {
public isDraggingSource(sourceId: Identifier): boolean {
return this.internalMonitor.isDraggingSource(sourceId)
}

public isOverTarget(
targetId: string,
targetId: Identifier,
options?: { shallow: boolean },
): boolean {
return this.internalMonitor.isOverTarget(targetId, options)
Expand All @@ -93,11 +93,11 @@ class SourceMonitor implements DragSourceMonitor {
return this.internalMonitor.subscribeToOffsetChange(listener)
}

public canDragSource(sourceId: string): boolean {
public canDragSource(sourceId: Identifier): boolean {
return this.internalMonitor.canDragSource(sourceId)
}

public canDropOnTarget(targetId: string): boolean {
public canDropOnTarget(targetId: Identifier): boolean {
return this.internalMonitor.canDropOnTarget(targetId)
}

Expand Down
7 changes: 3 additions & 4 deletions packages/react-dnd/src/createTargetConnector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
declare var require: any
import wrapConnectorHooks from './wrapConnectorHooks'
import { Backend, Unsubscribe } from 'dnd-core'
import { Backend, Unsubscribe, Identifier } from 'dnd-core'
const shallowEqual = require('shallowequal')

export default function createTargetConnector(backend: Backend) {
let currentHandlerId: string

let currentHandlerId: Identifier
let currentDropTargetNode: any
let currentDropTargetOptions: any
let disconnectCurrentDropTarget: Unsubscribe | undefined
Expand All @@ -25,7 +24,7 @@ export default function createTargetConnector(backend: Backend) {
}
}

function receiveHandlerId(handlerId: string) {
function receiveHandlerId(handlerId: Identifier) {
if (handlerId === currentHandlerId) {
return
}
Expand Down
20 changes: 13 additions & 7 deletions packages/react-dnd/src/createTargetMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
declare var require: any
import { DragDropManager, DragDropMonitor, Unsubscribe, Listener } from 'dnd-core'
import {
DragDropManager,
DragDropMonitor,
Unsubscribe,
Listener,
Identifier,
} from 'dnd-core'
import { DropTargetMonitor } from './interfaces'
const invariant = require('invariant')

let isCallingCanDrop = false

export class TargetMonitor implements DropTargetMonitor {
private internalMonitor: DragDropMonitor
private targetId: string | undefined
private targetId: Identifier | undefined

constructor(manager: DragDropManager<any>) {
this.internalMonitor = manager.getMonitor()
}

public receiveHandlerId(targetId: string) {
public receiveHandlerId(targetId: Identifier) {
this.targetId = targetId
}

public getHandlerId(): string | undefined {
public getHandlerId(): Identifier | undefined {
return this.targetId
}

public subscribeToStateChange(
listener: Listener,
options?: { handlerIds: string[] | undefined },
options?: { handlerIds: Identifier[] | undefined },
): Unsubscribe {
return this.internalMonitor.subscribeToStateChange(listener, options)
}
Expand All @@ -37,14 +43,14 @@ export class TargetMonitor implements DropTargetMonitor {

try {
isCallingCanDrop = true
return this.internalMonitor.canDropOnTarget(this.targetId as string)
return this.internalMonitor.canDropOnTarget(this.targetId!)
} finally {
isCallingCanDrop = false
}
}

public isOver(options: { shallow?: boolean }) {
return this.internalMonitor.isOverTarget(this.targetId as string, options)
return this.internalMonitor.isOverTarget(this.targetId!, options)
}

public getItemType() {
Expand Down

0 comments on commit ed2fc9d

Please sign in to comment.