improve intent with scheduler

This commit is contained in:
Peter Maquiran
2023-10-27 16:46:41 +01:00
parent a0609e5ba2
commit 1865240569
4 changed files with 180 additions and 55 deletions
+4 -12
View File
@@ -27,7 +27,7 @@ import { NewActionPage } from '../pages/publications/new-action/new-action.page'
import { PublicationsPage } from '../pages/publications/publications.page';
import { fetchData } from 'plugins/Echo';
import { Encoding, Filesystem, FilesystemDirectory } from '@capacitor/filesystem';
import { sendIntent } from 'src/app/services/shareIntent'
const { App } = Plugins;
@@ -140,15 +140,7 @@ export class HomePage implements OnInit {
}
/* navigator.serviceWorker.ready.then((registration) => {
console.log('yes please')
registration.active.postMessage(
"Test message sent immediately after creation",
);
}); */
/* this.checkSendIntentReceived(''); */
sendIntent.setRouteService(this.router)
}
@@ -172,7 +164,7 @@ export class HomePage implements OnInit {
if (result.url) {
let resultUrl = decodeURIComponent(result.url);
Filesystem.readFile({path: resultUrl})
.then(async (content) => {
const modal = await this.modalController.create({
@@ -184,7 +176,7 @@ export class HomePage implements OnInit {
cssClass: 'new-action modal modal-desktop',
backdropDismiss: false
});
modal.onDidDismiss().then(() => {
SendIntent.finish();
});
+125
View File
@@ -0,0 +1,125 @@
/**
* Manages a queue of callbacks and controls their execution with start, pause, and resume functionality.
*/
export class CallbackScheduler {
private callbackQueue: (() => void)[] = []; // A queue to store the callbacks.
private running: boolean = false; // Tracks whether the scheduler is running.
/**
* Enqueues a callback function to be executed in the queue.
* @param callback - The callback function to be executed.
* @returns A function to rerun the callback
*/
enqueueCallback(callback: () => void): Function {
const add = () => { this.callbackQueue.push(callback); };
add();
return add;
}
/**
* Adds a callback function to the queue or run immediately depending on `this.running` state
* @param callback - The callback function to be added to the queue.
* @returns A function to remove the callback from the queue.
*/
function<T>(callback: (...args: any) => any): (...args: any) => Promise<T> {
return async (...args: any): Promise<T> => {
return new Promise(async (resolve) => {
if (this.running) {
console.log("running")
resolve(await callback(...args));
} else {
console.log("callbackQueue")
this.callbackQueue.push(async () => {
resolve(await callback(...args));
});
}
});
};
}
/**
* Starts the execution of callbacks in the queue.
*/
start(): void {
if (!this.running) {
this.running = true;
this.executeNextCallback();
}
}
/**
* Pauses the execution of callbacks.
*/
pause(): void {
this.running = false;
}
/**
* Resumes the execution of callbacks.
*/
resume(): void {
if (!this.running) {
this.running = true;
this.executeNextCallback();
}
}
/**
* Executes the next callback in the queue, removing it after execution.
*/
executeNextCallback(): void {
if (this.running && this.callbackQueue.length > 0) {
const callback = this.callbackQueue.shift();
callback();
this.executeNextCallback(); // Execute the next callback
}
}
}
// // Example usage:
// const manager = new CallbackScheduler();
// manager.enqueueCallback(() => {
// console.log("Callback 1 executed.");
// });
// manager.enqueueCallback(() => {
// console.log("Callback 2 executed.");
// });
// manager.enqueueCallback(() => {
// console.log("Callback 3 executed.");
// });
// manager.start();
// setTimeout(() => {
// manager.pause();
// console.log("Manager paused.");
// }, 3000);
// setTimeout(() => {
// manager.resume();
// console.log("Manager resumed.");
// }, 6000);
// const timingManager = new CallbackScheduler();
// class someResponsibility{
// constructor() {
// setTimeout(()=> {
// timingManager.start()
// }, 1000)
// this.execute()
// }
// execute = timingManager.triggerToEnqueueCallback(()=> {
// })
// }
+50
View File
@@ -0,0 +1,50 @@
import { Filesystem } from '@capacitor/filesystem';
import { SendIntent } from "send-intent";
import { Router } from '@angular/router';
import { CallbackScheduler } from './callbackScheduler';
class SendIntent {
Router!: Router
callbackScheduler = new CallbackScheduler()
constructor() {
SendIntent.checkSendIntentReceived().then((result: any) => {
// logger
if (result) {
console.log('SendIntent received');
console.log(JSON.stringify(result));
}
// event handler
if (result.url) {
this.onReceive(result)
}
}).catch(err => console.error(err));
}
private initialize() {
this.callbackScheduler.start()
}
setRouteService(Router: Router) {
this.Router = Router
this.initialize()
}
private onReceive = this.callbackScheduler.function<any>((result)=> {
let resultUrl = decodeURIComponent(result.url);
Filesystem.readFile({path: resultUrl}).then(async (content) => {
this.Router.navigateByUrl("/home/publication");
alert('shared')
console.log(content.data);
})
})
}
export const sendIntent = new SendIntent()
+1 -43
View File
@@ -9,7 +9,7 @@ import { defineCustomElements } from '@ionic/pwa-elements/loader';
import "hammerjs"; // HAMMER TIME
import { SendIntent } from "send-intent";
import { Filesystem } from '@capacitor/filesystem';
import 'src/app/services/shareIntent'
if (environment.production) {
enableProdMode();
@@ -48,46 +48,4 @@ platformBrowserDynamic().bootstrapModule(AppModule)
// Call the element loader after the platform has been bootstrapped
SendIntent.checkSendIntentReceived().then((result: any) => {
if (result) {
console.log('SendIntent received');
console.log(JSON.stringify(result));
}
if (result.url) {
let resultUrl = decodeURIComponent(result.url);
Filesystem.readFile({path: resultUrl})
.then(async (content) => {
/* const modal = await this.modalController.create({
component: PublicationsPage,
componentProps: {
item: "item",
intent: content.data
},
cssClass: 'new-action modal modal-desktop',
backdropDismiss: false
});
modal.onDidDismiss().then(() => {
SendIntent.finish();
});
await modal.present();
*/
window["sharedintend"] = content.data
window["this.router"].navigateByUrl("/home/publication");
alert('shared')
console.log(content.data);
})
.catch((err) => console.error(err));
}
}).catch(err => console.error(err));
defineCustomElements(window);