Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
the grass is greener over here, you're the fog that makes it so clear

Hesham Yassin exhesham

💭
the grass is greener over here, you're the fog that makes it so clear
View GitHub Profile
@exhesham
exhesham / AndroidManifest.xml
Last active Mar 16, 2022
Google sign in process
View AndroidManifest.xml
<application>
....
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
View sol2.log
[LOG]: "Total tasks: 10"
[LOG]: "worker 1 is started"
[LOG]: "worker 1 handles task"
[LOG]: " --> Task 1 started"
[LOG]: "worker 2 is started"
[LOG]: "worker 2 handles task"
[LOG]: " --> Task 2 started"
[LOG]: "worker 3 is started"
[LOG]: "worker 3 handles task"
[LOG]: " --> Task 3 started"
View sol2-run.ts
await Promise.all([worker(1), worker(2), worker(3), worker(4)]);
View sol2-worker.ts
let worker = async (index: number) => {
console.log(`worker ${index} is started`);
while(tasksQueue.length > 0) {
console.log(`worker ${index} handles task`);
let task = tasksQueue[0]; // should be atomic
tasksQueue = tasksQueue.slice(1);
await task();
console.log(`worker ${index} finished handling task`);
}
console.log(`worker ${index} is retired`);
View sol2-configtasks.ts
let idx =1, tasksQueue = Array(numberOfTasks).fill(1)
.map(v => Math.round(Math.random() * 30) + v)
.map(v => (() => taskExecutor({delay: v, index: `${idx++}` })));
View sol1-output.log
[LOG]: "* start running batch 0"
[LOG]: " --> Task 0 started"
[LOG]: " --> Task 1 started"
[LOG]: " --> Task 2 started"
[LOG]: " <-- Task 0 finished"
[LOG]: " <-- Task 1 finished"
[LOG]: " <-- Task 2 finished"
[LOG]: "* finished running batch 0"
[LOG]: "* start running batch 3"
[LOG]: " --> Task 3 started"
View task-executor.ts
let taskExecutor = async (task: {delay: number, index: string}) => {
console.log(` --> Task ${task.index} started`);
await sleep(task.delay);
console.log(` <-- Task ${task.index} finished`);
};
View sol1.ts
async function solution1() {
for(let i = 0; i < tasksSleepInput.length; i+= chunkSize) {
let batch = tasksSleepInput.slice(i, i + chunkSize);
console.log(`* start running batch ${i}`);
await Promise.all(batch.map(taskExecutor));
console.log(`* finished running batch ${i}`);
}
}
View sol1-configtasks.ts
const chunkSize = 3;
const numberOfTasks = 10;
let tasksSleepInput = Array(numberOfTasks).fill(30).map(v => ({delay: v, index: '' }));
// add indices
Object.keys(tasksSleepInput).forEach(k => tasksSleepInput[Number.parseInt(k)].index = k);
View sol1-configtasks.ts
const chunkSize = 3;
const numberOfTasks = 10;
let tasksSleepInput = Array(numberOfTasks).fill(30).map(v => ({delay: v, index: '' }));
// add indices
Object.keys(tasksSleepInput).forEach(k => tasksSleepInput[Number.parseInt(k)].index = k);