如何使用 JavaScript 开发调度程序日期应用程序
How to develop a scheduler date application using JavaScript
我对如何使用JavaScript[=51编写此场景有一个菜鸟疑问 =].
Develop a data structure to help scheduling airplanes to take off and
land from this runway.
Each time a plane takes off or lands, it needs exclusive use of the
runway starting 10 minutes before the scheduled runway use, and ending
10 minutes after the scheduled runway use (assume the actual take-off
or landing is instantaneous).
Be sure that planes don’t have overlapping schedules.
调度程序 class 将使用 ReactJS 调用。
class 还需要使用 babel 和 mocha 进行测试。 (以后再关心)
我评估了 schedule.js
的使用,但事实并非如此。我想用setInterval()
来安排时间,但我不知道这是否是一个好的策略。
任务是实现一个调度器,大致如下API:
class Scheduler {
// returns true if there'ss room to schedule at `time`
CouldScheduleAt(Date time)
// returns true if we successfully scheduled
ScheduleAt(Date time)
// Choose an available time to schedule at, and return that time
Schedule()
// returns true if we successfully unscheduled something
UnscheduleAt(Date time)
}
所以..我如何使用 JavaScript 开发这个场景?
编辑 - 我根据 Tiago Henrique
实施实施。
class Scheduler {
constructor(timer) {
this.dates = new Set()
this.planes = {}
this.timer = timer
}
isPlaneInList(time) {
return (time in this.planes)
}
isDatesInconflicting(scheduledDate, time) {
if (time.getTime() <= scheduledDate.getTime()) return true
return (time.getTime() > this.timer)
}
// returns true if there's room to schedule at `time`
couldScheduleAt(time) {
if (this.dates.has(time)) return false
Object.keys(this.dates).map(scheduledDate => {
if (this.isDatesInconflicting(scheduledDate, time)) {
return false
}
return time
})
return true
}
// returns true if we successfully scheduled
scheduleAt(time, plane) {
if (!this.couldScheduleAt(time)) return false
this.dates.add(time)
this.planes[time.toString()] = plane
return this.isPlaneInList(time.toString())
}
// Choose an available time to schedule at, and return that time
schedule() {
// ......
}
// returns true if we successfully unscheduled something
unScheduleAt(time) {
const plane = this.planes[time.toString()]
if (!plane) return false
delete this.planes[time.toString()]
this.dates.delete(time)
return !this.isPlaneInList(time.toString())
}
}
export default Scheduler
我不确定 isDatesInconflicting
是否实施,但目前是这样。 schedule
函数暂时没有。
参考文献:
如果不涉及优先级,您只需要一个队列,其中出列方法应等待 20 分钟以使下一个项目出列。这可以使用 Promise
和 setTimeout
来实现。例如
class PlaneQueue {
constructor() {
this.dequeueChain = Promise.resolve()
this.planes = []
}
enqueue(plane) {
this.planes.push(plane)
}
dequeue() {
// this is returned to the caller so it gets the next plane
const intermediateChain = this.dequeueChain.then(() => {
return this.planes.shift()
})
// update the internal promise to resolve again only after 20 minutes
// this will cause the next dequeue call to wait for 20 minutes
this.dequeueChain = intermediateChain.then(() => {
return new Promise((resolve) => setTimeout(resolve, 1200000))
})
return intermediateChain
}
}
const planesQueue = new PlaneQueue()
planesQueue.enqueue({ name: 'Airfrance' })
planesQueue.enqueue({ name: 'American Airlines' })
planesQueue.enqueue({ name: 'Latan' })
planesQueue.dequeue().then((plane) => console.log(`${plane.name}: ${new Date()}`))
planesQueue.dequeue().then((plane) => console.log(`${plane.name}: ${new Date()}`))
planesQueue.dequeue().then((plane) => console.log(`${plane.name}: ${new Date()}`))
这是通过利用承诺必须 chain
的能力来实现的。当我们在其中使用 then
和 return 一个新的 Promise 时,结果是一个新的 promise,只有当所有 "chained" promise 也都实现时才会实现。
这里的想法是我们链接一个新的承诺,它在 20 分钟后解决,每次 dequeue
被调用,导致下一次调用实际上必须等待 20 分钟才能到达下一架飞机,依此类推.
编辑
我一开始并没有真正看到 API,上面的代码不适合这种情况。您需要的是预定日期列表和 link 日期与飞机的映射。要检查您是否可以安排某事,您必须遍历所有日期并检查是否有日期前后少于 20 分钟。
class PlaneQueue {
constructor() {
this.dates = new Set()
this.planes = {}
}
couldScheduleAt(date) {
if (set.has(date)) return false
for (scheduledDate of dates) {
// check the date is not less than 20 minutes before or after
if (conflicting(scheduledDate, date)) {
return false
}
}
return true
}
scheduleAt(date, plane) {
if (!couldScheduleAt(date)) return false
this.dates.add(date)
this.planes[date.toString()] = plane
return true
}
unscheduleAt(date) {
const plane = this.planes[date.toString()]
if (!plane) return false
delete this.planes[date.toString()]
this.dates.delete(date)
return plane
}
}
一个更好的方法是使用一个列表结构来保持其项目的顺序,这样你就可以提前退出循环(你只需要转到新日期将被放置的位置并且检查它是否不冲突)。
你如何在 React 组件中使用它并不重要,只要这个调度机制是正确的,你就可以在任何地方使用它,而不仅仅是在 React 组件中。
我对如何使用JavaScript[=51编写此场景有一个菜鸟疑问 =].
Develop a data structure to help scheduling airplanes to take off and land from this runway.
Each time a plane takes off or lands, it needs exclusive use of the runway starting 10 minutes before the scheduled runway use, and ending 10 minutes after the scheduled runway use (assume the actual take-off or landing is instantaneous).
Be sure that planes don’t have overlapping schedules.
调度程序 class 将使用 ReactJS 调用。
class 还需要使用 babel 和 mocha 进行测试。 (以后再关心)
我评估了 schedule.js
的使用,但事实并非如此。我想用setInterval()
来安排时间,但我不知道这是否是一个好的策略。
任务是实现一个调度器,大致如下API:
class Scheduler {
// returns true if there'ss room to schedule at `time`
CouldScheduleAt(Date time)
// returns true if we successfully scheduled
ScheduleAt(Date time)
// Choose an available time to schedule at, and return that time
Schedule()
// returns true if we successfully unscheduled something
UnscheduleAt(Date time)
}
所以..我如何使用 JavaScript 开发这个场景?
编辑 - 我根据 Tiago Henrique
实施实施。
class Scheduler {
constructor(timer) {
this.dates = new Set()
this.planes = {}
this.timer = timer
}
isPlaneInList(time) {
return (time in this.planes)
}
isDatesInconflicting(scheduledDate, time) {
if (time.getTime() <= scheduledDate.getTime()) return true
return (time.getTime() > this.timer)
}
// returns true if there's room to schedule at `time`
couldScheduleAt(time) {
if (this.dates.has(time)) return false
Object.keys(this.dates).map(scheduledDate => {
if (this.isDatesInconflicting(scheduledDate, time)) {
return false
}
return time
})
return true
}
// returns true if we successfully scheduled
scheduleAt(time, plane) {
if (!this.couldScheduleAt(time)) return false
this.dates.add(time)
this.planes[time.toString()] = plane
return this.isPlaneInList(time.toString())
}
// Choose an available time to schedule at, and return that time
schedule() {
// ......
}
// returns true if we successfully unscheduled something
unScheduleAt(time) {
const plane = this.planes[time.toString()]
if (!plane) return false
delete this.planes[time.toString()]
this.dates.delete(time)
return !this.isPlaneInList(time.toString())
}
}
export default Scheduler
我不确定 isDatesInconflicting
是否实施,但目前是这样。 schedule
函数暂时没有。
参考文献:
如果不涉及优先级,您只需要一个队列,其中出列方法应等待 20 分钟以使下一个项目出列。这可以使用 Promise
和 setTimeout
来实现。例如
class PlaneQueue {
constructor() {
this.dequeueChain = Promise.resolve()
this.planes = []
}
enqueue(plane) {
this.planes.push(plane)
}
dequeue() {
// this is returned to the caller so it gets the next plane
const intermediateChain = this.dequeueChain.then(() => {
return this.planes.shift()
})
// update the internal promise to resolve again only after 20 minutes
// this will cause the next dequeue call to wait for 20 minutes
this.dequeueChain = intermediateChain.then(() => {
return new Promise((resolve) => setTimeout(resolve, 1200000))
})
return intermediateChain
}
}
const planesQueue = new PlaneQueue()
planesQueue.enqueue({ name: 'Airfrance' })
planesQueue.enqueue({ name: 'American Airlines' })
planesQueue.enqueue({ name: 'Latan' })
planesQueue.dequeue().then((plane) => console.log(`${plane.name}: ${new Date()}`))
planesQueue.dequeue().then((plane) => console.log(`${plane.name}: ${new Date()}`))
planesQueue.dequeue().then((plane) => console.log(`${plane.name}: ${new Date()}`))
这是通过利用承诺必须 chain
的能力来实现的。当我们在其中使用 then
和 return 一个新的 Promise 时,结果是一个新的 promise,只有当所有 "chained" promise 也都实现时才会实现。
这里的想法是我们链接一个新的承诺,它在 20 分钟后解决,每次 dequeue
被调用,导致下一次调用实际上必须等待 20 分钟才能到达下一架飞机,依此类推.
编辑
我一开始并没有真正看到 API,上面的代码不适合这种情况。您需要的是预定日期列表和 link 日期与飞机的映射。要检查您是否可以安排某事,您必须遍历所有日期并检查是否有日期前后少于 20 分钟。
class PlaneQueue {
constructor() {
this.dates = new Set()
this.planes = {}
}
couldScheduleAt(date) {
if (set.has(date)) return false
for (scheduledDate of dates) {
// check the date is not less than 20 minutes before or after
if (conflicting(scheduledDate, date)) {
return false
}
}
return true
}
scheduleAt(date, plane) {
if (!couldScheduleAt(date)) return false
this.dates.add(date)
this.planes[date.toString()] = plane
return true
}
unscheduleAt(date) {
const plane = this.planes[date.toString()]
if (!plane) return false
delete this.planes[date.toString()]
this.dates.delete(date)
return plane
}
}
一个更好的方法是使用一个列表结构来保持其项目的顺序,这样你就可以提前退出循环(你只需要转到新日期将被放置的位置并且检查它是否不冲突)。
你如何在 React 组件中使用它并不重要,只要这个调度机制是正确的,你就可以在任何地方使用它,而不仅仅是在 React 组件中。