Created
April 14, 2021 09:46
-
-
Save MufidJamaluddin/287452d416db49fc6bda13fa3a4b5ff0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const daftarSupir: Array<any> = [] | |
function supir({ nama, mobil }: { nama: string, mobil: string }) { | |
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { | |
daftarSupir.push({ nama, mobil, propertyKey, target }) | |
}; | |
} | |
function timeout(ms: number) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
function izin() { | |
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { | |
let method: any = descriptor.value!; | |
// @ts-ignore | |
descriptor.value = async function ({supir, kotaTujuan}) { | |
console.log(`Izin ${supir} ke ${kotaTujuan}`) | |
await timeout(3000); | |
const hasil = method.apply(this, [{supir: `Bapak ${supir}`, kotaTujuan}]); | |
console.log(hasil); | |
return hasil; | |
}; | |
} | |
} | |
class UsahaRental { | |
private garasi: Array<any> = [] | |
constructor( | |
private readonly daftarKotaTujuan: Array<string> | |
) { } | |
handle(mobil: any) { | |
this.garasi.push(mobil); | |
} | |
kerja() { | |
this.daftarKotaTujuan.forEach(kotaTujuan => { | |
daftarSupir.forEach(item => { | |
this.garasi.forEach(mobil => { | |
if (mobil.nama === item.mobil) { | |
mobil[item.propertyKey]({supir: item.nama, kotaTujuan}) | |
} | |
}); | |
}); | |
}); | |
} | |
} | |
class Mobil { | |
constructor( | |
private readonly nama: string | |
) { | |
console.log(`Mobil ${nama} dibuat`) | |
} | |
@supir({ nama: 'Ahmad', mobil: 'Toyota' }) | |
@izin() | |
jalan({supir, kotaTujuan}: {supir: string, kotaTujuan: string}) { | |
console.log(`Mobil ${this.nama} dibawa pergi ke ${kotaTujuan} oleh ${supir}`) | |
return `Sampai di ${kotaTujuan}` | |
} | |
} | |
let rental = new UsahaRental(['Jakarta', 'Surabaya', 'Medan', 'Jogja']); | |
let mobil1 = new Mobil('Kijang') | |
let mobil2 = new Mobil('Toyota') | |
rental.handle(mobil1) | |
rental.handle(mobil2) | |
rental.kerja() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment