|
|
@ -9,6 +9,7 @@ import ctx from '../../environment/ctx'; |
|
|
|
import indexOfAndSplice from '../../helpers/array/indexOfAndSplice'; |
|
|
|
import indexOfAndSplice from '../../helpers/array/indexOfAndSplice'; |
|
|
|
import {IS_WORKER} from '../../helpers/context'; |
|
|
|
import {IS_WORKER} from '../../helpers/context'; |
|
|
|
import EventListenerBase from '../../helpers/eventListenerBase'; |
|
|
|
import EventListenerBase from '../../helpers/eventListenerBase'; |
|
|
|
|
|
|
|
import makeError from '../../helpers/makeError'; |
|
|
|
import {Awaited, WorkerTaskTemplate, WorkerTaskVoidTemplate} from '../../types'; |
|
|
|
import {Awaited, WorkerTaskTemplate, WorkerTaskVoidTemplate} from '../../types'; |
|
|
|
import {logger} from '../logger'; |
|
|
|
import {logger} from '../logger'; |
|
|
|
|
|
|
|
|
|
|
@ -61,7 +62,12 @@ interface CloseTask extends SuperMessagePortTask { |
|
|
|
// type: 'open'
|
|
|
|
// type: 'open'
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
type Task = InvokeTask | ResultTask | AckTask | PingTask | PongTask | BatchTask | CloseTask/* | OpenTask */; |
|
|
|
interface LockTask extends SuperMessagePortTask { |
|
|
|
|
|
|
|
type: 'lock', |
|
|
|
|
|
|
|
payload: string |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Task = InvokeTask | ResultTask | AckTask | PingTask | PongTask | BatchTask | CloseTask/* | OpenTask */ | LockTask; |
|
|
|
type TaskMap = { |
|
|
|
type TaskMap = { |
|
|
|
[type in Task as type['type']]?: (task: Extract<Task, type>, source: MessageEventSource, event: MessageEvent<any>) => void | Promise<any> |
|
|
|
[type in Task as type['type']]?: (task: Extract<Task, type>, source: MessageEventSource, event: MessageEvent<any>) => void | Promise<any> |
|
|
|
}; |
|
|
|
}; |
|
|
@ -133,7 +139,11 @@ export default class SuperMessagePort< |
|
|
|
this.log = logger('MP' + (logSuffix ? '-' + logSuffix : '')); |
|
|
|
this.log = logger('MP' + (logSuffix ? '-' + logSuffix : '')); |
|
|
|
this.debug = DEBUG; |
|
|
|
this.debug = DEBUG; |
|
|
|
|
|
|
|
|
|
|
|
if(typeof(window) !== 'undefined') { |
|
|
|
if('locks' in navigator) { |
|
|
|
|
|
|
|
const id = 'lock-' + Date.now() + (Math.random() * 0xFFFF | 0); |
|
|
|
|
|
|
|
navigator.locks.request(id, () => new Promise(() => {})); |
|
|
|
|
|
|
|
this.pushTask(this.createTask('lock', id)); |
|
|
|
|
|
|
|
} else if(typeof(window) !== 'undefined') { |
|
|
|
window.addEventListener('beforeunload', () => { |
|
|
|
window.addEventListener('beforeunload', () => { |
|
|
|
const task = this.createTask('close', undefined); |
|
|
|
const task = this.createTask('close', undefined); |
|
|
|
this.postMessage(undefined, task); |
|
|
|
this.postMessage(undefined, task); |
|
|
@ -146,8 +156,9 @@ export default class SuperMessagePort< |
|
|
|
invoke: this.processInvokeTask, |
|
|
|
invoke: this.processInvokeTask, |
|
|
|
ping: this.processPingTask, |
|
|
|
ping: this.processPingTask, |
|
|
|
pong: this.processPongTask, |
|
|
|
pong: this.processPongTask, |
|
|
|
close: this.processCloseTask |
|
|
|
close: this.processCloseTask, |
|
|
|
// open: this.processOpenTask
|
|
|
|
// open: this.processOpenTask,
|
|
|
|
|
|
|
|
lock: this.processLockTask |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -231,7 +242,7 @@ export default class SuperMessagePort< |
|
|
|
|
|
|
|
|
|
|
|
this.onPortDisconnect?.(port as any); |
|
|
|
this.onPortDisconnect?.(port as any); |
|
|
|
|
|
|
|
|
|
|
|
const error = new Error('PORT_DISCONNECTED'); |
|
|
|
const error = makeError('PORT_DISCONNECTED'); |
|
|
|
for(const id in this.awaiting) { |
|
|
|
for(const id in this.awaiting) { |
|
|
|
const task = this.awaiting[id]; |
|
|
|
const task = this.awaiting[id]; |
|
|
|
if(task.port === port) { |
|
|
|
if(task.port === port) { |
|
|
@ -406,6 +417,12 @@ export default class SuperMessagePort< |
|
|
|
// this.onPortConnect?.(source);
|
|
|
|
// this.onPortConnect?.(source);
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected processLockTask = (task: LockTask, source: MessageEventSource, event: MessageEvent) => { |
|
|
|
|
|
|
|
navigator.locks.request(task.payload, () => { |
|
|
|
|
|
|
|
this.processCloseTask(undefined, source, undefined); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
protected processInvokeTask = async(task: InvokeTask, source: MessageEventSource, event: MessageEvent) => { |
|
|
|
protected processInvokeTask = async(task: InvokeTask, source: MessageEventSource, event: MessageEvent) => { |
|
|
|
const id = task.id; |
|
|
|
const id = task.id; |
|
|
|
const innerTask = task.payload; |
|
|
|
const innerTask = task.payload; |
|
|
|